import Request from "./request";
const http = new Request();
http.setConfig(config => {
    /* 设置全局配置 */
    if (__wxConfig.envVersion == 'develop') {
        // let selectedService = uni.getStorageSync("sercviceChoice")
        // if (!selectedService || selectedService == 'aloneServe') {
        // config.baseUrl = "https://api.radar-power.asia:4443/portal-service-server/";
        // }
        config.baseUrl = "https://radar-power.cn/portal-service-server/";
        config.baseUrl = "https://api.radar-power.asia:4443/portal-service-server/";
    }
    if (__wxConfig.envVersion == 'trial') {
        // let selectedService = uni.getStorageSync("sercviceChoice")
        // if (!selectedService || selectedService == 'aloneServe') {
        //     config.baseUrl = "https://radar-power.cn/portal-service-server/";
        // }
        config.baseUrl = "https://radar-power.cn/portal-service-server/";
        config.baseUrl = "https://api.radar-power.asia:4443/portal-service-server/";
    }
    if (__wxConfig.envVersion == 'release') {
        // let selectedService = uni.getStorageSync("sercviceChoice")
        // if (!selectedService || selectedService == 'aloneServe') {
        //     config.baseUrl = "https://radar-power.cn/portal-service-server/";
        // }
        config.baseUrl = "https://radar-power.cn/portal-service-server/";
    }
    config.header = {
        // a: 1,
        // b: 2
    };
    return config;
});
http.interceptor.request((config, cancel) => {

    console.log(config.url, '调用了>>>接口')

    let containsUrl = ['wap/user/wx/loginByPhone', 'wap/user/loginWx']
    for (let i = 0; i < containsUrl.length; i++) {
        if (!config.url.includes(containsUrl[i])) {
            config.header = {
                "token": uni.getStorageSync('tokenValue') || '',
                ...config.header
            };
            return config;
        }
    }
    return config;
});
// 响应拦截器
http.interceptor.response(response => {
    uni.setStorageSync(
        "judgeFlage",
        false
    );
    if (response.data.code == '11011' || response.data.code == '11012') {
        uni.setStorageSync(
            "judgeFlage",
            false
        );
        let judgeFlage = uni.getStorageSync(
            "judgeFlage"
        )
        if (!judgeFlage) {
            uni.showModal({
                content: "登录过期，请重新登录",
                complete: (res) => {
                    if (res.confirm) {
                        uni.setStorageSync(
                            "judgeFlage",
                            true
                        );
                        uni.reLaunch({
                            url: "/pagesA/loginNew/loginNew"
                        })
                        return
                    }
                    if (res.cancel) {
                        uni.setStorageSync(
                            "judgeFlage",
                            true
                        );
                        uni.reLaunch({
                            url: "/pagesA/loginNew/loginNew"
                        })
                    }

                    return
                },
            });
        }

    }
    console.log(response, '接口打印了>>>')
    return response; // 正常返回响应

});


export {
    http
};
