import { Notify, Dialog } from 'vant'; import store from '../store' let veryAxiosConfig = { // 发生错误时,是否显示提示 tip: true, // default // 如何显示提示,可以传入显示message的方法 tipFn: (message) => { Notify({ type: 'warning', message: message }); }, errorHandlers: { // 支持 400/401/403/404/405/413/414/500/502/504/任意其他 errno 400: () => { // Dialog.confirm({ // title: '提示', // message: '你已被登出,可以取消继续留在该页面,或者重新登录', // }) // .then(() => { // console.log('location', location); // localStorage.setItem('fromUrl', location.hash); // // on confirm // store.dispatch('FedLogOut').then(() => { // // location.reload()// 为了重新实例化vue-router对象 避免bug // location.href = '/#/login'; // console.log('logOut'); // }).catch(err => { // console.log('logOut err', err); // }) // }) // .catch(() => { // // on cancel // }); }, 401: () => { Dialog.confirm({ title: '提示', message: '你已被登出,可以取消继续留在该页面,或者重新登录', }) .then(() => { console.log('location', location); localStorage.setItem('fromUrl', location.hash); // on confirm store.dispatch('FedLogOut').then(() => { // location.reload()// 为了重新实例化vue-router对象 避免bug location.href = '/#/login'; // console.log('logOut'); }).catch(err => { console.log('logOut err', err); }) }) .catch(() => { // on cancel }); }, // 403: () => {} // 404: () => {} // ... }, // 内置错误提示语言: 'zh-cn'/'en' lang: 'zh-cn', // default // 请求前的自定义操作 beforeHook: (config) => { let token = store?.state?.token; if (token) { config.headers['Authorization'] = 'Bearer ' + token // 让每个请求携带自定义token 请根据实际情况自行修改 // config.headers['Content-Type'] = 'application/x-www-form-urlencoded' // config.headers['responseType'] = 'blob' } return config }, // 请求后的自定义操作 afterHook: (responce, isError) => { // console.log('isError', isError); if (responce.status == '200') { if (responce.data.code == '400') { Notify({ message: responce.data.msg }) } } }, // 从请求响应中获取错误状态,默认取errno // 如果传入的不是一个函数也会使用默认值 getResStatus: (resData) => resData.code, // default // 从请求响应中获取错误消息,默认取errmsg // 如果传入的不是一个函数也会使用默认值 getResErrMsg: (resData) => resData.msg, // default // 从请求响应中获取返回数据,默认取data // 如果传入的不是一个函数也会使用默认值 getResData: (resData) => resData, // default // 是否开启取消重复请求 cancelDuplicated: false, // default // 如果开启了取消重复请求,如何生成重复标识 duplicatedKeyFn: (config) => `${config.method}${config.url}` // default } export default veryAxiosConfig