request.js 5.3 KB

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