123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- 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 ;// 重发请求的次数
- // 获取白名单
- import whiteList from './whiteList';
- // Add interceptors
- fly.interceptors.request.use((request) => {
- // console.log('进入fly-request', request);
- wx.showLoading({
- 'title': '加载中',
- 'mask': true
- });
- // 不显示加载页面的接口
- if (request.url.indexOf(whiteList.loading) === -1) {
- // 隐藏loading遮罩
- wx.hideLoading();
- }
- // 白名单内url不添加token
- if (request.url.indexOf(whiteList.nullHeaderToken) !== -1) {
- request.timeout = 30000; // 请求超时
- request.headers = {
- 'content-type': 'application/json',
- 'X-Tag': 'flyio'
- };
- console.log('nullHeaderToken()')
- return request;
- }
- //头文件带Authorization为请求token,不添加token
- if (request.headers.Authorization) {
- // 给所有请求添加自定义header
- request.timeout = 30000;
- Object.assign(request.headers, {
- 'content-type': 'application/json',
- 'X-Tag': 'flyio'
- })
- 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')}`
- })
- return request;
- }
- }, (error, promise) => {
- // Do something with request error
- console.log(error); // for debug
- promise.reject(error)
- });
- fly.interceptors.response.use(
- (response) => {
- //正常返回
- reqCount =0;
- wx.hideLoading();
- return response
- },
- (err, promise) => {
- wx.hideLoading();
- let msg = '';
- if (err.status === 0) {
- msg = '网络连接异常'
- }
- else if (err.status === 1) {
- 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); })}})
- }
- }
- else if (err.status === 502) {
- reqCount += 1
- if (reqCount < 3) {
- 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;
|