Obtain user session_key, openid, and unioni in the WeChat mini program. This is a function that can often be seen in mini programs. So how to implement the mini program to obtain session_key, openid, and unionid; this article will introduce to you about the backend. The node.js WeChat applet implements the method of obtaining user session_key, openid, and unionid.
Steps:
1. Obtain the code and jscode through the wx.login interface and pass it to the backend;
2 , backend request
https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
address, you can get the openid and unionid .
Mini program interface promise and encapsulation
1. Create the wechat.js file in the utils folder
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 |
|
2. Modify the app.js file of the mini program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
The back-end nodejs is a project framework generated using the express command line,
1. Create a common folder, create a utils file, use the request module to request the interface, and promise the request
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 |
|
2. Add a new route, and obtain the appId and secret in the background of the mini program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Result:
This return result does not have a unionid. According to the official statement, it needs to be sent to WeChat Open platformBind mini program;
Related recommendations:
How to get the user’s openid from WeChat mini program
# #Introduction to how to obtain the parameters openid & session_key in the mini program
The above is the detailed content of How to obtain user session_key, openid, unioni in WeChat applet (code). For more information, please follow other related articles on the PHP Chinese website!