//引入axios
import axios from 'axios'

//引入用户信息组件
import { userInfoStore } from '@/stores'
import router from "@/router";


const userInfo = userInfoStore()
//创建axios实例

//创建基础访问路径
const base_url = 'http://10.113.248.3:8086'

//设置axios 默认访问路径
axios.defaults.baseURL = base_url
//设置超时时间
axios.defaults.timeout = 30000

// 设置头部信息
axios.defaults.headers.common['Content-Type'] = 'application/JSON;charset=UTF-8'

//前端跨域配置
axios.defaults.withCredentials = true;

//设置 使用token的白名单
const whiteList = []

//设置请求前的拦截器
axios.interceptors.request.use(

    config => {
        //在请求前需要做什么
        //如果有token---则放在请求头上
        config.headers.Authorization = userInfo.token
            ? `Bearer ${userInfo.token}`
            : "";
        return config
    },
    error => {
        //对错误请求需要做些什么
        return Promise.reject(error)
    }
)

//添加相应拦截器

axios.interceptors.response.use(
    response => {
        //对响应中做些什么
        if (response.data.code === 401) {
            // 重定向到登录页
            router.replace({
                path: "/",
                // query: {redirect: router.currentRoute.fullPath}
            });
            //此时报告找不到路径
            // Toast.fail(response.data.msg);
            // Message.error(response.data.msg);
        }
        return response
    }, error => {
        Promise.reject(error)
    }
)

//封装请求
export const http = <T>(option: any) => {
    return new Promise((resolve, reject) => {
        axios({
            ...option,
        }).then(res => {
            //在这里应该机解密下
            resolve(res.data)
        }).catch(error => {
            reject(error)
        });
    })
}
