import axios from 'axios';
import { showNotify } from 'vant';
import Common from '../common/Common.js';

// 创建 axios 实例
const request = axios.create({
    // API 请求的默认前缀
    baseURL: '/',
    timeout: 60000, // 请求超时时间
});

// 异常拦截处理器
const errorHandler = error => {
    console.log(error);
    if (error.response) {
        const data = error.response.data;
        // 从 localstorage 获取 token
        const token = localStorage.getItem('#token');
        if (error.response.status === 403) {
            showNotify({
                type: 'danger',
                message: `Forbidden: ${data.message}`,
                duration: 3000,
            });
        }
        if (error.response.status === 401 && !(data.result && data.result.isLogin)) {
            console.log('401');
            showNotify({
                type: 'danger',
                message: 'Unauthorized: Authorization verification failed',
                duration: 3000,
            });
            if (token) {
                // store.dispatch('Logout').then(() => {
                //   setTimeout(() => {
                //     window.location.reload();
                //   }, 1500);
                // });
            }
        }
    }
    return Promise.reject(error);
};

// request interceptor
request.interceptors.request.use(config => {
    const token = localStorage.getItem('#token');
    // 如果 token 存在
    // 让每个请求携带自定义 token 请根据实际情况自行修改
    if (token) {
        config.headers['token'] = token;
    }
    return config;
}, errorHandler);


// 异常拦截处理器
const responseErrorHandler = error => {
    console.log(error);
    if (error.response.status === 401) {
        const currentUrl = window.location.href;
        if (currentUrl.indexOf('login') < 0 && currentUrl.indexOf('redirectUrl=') < 0) {

            // window.location = Common.getRedirectUrl('#/login?redirectUrl=' + encodeURIComponent(currentUrl));

            const isOut = localStorage.getItem('isOut');
            // 根据环境判断使用不同的 URL
            const isProduction = process.env.APP_ENV === 'production';
            const loginPath = isProduction ? 'board.html#/login' : '#/login';
            
            // if (isOut == 'true') {
            //     window.location = Common.getRedirectUrl(loginPath + '?isOut=true&redirectUrl=' + encodeURIComponent(currentUrl));
            // } else {
            //     window.location = Common.getRedirectUrl(loginPath + '?isOut=false&redirectUrl=' + encodeURIComponent(currentUrl));
            // }

            window.location = Common.getRedirectUrl(loginPath + '?redirectUrl=' + encodeURIComponent(currentUrl));
        }
    }
    if (error.response.status === 504) {
        showNotify({
            type: 'danger',
            message: `504: ${error.response.data}`,
            duration: 3000,
        });
    }
    return Promise.reject(error);
};


// response interceptor
request.interceptors.response.use(response => {
    return response.data;
}, responseErrorHandler);


export default request;

