|
@@ -7,19 +7,19 @@ fly.config.baseURL = "https://wx.sgsino.cn"
|
|
|
const newFly = new Fly();
|
|
|
newFly.config = fly.config;
|
|
|
let reqCount = 0 ;// 重发请求的次数
|
|
|
+let isShowing =false;
|
|
|
// 获取白名单
|
|
|
import whiteList from './whiteList';
|
|
|
// Add interceptors
|
|
|
-fly.interceptors.request.use((request) => {
|
|
|
-// console.log('进入fly-request', request);
|
|
|
-wx.showLoading({
|
|
|
- 'title': '加载中',
|
|
|
- 'mask': true
|
|
|
-});
|
|
|
+fly.interceptors.request.use(function (request) {
|
|
|
+ fly.lock();
|
|
|
// 不显示加载页面的接口
|
|
|
if (request.url.indexOf(whiteList.loading) === -1) {
|
|
|
- // 隐藏loading遮罩
|
|
|
- wx.hideLoading();
|
|
|
+ isShowing =true;
|
|
|
+ wx.showLoading({
|
|
|
+ 'title': '加载中',
|
|
|
+ 'mask': true
|
|
|
+ });
|
|
|
}
|
|
|
// 白名单内url不添加token
|
|
|
if (request.url.indexOf(whiteList.nullHeaderToken) !== -1) {
|
|
@@ -29,6 +29,7 @@ wx.showLoading({
|
|
|
'X-Tag': 'flyio'
|
|
|
};
|
|
|
console.log('nullHeaderToken()')
|
|
|
+ fly.unlock();
|
|
|
return request;
|
|
|
}
|
|
|
//头文件带Authorization为请求token,不添加token
|
|
@@ -39,6 +40,8 @@ wx.showLoading({
|
|
|
'content-type': 'application/json',
|
|
|
'X-Tag': 'flyio'
|
|
|
})
|
|
|
+ console.log("我携带Authorization,不要给我添加token,我会请求回token给其他请求授权用");
|
|
|
+ fly.unlock();
|
|
|
return request;
|
|
|
}
|
|
|
//其他请求需要在头文件中自动附加token
|
|
@@ -50,8 +53,36 @@ wx.showLoading({
|
|
|
'X-Tag': 'flyio',
|
|
|
'Authorization': `Bearer ${wx.getStorageSync('token')}`
|
|
|
})
|
|
|
+ // wx.hideLoading();
|
|
|
+ console.log("我请求到了token,携带token请求数据,我的token为" + wx.getStorageSync('token'));
|
|
|
+ fly.unlock();
|
|
|
return request;
|
|
|
}
|
|
|
+ else{
|
|
|
+ console.log("没有token也没有授权,我需要重新生成token,先请求token...");
|
|
|
+ wx.login({
|
|
|
+ success(wxres) {
|
|
|
+ return newFly.request({
|
|
|
+ url: '/auth/mobile/token/social?grant_type=mobil&mobile=MINI@'+wxres.code,
|
|
|
+ method: 'post',
|
|
|
+ headers:{
|
|
|
+ 'content-type': 'application/json',
|
|
|
+ 'X-Tag': 'flyio',
|
|
|
+ 'Authorization': 'Basic dGVzdDp0ZXN0'
|
|
|
+ }
|
|
|
+ }).then((res) => {
|
|
|
+ if (res.data.access_token) {
|
|
|
+ wx.setStorageSync('token', res.data.access_token);
|
|
|
+ wx.setStorageSync('username', res.data.username);
|
|
|
+ }
|
|
|
+ console.log("token请求成功,值为: " + res.data.access_token);
|
|
|
+ console.log(`继续完成请求:path:${request.url},baseURL:${request.baseURL}`)
|
|
|
+ return request
|
|
|
+ }).finally(() => fly.unlock()) //解锁后,会继续发起请求队列中的任务
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ }
|
|
|
}, (error, promise) => {
|
|
|
// Do something with request error
|
|
|
console.log(error); // for debug
|
|
@@ -61,12 +92,20 @@ wx.showLoading({
|
|
|
fly.interceptors.response.use(
|
|
|
(response) => {
|
|
|
//正常返回
|
|
|
+ if(isShowing)
|
|
|
+ {
|
|
|
+ wx.hideLoading();
|
|
|
+ isShowing =false;
|
|
|
+ }
|
|
|
reqCount =0;
|
|
|
- wx.hideLoading();
|
|
|
return response
|
|
|
},
|
|
|
(err, promise) => {
|
|
|
- wx.hideLoading();
|
|
|
+ if(isShowing)
|
|
|
+ {
|
|
|
+ wx.hideLoading();
|
|
|
+ isShowing =false;
|
|
|
+ }
|
|
|
let msg = '';
|
|
|
if (err.status === 0) {
|
|
|
msg = '网络连接异常'
|
|
@@ -75,24 +114,20 @@ fly.interceptors.response.use(
|
|
|
msg = '网络连接超时'
|
|
|
}
|
|
|
else if (err.status === 401) {
|
|
|
- reqCount += 1
|
|
|
- wx.clearStorageSync('token')
|
|
|
- if (reqCount < 3) {
|
|
|
- msg = '用户已过期,正在自动刷新用户,尝试次数'+reqCount.toString();
|
|
|
- this.lock(); //锁定响应拦截器
|
|
|
- wx.login({ success(wxres) {
|
|
|
- newFly.request(
|
|
|
- { url: '/auth/mobile/token/social?grant_type=mobil&mobile=MINI@'+ wxres.code,
|
|
|
- method: 'post',
|
|
|
- headers:{ 'Authorization': 'Basic dGVzdDp0ZXN0'}})
|
|
|
- .then(res => { if (res.data.access_token) {wx.setStorageSync('token', res.data.access_token); }})
|
|
|
- .finally(() => this.unlock())
|
|
|
- .then(() => { return fly.request(err.request); })}})
|
|
|
- }
|
|
|
+ if (err.request.url.indexOf('mobil') === -1) {
|
|
|
+ reqCount += 1
|
|
|
+ if (reqCount < 3) {
|
|
|
+ console.log("401错误,我需要重新请求+我现在使用的过期token"+ wx.getStorageSync('token'));
|
|
|
+ wx.clearStorageSync('token')
|
|
|
+ return fly.request(err.request);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
else if (err.status === 502) {
|
|
|
reqCount += 1
|
|
|
if (reqCount < 3) {
|
|
|
+ console.log("502错误,我需要重新请求重试次数" + reqCount.toString());
|
|
|
msg = '网络错误,正在进行自动重试,重试次数'+reqCount.toString();
|
|
|
return fly.request(err.request);
|
|
|
}
|
|
@@ -113,5 +148,4 @@ fly.interceptors.response.use(
|
|
|
}
|
|
|
)
|
|
|
|
|
|
-
|
|
|
export default fly;
|