import axios from "axios";
import { getToken, removeToken } from "@/utils/token";
import { messageFn } from '@/api/user'
const baseURL = "https://lygsh-api.wjzpgz.com/";
// const baseURL = "http://192.168.11.48:8080/";
// 创建一个 axios 实例
const instance = axios.create({
    baseURL, // 设置基础 URL
    // baseURL: "http://192.168.11.63:8080/", // 设置基础 URL
    timeout: 5000, // 设置请求超时时间
    headers: {
        "Content-Type": "application/json" // 设置默认请求头
    }
});

// 请求拦截器
instance.interceptors.request.use(
    config => {
        // 在这里可以添加请求发送前的处理逻辑，例如添加 token 等
        // const token = localStorage.getItem('token');
        // if (token) {
        //   config.headers.Authorization = `Bearer ${token}`;
        // }·
        // const token =
        //     "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxNTU4NTExMDY1NSIsInVzZXJ0eXBlIjoiYnVzaW5lc3MiLCJleHAiOjE3MjQzNzcwOTcsImNyZWF0ZWQiOjE3MjMwODEwOTc5MTcsImF1dGhvcml0aWVzIjpbeyJhdXRob3JpdHkiOiJCVVNJTkVTUyJ9XX0.pIxkAOBlIjc1WwjUR69hok0-k8me21FbtjBiC4amxZxewwEZCUi2yvlNpCyg-bGSkx4Vx-LaOBvhj_k32jN52g";
        const token = getToken();
        if (token) {
            config.headers["User-Token"] = token;
        }
        return config;
    },
    error => {
        // 处理请求错误
        return Promise.reject(error);
    }
);

// 响应拦截器
instance.interceptors.response.use(
    response => {
        // 在这里可以对响应数据进行预处理

        function handleError(message) {
            removeToken();
            xma.showToast({
                title: message,
                icon: "none",
                duration: 1900
            });
            setTimeout(() => {
                xma.reLaunch({
                    url: "/pages/login/index"
                });
            }, 2000);
        }
        let { code, message } = response.data;
        if (code == 200) {
            return response.data;
        } else if (code == 9) {
            handleError("登录已过期");
        } else if (code == 2) {
            handleError("登录超时");
        } else if (code == 8) {
            xma.showToast({
                title: message || "未查到该企业",
                icon: "none",
                duration: 1900
            });
        } else if (code == 13) {
            uni.showModal({
                title: '您的帐号已失效',
                content: '由于您超过1月未登录现已将您的账号禁用您可完成申述后继续使用',
                showCancel: true,
                confirmText: '立即申述',
                success: function (confirm) {
                    if (confirm.confirm) {
                        xma.navigateTo({
                            url: '/pages/user/businessLicense/fillIn?steps=2'
                        })
                    }

                }
            });
        } else if (code == 12) {
            uni.showModal({
                title: response.data.data.title,
                content: response.data.data.content,
                showCancel: false,
                confirmText: '去看看',
                success: function (confirm) {
                    messageFn(response.data.data.id)
                    if (confirm.confirm) {
                        xma.switchTab({
                            url: '/pages/recommend/index'
                        })
                    }

                }
            });
        }
    },
    error => {
        // 处理响应错误
        // 例如根据不同的错误状态码进行相应的处理
        return Promise.reject(error);
    }
);

export default instance;
