微信公众号消息模板开发

消息模板类

package cn.rmt.wxbase.vo;

import java.util.Map;

public class WeixinTemplateMessage {
    private String touser;  
    private String template_id;  
    private String url;  
    private Map<String,WeixinTemplateMessageData> data;  
      
    public String getTemplate_id() {  
        return template_id;  
    }  
    public void setTemplate_id(String template_id) {  
        this.template_id = template_id;  
    }  
    public String getTouser() {  
        return touser;  
    }  
    public void setTouser(String touser) {  
        this.touser = touser;  
    }  
    public String getUrl() {  
        return url;  
    }  
    public void setUrl(String url) {  
        this.url = url;  
    }  
    public Map<String,WeixinTemplateMessageData> getData() {  
        return data;  
    }  
    public void setData(Map<String,WeixinTemplateMessageData> data) {  
        this.data = data;  
    }  
}

催缴方法:

/**
     * 催缴待支付订单的用户
     */
    @RequestMapping(value = "/callFee", method = RequestMethod.POST)
    @ResponseBody
    public ResultJsonInfo callFee(HttpServletRequest request, @RequestBody net.sf.json.JSONObject requestJson) {
      //公众号配置的模板id     
String template_id = configKeyValue.get("TEMPLATE_ID");
//自己获取用户openid String openid="";
if (!StringUtils.isBlank(template_id)) { //2.拼装短信的信息 //设置消息模版 WeixinTemplateMessage template = new WeixinTemplateMessage(); template.setTemplate_id(template_id); template.setTouser(openId); //拼接催缴信息的链接url String url = url = eve + "/pinganWeb/" + tokenId + "/wxschool/wisdom/index.html?callback=login&scope=scope"; template.setUrl(url); //拼装发送消息 Map<String, WeixinTemplateMessageData> m = new HashMap<String, WeixinTemplateMessageData>(); WeixinTemplateMessageData first = new WeixinTemplateMessageData(); first.setValue(vo.getGradeName() + vo.getClassName() + "的" + vo.getStudentName() + "同学,您好,您有一笔待缴费订单,请登录学校系统完成缴费"); m.put("first", first); WeixinTemplateMessageData keyword1 = new WeixinTemplateMessageData(); keyword1.setValue("学杂费"); m.put("keyword1", keyword1); WeixinTemplateMessageData keyword2 = new WeixinTemplateMessageData(); keyword2.setValue("学生学号:" + vo.getStudentNo()); m.put("keyword2", keyword2); WeixinTemplateMessageData keyword3 = new WeixinTemplateMessageData(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:sss"); keyword3.setValue(format.format(vo.getCreateTime()).toString()); m.put("keyword3", keyword3); WeixinTemplateMessageData keyword4 = new WeixinTemplateMessageData(); keyword4.setValue(format.format(new Date()).toString()); m.put("keyword4", keyword4); WeixinTemplateMessageData remark = new WeixinTemplateMessageData(); remark.setValue("如有任何疑问,请拨打学校电话!"); m.put("remark", remark); template.setData(m); String body = JSONObject.toJSONString(template); System.out.println("发送消息内容:" + body); //发送 JSONObject recData = weixinTemplateMessageService.sendTempletMsg(Integer.parseInt(accountId), body); String errcode = (String) recData.getString("errcode"); String errmsg = (String) recData.getString("errmsg"); String msgid = (String) recData.getString("msgid"); System.out.println("发送消息返回errcode:" + errcode + ",errmsg:" + errmsg + ",msgid:" + msgid); //发送成功,修改催缴状态,插入催缴记录 if ("0".equals(errcode)) { vo.setCallFeeStatus("1"); wxSchoolWisdomPaymentDetailInfoService.updateAndInsertCallFee(vo); } else { return new ResultJsonInfo(true, "信息发送失败!"); } } return new ResultJsonInfo(true, "信息发送完成!"); } else { return new ResultJsonInfo(true, "消息模板为空!"); } } }

/**
* #发送消息模版
*/
public static final String API_SEND_MESSAGE_URL = "api.send.message.url";


/**
* 发送模版消息 */ @Override public JSONObject sendTempletMsg(Integer accountId,String body) { String token =getTokenByAppIdAndSec(appid,secret).getAccessToken();
String url
= String.format(WeixinEnv.get(WeixinURLConsts.API_SEND_MESSAGE_URL), token);
HttpPostUtil postHttp
=new HttpPostUtil(); String returnString = postHttp.postHttp(url,body);
JSONObject recData
=JSONObject.parseObject(returnString);
// Response response = executor.post(url, body); // JSONObject recData = response.getAsJson();
return recData;
}
 /**
* 通过appid和secret取得相应token
* @param appid
* @param secret
* @return
*/
private Token getTokenByAppIdAndSec(final String appid,final String secret){
String tokenUrl = String.format(WeixinEnv.get(WeixinURLConsts.API_TOKEN_URL), appid,secret);
// Response response = executor.get(tokenUrl);
// Token token = response.getAsObject(new TypeReference<Token>() {});
HttpPostUtil postHttp=new HttpPostUtil();
String returnString = postHttp.postHttp(tokenUrl,null);
JSONObject jsonObject =JSONObject.parseObject(returnString);
Token token = new Token();
token.setAccessToken(jsonObject.getString("access_token"));
token.setTime(System.currentTimeMillis());
return token;
}
 
weixin.properties
api.base.url=https://api.weixin.qq.com
api.cgi.url ={api.base.url}/cgi-bin
#消息管理
#发送消息模版
api.send.message.url={api.cgi.url}/message/template/send?access_token=%s

 

微信公众号消息模板开发

上一篇:getSharedPreferences()与getSharedPreferences()与getDefaultSharedPreferences()的区别


下一篇:java项目引用证书文件(微信支付的p12文件)