axios.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * axios定义
  3. * */
  4. import Vue from 'vue';
  5. import axios from 'axios';
  6. import ElementUI from 'element-ui';
  7. import store from '@/store/index.js';
  8. import { getCookie, setCookie } from '@/utils/util.js';
  9. import { ajaxCtx, ajaxCtx2, ajaxCtx0 } from '@/config/config.js';
  10. // import { ajaxCtx, ajaxCtx2, ajaxCtx0 } from '../../static/js/config.js';
  11. // 平台拦截处理
  12. import { apiIsPassProject, requestConfig, responseConfig, APIPro } from './axiosPass.js';
  13. // axios支持跨域cookie
  14. axios.defaults.withCredentials = true;
  15. // axios添加一个请求拦截器
  16. axios.interceptors.request.use((config) => {
  17. // console.log('nb', config.url)
  18. // console.log('hz', new Date().getTime())
  19. // var returnFlag = false;
  20. // let allPrivilege = window.localStorage.getItem('allPrivilege') ? JSON.parse(window.localStorage.getItem('allPrivilege')) : [];
  21. // let ownPrivilege = window.localStorage.getItem('ownPrivilege') && window.localStorage.getItem('ownPrivilege') !== 'undefined' ? JSON.parse(window.localStorage.getItem('ownPrivilege')) : [];
  22. // let menuID = '';
  23. // if (allPrivilege != null && allPrivilege.length !== 0) {
  24. // var tempArr = allPrivilege.filter(function (x) {
  25. // var setStratIndex = config.url.indexOf('pass') === -1 ? 0 : config.url.indexOf('pass') + 4;
  26. // var setEndIndex = config.url.indexOf('?') === -1 ? config.url.length : config.url.indexOf('?');
  27. // var a = config.url.substring(setStratIndex, setEndIndex);
  28. // // console.log('a', a);
  29. // // console.log('x.menuUrl', x.menuUrl);
  30. // // 判断当前菜单ID是否等于接口所在的菜单id
  31. // if (x.menuUrl && x.menuUrl.indexOf(a) !== -1) {
  32. // menuID = x.pId
  33. // }
  34. // return x.menuUrl ? x.menuUrl.indexOf(a) !== -1 : false;
  35. // });
  36. // if (tempArr.length > 0) {
  37. // var setArr = ownPrivilege.filter(function (x) {
  38. // var setStratIndex = config.url.indexOf('pass') === -1 ? 0 : config.url.indexOf('pass') + 4;
  39. // var setEndIndex = config.url.indexOf('?') === -1 ? config.url.length : config.url.indexOf('?');
  40. // var a = config.url.substring(setStratIndex, setEndIndex);
  41. // return x.menuUrl ? x.menuUrl.indexOf(a) !== -1 : false;
  42. // });
  43. // if (setArr.length === 0) {
  44. // returnFlag = menuID === window.localStorage.getItem('activeMenuId');
  45. // }
  46. // }
  47. // }
  48. // if (returnFlag) {
  49. // console.log('这个接口叫' + config.url)
  50. // if (config.method === 'get') {
  51. // config.method = 'post'
  52. // }
  53. // config.url = '';
  54. // return config;
  55. // }
  56. // 判断是否平台接口
  57. if (config.url.indexOf('configCenter') === 0) {
  58. config.url = 'pass/' + config.url
  59. }
  60. config = requestConfig(config);
  61. return config;
  62. }, (error) => {
  63. return Promise.reject(error);
  64. });
  65. // axios添加一个响应拦截器
  66. axios.interceptors.response.use(function (response) {
  67. // 判断是否是平台接口
  68. // if (response.config.url === '') {
  69. // response.status = '405'
  70. // }
  71. if (apiIsPassProject(response.config.url, APIPro)) {
  72. // 是平台接口
  73. // 处理配置
  74. response = responseConfig(response);
  75. } else {
  76. // 不是
  77. let flag = false, msg = '';
  78. if (response && response.data) {
  79. if (response.data.success === false) {
  80. flag = true;
  81. msg = response.data.message;
  82. }
  83. let resCode = response.data.state;
  84. if (resCode === -1 || resCode === '-1') {
  85. flag = true;
  86. msg = response.data.message;
  87. } else if (resCode === 500 || resCode === '500') {
  88. flag = true;
  89. msg = response.data.msgInfo ? response.data.msgInfo : response.data.errorInfo;
  90. } else if (resCode === 300 || resCode === '300') {
  91. // 后台session失效
  92. flag = true;
  93. msg = '登录已过期,请重新登录';
  94. window.top.location.href = store.getters.ctx;
  95. // window.location.href = this.$store.state.ctx + '/views/index.html';
  96. // window.location.href = './index.html';
  97. }
  98. }
  99. if (flag) {
  100. if (msg) { ElementUI.Message({ message: msg, type: 'error', dangerouslyUseHTMLString: true }); }
  101. return null;
  102. }
  103. }
  104. return response;
  105. }, function (error) {
  106. let errorMsg = '<span style="font-weight: bold; font-size: 13px; display: inline-block; padding-bottom: 5px;">操作失败</span><br/>' +
  107. '<span>请刷新页面重新操作,如果问题依旧存在,请联系管理员</span>';
  108. if ((error.response.data.exception && error.response.data.exception.indexOf('ShiroJwtException') >= 0) || (error.response.data.message && (error.response.data.message.indexOf('Token') >= 0))) {
  109. ElementUI.Message({
  110. message: '登录失效,请重新登录',
  111. dangerouslyUseHTMLString: true,
  112. type: 'error'
  113. });
  114. setTimeout(() => {
  115. window.top.location.href = store.getters.ctx;
  116. setCookie('accessToken', '', -1, '/');
  117. setCookie('refreshToken', '', -1, '/');
  118. setCookie('workDate', '', -1);
  119. setCookie('ticket', '', -1, '/');
  120. }, 500)
  121. return
  122. }
  123. if (error && error.response && error.response.data) {
  124. let errorData;
  125. if (typeof error.response.data === 'string') {
  126. errorData = error.response.data.substring(0, error.response.data.length - 2);
  127. try {
  128. errorData = JSON.parse(errorData);
  129. let index = errorData.message.indexOf(':') + 1
  130. if (errorData.message) { errorMsg = errorData.message.substring(index); }
  131. } catch (err) {
  132. errorData = {
  133. message: error.response.data,
  134. code: error.response.status
  135. }
  136. }
  137. } else {
  138. errorData = error.response.data
  139. if (errorData.message) { errorMsg = errorData.message; }
  140. }
  141. let mCode = errorData.code;
  142. // data.error
  143. if (errorData.error) {
  144. if (errorData.error.message) { errorMsg = errorData.error.message; }
  145. mCode = errorData.error.code;
  146. }
  147. if (mCode === '-99' || mCode === -99) {
  148. errorMsg = '登录已过期,请重新登录';
  149. window.top.location.href = store.getters.ctx;
  150. // window.location.href = './index.html';
  151. } else if (mCode === '405' || mCode === 405) {
  152. errorMsg = '无权限访问,请联系系统管理员';
  153. }
  154. // console.log('执行2', errorData.message, errorMsg)
  155. }
  156. // 提示
  157. ElementUI.Message({
  158. message: errorMsg,
  159. dangerouslyUseHTMLString: true,
  160. type: 'error'
  161. });
  162. return Promise.reject(error);
  163. });
  164. Vue.prototype.axios = axios;
  165. export default axios;