config.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import ENV_CONFIG from "@/.env.js";
  2. import { silentLogin } from "./auth";
  3. import { HOME_TAB_PATH } from "@/utils/policy-config.js";
  4. import { isPolicyConsented } from "@/utils/policy-consent-manager.js";
  5. function redirectToHomeTab() {
  6. const app = getApp();
  7. // 协议弹窗未关闭、或未同意协议时,禁止跳转(避免从启动页被拉到卖书 Tab)
  8. if (app && app.globalData && app.globalData.needPolicyConsent) {
  9. return;
  10. }
  11. if (!isPolicyConsented()) {
  12. return;
  13. }
  14. uni.switchTab({
  15. url: HOME_TAB_PATH,
  16. fail: () => {},
  17. });
  18. }
  19. // api前缀
  20. export const HTTP_URL_DEV_PREFIX = "/api";
  21. export const HTTP_URL_PROD_PREFIX = "/api";
  22. let envVersion = "";
  23. let info = uni.getAccountInfoSync();
  24. // 微信小程序:获取环境版本(开发版、体验版、正式版)
  25. envVersion = info.miniProgram.envVersion;
  26. // #ifdef MP-ALIPAY
  27. // 支付宝小程序:使用 my.getRunMode 获取环境
  28. my.getRunMode && my.getRunMode({
  29. success: (res) => {
  30. envVersion = res.runMode;
  31. console.log(envVersion, "envVersion");
  32. },
  33. });
  34. // #endif
  35. const env = ENV_CONFIG[envVersion == "release" ? "production" : "development"];
  36. // 基于uview-ui的http配置
  37. export const UVIEWUI_HTTP_CONFIG = {
  38. // 地址
  39. baseUrl: env.apiUrl + env.apiUrlPrefix,
  40. // 请求方式
  41. method: "post",
  42. // 参数类型
  43. dataType: "json",
  44. // 是否显示请求中的loading
  45. showLoading: true,
  46. // 请求loading中的文字提示
  47. loadingText: "请求中...",
  48. // 在此时间内,请求还没回来的话,就显示加载中动画,单位ms
  49. loadingTime: 800,
  50. // 是否在拦截器中返回服务端的原始数据
  51. originalData: true,
  52. // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透
  53. loadingMask: true,
  54. // header:{
  55. // token:uni.getStorageSync('token')
  56. // },
  57. };
  58. // 此处配置请求拦截器
  59. export const httpRequest = (config) => {
  60. const token = uni.getStorageSync("token");
  61. config.header.Authorization = token ? `Bearer ${token}` : "";
  62. return config;
  63. };
  64. // 此处配置响应拦截器
  65. export const httpResponse = (res) => {
  66. return new Promise(function (resolve, reject) {
  67. if (res.statusCode == 200) {
  68. if (res.data.code !== 200 && res.data.code !== 1001 && res.data.code !== 1002) {
  69. uni.showToast({
  70. icon: "none",
  71. title: res.data.msg || "服务器异常,请联系客服",
  72. });
  73. return resolve(res.data);
  74. }
  75. if (res.data.code == 401) {
  76. console.log("接口未登录>>>>4011",res.data.code);
  77. // 尝试静默登录
  78. silentLogin()
  79. .then((data) => {
  80. // 登录成功,重新发起之前失败的请求
  81. uni.$u.http.request(res.config).then(resolve).catch(reject);
  82. })
  83. .catch(() => {
  84. redirectToHomeTab();
  85. reject(res.data);
  86. });
  87. return resolve(res.data);
  88. }
  89. return resolve(res.data);
  90. }
  91. if (res.statusCode == 401) {
  92. console.log("接口未登录>>>>401",res.statusCode);
  93. silentLogin()
  94. .then((data) => {
  95. // 登录成功,重新发起之前失败的请求
  96. uni.$u.http.request(res.config).then(resolve).catch(reject);
  97. })
  98. .catch(() => {
  99. redirectToHomeTab();
  100. reject(res.data);
  101. });
  102. return resolve(res.data);
  103. }
  104. if (res.statusCode == 500) {
  105. console.log("服务器异常>>>>500", res.config);
  106. uni.showToast({
  107. title: "服务器异常,请联系客服!",
  108. icon: "none",
  109. duration: 3000,
  110. });
  111. return resolve(res.data);
  112. }
  113. return resolve(res.data);
  114. });
  115. };
  116. // http安装方法
  117. export const installHttpConfig = (Vue, vm) => {
  118. Vue.prototype.$u.http.setConfig(UVIEWUI_HTTP_CONFIG);
  119. Vue.prototype.$u.http.interceptor.request = httpRequest;
  120. Vue.prototype.$u.http.interceptor.response = httpResponse;
  121. };