request.js 3.8 KB

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