request.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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: "WX " + (token ? token : uni.getStorageSync(
  40. "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. } else if (res.data.code == 601) {
  96. uni.showModal({
  97. content: res.data.msg,
  98. showCancel: false,
  99. success: (k) => {
  100. if (k.confirm) {
  101. uni.switchTab({
  102. url: "/pages/index/index",
  103. });
  104. }
  105. },
  106. });
  107. reject();
  108. } else if (res.data.code == 699) {
  109. // #ifdef MP-WEIXIN
  110. resolve(res);
  111. // #endif
  112. // #ifdef H5
  113. window.open(res.data.msg)
  114. reject();
  115. // #endif
  116. }
  117. resolve(res);
  118. },
  119. fail: (err) => {
  120. uni.hideLoading();
  121. uni.showToast({
  122. title: "请求接口失败",
  123. icon: "none",
  124. });
  125. console.log("请求失败err---: ", options.url);
  126. if (options.url == "/face/certification/CompareFace") {
  127. console.log("人脸识别错误---1:", err);
  128. }
  129. reject(JSON.stringify(err));
  130. },
  131. complete: () => {
  132. // uni.hideLoading()
  133. // uni.hideToast()
  134. },
  135. });
  136. });
  137. async function doRequest(response) {
  138. let user_account = uni.getStorageSync("user_account");
  139. var datas = {
  140. url: "/refreshToken/" + user_account,
  141. method: "get",
  142. noToken: true,
  143. };
  144. const res = await myRequest(datas);
  145. if (res.data.code === 200) {
  146. uni.setStorageSync("token", res.data.data.token);
  147. var userInfo = {
  148. url: "/app/user/getInfo",
  149. method: "get",
  150. data: {
  151. fromPlat: 1
  152. }, // 来源平台 1小程序 2PC网站
  153. };
  154. const resUser = await myRequest(userInfo);
  155. if (resUser.data.code === 200) {
  156. store.state.userInfo = resUser.data.data;
  157. uni.setStorageSync("user_account", user_account);
  158. num = 1;
  159. }
  160. let onset = await myRequest(response);
  161. return onset;
  162. } else {
  163. var pages = getCurrentPages(); // 获取栈实例
  164. let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
  165. console.log("request-->333:", currentRoute);
  166. if (currentRoute != "pages4/login/login") {
  167. uni.navigateTo({
  168. url: "/pages4/login/login",
  169. });
  170. uni.removeStorageSync("h5_code");
  171. }
  172. }
  173. }
  174. };
  175. function getTenantId() {
  176. return new Promise((resolve, reject) => {
  177. uni.request({
  178. url: config.BASE_URL + "/app/common/findTenantId",
  179. method: "get",
  180. data: {
  181. hostH5: host,
  182. },
  183. success: (res) => {
  184. if (res.data.code == 200) {
  185. resolve(res.data.data);
  186. } else {
  187. uni.showToast({
  188. title: "请求接口失败",
  189. icon: "none",
  190. });
  191. reject();
  192. }
  193. },
  194. fail: (err) => {
  195. uni.showToast({
  196. title: "请求接口失败",
  197. icon: "none",
  198. });
  199. reject(JSON.stringify(err));
  200. },
  201. });
  202. });
  203. }
  204. function setHost() {
  205. let host = window.location.host;
  206. return method.checkDomain(host) ? host : "120.79.166.78:19012";
  207. }