
import {message} from "ant-design-vue";
import axios from "axios";
import router from "@/router";
import Cookies from 'vue-cookies'
// import { getCookie } from '../api/method'
// const Qs = require("qs");

// axios.defaults.headers.post["Content-Type"] =
//   "application/x-www-form-urlencoded";
// export const FILE_UPLOAD_URL = process.env.VUE_APP_BASE_API + '/file/upload'
// export const BATCH_IMPORT_SCORE = process.env.VUE_APP_BASE_API + '/admin/offcourse/batchImportScore'
axios.defaults.withCredentials = true;
const http = axios.create({
    baseURL: process.env.VUE_APP_SYS_API,
    timeout: 1000 * 15,
    headers: {"Content-Type": "application/json",},
});

http.interceptors.request.use(
    (config) => {
        const token = Cookies.get("token")
        if (token) {
          config.headers.token = token; //测试1111
        } else{
            message.error('未获取到登录信息，请先登录')
        }
        return config;
    },
    (err) => {
        console.log("登陆前拦截", err);
        return Promise.reject(err);
    }
);

http.interceptors.response.use(
    (response) => {
        // console.log('response', response)
        const {
            data: {code},
        } = response;
        if (code === 0 || code === 200) {
            return response.data?response.data:response;
        }
        if(code==500){
            return message.error('请求失败'); 
        }
        if(code==601){
             message.error('token过期请重新登陆'); 
        }
        if (code === 1000) {
            (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'alpine') ? router.push({path: 'login'}) : (window.location.href = process.env.VUE_APP_LOGIN_URL)
            return Promise.reject(response);
        }
        // show && message.error(msg);
        // console.log("api %o", msg);
        // return Promise.reject(response);
        return response
    },
    function (error) {
        if (error.message == "timeout of 1ms exceeded") {
            message.destroy();
            message.error("请求超时");
        }
        console.log("api error %o", error);
        return message.error(error.message);
    }
);

export default http;
