微信服务号发布

前段时间接触了一个微信服务号的,要给用户推送消息,啃了两天,在老大的指导下才算弄明白,今天就分享一下,需要实现的效果如下:

1、获取openid:

 openid是唯一标志微信用户的id,用来获取对应的用户信息

//通过微信code获取openid
	public static String getOpenId(String code) throws Exception {
			String webStr = HttpUtil.sendGetRequest(getTokenUrlfw(code), HttpUtil.CHARACTER_ENCODING);
			logger.debug("getAccessToken,webStr=" + webStr);
			HashMap map = (HashMap) JSONUtil.deserialize(webStr);
			String openid = (String) map.get("openid");
			return openid;
	}

2、获取token

public static final String CHARACTER_ENCODING = "UTF-8";
  
public static String getTokenUrlfw(String code){
        return "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx3d7c7ee3cdba0d82&secret=de41d135f5982d45e0fdf11c93e12bb0&code="+code+"&grant_type=authorization_code";
    }

    /**
     * 发送get请求,获取返回html
     * 
     * @param strUrl
     *            请求地址
     * @param encode
     *            页面编码
     * @return
     * @throws Exception
     */
    public static String sendGetRequest(String strUrl, String encode) throws Exception {
        URL newUrl = new URL(strUrl);
        HttpURLConnection hConnect = (HttpURLConnection) newUrl.openConnection();
        InputStream is = hConnect.getInputStream();
        String str = inputStreamToString(is, encode);
        is.close();
        hConnect.disconnect();
        return str;
    }

3、 调用模板消息

 public static boolean sendTemplateMsg(String deptName,String[] user,String courseurl,String resourceName) throws Exception{
    	//user[1]  openid    user[2]resourse_type    课程/考试的类型
    	//获取access_token
    	//tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3d7c7ee3cdba0d82&secret=de41d135f5982d45e0fdf11c93e12bb0";
    	String token = getAccessToken();
    	//发送模板消息
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        JSONObject params = new JSONObject();
        //params
        Map<String, Object>data=new HashMap<>();//数据对象
        Map<String, String>item=new HashMap<>();
        params.put("touser", user[1]);//收消息的人的openid
        if(user[2].equals("course")){      //课程
        	params.put("template_id", "EPN6J0kI2RJ4I2tB1SOhxb08pk4OkQXU-yKPDEUzuqo");//模板id
            params.put("url", courseurl);
            String str="您有一门新课程";
            item.put("value", str);//数据项的值
            item.put("color", "#000000");//数据项的颜色
            data.put("first", item);//数据项
            item=new HashMap<>();
            item.put("value", resourceName);
            item.put("color", "#173177");
    	    data.put("keyword1", item);
    	    item=new HashMap<>();
            item.put("value", deptName);
            item.put("color", "#173177");
    	    data.put("keyword2",item);
    	    item=new HashMap<>();
            item.put("value", "快速点击");
            item.put("color", "#173177");
    	    data.put("remark", item);
    	    params.put("data", data);
            String webStr = HttpUtil.doPost(url, params);
        }else{                //考试
        	 params.put("template_id", "S_bybqvE5IFDaOUm7J0h5vJptn2Q9tCkuU5ayH_PX3U");//模板id
             params.put("url", courseurl);
             String str="您有一门新考试";
             item.put("value", str);//数据项的值
             item.put("color", "#000000");//数据项的颜色
             data.put("first", item);//数据项
             item=new HashMap<>();
             item.put("value", resourceName);
             item.put("color", "#173177");
     	     data.put("keyword1", item);
     	     item=new HashMap<>();
             item.put("value", df.format(new Date()));
             item.put("color", "#173177");
             data.put("keyword2",item);
             item=new HashMap<>();
             item.put("value", "全天");
             item.put("color", "#173177");
     	     data.put("keyword3",item);
     	    item=new HashMap<>();
            item.put("value", "快速点击");
            item.put("color", "#173177");
    	    data.put("remark", item);
    	    params.put("data", data);
            String webStr = HttpUtil.doPost(url, params);
        }
  
    	return false;
    }
    

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值