request.ts 3.1 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. // console.log(res.data.code)
  49. if (res.data.code == 500) {
  50. // console.log(res.data.msg,'cuowu====>')
  51. if(!hideLoading) {
  52. setTimeout(() => {
  53. wx.showToast({
  54. title: res.data.msg,
  55. icon: "none",
  56. });
  57. }, 100);
  58. }
  59. reject(res.data.msg);
  60. } else if (res.data.code == 403) {
  61. wx.setStorageSync("token", "");
  62. if(!hideLoading) {
  63. setTimeout(() => {
  64. wx.showToast({
  65. title: "登录超时",
  66. icon: "none",
  67. });
  68. }, 100);
  69. }
  70. const pages = getCurrentPages();
  71. // console.log(pages, 'pages')
  72. const currentPage = pages[pages.length - 1];
  73. // console.log(currentPage.route,'currentPage==>')
  74. // 为了处理发版处理;
  75. // https://mp.weixin.qq.com/cgi-bin/announce?action=getannouncement&key=11669729383k7cis&version=1&lang=zh_CN&platform=2
  76. const route = currentPage.route === 'pages/purchaseRecord/record' ? currentPage.route : ''
  77. if (currentPage.route != "pages/login/index") {
  78. wx.redirectTo({
  79. url: "/pages/login/index?redirectUrl=" + route,
  80. });
  81. }
  82. } else {
  83. resolve(res);
  84. }
  85. //这里可以加状态判断
  86. },
  87. fail: function (error) {
  88. reject(error);
  89. },
  90. complete: () => {
  91. // setTimeout(() => {
  92. // wx.hideLoading();
  93. // }, 100);
  94. },
  95. });
  96. });
  97. };