App({
    globalData: {
        c: "1.4.0",
        userInfo: {},
        authorized: !1,
        openid: "",
        platform: ""
    },
    onLaunch: function(e) {
        var a = this, o = e.query.years ? parseFloat(e.query.years) : 0;
        wx.cloud ? wx.cloud.init({
            traceUser: !0,
            env: "release-bu5dc"
        }) : wx.showToast({
            title: "请先将微信升级至最新版本",
            icon: "none",
            duration: 1500
        });
        var t = this;
        wx.getSystemInfo({
            success: function(e) {
                t.globalData.platform = e.platform;
            }
        });
        var n = wx.cloud.database(), c = (n.command, n.command.aggregate, wx.getUpdateManager());
        c.onCheckForUpdate(function(e) {}), c.onUpdateReady(function() {
            wx.showModal({
                title: "发现新版本",
                content: "赛易评小程序已发布最新版本，请完成更新后使用",
                confirmColor: "#FF5A5F",
                confirmText: "确定更新",
                showCancel: !1,
                success: function(e) {
                    e.confirm && c.applyUpdate();
                }
            });
        }), wx.getStorageSync("getUserProfile") ? (this.globalData.authorized = !0, wx.getStorageSync("openid") ? (this.globalData.openid = wx.getStorageSync("openid"), 
        this.updateVip(wx.getStorageSync("openid"), o)) : wx.cloud.callFunction({
            name: "getOpenid",
            data: {
                page: "app"
            }
        }).then(function(e) {
            a.globalData.openid = e.result, a.updateVip(e.result, o), wx.setStorageSync("openid", e.result);
        }).catch(function(a) {
            t.errCodeShowModal(a.errCode, e);
        })) : wx.cloud.callFunction({
            name: "getOpenid",
            data: {
                page: "app"
            }
        }).then(function(t) {
            a.globalData.openid = t.result, a.updateVip(t.result, o), wx.setStorageSync("openid", t.result), 
            n.collection("users").where({
                _openid: a.globalData.openid
            }).count().then(function(o) {
                if (0 == o.total) {
                    wx.cloud.callFunction({
                        name: "focusExample",
                        data: {
                            operation: "focusExample"
                        }
                    }).then(function(e) {});
                    var t = {
                        scene: e.scene,
                        authorized: !1,
                        realName: "",
                        createTime: n.serverDate(),
                        updateTime: n.serverDate()
                    };
                    n.collection("users").add({
                        data: t
                    }).then(function(e) {}).catch(function(e) {
                        console.log(e);
                    });
                } else 1 == o.total && n.collection("users").where({
                    _openid: wx.getStorageSync("openid")
                }).field({
                    nickName: !0,
                    avatarUrl: !0
                }).get().then(function(e) {
                    a.globalData.userInfo = {
                        nickName: e.data[0].nickName,
                        avatarUrl: e.data[0].avatarUrl
                    };
                }).catch(function(e) {
                    console.log(e);
                });
            });
        }).catch(function(a) {
            t.errCodeShowModal(a.errCode, e);
        }), n.collection("behaviors").add({
            data: {
                createTime: n.serverDate(),
                type: "login",
                scene: e.scene,
                page: "app",
                c: this.globalData.c
            }
        }).then(function(e) {}).catch(console.error), e.query.marketing && n.collection("behaviors").add({
            data: {
                createTime: n.serverDate(),
                type: "marketing",
                scene: e.scene,
                from: "officialaccount"
            }
        }).then(function(e) {}).catch(console.error);
    },
    errCodeShowModal: function(e, a) {
        var o = this;
        e && wx.showModal({
            content: "网络出了点问题",
            showCancel: !1,
            confirmColor: "#FF5A5F",
            confirmText: "点此重试",
            success: function(e) {
                e.confirm && o.onLaunch(a);
            },
            fail: function(e) {
                console.log(e);
            }
        });
    },
    updateVip: function(e, a) {
        a = parseFloat(a), e && a > 0 && wx.cloud.callFunction({
            name: "updateVipV2",
            data: {
                creator_openid: e,
                years: a,
                operation: "updateUser"
            }
        }).then(function(e) {
            console.log(e);
        });
    }
});