Use of template messages, use of template messages
The template message push function was added to the company's project requirements last week. I will sort it out here today:
The template message is divided into the public number and the applet. They look similar, but there is a difference:
Template push location: the Public Account is the current public account page, and the applet is the service notification.
Template issuance conditions: the public account can be issued like a notification pushed in the native app, and the customer does not need to trigger it actively. In a applet, the user needs to trigger the notification after interacting with the page in the system, A applet cannot push messages. A message is received only when the user initiates an event and confirms that the user needs to receive subsequent feedback.
The number of templates is limited: the number of public accounts is more than, which is determined by the number of fans. The official mini-program documentation says that the number of push entries within 7 days is limited, three messages can be pushed at a time. If you don't know how to make a payment, you can submit the message at a time, so there is no limit.
It's just that you need to pay more attention to the form_id parameter in the template message of the applet, and you can only test it on the real machine, or you cannot get this form_id parameter value.
Step 1: Obtain the user's openId. There are two methods, the same idea: tune wx. login obtains the code (one-time, which cannot be reused) in exchange for openId (this step has two methods, which should be completed in the background. During front-end testing, configure as follows in the development tool ). The second method is generally used, because it can also return the basic information of the user, which seems to be required by General applets. The first method only obtains the openId and session_key.
Method 1:
wx.login({ success: function (logincode) { if (logincode.code) { wx.request({ url: 'https://api.weixin.qq.com/sns/jscode2session?appid=yourappid&secret=yoursecret&js_code=' + logincode.code + '&grant_type=authorization_code', header: { 'content-type': 'application/json' }, success: function (resle) { that.globalData.openId_true = resle.data.openid; } }) } }});
Method 2:
Note: When withCredentials is set to true, wx must have been called before. login and the logon status has not expired. The returned data will contain sensitive information such as encryptedData and iv. When withCredentials is set to false, do not request a logon status. The returned data does not contain encryptedData, iv.
wx.login({ success: function (loginres) { if (loginres.code) { wx.getUserInfo({ withCredentials: true, lang: 'zh_CN', success: function (res) { wx.request({ url: 'https://le.beiebi.com/lkt/api/scanLogin/appletScanLogin', method: 'POST', header: { 'content-type': 'application/x-www-form-urlencoded' }, data: { code: loginres.code, encryptedData: res.encryptedData, iv: res.iv, unionid: '' }, success: function (res) { openId = res.userInfo.openId }, fail: function () { } }) }, fail: function () { } }) } }})
Step 2: Obtain the access_token (which should also be completed in the background ).
Note: The validity period of access_token is two hours, and the number of requests generated per day is limited (2000 times). Therefore, you need to cache the token in the background. Here we just simulate it.
getAccess_token:function (client_credential, appid, secret,formId) { var that = this; if (that.data.isTime){ var dic = { grant_type: client_credential, appid: appid, secret: secret, } util.httpsGetRequest("https://api.weixin.qq.com/cgi-bin/token", dic, function (success) { console.info('-----access_token==' + success.access_token); access_token = success.access_token; wx.setStorageSync('access_token',success.access_token); that.sendMessage(formId); that.data.isTime = false; var date = new Date(); that.data.timestamp = date.getTime(); that.countdown(that, that.data.timestamp + 7200); }) } else { access_token = wx.getStorageSync('access_token'); that.sendMessage(formId); } },
Step 3: Send the template message.
Note: The following two dictionary installation parameters are written. dic1 is the parameter required for sending template messages to the public account, and dic is the parameter required for sending template messages to the applet. In the formId field, in the form submission scenario, the formId (
E. detail. formId); prepay_Id for this payment.
SendMessage: function (formId) {var dic1 = {"touser": app. globalData. openId_true, "template_id": "alert", "miniprogram": {"appid": appid, "pagepath": "pages/index/qqq/aaa? AnalysisReportId = 2050 & openId = 1 "}," color ":" #173177 "," data ": {" first ": {" value ":" Congratulations! "," Color ":" #173177 "}," keyword1 ": {" value ":" vip1867332110254 "," color ":" #173177 "}," keyword2 ": {"value": "Shenzhen Technology Co., Ltd.", "color": "#173177"}, "keyword3": {"value": "1500 RMB ", "color": "#173177"}, "remark": {"value": "Welcome to purchase again! "," Color ":" #173177 "}}var dic = {" touser ": app. globalData. openId_true, "template_id": "9I2cE23DPBi_nlYZLSJT-YK_DAMvGmmwyOnWFDFfgd", "page": "pages/index/qqq/aaa? AnalysisReportId = 2050 & openId = 1 "," form_id ": formId," data ": {" keyword1 ": {" value ":" Leben Express "," color ": "#173177"}, "keyword2": {"value": "November 13, 2017", "color": "#173177"}, "keyword3": {"value ": "iPhone11", "color": "#173177"}, "keyword4": {"value": "12545568461025", "color": "#173177 "}, "keyword5": {"value": "Bay", "color": "#173177" }}, "emphasis_keyword": "keyword1.DATA"} util. http SPostRequest ("https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send? Access_token = "+ access_token, dic, function (success) {console.info ('Access _ tokenerrmsg = '+ success. errmsg );})},
Other code:
// Form submission formSubmit: function (e) {if (wx. getStorageSync ('istime ')! = '') {This. data. isTime = wx. getStorageSync ('istime'); console. log ('sync = isTime = ', wx. getStorageSync ('istime'); console. log ('sync = access_token = ', wx. getStorageSync ('Access _ token');} this. getAccess_token ('client _ credential ', appid, secret, e. detail. formId) ;}, countdown: function (that, time) {if (time <that. data. timestamp + 60) {console. log ('= shijiandao =') that. data. isTime = true; wx. setStorageSync ('istime', that. data. isTime); return;} setTimeout (function () {time = time-1; that. countdown (that, time) ;}, 1000 )},
. Wxml
<! -- Note: report-submit must be set to true to get its formId -->
<Form bindsubmit = "formSubmit" bindreset = "formReset" report-submit = "true">
<Button formType = "submit" class = "mybtn" type = "primary"> send template message </button>
</Form>