微信端发送模版消息
MessageController.java
package com.alpha.modules.message.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alpha.modules.message.form.MessageForm;
import com.alpha.modules.message.model.Template;
import com.alpha.modules.message.model.TemplateParam;
import com.alpha.modules.message.utils.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping("/app")
@Api("APP支付接口")
public class MessageController {
/**
* 发送微信消息:订单发货提醒
*
* @param 物品名称
{{keyword1.DATA}}
订单号
{{keyword2.DATA}}
收货地址
{{keyword3.DATA}}
* @param model
*/
@PostMapping("sendMessage")
@ApiOperation("发送消息")
public void sendMessage(@RequestBody MessageForm messageForm, HttpServletRequest request, Model model) {
Template tem = new Template();
tem.setTemplateId("65X9x1h235HlUmmAi2Z6_3xc8-sUXGrXcAB_195yLDo");//
tem.setFormId(messageForm.getFormId());
tem.setTopColor("#00DD00");
tem.setToUser(messageForm.getOpenId());//"72517cdbf5274f2aabffdc235e96ef54"
tem.setUrl("");
List<TemplateParam> paras = new ArrayList<TemplateParam>();
paras.add(new TemplateParam("keyword1", "电风扇", "#AAAAAA"));
paras.add(new TemplateParam("keyword2", "287368726886", "#0044BB"));
paras.add(new TemplateParam("keyword3", "浙江温州鹿城区聚盟商10楼", "#AAAAAA"));
/*paras.add(new TemplateParam("keyword4", "666", "#0044BB"));
paras.add(new TemplateParam("keyword5", "131123456789", "#AAAAAA"));*/
tem.setTemplateParamList(paras);
boolean result = CommonUtil.sendTemplateMsg(tem);
System.out.println(result);
}
}
MessageForm.java
package com.alpha.modules.message.form;
import org.hibernate.validator.constraints.NotBlank;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* 消息表单
*/
@ApiModel(value = "消息表单")
public class MessageForm {
@ApiModelProperty(value = "openId")
@NotBlank(message="openId不能为空")
private String openId;
@ApiModelProperty(value = "formId")
private String formId;
public String getOpenId() {
return openId;
}
public void setOpenId(String openId) {
this.openId = openId;
}
public String getFormId() {
return formId;
}
public void setFormId(String formId) {
this.formId = formId;
}
}
model Template.java
package com.alpha.modules.message.model;
import java.util.List;
public class Template {
// 消息接收方
private String toUser;
// 模板id
private String templateId;
// form_id
private String formId;
// 模板消息详情链接
private String url;
// 消息顶部的颜色
private String topColor;
// 参数列表
private List<TemplateParam> templateParamList;
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 getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTopColor() {
return topColor;
}
public void setTopColor(String topColor) {
this.topColor = topColor;
}
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("\"form_id\":\"%s\"", this.formId))
.append(",");
buffer.append(String.format("\"url\":\"%s\"", this.url)).append(",");
buffer.append(String.format("\"topcolor\":\"%s\"", this.topColor))
.append(",");
buffer.append("\"data\":{");
TemplateParam param = null;
if (this.templateParamList.size() > 0) {
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 getFormId() {
return formId;
}
public void setFormId(String formId) {
this.formId = formId;
}
}
TemplateParam.java
package com.alpha.modules.message.model;
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;
}
}