(一).部分截图
(二).部分代码
(一).部分截图:
(二).部分代码:
//此处 给用户微信发消息... Map<String,String> paraMap = new HashMap<String,String>(); String pk_paydata = (String)curDataMap.get("pk_paydata"); String personname = (String)curDataMap.get("personname"); String wxid = (String)curDataMap.get("wxid"); if(null==wxid || "".equals(wxid)){ throw new Exception(personname+"的没有绑定微信账户,请先绑定后再下发!"); } paraMap.put("@[email protected]", wxid); paraMap.put("@[email protected]", CommendDef.PAYROLL_TEMPLATE_ID); paraMap.put("@[email protected]", CommendURL.viewPayRollUrl()+"period="+period+"&wxid="+wxid); paraMap.put("@[email protected]", personname+",您好,"+period+"的信息已下发,请点击查看详情。"); String errcode = TemplateMessageUtil.sendTemplateMsg(paraMap); if("0".equals(errcode)){ logger.info(personname+"微信下发成功!"); }else{ logger.info(personname+"微信下发失败!"); }
TemplateMessageUtil:
package com.payroll.wx.util; import java.util.Map; import java.util.Map.Entry; import net.sf.json.JSONObject; import com.util.EmailUtil; /** * 微信发送 模板消息util * * @author lifq * @date 2015-3-18 下午05:43:40 */ public class TemplateMessageUtil { //下发薪资单 模板消息 public static final String SEND_PERIOD_TEMPLATE = "{\n"+ "\"touser\":\"@[email protected]\",\n"+ "\"template_id\":\"@[email protected]\",\n"+ "\"url\":\"@[email protected]\",\n"+ "\"topcolor\":\"#FF0000\",\n"+ "\"data\":{\n"+ " \"content\":{\n"+ "\"value\":\"@[email protected]\",\n"+ "\"color\":\"#173177\"\n"+ "}\n"+ "}\n"+ "}"; /** * 发送模板消息 * * @return boolean * @author lifq * @date 2015-2-11 下午03:25:07 */ public static String sendTemplateMsg (Map<String,String> paraMap){ String data = TemplateMessageUtil.SEND_PERIOD_TEMPLATE; //替换变量 for (Entry<String, String> entry: paraMap.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); data = EmailUtil.replace(data,key,value); } String res = HttpPostUtil.doHttpPostJson(CommendURL.TemplateUrl(), data); JSONObject demoJson = JSONObject.fromObject(res); String errcode = demoJson.getString("errcode"); return errcode; } }
HttpPostUtil:
package com.payroll.wx.util; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class HttpPostUtil { public static String doHttpPostJson(String Url,String json) { String message = ""; System.out.println(json); try { URL url = new URL(Url); HttpURLConnection http = (HttpURLConnection) url.openConnection(); http.setRequestMethod("POST"); http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); http.setDoOutput(true); http.setDoInput(true); System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒 http.connect(); OutputStream os= http.getOutputStream(); os.write(json.getBytes("UTF-8"));//传入参数 os.flush(); os.close(); InputStream is =http.getInputStream(); int size =is.available(); byte[] jsonBytes =new byte[size]; is.read(jsonBytes); message=new String(jsonBytes,"UTF-8"); System.out.println(message); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return message; } }
时间: 2024-12-26 15:48:41