request.js 3.6 KB

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