import Request from "luch-request"; import { isEmpty } from "radash"; import { userApi } from "@@/api"; import { router, constant } from "@@/index"; const options = { // 显示操作成功消息 默认不显示 showSuccess: false, // 成功提醒 默认使用后端返回值 successMsg: "", // 显示失败消息 默认显示 showError: true, // 失败提醒 默认使用后端返回信息 errorMsg: "", // 显示请求时loading模态框 默认显示 showLoading: true, // loading提醒文字 loadingMsg: "加载中", // 需要授权才能请求 默认放开 auth: false, }; let LoadingInstance = { target: null, count: 0, }; function closeLoading() { if (LoadingInstance.count > 0) LoadingInstance.count--; if (LoadingInstance.count === 0) uni.hideLoading(); } const http = new Request({ baseURL: __APP_SERVICE__, timeout: 8000, method: "GET", header: { Accept: "*/*", "Content-Type": "application/json;charset=UTF-8", Authorization: "", "tenant-id": "", }, withCredentials: true, custom: options, }); http.interceptors.request.use( (config) => { const tokenKey = constant.storageConstant.TOKEN; const tokenStorage = uni.getStorageSync(tokenKey); const config_header: any = config?.header; config_header["tenant-id"] = "1"; if (tokenStorage && tokenStorage.accessToken && config_header) { config_header.Authorization = tokenStorage.accessToken; } if (config.custom && config.custom.showLoading) { LoadingInstance.count++; LoadingInstance.count === 1 && uni.showLoading({ title: config.custom.loadingMsg, mask: true, fail: () => { uni.hideLoading(); }, }); } return config; }, (error) => { return Promise.reject(error); } ); http.interceptors.response.use( async (response) => { response.config && response.config.custom && response.config.custom.showLoading && closeLoading(); if (response.data.code !== 0) { if ( response.config && response.config.custom && response.config.custom.showError ) { switch (response.data.code) { case 401: setTimeout(() => { const tokenKey = constant.storageConstant.TOKEN; const userInfoKey = constant.storageConstant.USERINFO; uni.removeStorageSync(tokenKey); uni.removeStorageSync(userInfoKey); router.jump("/pages/tabbar/my", {}, 2); }, 500); break; case 500: uni.showToast({ title: response.data.message || "服务器开小差啦,请稍后再试~", icon: "none", mask: true, }); break; } return Promise.resolve(response.data); } } if ( response.data.code === 200 && response.data.message !== "" && response.config && response.config.custom && response.config.custom.showSuccess ) { uni.showToast({ title: response.config.custom.successMsg || response.data.message, icon: "none", }); } const tokenKey = constant.storageConstant.TOKEN; const tokenStorage = uni.getStorageSync(tokenKey); if ( !isEmpty(tokenStorage) && response.config.url != "/sys/auth/refresh-token" ) { const { refreshToken, expiresTime } = tokenStorage; const now = new Date().getTime(); const remain = expiresTime - now; if (remain <= 1000 * 60 * 20) { const _pa = { refreshToken: refreshToken }; const refreshData = await userApi.refreshToken(_pa); console.log(refreshData, "refreshData"); if (!isEmpty(refreshData.data)) { uni.setStorageSync(tokenKey, refreshData.data); } } } return Promise.resolve(response.data); }, (error) => { let errorMessage = ""; if (error !== undefined) { if (errorMessage) { uni.showToast({ title: errorMessage || "", icon: "none", mask: true, }); } } return false; } ); const request = (config: any) => { return http.middleware(config); }; export default request;