request.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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; // 祥粤云学堂:867735392558919680,祥粤学堂: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. ? {
  36. TenantId: tenantId,
  37. }
  38. : {
  39. AuthorizationToken:
  40. "WX " + (token ? token : uni.getStorageSync("token_temp")),
  41. TenantId: tenantId,
  42. },
  43. success: async (res) => {
  44. if (!options.compleLoading) {
  45. // 请求的接口有带compleLoading 就不隐藏加载中
  46. uni.hideLoading();
  47. }
  48. if (res.data.code == 401) {
  49. if (num <= 10) {
  50. if (!uni.getStorageSync("user_account")) {
  51. var pages = getCurrentPages(); // 获取栈实例
  52. let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
  53. if (currentRoute != "pages4/login/login") {
  54. uni.navigateTo({
  55. url: "/pages4/login/login",
  56. });
  57. uni.removeStorageSync("h5_code");
  58. }
  59. } else {
  60. num++;
  61. res = await doRequest(options);
  62. }
  63. } else {
  64. uni.removeStorageSync("user_account");
  65. var pages = getCurrentPages(); // 获取栈实例
  66. let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
  67. if (currentRoute != "pages4/login/login") {
  68. uni.navigateTo({
  69. url: "/pages4/login/login",
  70. });
  71. uni.removeStorageSync("h5_code");
  72. }
  73. }
  74. } else if (res.data.code == 409) {
  75. if(isLoginLose){
  76. return
  77. }
  78. isLoginLose = true
  79. uni.removeStorageSync("user_account");
  80. uni.removeStorageSync("token");
  81. uni.removeStorageSync("h5_code");
  82. uni.showModal({
  83. content: "已从其他设备登录,是否重新登录!",
  84. showCancel: false,
  85. success: (k) => {
  86. if (k.confirm) {
  87. isLoginLose = false
  88. uni.navigateTo({
  89. url: "/pages4/login/login",
  90. });
  91. }
  92. },
  93. });
  94. reject();
  95. }
  96. resolve(res);
  97. },
  98. fail: (err) => {
  99. uni.hideLoading();
  100. uni.showToast({
  101. title: "请求接口失败",
  102. icon: "none",
  103. });
  104. console.log("请求失败err---: ", options.url);
  105. if (options.url == "/face/certification/CompareFace") {
  106. console.log("人脸识别错误---1:", err);
  107. }
  108. reject(JSON.stringify(err));
  109. },
  110. complete: () => {
  111. // uni.hideLoading()
  112. // uni.hideToast()
  113. },
  114. });
  115. });
  116. async function doRequest(response) {
  117. let user_account = uni.getStorageSync("user_account");
  118. var datas = {
  119. url: "/refreshToken/" + user_account,
  120. method: "get",
  121. noToken: true,
  122. };
  123. const res = await myRequest(datas);
  124. if (res.data.code === 200) {
  125. uni.setStorageSync("token", res.data.data.token);
  126. var userInfo = {
  127. url: "/app/user/getInfo",
  128. method: "get",
  129. data: { fromPlat: 1 }, // 来源平台 1小程序 2PC网站
  130. };
  131. const resUser = await myRequest(userInfo);
  132. if (resUser.data.code === 200) {
  133. store.state.userInfo = resUser.data.data;
  134. uni.setStorageSync("user_account", user_account);
  135. num = 1;
  136. }
  137. let onset = await myRequest(response);
  138. return onset;
  139. } else {
  140. var pages = getCurrentPages(); // 获取栈实例
  141. let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
  142. console.log("request-->333:", currentRoute);
  143. if (currentRoute != "pages4/login/login") {
  144. uni.navigateTo({
  145. url: "/pages4/login/login",
  146. });
  147. uni.removeStorageSync("h5_code");
  148. }
  149. }
  150. }
  151. };
  152. function getTenantId() {
  153. return new Promise((resolve, reject) => {
  154. uni.request({
  155. url: config.BASE_URL + "/app/common/findTenantId",
  156. method: "get",
  157. data: {
  158. hostH5: host,
  159. },
  160. success: (res) => {
  161. if (res.data.code == 200) {
  162. resolve(res.data.data);
  163. } else {
  164. uni.showToast({
  165. title: "请求接口失败",
  166. icon: "none",
  167. });
  168. reject();
  169. }
  170. },
  171. fail: (err) => {
  172. uni.showToast({
  173. title: "请求接口失败",
  174. icon: "none",
  175. });
  176. reject(JSON.stringify(err));
  177. },
  178. });
  179. });
  180. }
  181. function setHost() {
  182. let host = window.location.host;
  183. return method.checkDomain(host) ? host : "120.79.166.78:19012";
  184. }