您的位置:首页 > 移动开发 > 微信开发

微信 公众号 和 小程序 模板消息 demo

2017-08-03 00:00 543 查看
需要导入 weixin4j 框架(jar包)

公众号:

public String transHints(String openId,String url,String first,String merchant,String transType,String orderId,String time,String acmount,String remark) throws WeixinException{
WeixinProxy weixinProxy = new WeixinProxy(new WeixinAccount(qijukejiEnvironment.getWechatAppID(),qijukejiEnvironment.getWxAppletAppSecret()), new FileCacheStorager<Token>());//你的AppID和AppSecret
TemplateMessage templateMessage = new TemplateMessage(openId, qijukejiEnvironment.getWechatTemplatesTransHints(), url);//你的模板id,和点击进入界面的url
Map<String, NameValue> mes = new HashMap<String, NameValue>();
mes.put("first", new NameValue("first", first));
mes.put("keyword1", new NameValue("keyword1", merchant));
mes.put("keyword2", new NameValue("keyword2", transType));
mes.put("keyword3", new NameValue("keyword3", orderId));
mes.put("keyword4", new NameValue("keyword4", time));
mes.put("keyword5", new NameValue("keyword5", acmount));
mes.put("remark", new NameValue("remark", remark));
templateMessage.setItems(mes);
return weixinProxy.sendTmplMessage(templateMessage);
}

小程序:

public void transHints(String openId,String url,String formId,String activity,String merchantName,String merchantInfo,String time,String orderId,String remark,String discount) throws WeixinException{
String urlApplet = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" + weixinAppletProxy.getTokenManager().getAccessToken();//你的access_token
//根据你的模板
JSONObject jsonObjectData = new JSONObject();

JSONObject jsonObjectValue = new JSONObject();
jsonObjectValue.put("value",activity);
jsonObjectData.put("keyword1", jsonObjectValue);

jsonObjectValue = new JSONObject();
jsonObjectValue.put("value",merchantName);
jsonObjectData.put("keyword2", jsonObjectValue);

jsonObjectValue = new JSONObject();
jsonObjectValue.put("value",merchantInfo);
jsonObjectData.put("keyword3", jsonObjectValue);

jsonObjectValue = new JSONObject();
jsonObjectValue.put("value",time);
jsonObjectData.put("keyword4", jsonObjectValue);

jsonObjectValue = new JSONObject();
jsonObjectValue.put("value",orderId);
jsonObjectData.put("keyword5", jsonObjectValue);

jsonObjectValue = new JSONObject();
jsonObjectValue.put("value",remark);
jsonObjectData.put("keyword6", jsonObjectValue);

jsonObjectValue = new JSONObject();
jsonObjectValue.put("value",discount);
jsonObjectData.put("keyword7", jsonObjectValue);

JSONObject jsonObject = new JSONObject();
jsonObject.put("touser",openId);
jsonObject.put("template_id",qijukejiEnvironment.getWxAppletTemplatesTransHints());//你的模板id
jsonObject.put("page",url);
jsonObject.put("form_id",formId);
jsonObject.put("data",jsonObjectData);

WeixinRequestExecutor weixinExecutor = new WeixinRequestExecutor();
WeixinResponse response = weixinExecutor.post(urlApplet,jsonObject.toJSONString());
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: