request.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "Bearer " + (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. try {
  91. const res = await myRequest({
  92. url: "/app/common/seller/refreshToken/" + user_account,
  93. method: "get",
  94. noToken: true,
  95. });
  96. uni.setStorageSync("token", res.token);
  97. num = 1;
  98. store.dispatch("getUserInfo");
  99. myRequest(response);
  100. } catch (error) {
  101. uni.navigateTo({
  102. url: "/pages/login/login",
  103. });
  104. }
  105. }
  106. };
  107. function getTenantId() {
  108. return new Promise((resolve, reject) => {
  109. uni.request({
  110. url: BASE_URL + "/common/free/findTenantId",
  111. method: "get",
  112. data: {
  113. hostH5Seller: host,
  114. },
  115. success: (res) => {
  116. if (res.data.code == 200) {
  117. resolve(res.data.data);
  118. } else {
  119. uni.showToast({
  120. title: "请求接口失败",
  121. icon: "none",
  122. });
  123. reject();
  124. }
  125. },
  126. fail: (err) => {
  127. uni.showToast({
  128. title: "请求接口失败",
  129. icon: "none",
  130. });
  131. reject(JSON.stringify(err));
  132. },
  133. });
  134. });
  135. }
  136. function setHost() {
  137. let host = window.location.host;
  138. return checkDomain(host) ? host : "120.79.166.78:19012";
  139. }