requestPhp.js 2.1 KB

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