微信自定义推送模版消息

/** * 组装模版消息 */WxTemplate t = new WxTemplate();// 点击的时候执行的方法//t.setFirst("欢迎关注爱窝智慧商城!");t.setUrl("baidu.com");// 当前用户openidt.setTouser(openid);t.setTopcolor("#FF0000");// 模版IDt.setTemplate_id("JktIWQcrsFgKx7yrgNsPBvFGyM53jm-qV3TZtgaA9AIasdasd");// 设置日期格式SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Map m = new HashMap();// TemplateData first = new TemplateData();// first.setColor("#000000");// first.setValue("欢迎关注商城!");// m.put("first", first);TemplateData productType = new TemplateData();productType.setColor("#000000");productType.setValue("商品名:" + product.getProName());m.put("productType", productType);// 商品个数TemplateData number = new TemplateData();number.setColor("#000000");number.setValue(map.get("cash_fee") + "个");m.put("number", number);// 当前时间TemplateData expDate = new TemplateData();expDate.setColor("#000000");expDate.setValue(df.format(new Date()));m.put("expDate", expDate);// 当前时间// TemplateData price = new TemplateData();// price.setColor("#000000");// price.setValue("1元");// m.put("price", price);// 当前时间TemplateData remark = new TemplateData();remark.setColor("#000000");remark.setValue("您的订单以提交!如有疑问,请致电1234联系我们");m.put("remark", remark);// t.setRemark("您的订单以提交!");t.setData(m);/** * 推送成功消息至微信 */System.err.println(jsonObject.fromObject(t).toString());CommonUtil.httpRequest("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+ Access_Token, "POST", jsonObject.fromObject(t).toString());

CommonUtil工具类: /** * 发送https请求 * * @param requestUrl * 请求地址 * @param requestMethod * 请求方法 * @param outputStr * 提交的数据 * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值) */public static JSONObject httpsRequest(String requestUrl,String requestMethod, String outputStr) {JSONObject jsonObject = null;try {// 创建SSLContext对象,并使用我们指定的信任管理器初始化TrustManager[] tm = { new MyX509TrustManager() };SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");sslContext.init(null, tm, new java.security.SecureRandom());// 从上述SSLContext对象中得到SSLSocketFactory对象SSLSocketFactory ssf = sslContext.getSocketFactory();URL url = new URL(requestUrl);HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();conn.setSSLSocketFactory(ssf);conn.setDoOutput(true);conn.setDoInput(true);conn.setUseCaches(false);// // 设置请求方式(GET/POST)conn.setRequestMethod(requestMethod);// 当outputStr不为null时向输出流写数据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;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 ce) {log.error("连接超时:", ce);} catch (Exception e) {log.error("https请求异常", e);}return jsonObject;}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值