requestPhp.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {getPhpToken} from "../utils/readAppInfo"
  2. export let baseURL = 'https://testapi.hxxfb.com/'
  3. // 请求超出时间
  4. const timeout = 50000
  5. // const token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhcGkuaHh4ZmIuY29tIiwiYXVkIjoiYXBpLmh4eGZiLmNvbSIsImlhdCI6MTc0MDM3MTM1NSwibmJmIjoxNzQwMzcxMzU1LCJleHAiOjE3NTExNzEzNTUsImp0aSI6WzI5OCwibWVyIl19.j2f8VwdwWY4zBpcijHntzUZ9YUms2SxChaVsYf7KnPs"
  6. // const token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhcGkuaHh4ZmIuY29tIiwiYXVkIjoiYXBpLmh4eGZiLmNvbSIsImlhdCI6MTc0MDM3MTM1NSwibmJmIjoxNzQwMzcxMzU1LCJleHAiOjE3NTExNzEzNTUsImp0aSI6WzI5OCwibWVyIl19.j2f8VwdwWY4zBpcijHntzUZ9YUms2SxChaVsYf7KnPs"
  7. // const token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhcGkuaHh4ZmIuY29tIiwiYXVkIjoiYXBpLmh4eGZiLmNvbSIsImlhdCI6MTc0MDU3MTQ0OCwibmJmIjoxNzQwNTcxNDQ4LCJleHAiOjE3NTEzNzE0NDgsImp0aSI6WzM1NCwibWVyIl19.PVwYv71Gq9RxTxQclUoFMgc8cZt0ju3zQkxk6SGCQJw"
  8. export const phpToken = uni.getStorageSync('phpToken') || ""
  9. // 需要修改token,和根据实际修改请求头
  10. export function myRequestPhp(options) {
  11. let url = options.url;
  12. let method = options.method || "get";
  13. let data = options.data || {};
  14. let contentType = options.contentType || "application/json;charset=UTF-8";
  15. let header = {
  16. 'Content-Type': contentType,
  17. "X-Token": phpToken,
  18. ...options.header
  19. }
  20. return new Promise((resolve, reject) => {
  21. uni.request({
  22. url: baseURL + url,
  23. method: method,
  24. header: header,
  25. data: data,
  26. timeout,
  27. success:async function(response) {
  28. const res = response
  29. const message = res.data && res.data.message || ""
  30. // console.log(res, "resresresresres")
  31. // console.log(message, "99999message")
  32. if (res.data.status == 200) { //接口返回200走一下操作
  33. resolve(res)
  34. } else if (res.data.status == 500) { //接口返回500走一下操作
  35. return uni.showToast({
  36. title: res.data.error,
  37. icon: 'none'
  38. })
  39. } else if ((res.data.status == 400 || res.data.status == 40000) && (message.indexOf('登录') > -1 || message.indexOf('过期') > -1 || message.indexOf('token') > -1)) { //接口返回400走一下操作500走一下操作
  40. await getPhpToken()
  41. resolve(res)
  42. } else { //除了500都会走以下操作
  43. // console.log(message, "messag9999e")
  44. uni.showToast({
  45. title: message,
  46. icon: 'none',
  47. duration: 6000
  48. })
  49. // reject(res)
  50. }
  51. },
  52. fail(err) {
  53. console.log(err,"接口")
  54. if (err.errMsg.indexOf('request:fail') !== -1) {
  55. uni.showToast({
  56. title: '网络异常',
  57. icon: "error",
  58. duration: 2000
  59. })
  60. } else {
  61. uni.showToast({
  62. title: '未知异常',
  63. duration: 2000
  64. })
  65. }
  66. uni.hideToast();
  67. reject(err);
  68. },
  69. complete() {
  70. // uni.hideToast();
  71. }
  72. });
  73. }).catch(() => {
  74. uni.hideToast();
  75. });
  76. };