request.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import axios from 'axios';
  2. import { UniAdapter } from 'uniapp-axios-adapter';
  3. import store from '@/store';
  4. import {
  5. getAccessToken,
  6. setAccessToken,
  7. setRefreshToken,
  8. refreshToken,
  9. isRefreshRequest,
  10. } from './auth';
  11. // 每次请求都创建一个新的实例
  12. const instance = axios.create({
  13. // baseURL: 'https://test.api.chelvc.com',
  14. // baseURL: "http://192.168.68.77:11000",
  15. // baseURL: "https://358175z5l5.yicp.fun",
  16. baseURL: "http://localhost:11000",
  17. timeout: 10000,
  18. adapter: UniAdapter,
  19. });
  20. instance.interceptors.request.use(config => {
  21. uni.showLoading({
  22. title: '加载中',
  23. });
  24. // 带上token 和其他请求头信息
  25. if (store.getters.accessToken) {
  26. config.headers['Authorization'] = `Bearer ${getAccessToken()}`;
  27. }
  28. // console.log('@@@@config', config);
  29. // console.log('@@@@store.getters.app.system', store.getters.app.system);
  30. config.headers = {
  31. ...config.headers,
  32. platform: store.getters.app.system.osName.toUpperCase(),
  33. terminal: 'APPLET', // TODO:
  34. version: store.getters.app.system.appVersion.toUpperCase(),
  35. // scope: store.getters.scope,
  36. device: store.getters.app.system.deviceId,
  37. timestamp: new Date().getTime(),
  38. };
  39. return config;
  40. });
  41. instance.interceptors.response.use(
  42. async res => {
  43. uni.hideLoading();
  44. const { code, data, message } = res.data;
  45. if (data && data.accessToken) {
  46. setAccessToken(data.accessToken);
  47. }
  48. if (data && data.refreshToken) {
  49. setRefreshToken(data.refreshToken);
  50. }
  51. // 未知错误
  52. if (code === 'ERROR') {
  53. uni.showToast({
  54. title: `${message ? message : '未知错误'}`,
  55. icon: 'none',
  56. });
  57. return res.data;
  58. }
  59. // 请求错误
  60. if (code === 'BAD_REQUEST') {
  61. uni.showToast({
  62. title: `${message ? message : '请求异常'}`,
  63. icon: 'none',
  64. });
  65. return res.data;
  66. }
  67. // 拒绝访问
  68. if (code === 'FORBIDDEN') {
  69. uni.showToast({
  70. title: `${message ? message : '拒绝访问'}`,
  71. icon: 'none',
  72. });
  73. return;
  74. }
  75. // 请求参数异常
  76. if (code === 'PARAMETER_INVALID') {
  77. const errorTextList = Object.keys(res.data.data).map(key => {
  78. return res.data.data[key];
  79. });
  80. uni.showToast({
  81. title: errorTextList.join(','),
  82. icon: 'none',
  83. });
  84. return res.data;
  85. }
  86. // 访问资源异常
  87. if (code === 'UNAVAILABLE') {
  88. uni.showToast({
  89. title: `${message ? message : '访问资源不可用'}`,
  90. icon: 'none',
  91. });
  92. return;
  93. }
  94. // 未登录
  95. if (code === 'UNAUTHORIZED') {
  96. uni.navigateTo({
  97. url: '/pages/login/login',
  98. });
  99. return res.data;
  100. }
  101. // 身份切换
  102. if (code === 'SCOPE_CHANGED') {
  103. uni.reLaunch({
  104. url: '/pages/login/login',
  105. });
  106. uni.showToast({
  107. title: '您的身份信息已切换',
  108. icon: 'none',
  109. });
  110. return res.data;
  111. }
  112. if (code === 'TOKEN_CHANGED') {
  113. uni.showModal({
  114. title: '提示',
  115. content: res.data.messsage,
  116. showCancel: false,
  117. success: res => {
  118. if (res.confirm) {
  119. // 小程序用户跳转到我的页面 , 登录状态为未登录
  120. }
  121. },
  122. });
  123. return res.data;
  124. }
  125. // token过期处理
  126. if (code === 'TOKEN_EXPIRED' && !isRefreshRequest(res.config)) {
  127. // 刷新token
  128. const isSuccess = await refreshToken();
  129. if (isSuccess) {
  130. // 重新请求
  131. instance.config.headers.authorization = `Bearer ${getAccessToken()}`;
  132. const resp = await instance.request(res.config);
  133. return resp;
  134. } else {
  135. // 无权限
  136. store.commit('SET_ACCESS_TOKEN', '');
  137. store.commit('SET_REFRESH_TOKEN', '');
  138. uni.showModal({
  139. title: '提示',
  140. content: '您的登录信息已过期,请重新登录',
  141. showCancel: false,
  142. success: res => {
  143. uni.navigateTo({
  144. url: '/pages/login/login',
  145. });
  146. },
  147. });
  148. return res.data;
  149. }
  150. }
  151. return res.data;
  152. },
  153. err => {
  154. console.log('->', err);
  155. uni.hideLoading();
  156. },
  157. );
  158. export default instance;