123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import baseUrl from '@/utils/baseurl.js'
- import { getToken } from '@/utils/auth';
- import errorCode from '@/utils/errorCode';
- let Fly = require("flyio/dist/npm/wx")
- let fly = new Fly
- //设置超时
- fly.config.timeout = 30000;
- //设置请求基地址
- fly.config.baseURL = baseUrl;
- fly.interceptors.request.use((config) => {
- // 在发送请求之前做些什么
- // if(isLoading){
- // console.log(isLoading,222)
- // uni.showLoading({
- // title: "加载中",
- // mask: true,
- // })
- // }
- const isToken = (config.headers || {}).isToken === false
- if (getToken() && !isToken) {
- config.headers['Authorization'] = `Bearer ${getToken()}`;
- }
- return config
- }, function(error) {
- // 对请求错误做些什么
- // if(isLoading){
- // uni.hideLoading();
- // }
- uni.showToast({
- title: error,
- icon:'error'
- });
- return Promise.reject(error)
- })
- // 添加响应拦截器
- fly.interceptors.response.use((res) => {
- // 对响应数据做些事
- const { code, msg, data } = res.data
- const rescode = code || 200;
- // 获取错误信息
- const resmsg = errorCode[rescode] || msg || errorCode['default']
- // 二进制数据则直接返回
- // if(isLoading){
- // uni.hideLoading();
- // }
- if(res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer'){
- return resmsg
- }
- if (rescode === 401) {
- //#ifndef MP-WEIXIN
- uni.showModal({
- title: '系统提示',
- content: '请先进行登录',
- mask:true,
- success: function (res) {
- if (res.confirm) {
- uni.reLaunch({
- url:"/pages/logo/index"
- })
- } else if (res.cancel) {
- console.log('用户点击取消');
- }
- }
- });
- //#endif
- //#ifdef MP-WEIXIN
- wx.showModal({
- title: '系统提示',
- content: '请先进行登录',
- mask:true,
- success (res) {
- if (res.confirm) {
- uni.reLaunch({
- url:"/pages/logo/index"
- })
- } else if (res.cancel) {
- console.log('用户点击取消')
- }
- }
- })
- //#endif
- } else if (rescode === 500) {
- let mess = ""
- if(resmsg==="用户不存在/密码错误"){
- mess = "工号/密码错误"
- }else if(resmsg==="登录用户: 不存在"){
- mess = "用户不存在"
- }else{
- mess = resmsg
- }
- uni.showToast({
- title: mess,
- icon:'error'
- });
- return Promise.reject(new Error(resmsg))
- } else if (rescode !== 200) {
- uni.showToast({
- title: resmsg,
- icon:'error'
- });
- return Promise.reject('error')
- } else {
- return data
- }
- }, (error) => {
- const { message } = error.message
- uni.showToast({
- title: message,
- duration: 5*1000,
- icon:'error'
- });
- return Promise.reject(error);
- })
- export default fly;
|