class HTTP {
    constructor() {
        this.ENV = wx.getAccountInfoSync().miniProgram.envVersion;
        this.urls = [{
            name: "生产环境",
            url: "https://oms.idcgroup.com.cn:8079"
        }, {
            name: "开发环境",
            url: "http://61.164.207.46:8100"
        }]
        if (this.ENV === 'release') { // 正式版
            this.baseUrl = "https://oms.idcgroup.com.cn:8079";
        } else {
            this.baseUrl = "http://61.164.207.46:8100";
            // this.baseUrl = "https://oms.idcgroup.com.cn:8079";
        }

        this.init = (content, init = false) => {
            return new Promise((resolve, reject) => {
                if (init.detail != undefined) init = init.detail;
                if (init) content.pageNumber = 1;
                content.pageNumber > content.pageTotal ? reject() : resolve(content)
            })
        }

        this.paging = (data, res, that, ownertable, list = [], _Http) => {
            let content = JSON.parse(JSON.stringify(data))
            content.pageNumber = res.pageNumber + 1;
            content.pageTotal = res.pageTotal;
            content.total = res.total;
            content.sort = res.sort;
            if (this.isShowEmpty) this.empty = res.total == 0;
            if (list.length) this.getTabs(that, ownertable, list, _Http)
            return content;
        }


        this.getTabs = (that, ownertable, list = [], _Http) => {
            if (list.length) _Http.basic({
                "id": 20221018102001,
                "content": {
                    ownertable,
                    ownerids: list.map(v => v[ownertable + 'id'])
                }
            }).then(res => {
                if (res.code != 1) return;
                list.forEach(item => item.tags = res.data[item[`${ownertable}id`]] || []);
                let newList = that.data.list;
                newList.splice(newList.length - list.length, list.length, ...list);
                that.setData({
                    list: newList
                })
            })
        }

        this.getTypes = async (name, _Http) => {
            let params = null;
            if (name == 'tags') {
                params = {
                    "id": 20221013104401,
                    "content": {
                        "nochace": true,
                        "isExport": 1,
                        "pageNumber": 1,
                        sys_systemtagid: 2,
                        "pageSize": 1000,
                        "where": {
                            "condition": ""
                        }
                    }
                }
            } else {
                params = {
                    "classname": "sysmanage.develop.optiontype.optiontype",
                    "method": "optiontypeselect",
                    "content": {
                        "nochace": true,
                        "pageNumber": 1,
                        "pageSize": 1000,
                        "typename": name,
                        "parameter": {
                            "siteid": wx.getStorageSync('userMsg').siteid
                        }
                    }
                }
            }
            if (!params) return;
            let res = await _Http.basic(params);
            return res.code == '1' ? res.data : [];
        }

    }
    request({
        url,
        data = {},
        method = "POST",
        header = {
            'content-type': 'application/json'
        },
        loading = true
    }) {
        return new Promise((resolve, reject) => {
            this._request(url, resolve, reject, data, method, header, loading);
        })
    }
    _request(url, resolve, reject, data, method, header, loading) {
        /* if (loading) wx.showLoading({
            title: getApp().globalData.Language.getMapText('加载中...'),
            mask: true
        }) */
        if (data.method && data.method != "query_userauth") data.languagecode = wx.getStorageSync('languagecode') || 'ZH'
        wx.request({
            url: this.baseUrl + '/yos/rest/index' + url,
            data: data,
            method: method,
            header: header,
            timeout: 60000,
            success: res => resolve(res.data),
            fial: err => reject(err),
            complete: (res) => {
                // if (loading) wx.hideLoading()
                if (res.data.code != 1) {
                    console.error("错误的请求", {
                        request: data,
                        result: res.data
                    })
                }
                if (res.errMsg != 'request:ok') {
                    wx.showToast({
                        title: getApp().globalData.Language.getMapText('网络异常，请重新进入'),
                        icon: "none"
                    })
                } else if (res.data.msg == '登陆状态已过期，请重新登陆！' || res.data.msg == getApp().globalData.Language.getMapText('登陆状态已过期，请重新登陆！')) {
                    wx.showToast({
                        title: getApp().globalData.Language.getMapText(res.data.msg),
                        icon: "none",
                        mask: true
                    })
                    getApp().globalData.LaunchOptions = wx.getLaunchOptionsSync();
                    count = setTimeout(() => {
                        wx.reLaunch({
                            url: '/pages/login/phone',
                        });
                    }, 1000)
                }
            }
        })
    }
}
export {
    HTTP
}