request.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const store_index = require("../store/index.js");
  4. function initRequest() {
  5. console.log("初始化了 http 请求代码");
  6. common_vendor.index.$u.http.setConfig((config) => {
  7. config.baseURL = `http://tf.hqzf100.com/dev-api/`;
  8. config.custom.toast = true;
  9. return config;
  10. });
  11. common_vendor.index.$u.http.interceptors.request.use((config) => {
  12. config.data = config.data || {};
  13. let token = store_index.store.token;
  14. config.header.Authorization = token ? `Bearer ${token}` : "";
  15. return config;
  16. }, (config) => {
  17. return Promise.reject(config);
  18. });
  19. common_vendor.index.$u.http.interceptors.response.use((response) => {
  20. var _a;
  21. const data = response.data;
  22. const custom = (_a = response.config) == null ? void 0 : _a.custom;
  23. if (data.code !== 200) {
  24. if (data.code === 401) {
  25. common_vendor.index.$u.toast(data.msg || "身份认证失败,请先登录");
  26. store_index.store.setToken("");
  27. setTimeout(() => {
  28. common_vendor.index.$u.route({
  29. type: "reLaunch",
  30. url: "/pages/login/login"
  31. // 跳转到登录页
  32. });
  33. }, 1e3);
  34. } else {
  35. if (custom.toast !== false) {
  36. common_vendor.index.$u.toast(data.msg);
  37. }
  38. return data;
  39. }
  40. }
  41. return data;
  42. }, (response) => {
  43. return Promise.reject(response);
  44. });
  45. }
  46. exports.initRequest = initRequest;