request.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import Vue from 'vue'
  2. import axios from 'axios'
  3. import store from '@/store'
  4. import { message, Modal, notification } from 'ant-design-vue' ///es/notification
  5. import { VueAxios } from './axios'
  6. import { ACCESS_TOKEN } from '@/store/mutation-types'
  7. // 创建 axios 实例
  8. const service = axios.create({
  9. baseURL: '/api', // api base_url
  10. timeout: 6000 // 请求超时时间
  11. })
  12. const err = (error) => {
  13. if (error.response) {
  14. const data = error.response.data
  15. const token = Vue.ls.get(ACCESS_TOKEN)
  16. if (error.response.status === 403) {
  17. console.log('服务器403啦,要重新登录!')
  18. notification.error({
  19. message: 'Forbidden',
  20. description: data.message
  21. })
  22. }
  23. if (error.response.status === 500) {
  24. if (data.message.length > 0) {
  25. message.error(data.message)
  26. }
  27. }
  28. if (error.response.status === 401 && !(data.result && data.result.isLogin)) {
  29. notification.error({
  30. message: 'Unauthorized',
  31. description: 'Authorization verification failed'
  32. })
  33. if (token) {
  34. store.dispatch('Logout').then(() => {
  35. setTimeout(() => {
  36. window.location.reload()
  37. }, 1500)
  38. })
  39. }
  40. }
  41. }
  42. return Promise.reject(error)
  43. }
  44. // request interceptor
  45. service.interceptors.request.use(config => {
  46. const token = Vue.ls.get(ACCESS_TOKEN)
  47. if (token) {
  48. config.headers['Authorization'] = 'Bearer ' + token
  49. }
  50. return config
  51. }, err)
  52. /**
  53. * response interceptor
  54. * 所有请求统一返回
  55. */
  56. service.interceptors.response.use((response) => {
  57. if (response.request.responseType === 'blob') {
  58. return response
  59. }
  60. if (response.data.code === 1011009) {
  61. Modal.error({
  62. title: '提示:',
  63. content: response.data.message,
  64. okText: '重新登录',
  65. onOk: () => {
  66. Vue.ls.remove(ACCESS_TOKEN)
  67. window.location.reload()
  68. }
  69. })
  70. } else if (response.data.code === 1011007) {
  71. Modal.error({
  72. title: '提示:',
  73. content: response.data.message,
  74. okText: '重新登录',
  75. onOk: () => {
  76. Vue.ls.remove(ACCESS_TOKEN)
  77. window.location.reload()
  78. }
  79. })
  80. } else if (response.data.code === 1011008) {
  81. Modal.error({
  82. title: '提示:',
  83. content: response.data.message,
  84. okText: '重新登录',
  85. onOk: () => {
  86. Vue.ls.remove(ACCESS_TOKEN)
  87. window.location.reload()
  88. }
  89. })
  90. } else if (response.data.code === 1013002) {
  91. message.error(response.data.message)
  92. return response.data
  93. } else if (response.data.code === 1016002) {
  94. message.error(response.data.message)
  95. return response.data
  96. } else if (response.data.code === 1015002) {
  97. message.error(response.data.message)
  98. return //response.data
  99. } else {
  100. return response.data
  101. }
  102. }, err)
  103. const installer = {
  104. vm: {},
  105. install(Vue) {
  106. Vue.use(VueAxios, service)
  107. }
  108. }
  109. export {
  110. installer as VueAxios,
  111. service as axios
  112. }