interceptors.js 2.3 KB
"use strict";
const common_vendor = require("../common/vendor.js");
const uni_modules_uviewPlus_index = require("../uni_modules/uview-plus/index.js");
const store_index = require("../store/index.js");
const uni_modules_uviewPlus_libs_function_index = require("../uni_modules/uview-plus/libs/function/index.js");
const requestInterceptors = (vm) => {
  uni_modules_uviewPlus_index.http.interceptors.request.use((config) => {
    var _a;
    config.data = config.data || {};
    if ((_a = config == null ? void 0 : config.custom) == null ? void 0 : _a.auth) {
      const { user } = store_index.store();
      config.header.Authorization = user.token;
    }
    return config;
  }, (config) => (
    // 可使用async await 做异步操作
    Promise.reject(config)
  ));
};
let loginModal = false;
const responseInterceptors = (vm) => {
  uni_modules_uviewPlus_index.http.interceptors.response.use((response) => {
    var _a;
    const data = response.data;
    const custom = (_a = response.config) == null ? void 0 : _a.custom;
    if (data.code !== 200) {
      if (custom.toast !== false) {
        uni_modules_uviewPlus_libs_function_index.toast(data.msg);
      }
      if (custom == null ? void 0 : custom.catch) {
        return Promise.reject(data);
      } else {
        return new Promise(() => {
        });
      }
    }
    return data || {};
  }, (response) => {
    let { code, msg } = response.data;
    if (code === 401) {
      const { user } = store_index.store();
      user.Logout();
      if (!loginModal) {
        loginModal = true;
        common_vendor.index.showModal({
          title: "温馨提示",
          content: "你的身份信息已过期,需要重新登录~",
          // showCancel: false,
          confirmText: "前往登录",
          cancelText: "暂不登录",
          success: (res) => {
            if (res.confirm) {
              common_vendor.index.switchTab({
                url: "/pages/my/my"
              });
            }
            if (res.cancel) {
              common_vendor.index.navigateBack();
            }
            loginModal = false;
          }
        });
      }
    } else {
      common_vendor.index.$u.toast(msg);
    }
    return Promise.reject(response);
  });
};
exports.requestInterceptors = requestInterceptors;
exports.responseInterceptors = responseInterceptors;