request copy 1.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import axios from 'axios';
  2. import { Md5 } from 'ts-md5/dist/md5';
  3. import config from '@/config'
  4. let noTimeoutLimitArr = [
  5. ];
  6. // create an axios instance
  7. const service = axios.create({
  8. baseURL: config.baseUrl, // url = base url + request url
  9. withCredentials: false, // send cookies when cross-domain requests
  10. timeout: 20000, // request timeout
  11. });
  12. export let needLoadingRequestCount = 0;
  13. export function showFullScreenLoading(config) {
  14. if (needLoadingRequestCount === 0) {
  15. store.state.isLoading = true
  16. }
  17. needLoadingRequestCount++;
  18. }
  19. export function tryHideFullScreenLoading() {
  20. if (needLoadingRequestCount <= 0) {
  21. return;
  22. }
  23. needLoadingRequestCount--;
  24. if (needLoadingRequestCount === 0) {
  25. store.state.isLoading = false
  26. }
  27. }
  28. // request interceptor
  29. service.interceptors.request.use(
  30. config => {
  31. // do something before request is sent
  32. // console.log(config);
  33. function setTimeoutunlimit(config) {
  34. // if (_.indexOf(noTimeoutLimitArr, config.url) > -1) {
  35. // config.timeout = 9999999999;
  36. // }
  37. }
  38. function mixSignAndToken(config) {
  39. // debugger
  40. // console.log(config.data, process.env.VUE_APP_BASE_API, process.env.VUE_APP_SECRET_KEY);
  41. const checkurl = function (url) {
  42. if (url === '/common/active') {
  43. return false;
  44. }
  45. // 用户端白名单
  46. if (url.indexOf('/common') > -1) {
  47. return true;
  48. }
  49. // 管理端白名单
  50. if (url === '/admin/user/auth') {
  51. return true;
  52. }
  53. // 短信服务白名单
  54. if (url === 'sms/sendCode') {
  55. return true;
  56. }
  57. return false;
  58. };
  59. if (checkurl(config.url)) {
  60. if(config.url.indexOf('/open' >-1)){
  61. }else {
  62. config.headers['X-AUTH-SIGN'] = Md5.hashStr(JSON.stringify(config.data) + '123');
  63. }
  64. } else {
  65. if (store.state.auth) {
  66. config.headers['X-AUTH-SIGN'] = Md5.hashStr(JSON.stringify(config.data) + store.state.auth.secret);
  67. config.headers['X-AUTH-TOKEN'] = store.state.auth.token;
  68. } else {
  69. // 非登录操作,但没有auth,跳转登录
  70. router.push('/' + store.state.tenantCode + '/login');
  71. }
  72. }
  73. }
  74. // if (!JSON.stringify(config.data.customLoadingSwitch)) {
  75. // // 如不需要在data中 LoadingSwitch:false
  76. // console.log('有loading');
  77. // showFullScreenLoading(config);
  78. // }
  79. mixSignAndToken(config);
  80. setTimeoutunlimit(config);
  81. /*if (store.getters.token) {
  82. // let each request carry token
  83. // ['X-Token'] is a custom headers key
  84. // please modify it according to the actual situation
  85. config.headers['X-Token'] = getToken();
  86. }*/
  87. return config;
  88. },
  89. error => {
  90. // do something with request error
  91. // console.log(error); // for debug
  92. return Promise.reject(error);
  93. },
  94. );
  95. // response interceptor
  96. service.interceptors.response.use(
  97. /**
  98. * If you want to get http information such as headers or status
  99. * Please return response => response
  100. */
  101. /**
  102. * Determine the request status by custom code
  103. * Here is just an example
  104. * You can also judge the status by HTTP Status Code
  105. */
  106. response => {
  107. const res = response.data;
  108. // if the custom code is not 20000, it is judged as an error.
  109. if (res.code !== 0) {
  110. // 401:登录超时
  111. if (res.code === 401) {
  112. setTimeout(function () {
  113. if (window.loading) {
  114. window.loading.close();
  115. }
  116. // console.log(router);
  117. /*if (router.app.$route.path.indexOf('/c/') === 0) {
  118. router.push('/c/' + store.state.tenantCode + '/login');
  119. } else {
  120. router.push('/a/' + store.state.tenantCode + '/login');
  121. }*/
  122. store.state.showBottomNav = true;
  123. router.push('/login');
  124. }, 2000);
  125. }
  126. if (res.code === 405) {
  127. setTimeout(function () {
  128. if (window.loading) {
  129. window.loading.close();
  130. }
  131. store.state.showBottomNav = true;
  132. router.push('/login');
  133. }, 2000);
  134. }
  135. if (res.code === 502) {
  136. // Toast.fail('bad gateway');
  137. }
  138. if (res.code === 500 || res.code === 1001|| res.code ===1002) {
  139. tryHideFullScreenLoading();
  140. return Promise.reject('error' + res.code + '-' + (res.message === undefined ? 'nomessage': res.message));
  141. }
  142. tryHideFullScreenLoading();
  143. return Promise.reject(res.message || 'error');
  144. } else {
  145. tryHideFullScreenLoading();
  146. return res;
  147. }
  148. },
  149. error => {
  150. if (error == 'Error: Network Error') {
  151. // Toast.fail('网络断开,请检查网络');
  152. } else {
  153. // Toast.fail('其他错误,请联系管理员');
  154. }
  155. tryHideFullScreenLoading();
  156. return Promise.reject(error);
  157. },
  158. );
  159. export default service;