veryAxiosConfig.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { Notify, Dialog } from 'vant';
  2. import store from '../store'
  3. let veryAxiosConfig = {
  4. // 发生错误时,是否显示提示
  5. tip: true, // default
  6. // 如何显示提示,可以传入显示message的方法
  7. tipFn: (message) => { Notify({ type: 'warning', message: message }); },
  8. errorHandlers: {
  9. // 支持 400/401/403/404/405/413/414/500/502/504/任意其他 errno
  10. 400: () => {
  11. // Dialog.confirm({
  12. // title: '提示',
  13. // message: '你已被登出,可以取消继续留在该页面,或者重新登录',
  14. // })
  15. // .then(() => {
  16. // console.log('location', location);
  17. // localStorage.setItem('fromUrl', location.hash);
  18. // // on confirm
  19. // store.dispatch('FedLogOut').then(() => {
  20. // // location.reload()// 为了重新实例化vue-router对象 避免bug
  21. // location.href = '/#/login';
  22. // console.log('logOut');
  23. // }).catch(err => {
  24. // console.log('logOut err', err);
  25. // })
  26. // })
  27. // .catch(() => {
  28. // // on cancel
  29. // });
  30. },
  31. 401: () => {
  32. Dialog.confirm({
  33. title: '提示',
  34. message: '你已被登出,可以取消继续留在该页面,或者重新登录',
  35. })
  36. .then(() => {
  37. console.log('location', location);
  38. localStorage.setItem('fromUrl', location.hash);
  39. // on confirm
  40. store.dispatch('FedLogOut').then(() => {
  41. // location.reload()// 为了重新实例化vue-router对象 避免bug
  42. location.href = '/#/login';
  43. // console.log('logOut');
  44. }).catch(err => {
  45. console.log('logOut err', err);
  46. })
  47. })
  48. .catch(() => {
  49. // on cancel
  50. });
  51. },
  52. // 403: () => {}
  53. // 404: () => {}
  54. // ...
  55. },
  56. // 内置错误提示语言: 'zh-cn'/'en'
  57. lang: 'zh-cn', // default
  58. // 请求前的自定义操作
  59. beforeHook: (config) => {
  60. let token = store?.state?.token;
  61. if (token) {
  62. config.headers['Authorization'] = 'Bearer ' + token // 让每个请求携带自定义token 请根据实际情况自行修改
  63. // config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
  64. // config.headers['responseType'] = 'blob'
  65. }
  66. return config
  67. },
  68. // 请求后的自定义操作
  69. afterHook: (responce, isError) => {
  70. // console.log('isError', isError);
  71. if (responce.status == '200') {
  72. if (responce.data.code == '400') {
  73. Notify({ message: responce.data.msg })
  74. }
  75. }
  76. },
  77. // 从请求响应中获取错误状态,默认取errno
  78. // 如果传入的不是一个函数也会使用默认值
  79. getResStatus: (resData) => resData.code, // default
  80. // 从请求响应中获取错误消息,默认取errmsg
  81. // 如果传入的不是一个函数也会使用默认值
  82. getResErrMsg: (resData) => resData.msg, // default
  83. // 从请求响应中获取返回数据,默认取data
  84. // 如果传入的不是一个函数也会使用默认值
  85. getResData: (resData) => resData, // default
  86. // 是否开启取消重复请求
  87. cancelDuplicated: false, // default
  88. // 如果开启了取消重复请求,如何生成重复标识
  89. duplicatedKeyFn: (config) => `${config.method}${config.url}` // default
  90. }
  91. export default veryAxiosConfig