当前位置:   article > 正文

Laya 微信小游戏登录问题_laya登录注册

laya登录注册

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

为优化用户体验,使用 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

请各位开发者注意及时调整接口。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

所以只能使用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);
                        }
                    }
                });
            });
        }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/99891
推荐阅读
相关标签
  

闽ICP备14008679号