index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* eslint-disable */
  2. import Request from './request';
  3. import { refreshToken } from '@/api/login';
  4. import indexConfig from '@/config/index.config';
  5. import mHelper from '@/utils/helper';
  6. import store from '@/store';
  7. const http = new Request();
  8. // request全局参数设置
  9. http.setConfig(config => {
  10. /* 设置全局配置 */
  11. config.baseUrl = indexConfig.baseUrl; /* 根域名不同 */
  12. const systemInfo = uni.getSystemInfoSync();
  13. const systemInfoHeaders = {
  14. 'device-name': systemInfo.brand, // 设备名称
  15. width: systemInfo.screenWidth, // 屏幕宽度
  16. height: systemInfo.screenHeight, // 屏幕高度
  17. os: systemInfo.platform, // 客户端平台
  18. 'os-version': systemInfo.system // 操作系统版本
  19. };
  20. config.header = {
  21. ...config.header,
  22. ...systemInfoHeaders
  23. };
  24. return config;
  25. });
  26. let isRefreshing = false;
  27. let requests = [];
  28. http.interceptor.request(
  29. config => {
  30. /* 请求之前拦截器 */
  31. config.header['x-api-key'] = uni.getStorageSync('accessToken');
  32. // 单商户
  33. // config.header['merchant-id'] = uni.getStorageSync('merchantId') || 1;
  34. return config;
  35. },
  36. error => {
  37. return Promise.reject(error);
  38. }
  39. );
  40. // 刷新refreshToken
  41. async function handleRefreshToken(refresh_token) {
  42. const params = {};
  43. params.group = mHelper.platformGroupFilter();
  44. params.refresh_token = refresh_token;
  45. await http.post(refreshToken, params).then(async r => {
  46. store.commit('login', r.data);
  47. isRefreshing = false;
  48. });
  49. }
  50. http.interceptor.response(
  51. async response => {
  52. /* 请求之后拦截器 */
  53. switch (response.data.code) {
  54. case 200:
  55. return response.data;
  56. case 400:
  57. mHelper.toast('错误的请求');
  58. return Promise.reject(response.data.message);
  59. break;
  60. case 401:
  61. isRefreshing = false;
  62. // refreshToken 的返回状态为401
  63. if (response.config.url === refreshToken) {
  64. uni.removeStorageSync('accessToken');
  65. await store.commit('logout');
  66. uni.showModal({
  67. content: '会话已过期,是否跳转登录页面?',
  68. success: confirmRes => {
  69. if (confirmRes.confirm) {
  70. mHelper.backToLogin();
  71. throw response.data.message;
  72. }
  73. }
  74. });
  75. break;
  76. } else {
  77. // 如果refreshToken为空 则直接跳转登录
  78. if (!store.state.refreshToken) {
  79. uni.removeStorageSync('accessToken');
  80. await store.commit('logout');
  81. mHelper.backToLogin();
  82. // uni.showModal({
  83. // content: '会话已过期,是否跳转登录页面?',
  84. // success: confirmRes => {
  85. // if (confirmRes.confirm) {
  86. // mHelper.backToLogin();
  87. // throw response.data.message;
  88. // }
  89. // }
  90. // });
  91. throw response.data.message;
  92. } else {
  93. // isRefreshing同一个页面只执行一次
  94. if (!isRefreshing) {
  95. isRefreshing = true;
  96. // 刷新token
  97. await handleRefreshToken(store.state.refreshToken, response);
  98. requests.forEach(cb => cb());
  99. requests = [];
  100. isRefreshing = false;
  101. return http.request(response.config);
  102. } else {
  103. return new Promise(resolve => {
  104. // 将resolve放进队列,用一个函数形式来保存,等token刷新后直接执行
  105. requests.push(() => {
  106. resolve(http.request(response.config));
  107. });
  108. });
  109. }
  110. }
  111. }
  112. break;
  113. case 405:
  114. mHelper.toast('当前操作不被允许');
  115. return Promise.reject(response.data.message);
  116. case 404:
  117. mHelper.toast(response.data.message);
  118. return Promise.reject(response.data.message);
  119. case 429:
  120. mHelper.toast('请求过多,先休息一下吧');
  121. return Promise.reject(response.data.message);
  122. case 500:
  123. mHelper.toast('访问服务器异常');
  124. return Promise.reject(response.data.message);
  125. default:
  126. mHelper.toast(response.data.message);
  127. return Promise.reject(response.data.message);
  128. }
  129. },
  130. error => {
  131. return Promise.reject(error);
  132. }
  133. );
  134. export { http };