123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- const Fly = require("../lib/wx.umd.min") //wx.js is your downloaded code
- const fly = new Fly(); //Create an instance of Fly
- const app = getApp()
- fly.config.timeout = 20000;
- 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(function (request) {
- fly.lock();
- // 不显示加载页面的接口
- if (request.url.indexOf(whiteList.loading) === -1) {
- isShowing =true;
- wx.showLoading({
- 'title': '加载中',
- 'mask': true
- });
- }
- // 白名单内url不添加token
- if (request.url.indexOf(whiteList.nullHeaderToken) !== -1) {
- request.timeout = 30000; // 请求超时
- request.headers = {
- 'content-type': 'application/json',
- 'X-Tag': 'flyio'
- };
- console.log('nullHeaderToken()')
- fly.unlock();
- return request;
- }
- //头文件带Authorization为请求token,不添加token
- if (request.headers.Authorization) {
- // 给所有请求添加自定义header
- request.timeout = 30000;
- Object.assign(request.headers, {
- 'content-type': 'application/json',
- 'X-Tag': 'flyio'
- })
- console.log("我携带Authorization,不要给我添加token,我会请求回token给其他请求授权用");
- fly.unlock();
- return request;
- }
- //其他请求需要在头文件中自动附加token
- if(wx.getStorageSync('token')) {
- // 给所有请求添加自定义header
- request.timeout = 30000;
- Object.assign(request.headers, {
- 'content-type': 'application/json',
- '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
- promise.reject(error)
- });
- fly.interceptors.response.use(
- (response) => {
- //正常返回
- if(isShowing)
- {
- wx.hideLoading();
- isShowing =false;
- }
- reqCount =0;
- return response
- },
- (err, promise) => {
- if(isShowing)
- {
- wx.hideLoading();
- isShowing =false;
- }
- let msg = '';
- if (err.status === 0) {
- msg = '网络连接异常'
- }
- else if (err.status === 1) {
- msg = '网络连接超时'
- }
- else if (err.status === 401) {
- 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);
- }
- }
- else {
- if (err.response.data.message) {
- msg = err.response.data.message
- } else {
- msg = '程序已3次连接失败,请稍后手动登录后再试'
- }
- }
- wx.showToast({
- title: msg,
- icon: 'none',
- duration: 3000
- });
- return promise.resolve(err)
- }
- )
- export default fly;
|