2022-02-14 17:25:54 +08:00
|
|
|
import config from '@/utils/config';
|
2022-03-02 18:37:22 +08:00
|
|
|
import util from 'dvcp-wui/utils/util';
|
2022-03-22 13:46:42 +08:00
|
|
|
import store from "../store"
|
2022-02-14 17:25:54 +08:00
|
|
|
|
|
|
|
|
const Fly = require('flyio/dist/npm/wx');
|
2022-03-22 13:46:42 +08:00
|
|
|
let instance = new Fly();
|
2022-02-14 17:25:54 +08:00
|
|
|
const baseURL = config.baseUrl;
|
|
|
|
|
let count = 0;
|
|
|
|
|
instance.config.timeout = 50000;
|
|
|
|
|
instance.config.baseURL = baseURL;
|
|
|
|
|
|
|
|
|
|
const getToken = () => {
|
2022-03-22 13:46:42 +08:00
|
|
|
if (store.state.token) return store.state.token;
|
|
|
|
|
else return '';
|
2022-02-14 17:25:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
instance.interceptors.request.use((config) => {
|
2022-03-22 13:46:42 +08:00
|
|
|
if (!config.withoutToken) {
|
|
|
|
|
config.headers['Authorization'] = getToken();
|
|
|
|
|
}
|
2022-02-14 17:25:54 +08:00
|
|
|
return config;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
instance.interceptors.response.use(
|
|
|
|
|
function (response) {
|
|
|
|
|
util.$hideLoading();
|
|
|
|
|
if (response.data.code === 1) {
|
2022-03-02 15:37:27 +08:00
|
|
|
util.$toast({title: response.data.msg, duration: 3000});
|
2022-02-14 17:25:54 +08:00
|
|
|
} else if (response.data.code == 2) {
|
|
|
|
|
//首次静默登录异常不做任何返回
|
2022-03-22 13:46:42 +08:00
|
|
|
} else if (response.data.code == 401) {
|
2022-02-14 17:25:54 +08:00
|
|
|
if (count > 3) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-03-22 13:46:42 +08:00
|
|
|
count++;
|
2022-02-14 17:25:54 +08:00
|
|
|
return util.$getLoginCode().then((res) => {
|
2022-03-22 13:46:42 +08:00
|
|
|
store.commit("getToken", {code: res.code})
|
2022-02-14 17:25:54 +08:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
count = 0;
|
|
|
|
|
return response.data;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
function (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
instance,
|
|
|
|
|
baseURL
|
|
|
|
|
};
|