uniapp/WeChat applet login and user information acquisition function related issues uni.login() uni.getUserinfo()

1. Mini-program login interface related wx.login() / uni.login()

The process of logging in to the applet is not difficult, but it is simple but it takes a long time for people to gradually understand. When I first started working, I always encountered related problems and couldn’t figure it out. I checked various documents and found that I read many different ones. I don’t know the correct solution, and try to experiment one by one, and what I make always makes me feel dissatisfied. After a series of groping, I gradually realized that it was not because I didn’t understand it, but because the many materials that Shang Du Niang saw were inconsistent, which may be caused by the adjustment of the rules of the wx applet itself for the interface.
The following is a general summary, how to do it specifically, in order to record the process, let’s start from the beginning (of course I am also a novice, so I will roughly express my understanding, welcome to correct me, don’t hate me, thin-skinned )

1. The applet login process does not require annoying authorization pop-up windows

Regardless of whether it is a small program or other applications that require login, the logic of login is not bad, which can be roughly represented by the relationship in the figure below.
Easy login process
The following is the mini program login process provided by the official mini program. The difference from the above logic is that the applet has an additional WeChat server (but the communication with this end is mostly the operation of the back-end personnel, unless the front-end personnel of cloud development, otherwise this end does not need the participation of front-end development).
insert image description here
We can find that when logging in to the applet, the front-end staff no longer need to provide a mobile phone number and password, but need a code value to be passed to the back-end.
And this code value is obtained through an api called wx.login() provided by WeChat, which can be obtained only by the user (uni.login() in uniapp).
Here you may wonder why the authorization window does not pop up. In fact, this interface does not pop up an authorization pop-up window. Instead, you can get it by using it directly. In the past, the pop-up authorization window only popped up when we obtained user information (when using getUserProfile). When using uniapp,
the code is as follows (the applet is similar, and the following code will not be explained):

	uni.login({
    
    
	  provider: 'weixin', //使用微信登录 必要的参数详情看官方文档
	  success: function (loginRes) {
    
    
	    console.log(loginRes); //返回的参数包含在loginRes中
	  }
	});

So in the future, we don't need to worry about authorization and other issues. We just need to write this code, get the code value, and pass it to the back-end programmer. The back-end can add a small program based on the code you gave Get the session_key and openid in the appid and appsecret of the WeChat service interface, (the two values ​​of appid and appsecret need to go to the WeChat public platform to log in to the background of our applet, and then in theDevelopment Management > Development SettingsYou need to know about this) , and then the backend will process the data with a high probability, and then pass the encrypted token to you. With this, we only need to cache the token returned by the backend, and attach it to every request for a function that needs to be logged in, so that the login function is roughly complete, and other details are based on the business Or you can cooperate with the back-end programmer to improve it.

When will there be a login popup?

wx.getUserinfo (don't use it, it's useless)

	已废弃,微信收回了,不建议用,就算版本老编译成功了,后续发布可能会有无法通过审核之类的问题。

wx.getUserProfile (don't use it either, unless the boss is more stubborn than a bull)

This interface can obtain user information. An authorization window will pop up for each request, and userInfo will be returned after the user agrees. But it is only limited to WeChat mini-program base library version 2.27.1 and above.
Change here:
insert image description here
why?
Because the wx.getUserProfile interface has been withdrawn, please refer to the "Adjustment Announcement of the Rules for Obtaining Mini Program User Avatar Nicknames" for details.
What do you mean? It is that WeChat recommends that you stop using it and use other methods provided by him. What method? Just don’t use WeChat’s avatar and nickname, you can directly let users fill in their nickname and avatar, and he has prepared an easy-to-use method for you.
But the boss is more stubborn than a cow. If he doesn't listen to me, I won't listen, so what should I do?
You can only adjust to the version 2.10.4 - 2.27.0 of the basic library according to the above picture, otherwise you may be troubled by the pop-up window not popping up.
That’s roughly the above. My technical ability is limited. Don’t scold me. I’m thin-skinned. If there are any mistakes, please guide me. I reiterate that I’m just a beginner. Out of doubts, I hope to communicate with Xiaobai, who is not very talented like me.

Guess you like

Origin blog.csdn.net/SANGZHU_001/article/details/128125384