This article mainly introduces the sample code of node.js to implement the WeChat JS-API encapsulation interface. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
Wechat JS-API interface
Function:
Used to manage and obtain the access_token produced by WeChat JSSDK, jsapi_ticket and signature
Installation
npm i wechat_interaction_jsapi
Init
let Jsapi = require("wechat_interaction_jsapi");
Usage
WECHAT_APPID, WECHAT_APPSECRET are the developer id and password respectively, which can be found in WeChat Public Platform->Development->Basic Configuration
const jssdk = new Jsapi(WECHAT_APPID, WECHAT_APPSECRET);
1. Get access_token, return the promise object, the resolve callback returns string
jssdk.getAccessToken().then( re => console.log(re) ).catch(err => console.error(err));
2. Get jsapi_ticket, return the promise object, the resolve callback returns string
jssdk.getJsApiTicket().then( re => console.log(re) ).catch(err => console.error(err));
3. Obtain the JS-SDK permission verification signature, return the promise object, and the resolve callback returns json
jssdk.getSignPackage(url).then( re => console.log(JSON.stringify(re)) ).catch(err => console.error(err));
Flowchart
1. The first method (parameters appear repeatedly and are clearly organized)
2. The second method (Parameters do not appear repeatedly and the order is not clear)
Front-end calling method
1. Refer to the official documentation
https://mp .weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
2. Notes:
(1) Confirm that the whitelist has been configured: WeChat Public Platform->Development- >Basic configuration->ip whitelist
(2) Confirm that the JS interface security domain name has been configured: WeChat public platform->Settings->Official account configuration->JS interface security domain name
The above is the detailed content of Examples to explain how node.js implements WeChat JS-API encapsulation interface. For more information, please follow other related articles on the PHP Chinese website!