How to send a small program template message and send a small program template message
How to send the applet template Message 1. Get access_token and user openid
You need to request the corresponding interface in app. js. In fact, obtaining the access_token should be the interface requested by the background and then returning the access_token. It is recommended that the front-end do not contain important information about the key.
OnLaunch: function () {let setInfoSuccess = wx. getStorageSync ('setinfosuccess ') | 0; let bearer_token = wx. getStorageSync ('bearer-token'); // log on to wx. login ({success: res => {this. globalData. js_code = res. code // This part is to get bearer-token, you can remove if (new Date (). getTime ()-setInfoSuccess> 86400000 |! Bearer_token) {api. mine. login ({code: res. code }). then (d => {wx. setStorageSync ('bearer-token', d. data );}). catch (function (e) {});} this. getOpenId_token ()}, complete: res => this. getSetting ()}, getOpenId_token: function () {// get access_token wx. request ({url :' https://api.weixin.qq.com/cgi-bin/token ', Data: {grant_type: 'client _ credential', appid: 'applet id', secret: 'applet secret key'}, method: 'get', // header: header, success: function (res) {wx. setStorageSync ('Access _ token', res. data. access_token) ;}, fail: res => (res)}); // obtain openid wx. request ({url :' https://api.weixin.qq.com/sns/jscode2session?appid= $ {This. globalData. appid} & secret =$ {this. globalData. secret} & js_code =$ {this. globalData. js_code} & grant_type = authorization_code ', data :{}, method: 'get', success: function (res) {var obj ={}; obj. openid = res. data. openid; wx. setStorageSync ('user', obj); // storage openid }});},2. on the trigger template message page, obtain form_id or prepay_id.
To customize a form, you must add the report-submit attribute to obtain the form_id.
Test template information
Test: function (e) {// e. detail. if formId is requested on the development tool, the formId is a mock one // you need to scan the development edition on your mobile phone to obtain the formId, then assign the value to this fId and then request // the mobile phone end cannot request it because the server cannot configure api.weixin.qq.com let fId = e. detail. formId; wx. showToast ({title: fId, duration: 3000}); let url =' https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token= $ {Wx. getStorageSync ('Access _ token')} 'let data = {"touser": wx. getStorageSync ('user '). openid, "template_id": "applet template id, see document to get", // click the page path to jump to the template. If this parameter is not specified, the page will not jump to "page ": "pages/index", "form_id": fId, "data": {"keyword1": {"value": "339208499", "color ": "#173177"}, "keyword2": {"value": "January 05, 2015", "color": "#173177"}, "keyword3": {"value ": "Sheraton Yuehai Hotel", "color": "#173177"}, "keyword4": {"value": "208 Tianhe Road, Tianhe District, Guangzhou", "color ": "#173177"}, "keyword5": {"value": "Good weather today", "color": "#173177" }}, "emphasis_keyword ": "keyword1.DATA"} // sends the template message wx. request ({url: url, data: data, method: 'post', success: function (res ){}});}
The result is as follows: