过程:
1. 申请消息模板功能,审核可能1天左右吧
2. 添加需要的消息模板
3. 开发
1.申请模板
2.添加消息模板
点击“模板库”–选择模板–点击详情–然后点击添加
然后在我的模板中可以看到自己可以使用到的模板
到这里,准备工作就做完了,接下来就是开发了
3.开发
需要知道的属性
模板ID, 接收方的open_id, 发送接口
以购买成功通知 模板为例,
模板ID:iWwH4LKB3PcrP3Qz5UZcbMIbmJGVovR4k-jIMzicTuw(这个每次添加后都在改变)
发送接口: https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN (注意接口不要找错了)
然后向这个接口里面post数据
现在看一下 购买成功通知 模板的样式:
现在贴出核心代码:
private void consume(HttpServletRequest request, HttpServletResponse response,String open_id,String url) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
JSONObject json = new JSONObject();
json.put("touser", open_id);
json.put("template_id", "iWwH4LKB3PcrP3Qz5UZcbMIbmJGVovR4k-jIMzicTuw");
json.put("url", "");
json.put("topcolor", "#ff0000");
JSONObject data = new JSONObject();
data.put("first", toJson("你好,欢迎使用新牙行服务"));
System.out.println(request.getCharacterEncoding());
String product = new String(request.getParameter("product").getBytes("ISO-8859-1"),"UTF-8");
data.put("product", toJson(product));
String price = new String(request.getParameter("price").getBytes("ISO-8859-1"),"UTF-8");
data.put("price", toJson(price+"元"));
String cur_time = CommonUtil.stampToTime(String.valueOf(System.currentTimeMillis()/1000));//当前时间
data.put("time", toJson(cur_time));
data.put("remark", toJson("祝你生活愉快"));
json.put("data", data);
JSONObject result = CommonUtil.httpsRequest(url, "POST",json.toString());
logger.debug("url:"+url);
logger.debug("json:"+json.toString());
System.out.println("json:"+json.toString());
logger.debug("result:"+result.toString());
toJson()函数:
public JSONObject toJson(String value){
JSONObject json = new JSONObject();
json.put("value", value);
json.put("color", "#173177");//消息字体颜色
return json;
}
httpsRequest 代码:
public static JSONObject httpsRequest(String requestUrl,String requestMethod, String outputStr){
JSONObject jsonObject = null;
try{
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
//设置请求方式(GET/POST)
conn.setRequestMethod(requestMethod);
if(null != outputStr){
OutputStream outputStream = conn.getOutputStream();
//注意编码格式
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
//从输入流读取返回内容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
logger.debug("service response:"+conn.getResponseCode()+" "+conn.getResponseMessage());
StringBuffer buffer = new StringBuffer();
while((str = bufferedReader.readLine()) != null){
buffer.append(str);
}
//释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
conn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
}catch(ConnectException e){
logger.debug("service run over");
System.out.println("连接超时");
}catch(Exception e){
logger.debug("http request problem");
System.out.println("https请求异常:{}");
}
return jsonObject;
}