request.js 4.0 KB

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