request.js 3.8 KB

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