request.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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("seller_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: "Bearer " + token,
  33. TenantId: tenantId,
  34. },
  35. success: async (res) => {
  36. if (!options.compleLoading) {
  37. // 请求的接口有带compleLoading 就不隐藏加载中
  38. uni.hideLoading();
  39. }
  40. if (res.data.code == 401) {
  41. if (num <= 2) {
  42. if (!uni.getStorageSync("seller_account")) {
  43. var pages = getCurrentPages(); // 获取栈实例
  44. let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
  45. if (currentRoute != "pages/login/login") {
  46. uni.navigateTo({
  47. url: "/pages/login/login",
  48. });
  49. }
  50. } else {
  51. num++;
  52. res = await doRequest(options);
  53. }
  54. } else {
  55. uni.removeStorageSync("seller_account");
  56. var pages = getCurrentPages(); // 获取栈实例
  57. let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
  58. if (currentRoute != "pages/login/login") {
  59. uni.navigateTo({
  60. url: "/pages/login/login",
  61. });
  62. }
  63. }
  64. } else if (res.data.code == 500) {
  65. uni.showToast({
  66. icon: "none",
  67. title: res.data.msg,
  68. });
  69. reject();
  70. } else if (res.data.code == 200) {
  71. if (res.data.hasOwnProperty("rows")) {
  72. resolve(res.data);
  73. }
  74. resolve(res.data.data);
  75. }
  76. },
  77. fail: (err) => {
  78. uni.hideLoading();
  79. uni.showToast({
  80. title: "请求接口失败",
  81. icon: "none",
  82. });
  83. reject(JSON.stringify(err));
  84. },
  85. });
  86. });
  87. async function doRequest(response) {
  88. let seller_account = uni.getStorageSync("seller_account");
  89. try {
  90. const res = await myRequest({
  91. url: "/app/common/seller/refreshToken/" + seller_account,
  92. method: "get",
  93. noToken: true,
  94. });
  95. uni.setStorageSync("seller_token", res.token);
  96. num = 1;
  97. store.dispatch("getUserInfo");
  98. myRequest(response);
  99. } catch (error) {
  100. uni.navigateTo({
  101. url: "/pages/login/login",
  102. });
  103. }
  104. }
  105. };
  106. function getTenantId() {
  107. return new Promise((resolve, reject) => {
  108. uni.request({
  109. url: BASE_URL + "/common/free/findTenantId",
  110. method: "get",
  111. data: {
  112. hostH5Seller: host,
  113. },
  114. success: (res) => {
  115. if (res.data.code == 200) {
  116. resolve(res.data.data);
  117. } else {
  118. uni.showToast({
  119. title: "请求接口失败",
  120. icon: "none",
  121. });
  122. reject();
  123. }
  124. },
  125. fail: (err) => {
  126. uni.showToast({
  127. title: "请求接口失败",
  128. icon: "none",
  129. });
  130. reject(JSON.stringify(err));
  131. },
  132. });
  133. });
  134. }
  135. function setHost() {
  136. let host = window.location.host;
  137. return checkDomain(host) ? host : "120.79.166.78:19012";
  138. }