background

When the front end invokes functions such as sharing, photographing and scanning provided by wechat, it needs to obtain the configuration, mainly signature, from the background. Node development can use pu Ling big brother’S SDK — co-weike-API.

configuration

Go to the wechat public platform, enter the “public account Settings” “Function Settings” and fill in the “JS interface security domain name”. The premise is that the verification file provided by wechat is in the project directory of this domain name. If local debugging or test environment debugging is required, you can also configure the IP whitelist.

The development of

Instantiate the WechatAPI object

const WECHAT_API = new WechatAPI(
  appid,
  secret,
  get_access_token,  // function
  save_access_token,  // function cache access_token
);

WECHAT_API.registerTicketHandle(
  get_js_api_ticket,  // function
  save_js_api_ticket,  // function cache jsapi_ticket
);
Copy the code

Note: Jsapi_ticket is a temporary ticket used by the public account to invoke the wechat JS interface. Normally, the jSAPi_ticket validity period is 7200 seconds. The value can be obtained by access_token. The number of API calls used to obtain jSAPi_ticket is limited. If you refresh jSAPi_ticket frequently, the API calls are limited and services are affected. Therefore, developers must cache jSAPi_ticket globally in their own services. Similarly, access_token is valid for 7200 seconds and also requires a global cache.

The cached function is simply json.stringify and stored in Redis or some other database.

call

const params = {
      debug: false.jsApiList: [].// The capabilities to be acquired are passed in from the front end
      url: ' '.// Apply for a domain name that is capable and configured in the background
};
ctx.body = await WECHAT_API.getJsConfig(params);
Copy the code

Access multiple wechat public accounts

When I came back…