request.js 3.9 KB

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