微信公众号开发---消息模板的使用

过程:
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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值