import Vue from "vue";
import axios from "axios";

import { Toast } from "vant";
Vue.use(Toast);
// 配置环境
const env = process.env.NODE_ENV === "development" ? "dev" : "prod";

const baseUrl = {
  // dev: "http://192.168.3.144:10010/",
  dev: "http://81.69.231.72:10010/",
  // dev: "https://www.smart-zj.cn:55901/smartApp/",

  // prod: 'http://81.69.231.72:9090/',
  prod: "https://www.smart-zj.cn:55901/smartApp/",
};
const baseH5 = {
  // dev: "http://www.smart-zj.cn:55902/zjh/html/pages/#/",
  dev: "http://81.69.44.115/zjh/html/pages2/#/",
  // prod: 'http://81.69.44.115/zjh/html/pages/#/',
  prod: "http://www.smart-zj.cn:55902/smart-h5/#/",
};
const config2 = {
  baseUrl: baseUrl[env],
  baseH5: baseH5[env],
  // baseUrl: 'http://81.69.231.72:9090/',   // 测试
  // baseH5: 'http://81.69.44.115/zjh/html/pages/#/',  // 测试

  // baseUrl: 'https://www.smart-zj.cn:55901/cert/',   // 生产
  // baseH5: 'http://www.smart-zj.cn:55902/zjh/html/pages/#/', // 生产
  // baseSocUrl: "https://www.smart-zj.cn:55901/soc/", // 公积金社保
  baseSocUrl: "https://www.smart-zj.cn:55901/third-party/", // 公积金社保
  baseServiceUrl: "http://58.217.53.110/epoint-web-zwdt/rest/", // 政务np服务
  basePurchase:
    "http://zfcg.ggzy.zhenjiang.gov.cn/WebBuilderMobileService/appservice/", // 公共资源采购
};

const request2 = axios.create({
  timeout: 30000,
  withCredentials: false,
});
// sessionStorage.setItem('token',"cAu7hNfHsBhC0slp3t2fpTFhYb1P9I5X339Hq7Y7BRSqIn18qGEVJSGZ7xy5Woex")
request2.interceptors.request.use(
  (config) => {
    config.headers = {
      "Content-Type": "application/json;multipart/form-data;",
      Authorization: "Bearer " + sessionStorage.getItem("token"),
      // "Authorization": "Bearer lkT98D62spmAUVpkZv5TwTlZKSYncn1Tww7NNPe7jHtxV0Yjj7zyjIxBYizEAxMZ" //测试token
    };
    // alert(JSON.stringify(config.headers))
    // if (config.method === 'post') {
    //   config.data = { ...config.data }
    // } else if (config.method === 'get') {
    //   config.params = { ...config.params }
    // }
    return config;
  },
  (error) => {
    console.log(error);
    return Promise.reject(error);
  }
);

request2.interceptors.response.use(
  (response) => {
    const res = response.data;
    console.log(res, "tijaio--------");
    if (response.config.url.indexOf(config2.basePurchase) !== -1) {
      console.log(res ,"1234");

      if (res.EpointDataBody.DATA.ReturnInfo.Status === "True") {
        return res;
      } else {
        Toast(res.EpointDataBody.DATA.ReturnInfo.Description || "网络错误");
        return Promise.reject(
          new Error(
            res.EpointDataBody.DATA.ReturnInfo.Description || "网络错误"
          )
        );
      }
    } else if (response.config.url.indexOf(config2.baseServiceUrl) !== -1) {
      console.log(res.status.text ,"67777");
      if (res.status.code === 200) {
        return res;
      } else {
       
        Toast(res.status.text || "网络错误");
        return Promise.reject(new Error(res.status.text || "网络错误"));
      }
    } else {
      // alert(JSON.stringify(res))
      console.log(res ,"shihsihsi");

      if (parseInt(res.code) === 200 ||parseInt(res.code) === 1004011003) {
        return res;
      } else {
        Toast(res.message || res.msg || "网络错误");
        return Promise.reject(new Error(res.message || "网络错误"));
      }
    }
  },
  (error) => {
    Toast.clear();
    console.log("err" + error);
    Toast(error.message || "网络错误");
    return Promise.reject(error);
  }
);

const request3 = axios.create({
  timeout: 30000,
  withCredentials: false,
});

request3.interceptors.request.use(
  (config) => {
    config.headers = {
      "Content-Type": "application/json;multipart/form-data;",
      Authorization: "Bearer " + sessionStorage.getItem("token"),
      // "Authorization": "Bearer lkT98D62spmAUVpkZv5TwTlZKSYncn1Tww7NNPe7jHtxV0Yjj7zyjIxBYizEAxMZ" //测试token
    };
    // if (config.method === 'post') {
    //   config.data = { ...config.data }
    // } else if (config.method === 'get') {
    //   config.params = { ...config.params }
    // }
    return config;
  },
  (error) => {
    console.log(error);
    return Promise.reject(error);
  }
);

request3.interceptors.response.use(
  (response) => {
    const res = response.data;
    if (response.config.url.indexOf(config2.basePurchase) !== -1) {
      if (res.EpointDataBody.DATA.ReturnInfo.Status === "True") {
        return res;
      } else {
        Toast(res.EpointDataBody.DATA.ReturnInfo.Description || "网络错误");
        return Promise.reject(
          new Error(
            res.EpointDataBody.DATA.ReturnInfo.Description || "网络错误"
          )
        );
      }
    } else if (response.config.url.indexOf(config2.baseServiceUrl) !== -1) {
      if (res.status.code === 200) {
        return res;
      } else {
        Toast(res.status.text || "网络错误");
        return Promise.reject(new Error(res.status.text || "网络错误"));
      }
    } else {
      // alert(JSON.stringify(res))
      if (parseInt(res.code) === 200) {
        return res.data || res.message;
      } else {
        Toast(res.message || res.msg || "网络错误");
        return Promise.reject(new Error(res.message || "网络错误"));
      }
    }
  },
  (error) => {
    Toast.clear();
    console.log("err" + error);
    Toast(error.message || "网络错误");
    return Promise.reject(error);
  }
);

const formatUrl = (params) => {
  let ary = [];
  Object.keys(params).map((key) => {
    ary.push(encodeURIComponent(key) + "=" + encodeURIComponent(params[key]));
    // if (params[key]) {
    //   ary.push(encodeURIComponent(key) + '=' + encodeURIComponent(params[key]));
    // }
  });
  let urlStr = ary.join("&");
  return `?${urlStr}`;
};

export { config2, request2, request3, formatUrl };
