request.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import * as db from "./db.js";
  2. import { baseUrl } from "./config.js";
  3. import { User } from "@/config/api.js";
  4. import { modal } from "@/config/util.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. uni.showLoading({
  63. title: "加载中",
  64. });
  65. }
  66. return new Promise((resolve, reject) => {
  67. uni.request({
  68. url: baseUrl + url,
  69. data: data,
  70. header: {
  71. // 'Accept': 'application/json',
  72. // 'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
  73. Authorization: authHeader,
  74. },
  75. method: "GET",
  76. success: (res) => {
  77. status && uni.hideLoading();
  78. // 408 token失效
  79. if (res.data.Ret === 408) {
  80. refreshToken(getHttp, url, data, resolve);
  81. return;
  82. }
  83. res.data.Ret === 403 &&
  84. uni.showModal({
  85. title: "",
  86. confirmColor: "#3385FF",
  87. content: res.data.Msg,
  88. showCancel: false,
  89. });
  90. resolve(res.data);
  91. },
  92. fail: (error) => {
  93. status && uni.hideLoading();
  94. if (error && error.response) {
  95. showError(error.response);
  96. }
  97. },
  98. complete: () => {
  99. setTimeout(() => {
  100. status && uni.hideLoading();
  101. }, 250);
  102. },
  103. });
  104. });
  105. };
  106. // post请求
  107. export const postHttp = (url, data, status = 1) => {
  108. // 获取用户token和用户信息
  109. let token = db.get("access_token");
  110. let authHeader = token || "";
  111. if (status) {
  112. uni.showLoading({
  113. title: "加载中",
  114. });
  115. }
  116. return new Promise((resolve, reject) => {
  117. uni.request({
  118. url: baseUrl + url,
  119. data: data,
  120. header: {
  121. // 'Accept': 'application/json',
  122. "Content-Type": "application/json",
  123. Authorization: authHeader,
  124. },
  125. method: "POST",
  126. success: (res) => {
  127. status && uni.hideLoading();
  128. // 408 token失效
  129. if (res.data.Ret === 408) {
  130. refreshToken(postHttp, url, data, resolve);
  131. return;
  132. }
  133. res.data.Ret === 403 &&
  134. uni.showModal({
  135. title: "",
  136. confirmColor: "#3385FF",
  137. content: res.data.Msg,
  138. showCancel: false,
  139. });
  140. resolve(res.data);
  141. },
  142. fail: (error) => {
  143. status && uni.hideLoading();
  144. if (error && error.response) {
  145. showError(error.response);
  146. }
  147. },
  148. complete: () => {
  149. setTimeout(() => {
  150. status && uni.hideLoading();
  151. }, 250);
  152. },
  153. });
  154. });
  155. };
  156. let requestList = []; //存放token失效时请求队列
  157. let isRefreshing = false; //是否正在刷新token
  158. const refreshToken = async (type, url, data, resolve) => {
  159. requestList.push(() => {
  160. resolve(type(url, data));
  161. });
  162. if (!isRefreshing) {
  163. isRefreshing = true;
  164. uni.login({
  165. success: async (result) => {
  166. const res = await User.wechatLog({ Code: result.code });
  167. if (res.Ret === 200) {
  168. let token = res.Data.Authorization;
  169. db.set("access_token", token);
  170. // 重新请求队列
  171. requestList.map((MT) => {
  172. MT();
  173. });
  174. requestList = [];
  175. isRefreshing = false;
  176. }
  177. },
  178. });
  179. }
  180. };