25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

117 lines
3.5 KiB

  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. /**
  8. * 【指定 axios的 baseURL】
  9. * 如果手工指定 baseURL: '/jeecg-boot'
  10. * 则映射后端域名,通过 vue.config.js
  11. * @type {*|string}
  12. */
  13. let apiBaseUrl = window._CONFIG['domianURL'] || "/jeecg-boot";
  14. console.log("apiBaseUrl= ",apiBaseUrl)
  15. // 创建 axios 实例
  16. const service = axios.create({
  17. //baseURL: '/jeecg-boot',
  18. baseURL: apiBaseUrl, // api base_url
  19. timeout: 9000 // 请求超时时间
  20. })
  21. const err = (error) => {
  22. if (error.response) {
  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. if(config.url.indexOf("sys/dict/getDictItems")<0){
  88. config.params = {
  89. _t: Date.parse(new Date())/1000,
  90. ...config.params
  91. }
  92. }
  93. }
  94. return config
  95. },(error) => {
  96. return Promise.reject(error)
  97. })
  98. // response interceptor
  99. service.interceptors.response.use((response) => {
  100. return response.data
  101. }, err)
  102. const installer = {
  103. vm: {},
  104. install (Vue, router = {}) {
  105. Vue.use(VueAxios, router, service)
  106. }
  107. }
  108. export {
  109. installer as VueAxios,
  110. service as axios
  111. }