小程序产品库完成
This commit is contained in:
79
src/utils/axios.js
Normal file
79
src/utils/axios.js
Normal file
@@ -0,0 +1,79 @@
|
||||
import config from '@/utils/config';
|
||||
import util from '@/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(response.data.msg);
|
||||
} 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
|
||||
};
|
||||
Reference in New Issue
Block a user