request.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import Vue from 'vue'
  2. import axios from 'axios'
  3. import store from '@/store'
  4. import {VueAxios} from './axios'
  5. import {Modal, notification} from 'ant-design-vue'
  6. import {ACCESS_TOKEN} from "@/store/mutation-types"
  7. // 创建 axios 实例
  8. if (process.env.NODE_ENV == 'development') {
  9. axios.defaults.baseURL = '/jeecg-boot'
  10. }
  11. else if (process.env.NODE_ENV == 'debug') {
  12. }
  13. else if (process.env.NODE_ENV == 'production') {
  14. // axios.defaults.baseURL = 'http://api.tjyourong.com.cn/jeecg-boot';
  15. axios.defaults.baseURL = '/jeecg-boot'
  16. }
  17. const service = axios.create({
  18. // baseURL: '/jeecg-boot', // api base_url
  19. timeout: 6000000 // 请求超时时间
  20. })
  21. const err = (error) => {
  22. if (error.esponse) {
  23. let data = error.response.data
  24. const token = Vue.ls.get(ACCESS_TOKEN)
  25. console.log("------异常响应------", token)
  26. console.log("------异常响应------", error.response.status)
  27. switch (error.response.status) {
  28. case 403:
  29. notification.error({message: '系统提示', description: '拒绝访问', duration: 4})
  30. break
  31. case 500:
  32. //notification.error({ message: '系统提示', description:'Token失效,请重新登录!',duration: 4})
  33. if (token && data.message == "Token失效,请重新登录") {
  34. // update-begin- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式,不直接跳转----
  35. // store.dispatch('Logout').then(() => {
  36. // window.location.reload()
  37. // })
  38. Modal.error({
  39. title: '登录已过期',
  40. content: '很抱歉,登录已过期,请重新登录',
  41. okText: '重新登录',
  42. mask: false,
  43. onOk: () => {
  44. store.dispatch('Logout').then(() => {
  45. Vue.ls.remove(ACCESS_TOKEN)
  46. window.location.reload()
  47. })
  48. }
  49. })
  50. // update-end- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式,不直接跳转----
  51. }
  52. break
  53. case 404:
  54. notification.error({message: '系统提示', description: '很抱歉,资源未找到!', duration: 4})
  55. break
  56. case 504:
  57. notification.error({message: '系统提示', description: '网络超时'})
  58. break
  59. case 401:
  60. notification.error({message: '系统提示', description: '未授权,请重新登录', duration: 4})
  61. if (token) {
  62. store.dispatch('Logout').then(() => {
  63. setTimeout(() => {
  64. window.location.reload()
  65. }, 1500)
  66. })
  67. }
  68. break
  69. default:
  70. notification.error({
  71. message: '系统提示',
  72. description: data.message,
  73. duration: 4
  74. })
  75. break
  76. }
  77. }
  78. return Promise.reject(error)
  79. };
  80. // request interceptor
  81. service.interceptors.request.use(config => {
  82. const token = Vue.ls.get(ACCESS_TOKEN)
  83. if (token) {
  84. config.headers['X-Access-Token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  85. }
  86. if (config.method == 'get') {
  87. config.params = {
  88. _t: Date.parse(new Date()) / 1000,
  89. ...config.params
  90. }
  91. }
  92. return config
  93. }, (error) => {
  94. return Promise.reject(error)
  95. })
  96. // response interceptor
  97. service.interceptors.response.use((response) => {
  98. return response.data
  99. }, err)
  100. const installer = {
  101. vm: {},
  102. install(Vue, router = {}) {
  103. Vue.use(VueAxios, router, service)
  104. }
  105. }
  106. export {
  107. installer as VueAxios,
  108. service as axios
  109. }