Commit c41dbd09 authored by hewenhao's avatar hewenhao

feat: token

parent 38fa81e4
import axios, { AxiosRequestConfig } from "axios"; import axios, { AxiosRequestConfig } from "axios";
import { unref } from "vue";
import router from "../router";
const instance = axios.create({ const instance = axios.create({
// headers: { // headers: {
// 'content-type': 'application/json;charset=UTF-8', // 'content-type': 'application/json;charset=UTF-8',
...@@ -39,9 +42,30 @@ const instance = axios.create({ ...@@ -39,9 +42,30 @@ const instance = axios.create({
// } // }
// } // }
let globalToken: string | null = null;
// 拦截器 在请求之前拦截 // 拦截器 在请求之前拦截
instance.interceptors.request.use( instance.interceptors.request.use(
(config: AxiosRequestConfig) => { (config: AxiosRequestConfig) => {
const route = unref(router.currentRoute);
let token = route.query.token;
token = Array.isArray(token) ? token[0] : token;
// refresh token
if (token && token !== globalToken) {
globalToken = token;
}
if (globalToken) {
const pre = config.headers || {};
return {
...config,
headers: {
...pre,
Authorization: `Bearer ${globalToken}`
}
};
}
return config; return config;
}, },
error => { error => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment