The app pushes notifications to wechat users. It's a subscription message, not a template message. The template message can't be used anymore. This is an exercise version written by my company's app. Click the button to push the message. wxml Code:
<button bindtap="aaa">Button</button>
js code:
aaa: function () { wx.requestSubscribeMessage({ tmplIds: ['Dw-Dlh5KRd6ce7wUlf259QaaApG7dhdQHN50BOJot1w'], success(res) { if (res['Dw-Dlh5KRd6ce7wUlf259QaaApG7dhdQHN50BOJot1w'] == 'accept'){ wx.request({ url: app.globalData.url + '/appInterface/wxPush.do', method: 'POST', header: { 'Content-Type': 'application/x-www-form-urlencoded' }, data: { userOpenid: app.globalData.openid }, success: function (res) { console.log(res) } }) } } }) },
java controller code:
@RequestMapping("/wxPush") @ResponseBody public Json wxPush(String userOpenid){ logger.info("-----------Start wechat push interface-------------"); Json j = new Json(); j.setSuccess(false); logger.info("userOpenid:" + userOpenid); try{ SendWxMessage sendWxMessage = new SendWxMessage(); j = sendWxMessage.push(userOpenid); if(!PbUtils.isEmpty(j.getObj())){ j.setMsg("Push successfully"); j.setSuccess(true); logger.error("------------"+j.getMsg()+"------------"); }else{ j.setMsg("Push failure"); logger.error("------------"+j.getMsg()+"------------"); } }catch(Exception e){ e.printStackTrace(); j.setMsg("Wechat push interface exception" + e.getMessage()); logger.error(e.getMessage()); } logger.info("-----------End wechat push interface-------------"); return j; }
Wechat tool code:
package com.gt.utils.wx; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.google.gson.Gson; import com.gt.pageModel.Json; public class SendWxMessage { ConfigInfo configInfo = new ConfigInfo(); /* * Send subscription message * */ public Json push(String openid) { //Subscription template id String template_id = "Dw-Dlh5KRd6ce7wUlf259QaaApG7dhdQHN50BOJot1w"; Json j = new Json(); RestTemplate restTemplate = new RestTemplate(); //For the sake of simplicity, we get the latest access_token every time (in time development, we should get it again when the access_token is about to expire) String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getAccessToken(configInfo.getWxAppId(),configInfo.getWxAppSecret()); //Mosaic push template WxMssVo wxMssVo = new WxMssVo(); wxMssVo.setTouser(openid);//User's openid (to send to that user, usually it should be passed in dynamically here) wxMssVo.setTemplate_id(template_id);//Subscription message template id wxMssVo.setPage("pages/main/main"); Map<String, TemplateData> m = new HashMap<>(5); //Practice code. The data is dead. The key value of map must be the same as the name of the template used in wechat public platform m.put("date2", new TemplateData("2019 October 15, 2000:00:00")); m.put("date3", new TemplateData("2019 October 16, 2000:00:00")); m.put("amount4", new TemplateData("¥100")); m.put("phrase7", new TemplateData("Successful transfer")); m.put("thing9", new TemplateData("Well")); wxMssVo.setData(m); ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, wxMssVo, String.class); j.setObj(responseEntity.getBody()); return j; } public String getAccessToken(String appid, String appsecret) { RestTemplate restTemplate = new RestTemplate(); Map<String, String> params = new HashMap<>(); params.put("APPID", appid); // params.put("APPSECRET", appsecret); // ResponseEntity<String> responseEntity = restTemplate.getForEntity( "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}", String.class, params); String body = responseEntity.getBody(); JSONObject object = JSON.parseObject(body); String Access_Token = object.getString("access_token"); String expires_in = object.getString("expires_in"); System.out.println("Effective duration expires_in: " + expires_in); return Access_Token; } }