diff --git a/.merge_file_uAPEJh b/.merge_file_uAPEJh deleted file mode 100644 index e459224..0000000 --- a/.merge_file_uAPEJh +++ /dev/null @@ -1,112 +0,0 @@ -import axios, { AxiosInstance, AxiosError, AxiosRequestConfig, InternalAxiosRequestConfig, AxiosResponse } from "axios"; -import { showFullScreenLoading, tryHideFullScreenLoading } from "@/components/Loading/fullScreen"; -import { LOGIN_URL } from "@/config"; -import { ElMessage } from "element-plus"; -import { ResultData } from "@/api/interface"; -import { ResultEnum } from "@/enums/httpEnum"; -import { checkStatus } from "./helper/checkStatus"; -import { useUserStore } from "@/stores/modules/user"; -import router from "@/routers"; - -export interface CustomAxiosRequestConfig extends InternalAxiosRequestConfig { - loading?: boolean; -} - -const config = { - // 默认地址请求地址,可在 .env.** 文件中修改 - baseURL: import.meta.env.VITE_API_URL as string, - // 设置超时时间 - timeout: ResultEnum.TIMEOUT as number, - // 跨域时候允许携带凭证 - withCredentials: true, - // post请求指定数据类型以及编码 - headers: { 'Content-Type': 'application/json;charset=utf-8' } -}; - -class RequestHttp { - service: AxiosInstance; - public constructor(config: AxiosRequestConfig) { - // 创建实例 - this.service = axios.create(config); - - /** - * @description 请求拦截器 - * 客户端发送请求 -> [请求拦截器] -> 服务器 - * token校验(JWT) : 接受服务器返回的 token,存储到 vuex/pinia/本地储存当中 - */ - this.service.interceptors.request.use( - (config: CustomAxiosRequestConfig) => { - const userStore = useUserStore(); - // 当前请求不需要显示 loading,在 api 服务中通过指定的第三个参数: { loading: false } 来控制 - config.loading ?? (config.loading = true); - config.loading && showFullScreenLoading(); - if (config.headers && typeof config.headers.set === "function") { - config.headers.set("x-access-token", userStore.token); - } - return config; - }, - (error: AxiosError) => { - return Promise.reject(error); - } - ); - - /** - * @description 响应拦截器 - * 服务器换返回信息 -> [拦截统一处理] -> 客户端JS获取到信息 - */ - this.service.interceptors.response.use( - (response: AxiosResponse) => { - const { data } = response; - const userStore = useUserStore(); - tryHideFullScreenLoading(); - // 登陆失效 - if (data.code == ResultEnum.OVERDUE) { - userStore.setToken(""); - router.replace(LOGIN_URL); - ElMessage.error(data.message); - return Promise.reject(data); - } - // 全局错误信息拦截(防止下载文件的时候返回数据流,没有 code 直接报错) - if (data.code && data.code !== ResultEnum.SUCCESS) { - ElMessage.error(data.message); - return Promise.reject(data); - } - // 成功请求(在页面上除非特殊情况,否则不用处理失败逻辑) - return data; - }, - async (error: AxiosError) => { - const { response } = error; - tryHideFullScreenLoading(); - // 请求超时 && 网络错误单独判断,没有 response - if (error.message.indexOf("timeout") !== -1) ElMessage.error("请求超时!请您稍后重试"); - if (error.message.indexOf("Network Error") !== -1) ElMessage.error("网络错误!请您稍后重试"); - // 根据服务器响应的错误状态码,做不同的处理 - if (response) checkStatus(response.status); - // 服务器结果都没有返回(可能服务器错误可能客户端断网),断网处理:可以跳转到断网页面 - if (!window.navigator.onLine) router.replace("/500"); - return Promise.reject(error); - } - ); - } - - /** - * @description 常用请求方法封装 - */ - get(url: string, params?: object, _object = {}): Promise> { - return this.service.get(url, { params, ..._object }); - } - post(url: string, params?: object | string, _object = {}): Promise> { - return this.service.post(url, params, _object); - } - put(url: string, params?: object, _object = {}): Promise> { - return this.service.put(url, params, _object); - } - delete(url: string, params?: any, _object = {}): Promise> { - return this.service.delete(url, { params, ..._object }); - } - download(url: string, params?: object, _object = {}): Promise { - return this.service.post(url, params, { ..._object, responseType: "blob" }); - } -} - -export default new RequestHttp(config); diff --git a/frontend/src/api/index.ts b/frontend/src/api/index.ts index 65b7fb4..fc57d06 100644 --- a/frontend/src/api/index.ts +++ b/frontend/src/api/index.ts @@ -45,7 +45,7 @@ class RequestHttp { config.loading && showFullScreenLoading() if (config.headers && typeof config.headers.set === 'function') { config.headers.set('Authorization', 'Bearer ' + userStore.accessToken) - config.headers.set('Refresh-Token', userStore.refreshToken) + config.headers.set('Is-Refresh-Token', userStore.isRefreshToken+"") } return config }, @@ -67,6 +67,8 @@ class RequestHttp { if(data.code === ResultEnum.ACCESSTOKEN_EXPIRED){ // 用长token去换短token + userStore.setAccessToken(userStore.refreshToken) + userStore.setIsRefreshToken(true) const result = await refreshToken() if (result) { //获取新token成功的话 // 有新的token后,重新请求 diff --git a/frontend/src/stores/interface/index.ts b/frontend/src/stores/interface/index.ts index 7f7d416..9ec75a1 100644 --- a/frontend/src/stores/interface/index.ts +++ b/frontend/src/stores/interface/index.ts @@ -27,7 +27,9 @@ export interface GlobalState { /* UserState */ export interface UserState { - token: string; + accessToken: string; + refreshToken: string; + isRefreshToken: boolean; userInfo: { name: string }; } diff --git a/frontend/src/stores/modules/user.ts b/frontend/src/stores/modules/user.ts index ddd9db3..ea186f3 100644 --- a/frontend/src/stores/modules/user.ts +++ b/frontend/src/stores/modules/user.ts @@ -8,6 +8,7 @@ export const useUserStore = defineStore({ state: (): UserState => ({ accessToken: "", refreshToken: "", + isRefreshToken:false, userInfo: { name: "admin" }, }), getters: {}, @@ -19,6 +20,9 @@ export const useUserStore = defineStore({ setRefreshToken(refreshToken: string) { this.refreshToken = refreshToken; }, + setIsRefreshToken(isRefreshToken: boolean) { + this.isRefreshToken = isRefreshToken; + }, // Set setUserInfo setUserInfo(userInfo: UserState["userInfo"]) { this.userInfo = userInfo;