Laya 微信小游戏登录问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/uisoul/article/details/82534600

微信官方通知:小程序与小游戏获取用户信息接口调整,请开发者注意升级。

为优化用户体验,使用 wx.getUserInfo 接口直接弹出授权框的开发方式将逐步不再支持。从2018430日开始,小程序与小游戏的体验版、开发版调用 wx.getUserInfo 接口,将无法弹出授权询问框,默认调用失败。正式版暂不受影响。开发者可使用以下方式获取或展示用户信息:

一、小程序:
1、使用 button 组件,并将 open-type 指定为 getUserInfo 类型,获取用户基本信息。
详情参考文档:
https://developers.weixin.qq.com/miniprogram/dev/component/button.html

2、使用 open-data 展示用户基本信息。
详情参考文档:
https://developers.weixin.qq.com/miniprogram/dev/component/open-data.html

二、小游戏:
1、使用用户信息按钮 UserInfoButton。
详情参考文档:
https://developers.weixin.qq.com/minigame/dev/document/open-api/user-info/wx.createUserInfoButton.html

2、开放数据域下的展示用户信息。
详细参考文档:
https://developers.weixin.qq.com/minigame/dev/document/open-api/data/wx.getUserInfo.html

请各位开发者注意及时调整接口。

所以只能使用createUserInfoButton来登录授权获取用户信息了


checkLogin():void{
        if (Laya.Browser.onMiniGame) {
            var createLoginBtn = this.createLoginBtn;
            wx.getSetting({
                success: function (res) {
                    var authSetting = res.authSetting
                    if (authSetting['scope.userInfo'] === true) {
                        //用户已授权,可以直接调用相关 API
                        //TODO: 调用wx.login, wx.getUserInfo 
                        //TODO: 调用自己的注册登录接口
                    } else if (authSetting['scope.userInfo'] === false){
                        // 用户已拒绝授权,再调用相关 API 或者 wx.authorize 会失败,需要引导用户到设置页面打开授权开关
                        console.log('授权:请点击右上角菜单->关于(零下记忆)->右上角菜单->设置');
                        createLoginBtn();
                    } else {
                        // 未询问过用户授权,调用相关 API 或者 wx.authorize 会弹窗询问用户
                        createLoginBtn();
                    }
                }
            });
        }        
}

createLoginBtn():void{
    if (Laya.Browser.onMiniGame) {
            let sysInfo = wx.getSystemInfoSync();
            //获取微信界面大小
            let width = sysInfo.screenWidth;
            let height = sysInfo.screenHeight;

            let sdkVersion = sysInfo.SDKVersion;
            if (sdkVersion >= "2.0.1") {
                //微信SDK大于2.0.1需要使用createUserInfoButton获取用户信息
            }
            // let x = 226*(width/Laya.stage.width);
            let y = 760*(height/Laya.stage.height);

            let w = 279*(width/Laya.stage.width);
            let h = 96*(height/Laya.stage.height);
            let x = (width-w)/2;

            this.wxloginBtn = wx.createUserInfoButton({
                type: 'text',
                text: '微信登录',
                style: {
                    left: x,
                    top: y,
                    width: w,
                    height: h,
                    lineHeight: h,
                    // backgroundColor: '#7f0000',
                    color: '#dddddd',
                    textAlign: 'center',
                    fontSize: 18,
                    borderRadius: 4
                }
            })

            //var caller = this;
            //var func = this.loginComplete; //登录接口回调函数

            this.wxloginBtn.onTap((res) => {
                // button.destroy();//隐藏按钮
                var res2 = res;

                wx.login({
                    success: function (res) {
                        if (res.code) {
                            //jscode用于后台解密隐私数据encryptedData,参考https://developers.weixin.qq.com/minigame/dev/document/open-api/login/wx.login.html
                            var jscode=res.code;


                            var userInfo = res2.userInfo
                            var nickName = userInfo.nickName
                            var avatarUrl = userInfo.avatarUrl
                            var gender = userInfo.gender //性别 0:未知、1:男、2:女
                            var province = userInfo.province
                            var city = userInfo.city
                            var country = userInfo.country

                            var encryptedData = encodeURIComponent(res2.encryptedData);//一定要把加密串转成URI编码
                            var rawData = encodeURIComponent(res2.rawData);
                            var iv = res2.iv;
                            var signature = res2.signature;

                            //TODO: 调用自己的注册登录接口
                        }else{
                            console.log('登录失败!' + res.errMsg);
                        }
                    }
                });
            });
        }
}

猜你喜欢

转载自blog.csdn.net/uisoul/article/details/82534600