cocos creator 小游戏登录

buttonclick:function(event){

        let exportJson = {};

        let sysInfo = window.wx.getSystemInfoSync();

        //获取微信界面大小

        let width = sysInfo.screenWidth;

        let height = sysInfo.screenHeight;


        window.wx.login({

            success: (res) => {

                if (res.code) {

                    console.log("res.code:", res);

                    exportJson.code = res.code;//向服务端传递code用于获取微信小游戏的用户唯一标识


                    window.wx.getSetting({

                        success (res) {

                            console.log(res.authSetting);

                            if (res.authSetting["scope.userInfo"]) {

                                console.log("用户已授权");

                                window.wx.getUserInfo({

                                    success(res){

                                        console.log(res);

                                        exportJson.userInfo = res.userInfo;

                                        //此时可进行登录操作

                                    }

                                });

                            }else {

                                console.log("用户未授权");

                                let button = window.wx.createUserInfoButton({

                                    type: 'text',

                                    text: '',

                                    style: {

                                        left: 0,

                                        top: 0,

                                        width: width,

                                        height: height,

                                        backgroundColor: '#00000000',//最后两位为透明度

                                        color: '#ffffff',

                                        fontSize: 20,

                                        textAlign: "center",

                                        lineHeight: height,

                                    }

                                });

                                button.onTap((res) => {

                                    if (res.userInfo) {

                                        console.log("用户授权:", res);

                                        exportJson.userInfo = res.userInfo;

                                        //此时可进行登录操作

                                        button.destroy();

                                    }else {

                                        console.log("用户拒绝授权:", res);

                                    }

                                });

                            }

                        }

                    })

                }else{

                    console.log('登录失败!' + res.errMsg)

                }



            },

        });


    },

你可能感兴趣的:(cocos creator 小游戏登录)