import axios from "axios";
import Qs from "qs";
const JSONbig = require("json-bigint")({ storeAsString: true });
import cookie from "./cookie";
import httpErrorHandler from "./httpErrorHandler";

const instance = axios.create({
  baseURL: "/bill",
  //baseURL: process.env.VUE_APP_BASE_API,
  headers: {
    post: {
      "Content-Type": "application/json;charset=UTF-8",
      dataType: "json",
    },
  },
  transformResponse: [
    (data) => {
      try {
        JSON.parse(data);
        return JSONbig.parse(data);
      } catch (error) {
        return data;
      }
    },
  ],
});

instance.interceptors.request.use(
  (config) => {
    config.headers.Authorization = window.sessionStorage.getItem("token") || "";
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

instance.interceptors.response.use((response) => {
  return response.data;
}, httpErrorHandler);

/**
 * 封装后的axios post方法
 *
 * @param {string} url 请求路径
 * @param {object} option 请求参数
 * @param {object} [config] 特殊配置项（选填）
 * @returns
 */
export function post(url, option, config = {}) {
  const data = Object.assign({}, option, {
    // __mortals_token__: cookie.getItem('__mortals_token__'),
  });
  return instance.post(url, data, config);
}

/**
 * 封装后的axios get方法
 *
 * @param {string} url 请求路径
 * @param {object} option 请求参数
 * @param {object} [config] 特殊配置项（选填）
 * @returns
 */
export function get(url, option, config = {}) {
  const data = Object.assign({}, option, {
    //__mortals_token__: cookie.getItem('__mortals_token__'),
  });
  return instance.get(url, { params: data }, config);
}

// 文件上传
const uploadInstance = axios.create({
  baseURL: "/m",
  headers: {
    post: {
      "Content-Type": "multipart/form-data",
    },
  },
});

uploadInstance.interceptors.response.use((response) => {
  return response.data;
}, httpErrorHandler);

/**
 * 封装后的axios upload方法
 *
 * @param {string} url 请求路径
 * @param {object} option 请求参数
 * @param {object} [config] 特殊配置项（选填）
 * @returns
 */
export function upload(url, option, config = {}) {
  let formdata = new FormData();
  Object.keys(option).forEach((key) => {
    formdata.append(key, option[key]);
  });
  return uploadInstance.post(url, formdata, config);
}
