| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- import store from "@/store/index.js";
- import config from "@/common/config";
- import { checkDomain } from "@/common/tool";
- var num = 1;
- //接口api
- // export const BASE_URL = 'https://api.xyyxt.net' //release
- export const BASE_URL = config.BASE_URL;
- export const host = setHost();
- export let tenantId = uni.getStorageSync(host) || "";
- export const myRequest = (options) => {
- if (store.state.allowLoading && !options.noLoading) {
- uni.showLoading({
- title: "拼命加载中...",
- mask: true,
- });
- }
- return new Promise(async (resolve, reject) => {
- if (!tenantId) {
- tenantId = await getTenantId();
- uni.setStorageSync(host, tenantId);
- }
- let token = uni.getStorageSync("token");
- uni.request({
- url: BASE_URL + options.url,
- method: options.method || "GET",
- data: options.data,
- header: options.noToken
- ? {
- TenantId: tenantId,
- }
- : {
- AuthorizationToken:
- "Bearer " + (token ? token : uni.getStorageSync("token_temp")),
- TenantId: tenantId,
- },
- success: async (res) => {
- if (!options.compleLoading) {
- // 请求的接口有带compleLoading 就不隐藏加载中
- uni.hideLoading();
- }
- if (res.data.code == 401) {
- if (num <= 2) {
- if (!uni.getStorageSync("user_account")) {
- var pages = getCurrentPages(); // 获取栈实例
- let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
- if (currentRoute != "pages/login/login") {
- uni.navigateTo({
- url: "/pages/login/login",
- });
- }
- } else {
- num++;
- res = await doRequest(options);
- }
- } else {
- uni.removeStorageSync("user_account");
- var pages = getCurrentPages(); // 获取栈实例
- let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
- if (currentRoute != "pages/login/login") {
- uni.navigateTo({
- url: "/pages/login/login",
- });
- }
- }
- } else if (res.data.code == 500) {
- uni.showToast({
- icon: "none",
- title: res.data.msg,
- });
- reject();
- } else if (res.data.code == 200) {
- if (res.data.hasOwnProperty("rows")) {
- resolve(res.data);
- }
- resolve(res.data.data);
- }
- },
- fail: (err) => {
- uni.hideLoading();
- uni.showToast({
- title: "请求接口失败",
- icon: "none",
- });
- reject(JSON.stringify(err));
- },
- });
- });
- async function doRequest(response) {
- let user_account = uni.getStorageSync("user_account");
- try {
- const res = await myRequest({
- url: "/app/common/seller/refreshToken/" + user_account,
- method: "get",
- noToken: true,
- });
- uni.setStorageSync("token", res.token);
- num = 1;
- store.dispatch("getUserInfo");
- myRequest(response);
- } catch (error) {
- uni.navigateTo({
- url: "/pages/login/login",
- });
- }
- }
- };
- function getTenantId() {
- return new Promise((resolve, reject) => {
- uni.request({
- url: BASE_URL + "/common/free/findTenantId",
- method: "get",
- data: {
- hostH5Seller: host,
- },
- success: (res) => {
- if (res.data.code == 200) {
- resolve(res.data.data);
- } else {
- uni.showToast({
- title: "请求接口失败",
- icon: "none",
- });
- reject();
- }
- },
- fail: (err) => {
- uni.showToast({
- title: "请求接口失败",
- icon: "none",
- });
- reject(JSON.stringify(err));
- },
- });
- });
- }
- function setHost() {
- let host = window.location.host;
- return checkDomain(host) ? host : "120.79.166.78:19012";
- }
|