关闭

java判断微信号是否关注微信公众号

标签: JAVA微信微信公众号httpclientredis
2329人阅读 评论(0) 收藏 举报
分类:
	public CommonResult validateAttentionWxPublic(String openId) throws Exception {
		CommonResult cr = new CommonResult();
		String accessToken = "";
                String value = <span style="font-family: Arial, Helvetica, sans-serif;">utilRedisDao.getRedisKey(REDIS_KEY_GLOBAL_TOKEN);</span>

		if(StringUtils.equals(<span style="font-family: Arial, Helvetica, sans-serif;">value </span><span style="font-family: Arial, Helvetica, sans-serif;">, "0")){</span>
			String appid = StringUtils.trimToEmpty(WX_APPID);
			String secret = StringUtils.trimToEmpty(WX_SECRET);
			String url = WX_TOKEN_URL+"?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
			logger.info("获取token"+url);
			String tokenResult = HttpUtil.doGetUrl(url);
			logger.info("获取token响应结果"+tokenResult);
			JSONObject obj = JSONObject.fromObject(tokenResult);
			accessToken = obj.getString("access_token");
			utilRedisDao.setRedisKey(REDIS_KEY_GLOBAL_TOKEN, accessToken, 7000);
		}else{
			accessToken = <span style="font-family: Arial, Helvetica, sans-serif;">value </span>;
		}

		String url = "https://api.weixin.qq.com/cgi-bin/user/info"+"?access_token="+accessToken+"&openid="+openId+"&lang=zh_CN";
		String result = HttpUtil.doGetUrl(url);
		logger.info("获取微信用户信息"+result);
		JSONObject objUserInfo = JSONObject.fromObject(result);
		logger.info("获取微信用户信息返回响应结果:"+objUserInfo.toString());
		if(objUserInfo.containsKey("errcode")){
			cr.setErrorcode(WXTokenConstant.ERROR_TOKEN);
		}else{
			int subscribe = objUserInfo.getInt("subscribe");
			if(subscribe==1){
				cr.setErrorcode(WXTokenConstant.SUBSCRIBE);
			}else{
				cr.setErrorcode(WXTokenConstant.NO_SUBSCRIBE);
			}
		}
		return cr;
	}
public final String REDIS_KEY_GLOBAL_TOKEN="redis_key_global_token"; 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9542次
    • 积分:212
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:0篇
    • 译文:1篇
    • 评论:3条
    文章分类
    最新评论