request.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // const baseURL = "http://192.168.110.180:8888/3dshow";
  2. const baseURL = "https://www.3dqueen.cloud/3dshow/v2/user";
  3. export default function (url, options = {}) {
  4. return new Promise((resolve) => {
  5. if (options.method && options.method !== "GET")
  6. uni.showLoading({ title: "加载中" });
  7. uni.request({
  8. ...options,
  9. url: (options.baseURL ? options.baseURL : baseURL) + url,
  10. header: {
  11. Authorization: "Bearer " + uni.getStorageSync("access-token"),
  12. },
  13. success(res) {
  14. uni.hideLoading();
  15. console.log(res);
  16. if (res.statusCode === 200) {
  17. let data = res.data;
  18. // 兼容后端返回数据异常
  19. if (!res.data.code && res.data.body) {
  20. data = res.data.body;
  21. }
  22. if (data.code === 4006) {
  23. console.log("invalid auth");
  24. // #ifdef MP-WEIXIN
  25. uni.reLaunch({
  26. url: "guide?token=null",
  27. });
  28. // #endif
  29. // #ifdef H5
  30. wx.miniProgram.reLaunch({
  31. url: pageFrom + "?token=null",
  32. });
  33. // #endif
  34. }
  35. resolve(data);
  36. } else {
  37. resolve({
  38. code: res.statusCode,
  39. msg: res.errMsg,
  40. });
  41. }
  42. },
  43. fail(err) {
  44. uni.hideLoading();
  45. console.log("error", err);
  46. resolve({
  47. code: 500,
  48. msg: err.errMsg,
  49. });
  50. },
  51. });
  52. });
  53. }