request.js 3.9 KB

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