index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * Shopro-request
  3. * @description api模块管理,loading配置,请求拦截,错误处理
  4. */
  5. import Request from 'luch-request';
  6. import {
  7. baseUrl,
  8. apiPath
  9. } from '@/sheep/config';
  10. import $store from '@/sheep/store';
  11. import $platform from '@/sheep/platform';
  12. import {
  13. showAuthModal
  14. } from '@/sheep/hooks/useModal';
  15. const options = {
  16. // 显示操作成功消息 默认不显示
  17. showSuccess: false,
  18. // 成功提醒 默认使用后端返回值
  19. successMsg: '',
  20. // 显示失败消息 默认显示
  21. showError: true,
  22. // 失败提醒 默认使用后端返回信息
  23. errorMsg: '',
  24. // 显示请求时loading模态框 默认显示
  25. showLoading: true,
  26. // loading提醒文字
  27. loadingMsg: '加载中',
  28. // 需要授权才能请求 默认放开
  29. auth: false,
  30. // ...
  31. };
  32. // Loading全局实例
  33. let LoadingInstance = {
  34. target: null,
  35. count: 0,
  36. };
  37. /**
  38. * 关闭loading
  39. */
  40. function closeLoading() {
  41. if (LoadingInstance.count > 0) LoadingInstance.count--;
  42. if (LoadingInstance.count === 0) uni.hideLoading();
  43. }
  44. /**
  45. * @description 请求基础配置 可直接使用访问自定义请求
  46. */
  47. const http = new Request({
  48. baseURL: 'https://api.shopro.sheepjs.com/',
  49. timeout: 8000,
  50. method: 'GET',
  51. header: {
  52. Accept: 'text/json',
  53. 'Content-Type': 'application/json;charset=UTF-8',
  54. platform: $platform.name,
  55. },
  56. // #ifdef APP-PLUS
  57. sslVerify: false,
  58. // #endif
  59. // #ifdef H5
  60. // 跨域请求时是否携带凭证(cookies)仅H5支持(HBuilderX 2.6.15+)
  61. withCredentials: false,
  62. // #endif
  63. custom: options,
  64. });
  65. /**
  66. * @description 请求拦截器
  67. */
  68. http.interceptors.request.use(
  69. (config) => {
  70. // console.log(config);
  71. if (config.custom.auth && !$store('user').isLogin) {
  72. showAuthModal();
  73. return Promise.reject();
  74. }
  75. if (config.custom.showLoading) {
  76. LoadingInstance.count++;
  77. LoadingInstance.count === 1 &&
  78. uni.showLoading({
  79. title: config.custom.loadingMsg,
  80. mask: true,
  81. fail: () => {
  82. uni.hideLoading();
  83. },
  84. });
  85. }
  86. const token = uni.getStorageSync('token');
  87. if (token) config.header['Authorization'] = token;
  88. // TODO 芋艿:特殊处理
  89. if (config.url.indexOf('/app-api/') !== -1) {
  90. config.header['Accept'] = '*/*'
  91. config.header['tenant-id'] = '1';
  92. config.header['terminal'] = '20';
  93. config.header['Authorization'] = 'Bearer test247';
  94. }
  95. return config;
  96. },
  97. (error) => {
  98. return Promise.reject(error);
  99. },
  100. );
  101. /**
  102. * @description 响应拦截器
  103. */
  104. http.interceptors.response.use(
  105. (response) => {
  106. // 自动设置登陆令牌
  107. if (response.header.authorization || response.header.Authorization) {
  108. $store('user').setToken(response.header.authorization || response.header.Authorization);
  109. }
  110. response.config.custom.showLoading && closeLoading();
  111. if (response.data.code !== 0) {
  112. if (response.config.custom.showError)
  113. uni.showToast({
  114. title: response.data.msg || '服务器开小差啦,请稍后再试~',
  115. icon: 'none',
  116. mask: true,
  117. });
  118. return Promise.resolve(response.data);
  119. }
  120. if (
  121. response.data.error === 0 &&
  122. response.data.msg !== '' &&
  123. response.config.custom.showSuccess
  124. ) {
  125. uni.showToast({
  126. title: response.config.custom.successMsg || response.data.msg,
  127. icon: 'none',
  128. });
  129. }
  130. return Promise.resolve(response.data);
  131. },
  132. (error) => {
  133. const userStore = $store('user');
  134. const isLogin = userStore.isLogin;
  135. let errorMessage = '网络请求出错';
  136. if (error !== undefined) {
  137. switch (error.statusCode) {
  138. case 400:
  139. errorMessage = '请求错误';
  140. break;
  141. case 401:
  142. if (isLogin) {
  143. errorMessage = '您的登陆已过期';
  144. } else {
  145. errorMessage = '请先登录';
  146. }
  147. userStore.logout();
  148. showAuthModal();
  149. break;
  150. case 403:
  151. errorMessage = '拒绝访问';
  152. break;
  153. case 404:
  154. errorMessage = '请求出错';
  155. break;
  156. case 408:
  157. errorMessage = '请求超时';
  158. break;
  159. case 429:
  160. errorMessage = '请求频繁, 请稍后再访问';
  161. break;
  162. case 500:
  163. errorMessage = '服务器开小差啦,请稍后再试~';
  164. break;
  165. case 501:
  166. errorMessage = '服务未实现';
  167. break;
  168. case 502:
  169. errorMessage = '网络错误';
  170. break;
  171. case 503:
  172. errorMessage = '服务不可用';
  173. break;
  174. case 504:
  175. errorMessage = '网络超时';
  176. break;
  177. case 505:
  178. errorMessage = 'HTTP版本不受支持';
  179. break;
  180. }
  181. if (error.errMsg.includes('timeout')) errorMessage = '请求超时';
  182. // #ifdef H5
  183. if (error.errMsg.includes('Network'))
  184. errorMessage = window.navigator.onLine ? '服务器异常' : '请检查您的网络连接';
  185. // #endif
  186. }
  187. if (error && error.config) {
  188. if (error.config.custom.showError === false) {
  189. uni.showToast({
  190. title: error.data?.msg || errorMessage,
  191. icon: 'none',
  192. mask: true,
  193. });
  194. }
  195. error.config.custom.showLoading && closeLoading();
  196. }
  197. return false;
  198. },
  199. );
  200. const request = (config) => {
  201. if (config.url[0] !== '/') {
  202. config.url = '/app-api/' + config.url;
  203. }
  204. // TODO 芋艿:额外拼接
  205. if (config.url.indexOf('/app-api/') >= 0) {
  206. // 设置接口地址
  207. // config.url = 'http://api-dashboard.yudao.iocoder.cn' + config.url; // 调用【云端】
  208. // config.url = 'https://app.test.huizhizao.vip/prod-api' + config.url; // 调用【云端】
  209. config.url = 'http://127.0.0.1:48080' + config.url; // 调用【本地】
  210. // config.url = 'http://yunai.natapp1.cc' + config.url; // 调用【natapp】
  211. }
  212. return http.middleware(config);
  213. };
  214. export default request;