requestFn.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const app = getApp();
  2. const baseURL = "https://www.wxmini.getensh.com/api/v1";
  3. const FN = require('../public');
  4. const Request = (options) => {
  5. return new Promise((resolve, reject) => {
  6. FN.Loading()
  7. wx.request({
  8. url: baseURL + options.url || '',
  9. data: options.data || {},
  10. method: options.method || 'GET',
  11. header: {
  12. token: wx.getStorageSync('token') || "",
  13. "Access-Control-Allow-Origin": "*"
  14. },
  15. responseType: options.responseType || "",
  16. timeout: 15000,
  17. dataType: "json",
  18. success(res) {
  19. if (res.statusCode === 200) {
  20. if (res.data.code === 0) {
  21. if (['put', 'post', 'delete'].includes(options.method)) {
  22. resolve(res.data);
  23. } else {
  24. resolve(res.data.data);
  25. }
  26. } else if ([10009, 10008].includes(res.data.code)) {
  27. app.getWxCode();
  28. if (res.data.code == 10008) {
  29. FN.Toast("请先登录")
  30. setTimeout(() => {
  31. wx.switchTab({
  32. url: '/page/tabBar/mine/index',
  33. })
  34. }, 1000)
  35. } else {
  36. wx.showToast({
  37. title: '登录已过期,请重新登录',
  38. icon: "none",
  39. success() {
  40. app.globalData.user_encrypt = {};
  41. app.globalData.user_iv = {};
  42. app.loginout();
  43. wx.clearStorageSync(),
  44. setTimeout(() => {
  45. wx.switchTab({
  46. url: '/page/tabBar/mine/index',
  47. })
  48. }, 1000)
  49. }
  50. })
  51. }
  52. } else {
  53. if (res.data.message) {
  54. wx.showToast({
  55. title: res.data.message,
  56. icon: "none"
  57. })
  58. }
  59. resolve(res.data);
  60. }
  61. } else if (res.statusCode === 502) {
  62. FN.Toast("服务不可用");
  63. } else {
  64. FN.Toast(res.errMsg);
  65. };
  66. },
  67. fail(res) {
  68. FN.Toast("网络开小差了");
  69. reject(res);
  70. },
  71. complete(e) {
  72. setTimeout(() => {
  73. FN.LoadingOff();
  74. }, 500)
  75. }
  76. })
  77. })
  78. };
  79. module.exports = {
  80. Request
  81. };