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.

67 lines
2.4 KiB

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