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

39 lines
958 B

3 years ago
/*
* @Author: ch
* @Date: 2022-05-04 17:11:07
* @LastEditors: ch
* @LastEditTime: 2022-05-10 14:30:24
3 years ago
* @Description: file content
*/
let axiosTk = null;
export default function ({$axios, store, route}, inject) {
3 years ago
const $axiosTk = $axios.create();
$axiosTk.onRequest( config =>{
if(!store.state.token){
location.href = '/';
3 years ago
return Promise.reject({message : '要先登录才能操作哦~'});
}
config.headers.Authorization = store.state.token;
return config;
});
$axiosTk.onResponse(async response => {
const result = response.data;
if(response.status === 200){
if(result.code === 'SUCCESS'){
return result.data;
}
if(result.code === 'TOKEN_FAIL'){
await store.dispatch('logout');
store.commit('setLoginVisible');
return result;
}
return Promise.reject(result);
}
return Promise.reject({message:'请求出错'});
});
3 years ago
inject('$axiosTk', $axiosTk);
axiosTk = $axiosTk;
}
export {axiosTk}