request.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. function postUrlAction(url: string, parameter: any) {
  2. let params = "";
  3. for (let attr in parameter) {
  4. params += attr + "=" + parameter[attr] + "&";
  5. }
  6. params = "?" + params.substr(0, params.length - 1);
  7. return url + params;
  8. }
  9. module.exports = function (options: any) {
  10. const App = getApp();
  11. const baseUrl = App.globalData?.baseUrl;
  12. return new Promise((resolve, reject) => {
  13. let requestType = "";
  14. // 是否不显示提示
  15. let hideLoading = options.hideLoading || false
  16. let url = baseUrl + options.url;
  17. if (options.requestType === "form") {
  18. requestType = "application/x-www-form-urlencoded";
  19. // url = postUrlAction(url, options.data);
  20. } else if (options.requestType === "data") {
  21. requestType = "multipart/form-data";
  22. } else {
  23. requestType = "application/json; charset=UTF-8";
  24. }
  25. // token
  26. const Authorization = wx.getStorageSync("token");
  27. // open 接口不需要传token
  28. const isOpenApi = options.url.includes('/open/')
  29. const isCodeApi = options.url.includes('/code/')
  30. const noToken = isOpenApi || isCodeApi || options.noToken
  31. wx.request({
  32. url: url,
  33. method: options.method,
  34. data: options.data,
  35. header: noToken ? {
  36. "Content-Type": requestType,
  37. } : Authorization
  38. ? {
  39. "Content-Type": requestType,
  40. Authorization,
  41. // 'x-token': 'x-token'
  42. }
  43. : {
  44. "Content-Type": requestType,
  45. // 'x-token': 'x-token'
  46. },
  47. success: function (res: any) {
  48. const otherCode = [5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432];
  49. if (res.data.code !== 200 && !otherCode.includes(res.data.code)) {
  50. if (res.data.code == 5000 || res.data.code == 403) {
  51. wx.setStorageSync("token", "");
  52. if (!hideLoading) {
  53. setTimeout(() => {
  54. wx.showToast({
  55. title: "登录过期,请重新登录",
  56. icon: "none",
  57. });
  58. }, 100);
  59. }
  60. const pages = getCurrentPages();
  61. // console.log(pages, 'pages')
  62. const currentPage = pages[pages.length - 1];
  63. // console.log(currentPage.route,'currentPage==>')
  64. // 为了处理发版处理;
  65. // https://mp.weixin.qq.com/cgi-bin/announce?action=getannouncement&key=11669729383k7cis&version=1&lang=zh_CN&platform=2
  66. const route = currentPage?.route === 'pages/purchaseRecord/record' ? currentPage?.route : ''
  67. if (currentPage.route != "pages/login/index") {
  68. wx.redirectTo({
  69. url: "/pages/login/index?redirectUrl=" + route,
  70. });
  71. }
  72. } else {
  73. if (!hideLoading) {
  74. setTimeout(() => {
  75. wx.showToast({
  76. title: res.data.message,
  77. icon: "none",
  78. });
  79. }, 100);
  80. }
  81. reject(res);
  82. }
  83. } else {
  84. resolve(res);
  85. }
  86. //这里可以加状态判断
  87. },
  88. fail: function (error) {
  89. reject(error);
  90. },
  91. complete: () => {
  92. // setTimeout(() => {
  93. // wx.hideLoading();
  94. // }, 100);
  95. },
  96. });
  97. });
  98. };