request.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import store from "@/store/index.js";
  2. import config from "@/common/config";
  3. import method from "@/common/methodTool";
  4. var num = 1;
  5. let isLoginLose = false
  6. //接口api
  7. export const BASE_URL = config.BASE_URL;
  8. // #ifdef MP-WEIXIN
  9. export let tenantId = config.tenantId; // 祥粤云学堂:1,祥粤学堂:667735392758919630, 中建云学堂-567735392758918520
  10. // #endif
  11. // #ifdef H5
  12. export const host = setHost();
  13. export let tenantId = uni.getStorageSync(host) || "";
  14. // #endif
  15. export const myRequest = (options) => {
  16. if (store.state.allowLoading && !options.noLoading) {
  17. uni.showLoading({
  18. title: "拼命加载中...",
  19. mask: true,
  20. });
  21. }
  22. return new Promise(async (resolve, reject) => {
  23. // #ifdef H5
  24. if (!tenantId) {
  25. tenantId = await getTenantId();
  26. uni.setStorageSync(host, tenantId);
  27. }
  28. // #endif
  29. let token = uni.getStorageSync("token");
  30. uni.request({
  31. url: BASE_URL + options.url,
  32. method: options.method || "GET",
  33. data: options.data,
  34. header: options.noToken ? {
  35. TenantId: tenantId,
  36. } : {
  37. AuthorizationToken: "WX " + (token ? token : uni.getStorageSync(
  38. "token_temp")),
  39. TenantId: tenantId,
  40. },
  41. success: async (res) => {
  42. if (!options.compleLoading) {
  43. // 请求的接口有带compleLoading 就不隐藏加载中
  44. uni.hideLoading();
  45. }
  46. if (res.data.code == 401) {
  47. if (num <= 10) {
  48. if (!uni.getStorageSync("user_account")) {
  49. var pages = getCurrentPages(); // 获取栈实例
  50. let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
  51. if (currentRoute != "pages4/login/login") {
  52. uni.navigateTo({
  53. url: "/pages4/login/login",
  54. });
  55. uni.removeStorageSync("h5_code");
  56. }
  57. } else {
  58. num++;
  59. res = await doRequest(options);
  60. }
  61. } else {
  62. uni.removeStorageSync("user_account");
  63. var pages = getCurrentPages(); // 获取栈实例
  64. let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
  65. if (currentRoute != "pages4/login/login") {
  66. uni.navigateTo({
  67. url: "/pages4/login/login",
  68. });
  69. uni.removeStorageSync("h5_code");
  70. }
  71. }
  72. } else if (res.data.code == 409) {
  73. if (isLoginLose) {
  74. return
  75. }
  76. isLoginLose = true
  77. uni.removeStorageSync("user_account");
  78. uni.removeStorageSync("token");
  79. uni.removeStorageSync("h5_code");
  80. uni.showModal({
  81. content: "已从其他设备登录,是否重新登录!",
  82. showCancel: false,
  83. success: (k) => {
  84. if (k.confirm) {
  85. isLoginLose = false
  86. uni.navigateTo({
  87. url: "/pages4/login/login",
  88. });
  89. }
  90. },
  91. });
  92. reject();
  93. } else if (res.data.code == 601) {
  94. uni.switchTab({
  95. url: "/pages/learn/index",
  96. });
  97. uni.showModal({
  98. content: res.data.msg,
  99. showCancel: false,
  100. success: (k) => {
  101. if (k.confirm) {}
  102. },
  103. });
  104. reject();
  105. } else if (res.data.code == 699) {
  106. // #ifdef MP-WEIXIN
  107. resolve(res);
  108. // #endif
  109. // #ifdef H5
  110. window.location.href = res.data.msg
  111. reject();
  112. // #endif
  113. }
  114. resolve(res);
  115. },
  116. fail: (err) => {
  117. uni.hideLoading();
  118. uni.showToast({
  119. title: "系统升级中",
  120. icon: "none",
  121. });
  122. console.log("请求失败err---: ", options.url);
  123. if (options.url == "/face/certification/CompareFace") {
  124. console.log("人脸识别错误---1:", err);
  125. }
  126. reject(JSON.stringify(err));
  127. },
  128. complete: () => {
  129. // uni.hideLoading()
  130. // uni.hideToast()
  131. },
  132. });
  133. });
  134. async function doRequest(response) {
  135. let user_account = uni.getStorageSync("user_account");
  136. var datas = {
  137. url: "/refreshToken/" + user_account,
  138. method: "get",
  139. noToken: true,
  140. };
  141. const res = await myRequest(datas);
  142. if (res.data.code === 200) {
  143. uni.setStorageSync("token", res.data.data.token);
  144. var userInfo = {
  145. url: "/app/user/getInfo",
  146. method: "get",
  147. data: {
  148. fromPlat: 1
  149. }, // 来源平台 1小程序 2PC网站
  150. };
  151. const resUser = await myRequest(userInfo);
  152. if (resUser.data.code === 200) {
  153. store.state.userInfo = resUser.data.data;
  154. uni.setStorageSync("user_account", user_account);
  155. num = 1;
  156. }
  157. let onset = await myRequest(response);
  158. return onset;
  159. } else {
  160. var pages = getCurrentPages(); // 获取栈实例
  161. let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
  162. console.log("request-->333:", currentRoute);
  163. if (currentRoute != "pages4/login/login") {
  164. uni.navigateTo({
  165. url: "/pages4/login/login",
  166. });
  167. uni.removeStorageSync("h5_code");
  168. }
  169. }
  170. }
  171. };
  172. function getTenantId() {
  173. return new Promise((resolve, reject) => {
  174. uni.request({
  175. url: config.BASE_URL + "/app/common/findTenantId",
  176. method: "get",
  177. data: {
  178. hostH5: host,
  179. },
  180. success: (res) => {
  181. if (res.data.code == 200) {
  182. resolve(res.data.data);
  183. } else {
  184. uni.showToast({
  185. title: "请求接口失败",
  186. icon: "none",
  187. });
  188. reject();
  189. }
  190. },
  191. fail: (err) => {
  192. uni.showToast({
  193. title: "请求接口失败",
  194. icon: "none",
  195. });
  196. reject(JSON.stringify(err));
  197. },
  198. });
  199. });
  200. }
  201. function setHost() {
  202. let host = window.location.host;
  203. return method.checkDomain(host) ? host : "120.79.166.78:19012";
  204. }