微信公眾號消息模板開發


消息模板類

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM