request.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import * as db from "./db.js";
  2. import { baseUrl } from "./config.js";
  3. import { User } from "@/config/api.js";
  4. import store from "../store/index.js";
  5. const showError = (error) => {
  6. let errorMsg = "";
  7. switch (error.status) {
  8. case 400:
  9. errorMsg = "请求参数错误";
  10. break;
  11. case 401:
  12. errorMsg = "未授权,请登录";
  13. break;
  14. case 403:
  15. errorMsg = "跨域拒绝访问";
  16. break;
  17. case 404:
  18. errorMsg = `请求地址出错: ${error.config.url}`;
  19. break;
  20. case 408:
  21. errorMsg = "请求超时";
  22. break;
  23. case 500:
  24. errorMsg = "服务器内部错误";
  25. break;
  26. case 501:
  27. errorMsg = "服务未实现";
  28. break;
  29. case 502:
  30. errorMsg = "网关错误";
  31. break;
  32. case 503:
  33. errorMsg = "服务不可用";
  34. break;
  35. case 504:
  36. errorMsg = "网关超时";
  37. break;
  38. case 505:
  39. errorMsg = "HTTP版本不受支持";
  40. break;
  41. default:
  42. errorMsg = error.msg;
  43. break;
  44. }
  45. uni.showToast({
  46. title: errorMsg,
  47. icon: "none",
  48. duration: 1000,
  49. complete: function () {
  50. setTimeout(() => {
  51. uni.hideToast();
  52. }, 1000);
  53. },
  54. });
  55. };
  56. // get请求
  57. export const getHttp = (url, data, status = 1) => {
  58. // 获取用户token和用户信息
  59. let token = db.get("access_token");
  60. let authHeader = token || "";
  61. if (status) {
  62. store.commit("requestShowLoading");
  63. }
  64. return new Promise((resolve, reject) => {
  65. uni.request({
  66. url: baseUrl + url,
  67. data: data,
  68. header: {
  69. // 'Accept': 'application/json',
  70. // 'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
  71. Authorization: authHeader,
  72. },
  73. method: "GET",
  74. success: (res) => {
  75. status && store.commit("requestHideLoading");
  76. // 408 token失效
  77. if (res.data.Ret === 408) {
  78. refreshToken(getHttp, url, data, resolve);
  79. return;
  80. }
  81. res.data.Ret === 403 &&
  82. uni.showModal({
  83. title: "",
  84. confirmColor: "#376cbb",
  85. content: res.data.Msg,
  86. showCancel: false,
  87. });
  88. resolve(res.data);
  89. },
  90. fail: (error) => {
  91. status && store.commit("requestHideLoading");
  92. if (error && error.response) {
  93. showError(error.response);
  94. }
  95. },
  96. complete: () => {
  97. setTimeout(() => {
  98. status && store.commit("requestHideLoading");
  99. }, 250);
  100. },
  101. });
  102. });
  103. };
  104. // post请求
  105. export const postHttp = (url, data, status = 1) => {
  106. // 获取用户token和用户信息
  107. let token = db.get("access_token");
  108. let authHeader = token || "";
  109. if (status) {
  110. store.commit("requestShowLoading");
  111. }
  112. return new Promise((resolve, reject) => {
  113. uni.request({
  114. url: baseUrl + url,
  115. data: data,
  116. header: {
  117. "Content-Type": "application/json",
  118. Authorization: authHeader,
  119. },
  120. method: "POST",
  121. success: (res) => {
  122. status && store.commit("requestHideLoading");
  123. // 408 token失效
  124. if (res.data.Ret === 408) {
  125. refreshToken(postHttp, url, data, resolve);
  126. return;
  127. }
  128. res.data.Ret === 403 &&
  129. uni.showModal({
  130. title: "",
  131. confirmColor: "#376cbb",
  132. content: res.data.Msg,
  133. showCancel: false,
  134. });
  135. resolve(res.data);
  136. },
  137. fail: (error) => {
  138. status && store.commit("requestHideLoading");
  139. if (error && error.response) {
  140. showError(error.response);
  141. }
  142. },
  143. complete: () => {
  144. setTimeout(() => {
  145. status && store.commit("requestHideLoading");
  146. }, 250);
  147. },
  148. });
  149. });
  150. };
  151. let requestList = []; //存放token失效时请求队列
  152. let isRefreshing = false; //是否正在刷新token
  153. const refreshToken = async (type, url, data, resolve) => {
  154. requestList.push(() => {
  155. resolve(type(url, data));
  156. });
  157. if (!isRefreshing) {
  158. isRefreshing = true;
  159. uni.login({
  160. success: async (result) => {
  161. const res = await User.wechatLog({ Code: result.code });
  162. if (res.Ret === 200) {
  163. let token = res.Data.Authorization;
  164. db.set("access_token", token);
  165. // 重新请求队列
  166. requestList.map((MT) => {
  167. MT();
  168. });
  169. requestList = [];
  170. isRefreshing = false;
  171. }
  172. },
  173. });
  174. }
  175. };