This is the sixth day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

preface

This paper will introduce the OpenId, UnionId and other problems encountered in the development process of wechat, and provide a way to bind the relationship between small programs and public accounts.

I. Details of openId and unionId

In the development process of wechat, many companies have both public accounts and small programs. For example, when a user sends a message to the public account, the sender of the message received by the public account is an OpenId, and its generation rule is generated according to the encryption of the user’s wechat signal. Each user has a unique OpenId for the public account. The same is true for small programs. They are generated micro signals, but may be because the encryption is not right, so

They have different values, so how do you know they’re the same user? This requires the use of the third platform — wechat open platform, which provides the UnionId mechanism. OpenID is available to developers

User basic information, and if the developer has multiple applications (mobile application, website application and public account, the public account will only get the UnionID after being bound to the wechat open platform account), can be obtained through

Take the UnionID in the user’s basic information to distinguish the uniqueness of the user, because as long as it is a mobile application, website application and public account under the same wechat open platform account, the user’s UnionID is unique. In other words,

Said, the same user, for the same wechat open platform account under different applications, UnionID is the same.

Matters needing attention:

  • You must bind the public account and small program in the wechat development platform to get the unionId
  • Public account can obtain OpenID and unionID when concerned, public account can not obtain unionID when taking off;
  • The small program can obtain the OpenID and unionID when logging in
  • A public account or small program can only be bound to an open platform

The next goal is to maintain these three ids in the database.

2. Binding process

In fact, we have two cases.

  • Users first pay attention to the public number, in the use of small procedures
  • Users first use small programs, and then pay attention to the public number

2.1 case 1

If we know how to obtain OpenID and unionId, that is, when the user pays attention to it, we cannot know which user in the database only when we get these two values. So, I created a temporary table of public accounts to store this data.

Then when the applet logs in, get the openID and unionID of the applet, and then go to the temporary table to check whether there is a record equal to its unionID, if there is, then assign its value to it.

The specific process is as follows

Delete temporary table data when user close, and then update the user table public id field is null.

2.2 case 2

User small program login, the first openID and unionID to get, if not before, will update them, and then go to the temporary table to view, if there is, update the user public number field; If not, end. When waiting for the public account to pay attention, it will check the small program has a value, and then directly update the user table.

Iii. How to obtain OpenID and UnionID

3.1 Small program acquisition

The front end calls the applets API –wx.login () to get the login credentials

App({onLaunch: function() {wx.login({success: function(res) {if (res.code) {// launch: code}}}); }})Copy the code

The following is the interface provided by wechat, and the backend can be changed in this way

https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code
Copy the code
public static Code2SessionVO code2Session(Code2SessionDTO code2SessionDTO) { try{ String result = HttpRequest .get(String.format(LoginEnum.CODE_2_SESSION.getUrl(), code2SessionDTO.getAppid(), code2SessionDTO.getSecret(), Code2sessionto.getjs_code ())) // timeout.timeout(5000).execute().body(); ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.readValue(result, Code2SessionVO.class); }catch (Exception e){ e.printStackTrace(); } return null; }Copy the code

We pass in the target parameters appid, secret, js_code, grant_type. Js_code is the code retrieved by the front end (valid for about five minutes). Grant_type remains unchanged and returns the following values:

Openid Unique ID of a user session_key Session key UnionID Unique identifier of a user on the open platform. This field is returned only if certain conditions are met.Copy the code

3.2 Public account access

Public access is needed in the public network under the development of the internal network should not be. Set a domain name or path in the public account. The service then receives messages and events from the public account. After being parsed by the XML utility class, encrypted data of XML type can be sent to Kafak to be forwarded to the Intranet for consumption through the middleware.

Interface Call Request Description HTTP Request method: GET api.weixin.qq.com/cgi-bin/use…

Parameters that

parameter Whether must instructions
access_token is Call interface credentials
openid is Common user id, unique to the current public account
lang no Return country language version, zh_CN simplified, zh_TW traditional, en English

Under normal circumstances, wechat will return the following JSON packets to the public account:

parameter instructions
subscribe If the value is 0, it indicates that the user does not pay attention to the public account and cannot pull other information.
openid The user id is unique to the current public account
nickname User’s nickname
sex Gender of the user. If the value is 1, it is male, if the value is 2, it is female, and if the value is 0, it is unknown
city User’s city
country User’s country
province User’s Province
language The user language is zh_CN in simplified Chinese
headimgurl The last value represents the size of square head (0, 46, 64, 96, 132 are optional, 0 represents 640 x 640 square head). This parameter is null if the user does not have an image. If the user changes the profile picture, the URL of the original profile picture becomes invalid.
subscribe_time The user is concerned with the time, which is the timestamp. If the user has followed it several times, the last attention time is taken
unionid This field will only appear after the user binds the public account to the wechat open platform account.
remark The remarks of the public number operator to the fans, the public number operator can add remarks to the fans in the wechat public platform user management
groupid The group ID of the user (compatible with the old user group interface)
tagid_list A list of user tag ids
subscribe_scene ADD_SCENE_SEARCH Public id search, ADD_SCENE_ACCOUNT_MIGRATION Public ID migration, ADD_SCENE_PROFILE_CARD Card sharing, ADD_SCENE_QR_CODE Scan the QR code, ADD_SCENE_PROFILE_LINK Name in the text page Click, ADD_SCENE_PROFILE_ITEM menu in the upper right corner of the text page, ADD_SCENE_PAID Pay attention, ADD_SCENE_WECHAT_ADVERTISEMENT wechat advertisement ADD_SCENE_OTHERS Others
qr_scene Qr code scanning scenario (developer’s custom)
qr_scene_str Qr Code Scanning scenario Description (Developer’s custom)

The process of public number is more troublesome than that of Intranet development, and must be carried out in the public network environment. Can not get the unionID when fetching the pass.

The above is the process and method of obtaining OpenID and UnionID.