Recently, I have been writing wechat applets. Recently, I met the requirement of a redirect micro Reliance name authentication applets. Here, I need to use the API of real name authentication in wechat Payment documents:
Document link: Real name Verification (applets)
Jump target applet
The first step, of course, is to jump to the target applet, where the jump is micro reliance name authentication, official document: open another applet
NavigateToMiniProgram ({appId: 'wx88736d7d39e2eda6', // 'pages/oauth/ authIndex ', // Open the page path, if empty open the home page. ExtraData: extraData, // Data that needs to be passed to the target applet envVersion: 'release', // The version of the applet to open. This parameter is valid only when the applet is in the development or experience version. If the current applet is the official version, the opened applet must be the official version. success(res) {}, fail(res) {} })Copy the code
ExtraData:
The variable name | type | mandatory | The sample value | describe |
---|---|---|---|---|
api_version |
string |
is |
1.0 |
API interface version, fixed value “1.0” |
mch_id |
string(32) |
is |
1230000109 |
The merchant number assigned by wechat Pay |
appid |
string(32) |
is |
wx88736d7d39e2eda6 |
The appID that initiates the authorization applet |
response_type |
string(32) |
is |
code |
The value can only be “code” |
scope |
string |
is |
pay_identity |
Apply the authorization scope, Pay_identity is real-name authentication – Verifies whether the name and id card match |
openid |
string(128) |
is |
oUpF8uMuAJO_M2pxb1Q9zNjWeS6o |
This parameter is the unique id of the wechat user under the corresponding APPID of the merchant |
sign |
String(128) |
is |
029B52F67573D7E3BE74904BF9AEA2F48656AEBCBB93FA48D9B70F98D2E23D09 |
For details about the signature values calculated by the signature algorithm, see Signature Generation Algorithm |
nonce_str |
String(32) |
is |
ibuaiVcKdpRxkhJA |
The value is a random string of up to 32 characters. Random number generation algorithm is recommended |
sign_type |
String(128) |
is |
HMAC-SHA256 |
The signature type is HMAC-SHA256 |
{" API_version ": "1.0", "McH_id ": "1230000109"," APPID ": "wx88736D7D39e2EDa6 ", "scope": "pay_identity", "response_type": "code", "openid": "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o", "sign_type": "HMAC-SHA256", "sign": "DBB47C037C812B29E0E7B4C5F62972B92E61CF05DE14FA958D9054B2", "nonce_str": "190703203728315382",}Copy the code
Two, small program receive parameters
When returning from the target applet to the applet, the cut foreground event of the applet will be triggered, so if you need to get the value returned by the target applet, you need to get it in the callback parameter of the Wx. onAppShow API.
Document link: Listens for applets to cut foreground events
If correct, the following JSON packet is returned:
Return on success (auth_code for access token, valid for 600 seconds)
{
"auth_code" : "EYJV9lz4yHrepHG5ye8Cp0bMORiob11lGgDUGFi26vEX-sirjL5652SFxs9WP-zD6TcrbSArk_laGU6A08pgfHxX_tZWdPiP-Cu2a68d-3Q="
}
Copy the code
Return on failure
{"err_code" : "SYSTEMERROR", "err_code" : "SYSTEMERROR"}Copy the code
For example, in onLoad:
wx.onAppShow((res) => {
let auth_code = res.referrerInfo.extraData.auth_code;
});
Copy the code
That’s fine in theory, but there’s one caveat
Part of the version will be returned when no referrerInfo undefined, it is recommended to use the options. ReferrerInfo && options. ReferrerInfo. AppId for judgment.
So after modification, it looks like this
wx.onAppShow((res) => { if (res.referrerInfo && res.referrerInfo.extraData && res.referrerInfo.extraData.auth_code) { let auth_code = res.referrerInfo.extraData.auth_code; }});Copy the code
The general scenario is OK by now, but I encountered a small incident in the development process, onAppShow will be executed twice when returning our own small program, resulting in one of the code execution exception, which needs to be resolved.
Start by defining a global variable appShow to hold the scene value and initialize it in the onLoad and jump to the real-name authentication applet method. Then when the onAppShow event is triggered, set appShow to true and the code will not be executed after the onAppShow event is triggered a second time.
let appShow = false; // define a global variable when the page is initialized... NavigateToMiniProgram ({appId: 'wx88736d7d39e2eda6', // 'pages/oauth/ authIndex ', // Open the page path, if empty open the home page. ExtraData: extraData, // Data that needs to be passed to the target applet envVersion: 'release', // The version of the applet to open. This parameter is valid only when the applet is in the development or experience version. If the current applet is the official version, the opened applet must be the official version. success(res) { appShow = false; // set the page value to false}, fail(res) {}})... // onLoad function // small program cut foreground scene appShow = false; Wx.onappshow ((res) => {if (! appShow) { appShow = ! appShow; if (res.referrerInfo && res.referrerInfo.extraData && res.referrerInfo.extraData.auth_code) { // other methods } } })Copy the code
The article is written desultorily, still hope reader seabear, much criticism points out.