request.js 3.9 KB

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