"use strict";
import axios from "axios";
import { Toast } from 'vant';
import CryptoJS from './crypto'

let config = {
  baseURL: import.meta.env.VITE_APP_BASEAPIURL,
  timeout: 60 * 1000, // Timeout
};

const _axios = axios.create(config);

_axios.interceptors.request.use(
  function (config) {
    return config;
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
_axios.interceptors.response.use(
  function (response) {
    let data
    if(import.meta.env.MODE==='production'){
      const headKeyStr=response.headers.dk
      const desKey=CryptoJS.Des3Decrypt(headKeyStr,'JMCqSoUrTAmyNNIRb0TtlrPk') 
      data=JSON.parse(CryptoJS.Des3Decrypt(response.data,desKey));//解密
    }else{
      data=response.data
    }
      // const data=JSON.parse(CryptoJS.Des3Decrypt(response.data));//解密
      // const data = response.data;

    if(data.Ret === 403) {
      Toast(data.Msg)
    }
    return data;
  },
  function (error) {
    // Do something with response error
    return Promise.reject(error);
  }
);

/**
 * 导出get请求方法
 * @url 请求地址
 * @params get请求参数
 */
export const get = (url, params) => {
  return _axios.get(url, { params })
};

/**
 * 导出post请求方法
 * @url 请求地址
 * @params post请求参数
 */
export const post = (url,params) => {
  return _axios.post(url, params)
};
