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.

73 lines
2.6 KiB

4 months ago
3 months ago
4 months ago
3 months ago
4 months ago
3 months ago
3 months ago
4 months ago
3 months ago
4 months ago
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const config_site_config = require("./site.config.js");
  4. const store_index = require("../store/index.js");
  5. let { H5_API, WX_API, httpPrefix } = config_site_config.obj;
  6. const counterStore = store_index.useUserStoreHook();
  7. let secretKey = "22d90e09d1374f0f9e4accd07d333e55";
  8. const request = (vm) => {
  9. common_vendor.index.$u.http.setConfig((config) => {
  10. let prefix = config.prefix ? config.prefix : httpPrefix;
  11. config.baseURL = H5_API + WX_API + prefix;
  12. return config;
  13. });
  14. common_vendor.index.$u.http.interceptors.request.use((config) => {
  15. var _a;
  16. config.data = config.data || {};
  17. config.data["timestamp"] = Date.now();
  18. let jsonString = JSON.stringify(config.data);
  19. let strSecretKey = jsonString + secretKey;
  20. let hash = common_vendor.md5(strSecretKey);
  21. config.header["Signature"] = hash;
  22. config.data = jsonString;
  23. let token = common_vendor.index.getStorageSync("token");
  24. if (token) {
  25. config.header.Authorization = "Bearer " + token;
  26. }
  27. let noToken = (_a = config.custom) == null ? void 0 : _a.noToken;
  28. if (noToken && config.header.Authorization) {
  29. delete config.header.Authorization;
  30. }
  31. console.log("config");
  32. console.log(config);
  33. return config;
  34. }, (config) => {
  35. return Promise.reject(config);
  36. });
  37. common_vendor.index.$u.http.interceptors.response.use(async (response) => {
  38. var _a;
  39. const data = response.data;
  40. if (data.code == 406 && response.config.url != "member/auth/refresh-token" && response.config.url !== "member/auth/logout") {
  41. let obj = response.config;
  42. let method = obj.method.toLowerCase();
  43. if (method == "get") {
  44. return common_vendor.index.$u.http[method](obj.url, { params: obj.params });
  45. } else {
  46. return common_vendor.index.$u.http[method](obj.url, obj.data);
  47. }
  48. }
  49. if (data.code == 401) {
  50. console.log("报401的接口");
  51. console.log(response.config.url);
  52. return counterStore.goLogin();
  53. }
  54. const custom = (_a = response.config) == null ? void 0 : _a.custom;
  55. if (data.code !== 0 && data.code != 406 && data.code != "200240213") {
  56. console.log("不正常的code");
  57. console.log(data);
  58. if (custom.toast !== false) {
  59. common_vendor.index.$u.toast(data.msg);
  60. }
  61. if (custom == null ? void 0 : custom.catch) {
  62. return Promise.reject(data);
  63. } else {
  64. return new Promise(() => {
  65. });
  66. }
  67. }
  68. return data === void 0 ? {} : data;
  69. }, (response) => {
  70. return Promise.reject(response);
  71. });
  72. };
  73. exports.request = request;