config.js 3.5 KB

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