cc.Class({
    extends: cc.Component,

    properties: {
        头像: cc.Sprite,
    },

    onLoad() {
        this.获取用户信息();
    },

    start() {

    },
    获取用户信息() {
        var self = this;
        if (CC_WECHATGAME) {
            let button = wx.createUserInfoButton({
                type: 'text',
                text: '',//获取用户信息
                style: {
                    left: 0,
                    top: 0,
                    width: window.innerWidth,
                    height: window.innerHeight,
                    lineHeight: 40,
                    backgroundColor: '#968C8C00',
                    color: '#ffffff00',
                    textAlign: 'center',
                    fontSize: 16,
                    borderRadius: 4
                },
                withCredentials: false,
                lang: 'zh_CN'
            })
            button.onTap((res) => {
                button.hide();
                // console.log(typeof (res.userInfo.nickName) + "  " + res.userInfo.nickName);
                if (res.userInfo) {
                    var userInfo = res.userInfo;
                    //var nickName = userInfo.nickName;
                    // var gender = userInfo.gender;
                    var avatarUrl = userInfo.avatarUrl;
                    button.destroy();
                    self.头像设置(avatarUrl, self.头像);
                    wx.showToast({ title: "授权成功" });
                } else {
                    console.log("wxLogin auth fail");
                    wx.showToast({ title: "授权失败", icon: 'none' });

                }
            })
        }
    },
    弹窗(str) {
        if (CC_WECHATGAME) {
            wx.showToast({
                title: str,
                icon: 'none'
            });
        }
    },

    头像设置: function (avatar, sp) {
        if (avatar == null || avatar == "")
            return;
        cc.loader.load(
            {
                url: avatar,
                type: 'jpg'
            },
            function (err, texture) {
                console.log("avatar ==========", err, texture)
                if (err) {
                    console.log('加载玩家头像err === ', err);
                    return;
                }
                texture.width = 74;
                texture.height = 74;
                sp.spriteFrame = new cc.SpriteFrame(texture);
            }
        )
    },
    // update (dt) {},
});
