request.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import store from "@/store/index.js";
  2. import config from "@/common/config";
  3. import { checkDomain } from "@/common/tool";
  4. var num = 1;
  5. //接口api
  6. // export const BASE_URL = 'https://api.xyyxt.net' //release
  7. export const BASE_URL = config.BASE_URL;
  8. export const host = setHost();
  9. export let tenantId = uni.getStorageSync(host) || "";
  10. export const myRequest = (options) => {
  11. if (store.state.allowLoading && !options.noLoading) {
  12. uni.showLoading({
  13. title: "拼命加载中...",
  14. mask: true,
  15. });
  16. }
  17. return new Promise(async (resolve, reject) => {
  18. if (!tenantId) {
  19. tenantId = await getTenantId();
  20. uni.setStorageSync(host, tenantId);
  21. }
  22. let token = uni.getStorageSync("token");
  23. uni.request({
  24. url: BASE_URL + options.url,
  25. method: options.method || "GET",
  26. data: options.data,
  27. header: options.noToken
  28. ? {
  29. TenantId: tenantId,
  30. }
  31. : {
  32. AuthorizationToken:
  33. "SE " + (token ? token : uni.getStorageSync("token_temp")),
  34. TenantId: tenantId,
  35. },
  36. success: async (res) => {
  37. if (!options.compleLoading) {
  38. // 请求的接口有带compleLoading 就不隐藏加载中
  39. uni.hideLoading();
  40. }
  41. if (res.data.code == 401) {
  42. if (num <= 2) {
  43. if (!uni.getStorageSync("user_account")) {
  44. var pages = getCurrentPages(); // 获取栈实例
  45. let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
  46. if (currentRoute != "pages/login/login") {
  47. uni.navigateTo({
  48. url: "/pages/login/login",
  49. });
  50. }
  51. } else {
  52. num++;
  53. res = await doRequest(options);
  54. }
  55. } else {
  56. uni.removeStorageSync("user_account");
  57. var pages = getCurrentPages(); // 获取栈实例
  58. let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
  59. if (currentRoute != "pages/login/login") {
  60. uni.navigateTo({
  61. url: "/pages/login/login",
  62. });
  63. }
  64. }
  65. } else if (res.data.code == 500) {
  66. uni.showToast({
  67. icon: "none",
  68. title: res.data.msg,
  69. });
  70. reject();
  71. } else if (res.data.code == 200) {
  72. if (res.data.hasOwnProperty("rows")) {
  73. resolve(res.data);
  74. }
  75. resolve(res.data.data);
  76. }
  77. },
  78. fail: (err) => {
  79. uni.hideLoading();
  80. uni.showToast({
  81. title: "请求接口失败",
  82. icon: "none",
  83. });
  84. reject(JSON.stringify(err));
  85. },
  86. });
  87. });
  88. async function doRequest(response) {
  89. let user_account = uni.getStorageSync("user_account");
  90. var datas = {
  91. url: "/app/common/seller/refreshToken/" + user_account,
  92. method: "get",
  93. noToken: true,
  94. };
  95. const res = await myRequest(datas);
  96. if (res.data.code === 200) {
  97. uni.setStorageSync("token", res.data.data.token);
  98. var userInfo = {
  99. url: "/app/user/getInfo",
  100. method: "get",
  101. data: { fromPlat: 1 }, // 来源平台 1小程序 2PC网站
  102. };
  103. const resUser = await myRequest(userInfo);
  104. if (resUser.data.code === 200) {
  105. store.state.userInfo = resUser.data.data;
  106. uni.setStorageSync("user_account", user_account);
  107. num = 1;
  108. }
  109. let onset = await myRequest(response);
  110. return onset;
  111. } else {
  112. var pages = getCurrentPages(); // 获取栈实例
  113. let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
  114. console.log("request-->333:", currentRoute);
  115. if (currentRoute != "pages4/login/login") {
  116. uni.navigateTo({
  117. url: "/pages4/login/login",
  118. });
  119. }
  120. }
  121. }
  122. };
  123. function getTenantId() {
  124. return new Promise((resolve, reject) => {
  125. uni.request({
  126. url: BASE_URL + "/app/common/findTenantId",
  127. method: "get",
  128. data: {
  129. hostH5Seller: host,
  130. },
  131. success: (res) => {
  132. if (res.data.code == 200) {
  133. resolve(res.data.data);
  134. } else {
  135. uni.showToast({
  136. title: "请求接口失败",
  137. icon: "none",
  138. });
  139. reject();
  140. }
  141. },
  142. fail: (err) => {
  143. uni.showToast({
  144. title: "请求接口失败",
  145. icon: "none",
  146. });
  147. reject(JSON.stringify(err));
  148. },
  149. });
  150. });
  151. }
  152. function setHost() {
  153. let host = window.location.host;
  154. return checkDomain(host) ? host : "120.79.166.78:19012";
  155. }