WeChat Mini Game Privacy Permission Interface Upgraded to Cocos2.4.11 Latest Method for Obtaining User Nickname and Avatar 20230823

       Why does the console log output frequently disappear?
  Why does the wxss code frequently fail?
  Why does the wxml layout become a mess?
  Is it a loss of morality? Or a lack of human nature?
  Let's take  a walk  and get into science together


Preface

On August 15th, I received feedback from my colleagues that the game could not be opened. When I looked at the trial version of the mini-game, the avatar acquisition interface reported an error:
wx.getUserInfo() would return status code 1026, so the journey of cheating began...


text

I was too lazy to write a blog because when this problem arose, I looked around in the announcements, looked around in wx official documents, searched in csdn, and in the WeChat official development community. They all found solutions to small programs and even official documents of small games. If I read it correctly, there must be a colon missing here! It's not just this place. I've seen it in at least five or six places. Here's a step-by-step solution. Okay?

 1. First go to the WeChat official platform to adjust the privacy permissions
WeChat Public Platform => Settings => Service Content Description

For the first User Privacy Protection Guidelines, first click in and select what you need to use, then check everything you need to use, fill in everything and save it.

Then the second privacy authorization pop-up window said that if it is turned on, it cannot be turned off. Ignore it and activate it directly. Because I did not activate it, I repeated the same mistake and wrote the code all night.

ok At this point, all the WeChat public platforms that need to be activated here have been activated and the code has been uploaded!

 2. Let’s talk about this first because I have changed this privacy permission several times before. The previous method of obtaining it was to obtain user information. If it cannot be obtained, call wx.createUserInfoButton() to generate a button. Click the button to trigger the user authorization event. Now it is in the outer layer. Adding another layer of interface that agrees with authorization will solve the problem.

This is what I wrote before:

wx.getUserInfo({
            success(res) {
                console.log("获取权限成功:", res);

                let rawData = JSON.parse(res.rawData)
                userInfo.avatarUrl = rawData.avatarUrl;
                userInfo.nickName = rawData.nickName;
                _this.login2()//这是成功获取权限后的逻辑代码

            },
            fail(err) {
                console.log(err);
                //这里是生成那个授权按钮
                var info = wx.getSystemInfoSync()
                let button = wx.createUserInfoButton({
                    type: 'text',
                    text: '授权并进入游戏',
                    style: {
                        left: info.screenWidth / 2 - 100,
                        top: 600,
                        width: 200,
                        height: 40,
                        lineHeight: 40,
                        backgroundColor: '#8D99D5',
                        color: '#ffffff',
                        textAlign: 'center',
                        fontSize: 16,
                        borderRadius: 4
                    }
                })
                button.onTap((res) => {
                    console.log(res);
                    //用户按过按钮后 同意或拒绝后的回调
                    button.hide()
                    _this.login()//这里指本方法 通过能否获取到具体的用户昵称头像内容去判断用户是否同意 同意就走wx.getUserInfo中的success了 不会再到fail里新建按钮了
                })
            },
        })

Then I set an interface outside wx.createUserInfoButton() and it became like this:

wx.getSetting({
            success(res) {
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权,可以直接调用 getUserInfo 获取头像昵称
                    console.log("已经授权,可以直接调用 getUserInfo 获取头像昵称");

                    wx.getUserInfo({
                        success(res) {
                            console.log("获取权限成功:", res);

                            let rawData = JSON.parse(res.rawData)
                            userInfo.avatarUrl = rawData.avatarUrl;
                            userInfo.nickName = rawData.nickName;
                            _this.login2()

                        },
                        fail(err) {
                            console.log("获取权限失败:", err);
                        }
                    })
                } else {
                    wx.requirePrivacyAuthorize({
                        success: res => {
                            console.log("原因::", res);

                            // 非标准API的方式处理用户个人信息
                            var info = wx.getSystemInfoSync()
                            let button = wx.createUserInfoButton({
                                type: 'text',
                                text: '授权并进入游戏',
                                style: {
                                    left: info.screenWidth / 2 - 100,
                                    top: 600,
                                    width: 200,
                                    height: 40,
                                    lineHeight: 40,
                                    backgroundColor: '#8D99D5',
                                    color: '#ffffff',
                                    textAlign: 'center',
                                    fontSize: 16,
                                    borderRadius: 4
                                }
                            })
                            button.onTap((res) => {
                                console.log("结果::", res);

                                button.hide()
                                _this.login()
                            })
                        },
                        fail: (res) => {
                            console.log("拒绝", res);

                        },
                        complete: (res) => {
                            console.log("结果", res);
                        }
                    })
                }
            }
        })

ok According to the specific instructions of wx.requirePrivacyAuthorize written in the official document, when we do not obtain the user information, we will first call wx.requirePrivacyAuthorize to determine whether the user has clicked I Agree and then proceed to create a subsequent button to agree to obtain the nickname and avatar. The logic is now complete. Most of

3. This build publishes the above code build to the WeChat Developer Tools

First, change your debugging library to 2.32.3 or above. I directly changed it to 3.0.1. I saw yesterday that 82% of people here chose to change it to grayscale today because this privacy permission interface is only compatible with 2.32.3 or above.

Then add this to project.config.json

"__usePrivacyCheck__": true,

ok. That’s it. If there’s anything I didn’t remember or if there’s anything I’d like to correct, I’ll add it. Welcome everyone to correct me.


Summarize

Here is WeChat’s official announcement Mini Game Privacy Compliance Development Guide | WeChat Open Community

Here is a help post posted by my community. No one paid attention to it, but I answered it myself, which can be regarded as a solution.  What permissions does wx.getUserInfo need in the mini game? | WeChat open community

Here are the four new privacy permission interfaces in WeChat’s development documents. They belong to wx.requirePrivacyAuthorize(Object object) | WeChat Open Documents 

Yesterday, during the Ghost Festival, I worked overtime at the company until very late. I silently cursed the person who wrote the announcement. Go to hell. I apologize here. Orz

Indeed, there seems to be no similar posts about small programs. Yesterday, I looked at uni’s posts and looked at my cocos engine and fell into deep thought... 

Guess you like

Origin blog.csdn.net/m0_66016308/article/details/132604539