request.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // import Vue from 'vue'
  2. import axios from 'axios'
  3. import store from '@/store'
  4. import notification from 'ant-design-vue/es/notification'
  5. import modal from 'ant-design-vue/es/modal'
  6. import { VueAxios } from './axios'
  7. // import { ACCESS_TOKEN } from '@/store/mutation-types'
  8. // console.log(process.env.VUE_APP_PRO_NAME)
  9. // 创建 axios 实例
  10. const service = axios.create({
  11. baseURL: process.env.VUE_APP_API_BASE_URL, // api base_url
  12. timeout: 60000 // 请求超时时间
  13. })
  14. const err = (error) => {
  15. console.log(error.response.data.message, 'error')
  16. if (error.response) {
  17. const status = error.response.status
  18. if ((status == 503 || status == 500 || status == 900) && window.location.pathname != '/user/login') {
  19. notification.destroy()
  20. notification.error({
  21. message: '提示',
  22. description: error.response.data.message
  23. })
  24. }
  25. }
  26. // 超时无法访问服务
  27. if (error.message.indexOf('timeout') >= 0 && window.location.pathname != '/user/login') {
  28. notification.destroy()
  29. notification.error({
  30. message: '提示',
  31. description: '请求超时'
  32. })
  33. }
  34. return Promise.reject(error)
  35. }
  36. // request interceptor
  37. service.interceptors.request.use(config => {
  38. const token = store.getters.token
  39. config.headers['App-Type'] = 8 // 平台类型
  40. if (token) {
  41. config.headers['access-token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  42. }
  43. return config
  44. }, err)
  45. // response interceptor
  46. service.interceptors.response.use((response) => {
  47. console.log(response, 'response.data.status')
  48. if (window.location.pathname != '/user/login') {
  49. if (response.data.status == '500') {
  50. notification.destroy()
  51. notification.error({
  52. message: '提示',
  53. description: response.data.message
  54. })
  55. }
  56. const et = sessionStorage.getItem('errorTips')
  57. if (response.data.status && response.data.status.length == 4 && !et) {
  58. sessionStorage.setItem('errorTips', 1)
  59. modal.destroyAll()
  60. modal.error({
  61. title: '提示',
  62. content: response.data.message,
  63. onOk () {
  64. sessionStorage.setItem('errorTips', 0)
  65. if (['1001', '1002', '1006', '1099', '1100', '9000'].indexOf(response.data.status) >= 0) {
  66. store.dispatch('Logout').then(() => {
  67. window.location.reload()
  68. })
  69. }
  70. }
  71. })
  72. }
  73. }
  74. return response.data
  75. }, err)
  76. const installer = {
  77. vm: {},
  78. install (Vue) {
  79. Vue.use(VueAxios, service)
  80. }
  81. }
  82. export {
  83. installer as VueAxios,
  84. service as axios
  85. }