Template tem = new Template();
tem.setTemplateId("");
tem.setToUser(openid);
tem.setFormId(order.getFormId());
tem.setPage(“pages/index/index”);
List<TemplateParam> paras = new ArrayList<TemplateParam>();
paras.add();
paras.add();
paras.add();
paras.add(new TemplateParam();
paras.add(new TemplateParam();
paras.add(new TemplateParam());
tem.setTemplateParamList(paras);
boolean send = TemplateUtil.sendTemplateMsg(token.getAccessToken(), tem);
public class Template {
private String toUser;
private String templateId;
private String page;
private String formId;
private String transactionId;
// 参数列表
private List<TemplateParam> templateParamList;
private Integer id;
private long succ;
public String getTransactionId() {
return transactionId;
}
public void setTransactionId(String transactionId) {
this.transactionId = transactionId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public long getSucc() {
return succ;
}
public void setSucc(long succ) {
this.succ = succ;
}
public String getToUser() {
return toUser;
}
public void setToUser(String toUser) {
this.toUser = toUser;
}
public String getTemplateId() {
return templateId;
}
public void setTemplateId(String templateId) {
this.templateId = templateId;
}
public String toJSON() {
StringBuffer buffer = new StringBuffer();
buffer.append("{");
buffer.append(String.format("\“touser\”:\"%s\"", this.toUser)).append(",");
buffer.append(String.format("\“template_id\”:\"%s\"", this.templateId)).append(",");
buffer.append(String.format("\“url\”:\"%s\"", this.page)).append(",");
buffer.append(String.format("\“form_id\”:\"%s\"", this.formId)).append(",");
buffer.append("\“data\”:{");
TemplateParam param = null;
for (int i = 0; i < this.templateParamList.size(); i++) {
param = templateParamList.get(i);
// 判断是否追加逗号
if (i < this.templateParamList.size() - 1) {
buffer.append(String.format("\"%s\": {\“value\”:\"%s\",\“color\”:\"%s\"},", param.getName(),
param.getValue(), param.getColor()));
} else {
buffer.append(String.format("\"%s\": {\“value\”:\"%s\",\“color\”:\"%s\"}", param.getName(),
param.getValue(), param.getColor()));
}
}
buffer.append("}");
buffer.append("}");
return buffer.toString();
}
public List<TemplateParam> getTemplateParamList() {
return templateParamList;
}
public void setTemplateParamList(List<TemplateParam> templateParamList) {
this.templateParamList = templateParamList;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getFormId() {
return formId;
}
public void setFormId(String formId) {
this.formId = formId;
}
}
package com.mall.sms.send.vo;
public class TemplateParam {
// 参数名称
private String name;
// 参数值
private String value;
// 颜色
private String color;
public TemplateParam(String name,String value,String color){
this.name=name;
this.value=value;
this.color=color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
public static boolean sendTemplateMsg(String token,Template template){
boolean flag=false;
String requestUrl=“https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN”;
requestUrl=requestUrl.replace(“ACCESS_TOKEN”, token);
JSONObject jsonResult=CommonUtil.httpsRequest(requestUrl, “POST”, template.toJSON());
System.out.println(jsonResult);
if(jsonResult!=null){
int errorCode=jsonResult.getInt(“errcode”);
String errorMessage=jsonResult.getString(“errmsg”);
if(errorCode==0){
flag=true;
}else{
System.out.println(“模板消息发送失败:”+errorCode+","+errorMessage);
flag=false;
}
}
return flag;
}
}