"use strict";
const common_vendor = require("../vendor.js");
const common_store_useWeAppAuthStore = require("../store/useWeAppAuthStore.js");
const http = new common_vendor.Request();
http.setConfig((config) => {
  config.baseURL = "https://weapp-api.linyikj.com.cn/api/";
  config.header = {
    ...config.header
  };
  return config;
});
http.interceptors.request.use(
  (config) => {
    config.header = {
      ...config.header,
      Authorization: "Bearer " + common_vendor.index.getStorageSync("ACCESS_TOKEN_CUSTOMER")
    };
    return config;
  },
  (config) => {
    return Promise.reject(config);
  }
);
http.interceptors.response.use(
  (response) => {
    const data = response.data;
    common_vendor.index.hideLoading();
    if (data.success !== true) {
      if (data.errorCode === 10001) {
        const auth = common_store_useWeAppAuthStore.useWeAppAuthStore();
        auth.loading = false;
        common_vendor.index.showToast({ title: data.errorMessage, icon: "none" });
      } else {
        common_vendor.index.showToast({ title: data.errorMessage, icon: "none" });
      }
      return Promise.reject(response.data);
    }
    return response.data;
  },
  (response) => {
    console.log("error", response);
    const auth = common_store_useWeAppAuthStore.useWeAppAuthStore();
    auth.loading = false;
    return Promise.reject(response);
  }
);
function request(url, data) {
  return http.post(url, data.data);
}
exports.request = request;
