String appId = ""//小程序appId String appSecret = "" //小程序秘钥 String url = "https://api.weixin.qq.com/cgi-bin/token?appid="+appId+"&secret="+appSecret+"&grant_type=client_credential" RestTemplate restTemplate = new RestTemplate() ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, String.class) logger.info("responseEntity授权获取accessToken:"+responseEntity)
if(responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK){ String sessionData = responseEntity.getBody() logger.info("sessionData = "+ sessionData) JSONObject jsonObj = JSON.parseObject(sessionData) String accessToken = jsonObj.getString("access_token") //发送模板消息 到小程序进行数据封装 WxTemplate template = new WxTemplate() template.touser = "" Map<String,TemplateData> data = new HashMap<String,TemplateData>() TemplateData keyword1 = new TemplateData() keyword1.value = "" TemplateData keyword2 = new TemplateData() keyword2.value = "" TemplateData keyword3 = new TemplateData() keyword3.value = "" TemplateData keyword4 = new TemplateData() keyword4.value = "" data.put("keyword1",keyword1) data.put("keyword2",keyword2) data.put("keyword3",keyword3) data.put("keyword4",keyword4) template.data = data String json = JSONObject.toJSONString(template) logger.info("订单发送通知模板消息JSON数据:"+JsonUtils.toJson(template)) String ret = HttpClientUtil.sendPost(SEND_TEMPLATE_MESSAGE+accessToken, json) MessageRes res = getObject(ret,MessageRes) if("ok".equals(res.errmsg)){//发送成功 //业务逻辑处理 } logger.info("订单发送通知模板消息返回res:"+JSON.parseObject(ret)) }
WxTemplate 类: class WxTemplate { String template_id; String touser; String form_id;//请自行添加上set,get方法 String page; Map<String,TemplateData> data; }
TemplateData 类: class TemplateData { String value; String color } /** * 字符串转对象 * @param pojo * @param tclass * @return */ public static <T> T getObject(String pojo, Class<T> tclass) { try { return JSONObject.parseObject(pojo, tclass); } catch (Exception e) { e.printStackTrace() } return null; } MessageRes 类: class MessageRes implements Serializable{ Integer errcode String errmsg String template_id } /** * 发送post请求 json格式 * @param url * @param param * @return */ public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader ins = null; String result = ""; try { URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("Accept", "text/html"); conn.setRequestProperty("Content-Type", "text/html"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(param); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 ins = new BufferedReader( new InputStreamReader(conn.getInputStream(), "utf-8")); String line; while ((line = ins.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送 POST 请求出现异常!" + e); e.printStackTrace(); } //使用finally块来关闭输出流、输入流 finally { try { if (out != null) { out.close(); } if (ins != null) { ins.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; }