| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- export default function ({ $axios, redirect, app, store }, inject) {
- // $axios.onRequest(config => {
- // if(store.state.token){
- // config.headers['Authorization'] = 'Bearer ' + store.state.token;
- // }
- // return config;
- // });
- //
- // $axios.setHeader('Content-Type', 'application/json');
- //
- // $axios.onResponse(response =>{
- // console.log("response", response);
- // if(response.status === 401){
- // app.$cookies.removeAll();
- // store.commit('saveToken', null);
- // store.commit('saveUser', {});
- // this.$message({
- // showClose: true,
- // message: '未授权,请先登录',
- // type: 'warning'
- // });
- // }
- //
- // });
- // Create a custom axios instance
- const api = $axios.create({
- // headers: {
- // common: {
- // Accept: 'text/plain, */*'
- // }
- // },
- baseURL: '/api',
- timeout: 10000,
- withCredentials: true
- });
- api.onRequest(config => {
- if(store.state.token){
- config.headers['authorization'] = 'Bearer ' + store.state.token;
- }else{
- delete config.headers['authorization'];
- }
- return config;
- });
- api.setHeader('Content-Type', 'application/json');
- api.onResponse(response =>{
- if(response.status === 401){
- app.$cookies.removeAll();
- store.commit('saveToken', null);
- store.commit('saveUser', {});
- redirect('/unauthorized');
- }
- });
- api.onError(error => {
- if(error.response.status === 401) {
- app.$cookies.removeAll();
- store.commit('saveToken', null);
- store.commit('saveUser', {});
- redirect('/unauthorized');
- }
- });
- // Set baseURL to something different
- // api.setBaseURL('https://my_api.com')
- // Inject to context as $api
- inject('api', api)
- }
|