You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.5 KiB

1 week ago
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const config_site_config = require("./site.config.js");
  4. const config_api = require("./api.js");
  5. let { H5_API, WX_API, httpPrefix } = config_site_config.obj;
  6. let num = 0;
  7. const request = (vm) => {
  8. common_vendor.index.$u.http.setConfig((config) => {
  9. let prefix = config.prefix ? config.prefix : httpPrefix;
  10. config.baseURL = H5_API + WX_API + prefix;
  11. return config;
  12. });
  13. common_vendor.index.$u.http.interceptors.request.use((config) => {
  14. var _a;
  15. config.data = config.data || {};
  16. let loginInfo = common_vendor.index.getStorageSync("loginInfo") || {};
  17. let token = loginInfo.token;
  18. if (token) {
  19. config.header.Authorization = "Bearer " + token;
  20. }
  21. let noToken = (_a = config.custom) == null ? void 0 : _a.noToken;
  22. if (noToken && config.header.Authorization) {
  23. delete config.header.Authorization;
  24. }
  25. return config;
  26. }, (config) => {
  27. return Promise.reject(config);
  28. });
  29. common_vendor.index.$u.http.interceptors.response.use(async (response) => {
  30. var _a;
  31. const data = response.data;
  32. data.code = data.errorcode;
  33. if (data.code == 401) {
  34. console.log("报401的接口");
  35. console.log(response.config.url);
  36. common_vendor.index.$u.toast("登录异常,请稍后");
  37. if (num)
  38. return;
  39. num++;
  40. let loginInfo = common_vendor.index.getStorageSync("loginInfo");
  41. const res = await config_api.loginApi({
  42. "type": loginInfo.type,
  43. "username": loginInfo.phone
  44. });
  45. res.data.phone = loginInfo.phone;
  46. res.data.type = loginInfo.type;
  47. common_vendor.index.setStorageSync("loginInfo", res.data);
  48. num = 0;
  49. let obj = response.config;
  50. let method = obj.method.toLowerCase();
  51. if (method == "get") {
  52. return common_vendor.index.$u.http[method](obj.url, { params: obj.params });
  53. } else {
  54. return common_vendor.index.$u.http[method](obj.url, obj.data);
  55. }
  56. }
  57. const custom = (_a = response.config) == null ? void 0 : _a.custom;
  58. if (data.code !== 0) {
  59. console.log("不正常的code");
  60. console.log(data);
  61. if (custom.toast !== false) {
  62. common_vendor.index.$u.toast(data.message);
  63. }
  64. if (custom == null ? void 0 : custom.catch) {
  65. return Promise.reject(data);
  66. } else {
  67. return new Promise(() => {
  68. });
  69. }
  70. }
  71. return data === void 0 ? {} : data;
  72. }, (response) => {
  73. return Promise.reject(response);
  74. });
  75. };
  76. exports.request = request;