80 lines
2.0 KiB
JavaScript
80 lines
2.0 KiB
JavaScript
import config from '@/utils/config';
|
|
import util from 'dvcp-wui/utils/util';
|
|
|
|
const Fly = require('flyio/dist/npm/wx');
|
|
var instance = new Fly();
|
|
var refrashToken = new Fly();
|
|
const baseURL = config.baseUrl;
|
|
let count = 0;
|
|
instance.config.timeout = 50000;
|
|
instance.config.baseURL = baseURL;
|
|
|
|
const getToken = () => {
|
|
if (uni.getStorageSync('token')) return uni.getStorageSync('token');
|
|
else return '';
|
|
};
|
|
|
|
instance.interceptors.request.use((config) => {
|
|
if (!config.withoutToken && getToken()) {
|
|
config.headers['Authorization'] = getToken();
|
|
}
|
|
|
|
return config;
|
|
});
|
|
|
|
instance.interceptors.response.use(
|
|
function (response) {
|
|
util.$hideLoading();
|
|
if (response.data.code === 1) {
|
|
util.$toast({title: response.data.msg, duration: 3000});
|
|
} else if (response.data.code == 2) {
|
|
//首次静默登录异常不做任何返回
|
|
} else if (response.data.code === 401) {
|
|
if (count > 3) {
|
|
return;
|
|
}
|
|
count = count + 1;
|
|
this.lock();
|
|
|
|
return util.$getLoginCode().then((res) => {
|
|
return refrashToken
|
|
.post(
|
|
`${baseURL}/auth/wechat-con/token`,
|
|
{
|
|
code: res.code
|
|
},
|
|
{
|
|
headers: {
|
|
Authorization: 'Basic dmlsbGNsb3VkOnZpbGxjbG91ZA=='
|
|
}
|
|
}
|
|
)
|
|
.then((response) => {
|
|
if (response.data.access_token) {
|
|
uni.setStorageSync('token', `bearer ${response.data.access_token}`);
|
|
response.request.headers.Authorization = `bearer ${response.data.access_token}`;
|
|
}
|
|
})
|
|
.finally(() => {
|
|
util.$hideLoading();
|
|
this.unlock();
|
|
})
|
|
.then(() => {
|
|
return instance.request(response.request);
|
|
});
|
|
});
|
|
} else {
|
|
count = 0;
|
|
return response.data;
|
|
}
|
|
},
|
|
function (err) {
|
|
console.log(err);
|
|
}
|
|
);
|
|
|
|
export default {
|
|
instance,
|
|
baseURL
|
|
};
|