diff --git a/src/sheep/utils/request.ts b/src/sheep/utils/request.ts index 984f71e..ee1aafe 100644 --- a/src/sheep/utils/request.ts +++ b/src/sheep/utils/request.ts @@ -1,175 +1,160 @@ -import Request from "luch-request"; -import { isEmpty } from "radash"; +import Request from "luch-request" +import { isEmpty } from "radash" -import { userApi } from "@@/api"; -import { router, constant } from "@@/index"; +import { userApi } from "@@/api" +import { router, constant } from "@@/index" const options = { - // 显示操作成功消息 默认不显示 - showSuccess: false, - // 成功提醒 默认使用后端返回值 - successMsg: "", - // 显示失败消息 默认显示 - showError: true, - // 失败提醒 默认使用后端返回信息 - errorMsg: "", - // 显示请求时loading模态框 默认显示 - showLoading: true, - // loading提醒文字 - loadingMsg: "加载中", - // 需要授权才能请求 默认放开 - auth: false, -}; + // 显示操作成功消息 默认不显示 + showSuccess: false, + // 成功提醒 默认使用后端返回值 + successMsg: "", + // 显示失败消息 默认显示 + showError: true, + // 失败提醒 默认使用后端返回信息 + errorMsg: "", + // 显示请求时loading模态框 默认显示 + showLoading: true, + // loading提醒文字 + loadingMsg: "加载中", + // 需要授权才能请求 默认放开 + auth: false, +} let LoadingInstance = { - target: null, - count: 0, -}; + target: null, + count: 0, +} function closeLoading() { - if (LoadingInstance.count > 0) LoadingInstance.count--; - if (LoadingInstance.count === 0) uni.hideLoading(); + if (LoadingInstance.count > 0) LoadingInstance.count-- + if (LoadingInstance.count === 0) uni.hideLoading() } const http = new Request({ - baseURL: import.meta.env.VITE_API_URL, - timeout: 8000, - method: "GET", - header: { - Accept: "*/*", - "Content-Type": "application/json;charset=UTF-8", - }, - withCredentials: true, - custom: options, -}); + baseURL: import.meta.env.VITE_API_URL, + timeout: 8000, + method: "GET", + header: { + Accept: "*/*", + "Content-Type": "application/json;charset=UTF-8", + }, + 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; - // 添加终端类型 - const isToc = import.meta.env.VITE_IS_TOC; - if (isToc === "true") { - config_header["CLIENT-TOC"] = "Y"; - } - config_header["TENANT-ID"] = "1"; - if ( - config.url != "/sys/auth/refresh-token" && - tokenStorage && - tokenStorage.access_token && - config_header - ) { - config_header.Authorization = `Bearer ${tokenStorage.access_token}`; - } - 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); - } -); + (config) => { + const tokenKey = constant.storageConstant.TOKEN + const tokenStorage = uni.getStorageSync(tokenKey) + const config_header: any = config?.header + // 添加终端类型 + const isToc = import.meta.env.VITE_IS_TOC + if (isToc === "true") { + config_header["CLIENT-TOC"] = "Y" + } + config_header["TENANT-ID"] = "1" + if (config.url != "/sys/auth/refresh-token" && tokenStorage && tokenStorage.access_token && config_header) { + config_header.Authorization = `Bearer ${tokenStorage.access_token}` + } + 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.config.url === "/auth/oauth2/token") { - return Promise.resolve({ - code: 0, - data: response.data, - msg: null, - }); - } - 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; - default: - uni.showToast({ - title: response.data.msg || "服务器开小差啦,请稍后再试~", - 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 { refresh_token, expiresTime } = tokenStorage; - if (!isEmpty(refresh_token) && !isEmpty(expiresTime)) { - const now = new Date().getTime(); - const remain = expiresTime - now; - if (remain <= 1000 * 60 * 20) { - const _pa = { refreshToken: refresh_token }; - const refreshData = await userApi.refreshToken(_pa); - if (!isEmpty(refreshData.data)) { - uni.setStorageSync(tokenKey, refreshData.data); - } - } - } - } - return Promise.resolve(response.data); - }, - (error) => { - console.log(error); + async (response) => { + response.config && response.config.custom && response.config.custom.showLoading && closeLoading() + if (response.config.url === "/auth/oauth2/token") { + return Promise.resolve({ + code: 0, + data: response.data, + msg: null, + }) + } + 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 + default: + uni.showToast({ + title: response.data.msg || "服务器开小差啦,请稍后再试~", + 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 { refresh_token, expiresTime } = tokenStorage + if (!isEmpty(refresh_token) && !isEmpty(expiresTime)) { + const now = new Date().getTime() + const remain = expiresTime - now + if (remain <= 1000 * 60 * 20) { + const _pa = { refreshToken: refresh_token } + const refreshData = await userApi.refreshToken(_pa) + if (!isEmpty(refreshData.data)) { + uni.setStorageSync(tokenKey, refreshData.data) + } + } + } + } + return Promise.resolve(response.data) + }, + (error) => { + console.log(error) - let errorMessage = error.data.msg; - if (error !== undefined) { - uni.showToast({ - title: errorMessage || "", - icon: "none", - mask: true, - }); - } - return false; - } -); + let errorMessage = error.data.msg + if (error !== undefined) { + uni.showToast({ + title: errorMessage || "", + icon: "none", + mask: true, + }) + } + return false + }, +) const request = (config: any) => { - return http.middleware(config); -}; + return http.middleware(config) +} -export default request; +export default request