java 微信公众服务平台 下发 模板消息

java 微信公众服务平台 下发 模板消息

(一).部分截图

(二).部分代码

 

(一).部分截图:

java 微信公众服务平台 下发 模板消息

java 微信公众服务平台 下发 模板消息

 

(二).部分代码:

//此处 给用户微信发消息...
                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("@WXID@", wxid);
                paraMap.put("@TEMPLATE_ID@", CommendDef.PAYROLL_TEMPLATE_ID);
                paraMap.put("@URL@", CommendURL.viewPayRollUrl()+"period="+period+"&wxid="+wxid);
                paraMap.put("@CONTENT@", 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\":\"@WXID@\",\n"+
           "\"template_id\":\"@TEMPLATE_ID@\",\n"+
           "\"url\":\"@URL@\",\n"+
           "\"topcolor\":\"#FF0000\",\n"+
           "\"data\":{\n"+
                  " \"content\":{\n"+
                       "\"value\":\"@CONTENT@\",\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;
    }
    
}

 

java 微信公众服务平台 下发 模板消息

上一篇:ASP.NET MVC 接入微信公共平台


下一篇:FreeSwitch-中国电话号码编号规则