import { deleteUserMsg, getUserMsg } from '@/utils/userMsg/userMsg'
import axios, {
  AxiosProxyConfig,
  AxiosRequestConfig,
  AxiosResponse,
} from 'axios'
import { message, Modal } from 'ant-design-vue'
import router from '@/router'
import RouterUtil from '@/utils/router'

const baseAxios = axios.create({
  baseURL: '/proxyApi/api/v1',
})

baseAxios.interceptors.response.use(
  (value: AxiosResponse<iRet>) => {
    const ret = value.data
    if(ret.code === 3000){
      
    }
    else if (ret.code !== undefined && ret.code !== 200) {
      if ([504, 503].includes(ret.code)) {
        const role = getUserMsg()?.newRole
        window.localStorage.clear()
        RouterUtil.getInstance().gotoLogin(role)
        // deleteUserMsg();
      }
      message.error(ret.msg)
    }
    return ret
  },
  (error) => {
    Modal.error({
      title: '错误',
      content: error.message,
    })
    throw new Error(error)
  },
)

export interface iRet {
  code: number
  msg: string
  data: any
}

export default function <T = any>(config: AxiosRequestConfig) {
  return (baseAxios({
    ...config,
    headers: {
      Authorization: 'Bearer ' + getUserMsg()?.token,
    },
  }) as unknown) as Promise<{
    code: number
    msg: string
    data: T
  }>
}

interface i {
  data: string
}
type c = i | { data: number; c: string }
const C: c = {
  data: 2,
  c: '',
}
