/*
 * @Author: zy1125 1515706227@qq.com
 * @Date: 2023-12-25 10:38:37
 * @LastEditors: zy1125 1515706227@qq.com
 * @LastEditTime: 2023-12-25 10:39:45
 * @FilePath: \v3_yyz\src\request\http2.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import axios from 'axios'
import { ElMessage } from 'element-plus';


export const service = axios.create({
    baseURL: 'https://weizhi.huanghuai.edu.cn/cmccr-server',
    // withCredentials: true, // 跨域请求时发送 cookies
    timeout: 10000

})




// 请求拦截器
service.interceptors.request.use(
    config => {
        // const token = session.get('Token');
        // token && (config.headers.Authorization = token);
        if (config.method.toUpperCase() === "POST") {
            config.headers['Content-Type'] = 'application/json;charset=utf-8';
        }
        return config
    },
    error => {
        return Promise.error(error)
    }
)
// 响应请求拦截器

service.interceptors.response.use(
    response => {
        // console.log("response", response)
        if (response.status === 200) {
            return Promise.resolve(response.data)
        } else {
            return Promise.reject(response)
        }
    },
    error => {
        if (error.response.status) {
            switch (error.response.status) {
                case 500:
                    console.error("网络错误，请稍后再试");
                    break;
                case 404:
                    console.error("请求路径出错");
                    break;

                default:
                    console.error(error.response.data.console)
            }
            return Promise.reject(error.response)
        }
    }
)



export function get(url, params) {
    return new Promise((resolve, reject) => {
        service.get(url, {
            params
        }).then(res => {
            resolve(res)
        }).catch(err => {
            reject(err)
        })
    })
}

export function post(url, params) {
    return new Promise((resolve, reject) => {
        service.post(url,
            params
        ).then(res => {
            resolve(res)
        }).catch(err => {
            reject(err)
        })
    })
}