var e = getApp(), a = wx.cloud.database();

Page({
    data: {
        _id: "",
        fromPage: "",
        canIUseGetUserProfile: !1
    },
    onLoad: function(t) {
        var o = this;
        this.data.fromPage = t.page, wx.getUserProfile && this.setData({
            canIUseGetUserProfile: !0
        }), a.collection("users").where({
            _openid: e.globalData.openid
        }).field({
            _id: !0
        }).get().then(function(e) {
            o.data._id = e.data[0]._id;
        }).catch(function(e) {
            console.log(e), wx.showToast({
                title: "网络似乎出了点问题，请重试",
                icon: "none",
                duration: 1500
            });
        }), wx.hideShareMenu();
    },
    getUserInfo: function(e) {
        console.log(e.detail.userInfo), wx.showLoading({
            title: "正在登录",
            mask: !0
        });
        var a = e.detail.userInfo;
        a && (this.updateUserInfo(a), wx.setStorageSync("getUserProfile", !0));
    },
    getUserProfile: function(e) {
        var a = this;
        wx.getUserProfile({
            desc: "用于展示头像和昵称",
            lang: "zh_CN",
            success: function(e) {
                a.updateUserInfo(e.userInfo), wx.setStorageSync("getUserProfile", !0);
            }
        });
    },
    updateUserInfo: function(t) {
        var o = this.data.fromPage, n = this.data._id;
        a.collection("behaviors").add({
            data: {
                createTime: a.serverDate(),
                type: "authorization",
                page: "authorization",
                fromPage: o
            }
        }).then(function(e) {}).catch(console.error), a.collection("users").doc(n).update({
            data: {
                authorized: !0,
                nickName: t.nickName,
                gender: t.gender,
                language: t.language,
                city: t.city,
                province: t.province,
                country: t.country,
                avatarUrl: t.avatarUrl,
                updateTime: a.serverDate()
            }
        }).then(function(a) {
            wx.hideLoading(), e.globalData.authorized = !0, e.globalData.userInfo = {
                nickName: t.nickName,
                avatarUrl: t.avatarUrl
            }, wx.showModal({
                content: "授权登录成功!",
                showCancel: !1,
                confirmColor: "#FF5A5F",
                confirmText: "返回",
                success: function(e) {
                    e.confirm ? wx.navigateBack({
                        delta: 1
                    }) : e.cancel;
                }
            });
        }).catch(function(e) {
            console.log(e);
        });
    }
});