You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shop-pc/plugins/axiosTk.js

54 lines
1.4 KiB

3 years ago
/*
* @Author: ch
* @Date: 2022-05-04 17:11:07
* @LastEditors: ch
2 years ago
* @LastEditTime: 2022-06-29 16:01:55
3 years ago
* @Description: file content
*/
2 years ago
import { CreateUUID } from "@/plugins/utils";
import { UUID_KEY } from "@/constants";
3 years ago
let axiosTk = null;
export default function ({$axios, store, route, req}, inject) {
3 years ago
const $axiosTk = $axios.create();
2 years ago
let uuid = store.state.uuid;
if (!uuid && req.headers.cookie) {
uuid = req.headers.cookie.split(';').find(i => i.includes(UUID_KEY));
uuid = uuid && uuid.replace(`${UUID_KEY}=`,'')
}
2 years ago
if (!uuid) {
2 years ago
uuid = CreateUUID(16, 2);
2 years ago
store.commit('setUUID', uuid);
}
$axiosTk.defaults.timeout = 3000;
3 years ago
$axiosTk.onRequest( config =>{
2 years ago
config.headers.uid = uuid;
2 years ago
if(!store.state.token && !config.headers.notVerifyToken){
location.href = '/';
return Promise.reject({message : '要先登录才能操作哦~'});
3 years ago
}
2 years ago
delete config.headers.notVerifyToken;
3 years ago
config.headers.Authorization = store.state.token;
return config;
});
$axiosTk.onResponse(response => {
const result = response.data;
if(response.status === 200){
if(result.code === 'SUCCESS'){
return result.data;
}
if(result.code === 'TOKEN_FAIL'){
store.commit('setLoginOut');
store.commit('setLoginVisible');
}
return Promise.reject(result);
}
return Promise.reject({message:'请求出错'});
});
3 years ago
inject('$axiosTk', $axiosTk);
axiosTk = $axiosTk;
}
export {axiosTk}