import addPrefixToHeaderIcon from './addPrefixToHeaderIcon.js'
export default function request(urldata) {
    const { url, data, method, header, userInfo } = urldata;
    // const baseUrl = "http://49.235.115.212:8086/"+ url;
    // const baseUrl = "https://pk.zhukeping.com/"+ url;
    const baseUrl = "http://120.26.251.180:8086/"+ url;
    // const baseUrl = "http://192.168.1.218:8086/"+ url;
    if (userInfo) {
        return new Promise((resolve, reject) => {
            uni.getStorage({
                key: "userinfo",
                success: (res) => {
                    if (res.data) {
                        if (res.data.nickName) {
                            uni.request({
                                url: baseUrl,
                                data: data,
                                method: method,
                                header: header,
                                success: function (res) {
                                    resolve(addPrefixToHeaderIcon(res.data));
                                    // resolve(res.data);
                                },
                                fail: function (res) {
                                    reject(res);
                                }
                            });
                        } else {
                            uni.setStorageSync("lastPage", getCurrentPages()[getCurrentPages().length - 1].route);
                            uni.reLaunch({ url: "/pages/UserInformation/UserInformation" })
                        }
                    } else {
                        uni.setStorageSync("lastPage", getCurrentPages()[getCurrentPages().length - 1].route);
                        uni.navigateTo({ url: '/pages/login/login' })
                    }
                },
                fail: function (res) {
                    uni.setStorageSync("lastPage", getCurrentPages()[getCurrentPages().length - 1].route);
                    uni.navigateTo({ url: '/pages/login/login' })
                    reject(res);
                }
            });
        });
    } else {
        return new Promise((resolve, reject) => {
            uni.request({
                url: baseUrl,
                data: data,
                method: method,
                header: header,
                success: function (res) {
                    // resolve(res.data);
                    resolve(addPrefixToHeaderIcon(res.data));
                },
                fail: function (res) {
                    reject(res);
                }
            });
        });
    }

}