失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 判断用户是否关注公众号

判断用户是否关注公众号

时间:2020-08-16 14:01:32

相关推荐

判断用户是否关注公众号

/*** 需求功能 : 用户扫描一个二维码,跳转到后台获取 二维码中的参数,* 根据参数查询后台,给关注的公众号推送一条后台查询的结果。* 用户关注公众号,就直接发送模板消息,没有关注,跳转到 一个 “提示“长按图片识别”关注公众号的”页面。* 用户关注公众号后,再次扫描二维码,才会推送查询结果。*/@Controller@RequestMapping("/example")public class QueryCnterController1 extends BaseController {@Value("${weixin.appid}")private String appid;@Value("${weixin.appsecret}")private String appsecret;@Value("${wx-host}")public String wxHost;/*** 微信登陆 * 扫二维码就先让用户跳转到这个路径,没有关注公众号,微信就会自动弹出用户授权的确认框,* 确认后跳转到提示先关注公众号的页面,已关注的用户不会弹出。** @param id二维码中的参数id* @param session httpSession* @date /12/15 11:40*/@GetMapping(value = "/wxLogin")public String wxLogin(String id, HttpSession session) throws Exception {// 二维码中的参数64位编码的参数,参数根据自己业务来定, 不需要参数的可以忽略。String your_param = Base64Util.decryBASE64(id);session.setAttribute("your_param", your_param);//这个地址是成功后的回调地址,域名必须和公众号中配置的域名一致 // 我这里的回调方法就是指下面的callBack 方法String backUrl = wxHost + "/example/callBack";// 第一步:用户同意授权,获取codeString url = "https://open./connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + URLEncoder.encode(backUrl, "utf-8") + "&response_type=code" + "&scope=snsapi_userinfo" + "&state=STATE#wechat_redirect";return "redirect:" + url;}/*** 回调方法*/@GetMapping(value = "/callBack")public String callBack(HttpServletRequest req, HttpSession session) {String code = req.getParameter("code");//第二步:通过code换取网页授权access_tokenString url = "https://api./sns/oauth2/access_token?appid=" + appid + "&secret=" + appsecret + "&code=" + code + "&grant_type=authorization_code";// 发送get请求JSONObject jsonObject = WeiXinUtil.httpsRequest(url, "GET", null);if (!jsonObject.has("openId")) {jsonObject.put("openId", "");}String openid = jsonObject.getString("openid");logger.info("获取openid=============================" + openid);String infoUrl = "https://api./cgi-bin/user/info?access_token=" + WeiXinUtil.getToken(appid, appsecret).getAccessToken() + "&openid=" + openid + "&lang=zh_CN";// 获取用户信息JSONObject userInfo = WeiXinUtil.httpsRequest(infoUrl, "GET", null);// 已关注公众号 发送验证模板消息 并跳转到提示页面 公众号文档用户信息中 subscribe 等于1 表示已关注公众号,等于0表示没有关注公众号if (userInfo.getString("subscribe").equals("1")) {String yourParam = session.getAttribute("your_param").toString();return sendTemplateMessage(yourParam, openid);}// 没有关注公众号 就跳转到关注公众号的 提示页面return "/wechat/follow_official_account";}/*** 发送模板消息*/private String sendTemplateMessage(String yourParam, String openid) {// 发送验证成功的模板Token tokenObj = WeiXinUtil.getToken(appid, appsecret);String token = tokenObj.getAccessToken();String postUrl = "https://api./cgi-bin/message/template/send?access_token=" + token;// 构造微信模板 推送验证有效的模板JSONObject jsonObject = new JSONObject();jsonObject.put("touser", openid); // openidjsonObject.put("template_id", "5m31aSNP4abDQhNsG9xtS4ZoJ7Knv47t223Vjq3eaLU");jsonObject.put("url", "");JSONObject data = new JSONObject();// 标题JSONObject first = new JSONObject();first.put("value", "标题");first.put("color", "#173177");// keyword1JSONObject keyword1 = new JSONObject();keyword1.put("value", "关键词1");keyword1.put("color", "#173177");// keyword2JSONObject keyword2 = new JSONObject();keyword2.put("value", "关键词2");keyword2.put("color", "#173177");// keyword3JSONObject keyword3 = new JSONObject();keyword3.put("value", "关键词3");keyword3.put("color", "#173177");data.put("first", first);data.put("keyword1", keyword1);data.put("keyword2", keyword2);data.put("keyword3", keyword3);jsonObject.put("data", data);// 发送模板消息String string = HttpUtil.post(postUrl, jsonObject.toString());com.alibaba.fastjson.JSONObject result = JSON.parseObject(string);int errcode = result.getIntValue("errcode");if (errcode == 0) {// 发送成功System.out.println("发送成功");} else {// 发送失败System.out.println("发送失败");}// 跳转到发送成功的提示页面return "/mobile/success_tip";}

如果觉得《判断用户是否关注公众号》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。