/*
 * @Author: lixg lixg@dongwu-inc.com
 * @Date: 2022-11-21 14:32:52
 * @LastEditors: lixg lixg@dongwu-inc.com
 * @LastEditTime: 2023-01-04 13:49:54
 * @FilePath: /fe-manage/src/api/config.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {message} from "ant-design-vue";
import axios from "axios";
import router from "@/router";
// 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_BASE_API,
    timeout: 1000 * 15,
    // headers: { "Content-Type": "multipart/form-data" },
    headers: {"Content-Type": "application/json"},
});

http.interceptors.request.use(
    (config) => {
        // console.log("config", config);
        // const token = localStorage.getItem("token");
        // // const token = getCookie('token')
        // // console.log('token', token)
        // if (token) {
        //   config.headers.token = token; //测试1111
        // } else {
        // console.log("当前请求页面无token,请执行操作！！！");

        // // 此处测试默认配置token
        // config.headers.token =
        //   "eyJ0eXBlIjoidG9rZW4iLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC91LmJvZS5jb20iLCJpYXQiOjE2NzAxNTMxMDMsImV4cCI6MTY3MDE2MDMwMywiR2l2ZW5OYW1lIjoiYm9ldSIsInVzZXJJZCI6IjZCMDQ5RkFGLUMzMTQtN0NDRi0wRDI4LTBEMjNGNEM0MjUzMSIsInVJZCI6Ijk2NTM0MjAyNzQ5NzYwNzE2OCIsInBlcm1pc3Npb24iOiIifQ==.c937b2d3a59cbab2136fdde55fd38f06bdff041212aab0fa6741bc4be41e28a7";
        // // }
        return config;
    },
    (err) => {
        console.log("登陆前拦截", err);
        return Promise.reject(err);
    }
);

http.interceptors.response.use(
    (response) => {
        // console.log('response', response)
        const {
            data: {code, msg, show},
        } = response;
        console.log('code', code)
        if (code === 0 || code === 200) {
            return response;
        }
        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);
    },
    function (error) {
        if (error.message == "timeout of 1ms exceeded") {
            message.destroy();
            message.error("请求超时");
        }
        console.log("api error %o", error);
        return Promise.reject(error);
    }
);

export default http;
