import axios from "axios";
import { showToast,showDialog } from 'vant';
import host from "./host"
import wx from 'jweixin-1.6.0'

// 1. 创建axios实例
const instance = axios.create({
  // 接口
  baseURL: import.meta.env.VITE_BASE_URL,
  // 超时时间
  timeout: 100000,
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
   }
  
});
// 2.请求拦截
instance.interceptors.request.use(
  config => {
    let token='';
    let token_gandan = localStorage.getItem('token_gandan');
    let token_other = localStorage.getItem('token_other');
    const params = new URLSearchParams(window.location.search);
    let source=params.get('source');
    if(source==3){
      if(token_other){
        token=token_other;
      }
    }else{
      if(token_gandan){
        token=token_gandan;
      }
    }
    if (token) {
      config.headers['Authorization'] ='Bearer '+token
    }
    return config;
  },
  error => {
    //  请求发生错误，抛出异常
    Promise.reject(error);
  }
);

// 3.响应拦截
instance.interceptors.response.use(
  res => {
    const params = new URLSearchParams(window.location.search);
    let source=params.get('source');
    if (res.data && res.data.code === 200 ) {
      //let status=res.data.code;
      return res.data;
    }else{
      let status=res.data.code;
      switch (status) {
        case 201:
          localStorage.clear();
          showToast("该账户已被禁用");
          break;
        case -1:
          if(res.data.data.source==3){
            showDialog({
              width: '80%',
              title: '提示',
              message: '跳转失败，请返回重试',
            }).then(() => {
              wx.miniProgram.navigateBack({
                fail:function(res) {
                  wx.miniProgram.switchTab({
                    url: '/pages/media_home/index'
                    })
                }
              });
              // wx.miniProgram.switchTab({
              //   url: '/pages/media_home/index',
              //   success(res) {
              //     consoe.log('跳转成功');
              //   },
              //   fail(res) {
              //     consoe.log('跳转失败');
              //   }
              // });
            });
          }else{
            showToast(res.data.message);
          }
         return res.data;
          break;
          case -2:
          localStorage.clear();
         //showToast('该账户未注册');
         let url='https://dev-wx.igandan.com';
         if(host.indexOf('//dev-caseplatform.igandan.com')==-1){
           url='https://wx.igandan.com'
         }
          window.location.href = url+"/hcp/Signup2020online_tologin?back_url="+host+"/web/home"
          break;
        case 400:
         showToast("请求错误");
          break;
        case 401:
          showToast("未授权，请重新登录");
          if(source==3){
            localStorage.clear();
            wx.miniProgram.navigateBack({
              fail:function(res) {
                wx.miniProgram.switchTab({
                  url: '/pages/media_home/index'
                  })
              }
            });
            // wx.miniProgram.switchTab({
            //   url: '/pages/media_home/index',
            //   success(res) {
            //     consoe.log('跳转成功');
            //   },
            //   fail(res) {
            //     consoe.log('跳转失败');
            //   }
            // });
          }else{
            localStorage.clear();
            window.location.reload();
          }
          break;
        case 403:
         showToast("拒绝访问");
          break;
        case 404:
         showToast("请求错误，未找到相应的资源");
          break;
          case 405:
            showToast("未授权，请重新登录");
            if(source==3){
              localStorage.clear();
              wx.miniProgram.navigateBack({
                fail:function(res) {
                  wx.miniProgram.switchTab({
                    url: '/pages/media_home/index'
                    })
                }
              });
              // wx.miniProgram.switchTab({
              //   url: '/pages/media_home/index',
              //   success(res) {
              //     consoe.log('跳转成功');
              //   },
              //   fail(res) {
              //     consoe.log('跳转失败');
              //   }
              // });
            }else{
              localStorage.clear();
              window.location.reload();
            }
            break;
        case 408:
         showToast("请求超时");
          break;
        case 500:
         showToast("服务器内部错误");
          break;
        case 501:
         showToast("网络未实现");
          break;
        case 502:
         showToast("网络错误");
          break;
        case 503:
         showToast("服务不可用");
          break;
        case 504:
         showToast("网络超时");
          break;
        case 505:
         showToast("HTTP版本不支持该请求");
          break;
        default:
          return res.data;
          //ElMessage.error("请求失败");
      }
      //return res;
    }
   
  },
  error => {
    if (error && error.response) {
      const status = error.response.status
      switch (status) {
        case 201:
          localStorage.clear()
         showToast("该账户已被禁用");
          break;
        case 400:
         showToast("请求错误");
          break;
        case 401:
          localStorage.clear()
          window.location.reload();
         showToast("未授权，请重新登录");
          break;
        case 403:
         showToast("拒绝访问");
          break;
        case 404:
         showToast("请求错误，未找到相应的资源");
          break;
        case 408:
         showToast("请求超时");
          break;
        case 500:
         showToast("服务器内部错误");
          break;
        case 501:
         showToast("网络未实现");
          break;
        case 502:
         showToast("网络错误");
          break;
        case 503:
         showToast("服务不可用");
          break;
        case 504:
         showToast("网络超时");
          break;
        case 505:
         showToast("HTTP版本不支持该请求");
          break;
        default:
         showToast("请求失败");
      }
    } else {
      if (JSON.stringify(error).includes("timeout")) {
       showToast("服务器响应超时，请刷新页面");
      }
     showToast("连接服务器失败");
    }
    return Promise.reject(error);
  }
);
// 4.导出 axios 实例
export default instance;

