controller接口:
/**
*?
* @param touser 接收用户openId
* @param template_id 模板Id
* @param url url 调用方需要编码
* @param topcolor topcolor 调用方需要编码
* @param data 模板参数,传过来的是json串 ?调用方需要编码
* { "first": { "value":"恭喜你购买成功!", "color":"173177"},
* "keynote1":{ "value":"巧克力", "color":"173177"},
* "keynote2": { "value":"39.8元", "color":"173177"},
* "keynote3": { "value":"2014年9月16日", "color":"173177"},
* "remark":{ "value":"欢迎再次购买!", "color":"173177"}}
* @return
*/
@RequestMapping(value="sendMsg",produces = "application/json; charset=utf-8")
public String sendMsg(String touser,String template_id,String url,String topcolor,String data){
if(Strings.isNullOrEmpty(data)||Strings.isNullOrEmpty(touser)||Strings.isNullOrEmpty(template_id)){
return ?formatJson(1,"参数错误,请检查参数是否正确!");
}
?
try {
logger.info("模板消息接收参数(解码后):"+data);
data = URLDecoder.decode(data, "UTF-8");
logger.info("模板消息接收参数(解码后):"+data);
url = url==null?"":URLDecoder.decode(url, "UTF-8");
topcolor = topcolor==null?"":URLDecoder.decode(topcolor, "UTF-8");
} catch (UnsupportedEncodingException e) {
return ?formatJson(1,"解析参数异常,请检查data,topcolor,url参数");
}
?
?
?
JSONObject obj = null;
try {
obj = JSON.parseObject(data);
} catch (Exception e) {
logger.info("将data转换成json异常,"+e.getMessage());
return ?formatJson(1,"解析参数异常,请检查data是否为标准的JOSN串");
}
?
TemplateMsg templateMsg = new TemplateMsg();
templateMsg.touser = touser;
templateMsg.data = obj;
templateMsg.template_id = template_id;
if(!Strings.isNullOrEmpty(url)){
templateMsg.url = url;
}
?
if(!Strings.isNullOrEmpty(topcolor)){
templateMsg.topcolor = topcolor;
}
?
logger.info("接收模板消息:"+templateMsg.toString());
?
SendTemplateMsgResponse sendTemplateMsgResponse ?= templateMessageService.sendMsg(templateMsg);
?
if(sendTemplateMsgResponse == null){
return formatJson(1,"发送消息异常!");
}
?
if(sendTemplateMsgResponse.errcode == 0){//发送成功
return ?"{\"code\":\"0\",\"result\":\"发送成功\",\"msgid\":\""+sendTemplateMsgResponse.msgid+"\"}";
}else{
return formatJson(1,sendTemplateMsgResponse.errmsg);
}
}
?
service:
public interface TemplateMessageService {
? ?public SendTemplateMsgResponse sendMsg(TemplateMsg templateMsg);
}
serviceImpl:
public class TemplateMessageServiceImpl implements TemplateMessageService {
@Override
public SendTemplateMsgResponse sendMsg(TemplateMsg templateMsg) {
SendTemplateMsgResponse sendTemplateMsgResponse = null;
try {
// https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
//将这里修改为自己调用服务的方式
sendTemplateMsgResponse = Http.post(
"cgi-bin/message/template/send", templateMsg,
SendTemplateMsgResponse.class);
?
} catch (Exception e) {
logger.debug("Send Template Message Exception:" + e.getMessage());
}
?
return sendTemplateMsgResponse;
}
}
?
?
?model:
public class SendTemplateMsgResponse ?extends BaseResponse{
? ? ? ? public String msgid;
? ? ? ? public int errcode = 0;
? ? ? ? public String errmsg;
?
@Override
public String toString() {
return "SendTemplateMsgResponse [msgid=" + msgid + ", errcode="
+ errcode + ", errmsg=" + errmsg + "]";
}
?
}
?
?
public class TemplateMsg {
public String touser;
?
public String template_id;
?
public String url;
?
public String topcolor;
?
public JSONObject data;
?
}