requestHisStore.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. import axios from 'axios'
  3. import { Notification, MessageBox, Message } from 'element-ui'
  4. import store from '@/store'
  5. import { getToken } from '@/utils/auth'
  6. import errorCode from '@/utils/errorCode'
  7. axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
  8. // 创建axios实例
  9. const service = axios.create({
  10. // axios中请求配置有baseURL选项,表示请求URL公共部分
  11. // baseURL: process.env.VUE_APP_BASE_API,
  12. // 超时
  13. timeout: 1200000
  14. })
  15. // 存储当前激活的组件状态键
  16. let activeStateKey = null;
  17. // 重置指定页面的状态
  18. export function resetEndpoint() {
  19. setActive(null);
  20. }
  21. // 设置当前激活状态键
  22. export function setActive(stateKey) {
  23. activeStateKey = stateKey;
  24. }
  25. // request拦截器
  26. service.interceptors.request.use(config => {
  27. // 使用当前激活的组件状态来决定是否添加前缀
  28. let useNewEndpoint = false
  29. let routePrefix = ''
  30. let canReg = false
  31. if (activeStateKey) {
  32. const state = JSON.parse(sessionStorage.getItem(activeStateKey));
  33. useNewEndpoint = state.isUsingNewEndpoint;
  34. routePrefix = state.prefix;
  35. canReg= state.regexGate.split(',').any(reg => config.url.includes(reg));
  36. }
  37. if (useNewEndpoint && canReg) {
  38. config.url = routePrefix + config.url;
  39. }
  40. //判断watch请求
  41. if(config.url.indexOf('/watch-api') == -1){
  42. config.url = process.env.VUE_APP_BASE_API + config.url
  43. }
  44. // 是否需要设置 token
  45. const isToken = (config.headers || {}).isToken === false
  46. if (getToken() && !isToken) {
  47. config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  48. config.headers['X-Frontend-Type'] = 'admin'
  49. }
  50. // get请求映射params参数
  51. if (config.method === 'get' && config.params) {
  52. let url = config.url + '?';
  53. for (const propName of Object.keys(config.params)) {
  54. const value = config.params[propName];
  55. var part = encodeURIComponent(propName) + "=";
  56. if (value !== null && typeof(value) !== "undefined") {
  57. if (typeof value === 'object') {
  58. for (const key of Object.keys(value)) {
  59. if (value[key] !== null && typeof (value[key]) !== 'undefined') {
  60. let params = propName + '[' + key + ']';
  61. let subPart = encodeURIComponent(params) + '=';
  62. url += subPart + encodeURIComponent(value[key]) + '&';
  63. }
  64. }
  65. } else {
  66. url += part + encodeURIComponent(value) + "&";
  67. }
  68. }
  69. }
  70. url = url.slice(0, -1);
  71. config.params = {};
  72. config.url = url;
  73. }
  74. return config
  75. }, error => {
  76. console.log(error)
  77. Promise.reject(error)
  78. })
  79. // 响应拦截器
  80. service.interceptors.response.use(res => {
  81. // 未设置状态码则默认成功状态
  82. const code = res.data.code || 200;
  83. // 获取错误信息
  84. const msg = errorCode[code] || res.data.msg || errorCode['default']
  85. if (code === 401) {
  86. MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
  87. confirmButtonText: '重新登录',
  88. cancelButtonText: '取消',
  89. type: 'warning'
  90. }
  91. ).then(() => {
  92. store.dispatch('LogOut').then(() => {
  93. location.href = '/index';
  94. })
  95. }).catch(() => {});
  96. return Promise.reject('无效的会话,或者会话已过期,请重新登录。')
  97. } else if (code === 500) {
  98. Message({
  99. message: msg,
  100. type: 'error'
  101. })
  102. return Promise.reject(new Error(msg))
  103. } else if (code !== 200) {
  104. Notification.error({
  105. title: msg
  106. })
  107. return Promise.reject('error')
  108. } else {
  109. return res.data
  110. }
  111. },
  112. error => {
  113. console.log('err' + error)
  114. let { message } = error;
  115. if (message == "Network Error") {
  116. message = "后端接口连接异常";
  117. }
  118. else if (message.includes("timeout")) {
  119. message = "系统接口请求超时";
  120. }
  121. else if (message.includes("Request failed with status code")) {
  122. message = "系统接口" + message.substr(message.length - 3) + "异常";
  123. }
  124. Message({
  125. message: message,
  126. type: 'error',
  127. duration: 5 * 1000
  128. })
  129. return Promise.reject(error)
  130. }
  131. )
  132. export default service
  133. */