判断用户是否关注公众号

/**
 * 需求功能 :  用户扫描一个二维码,跳转到后台获取 二维码中的参数,
 * 根据参数查询后台,给关注的公众号推送一条后台查询的结果。
 * 用户关注公众号,就直接发送模板消息,没有关注,跳转到 一个 “提示“长按图片识别”关注公众号的”页面。
 * 用户关注公众号后,再次扫描二维码,才会推送查询结果。
 */
@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 2021/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";
        // 第一步:用户同意授权,获取code
        String url = "https://open.weixin.qq.com/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_token
        String url = "https://api.weixin.qq.com/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.weixin.qq.com/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.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;

        // 构造微信模板 推送验证有效的模板
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("touser", openid);   // openid
        jsonObject.put("template_id", "5m31aSNP4abDQhNsG9xtS4ZoJ7Knv47t223Vjq3eaLU");
        jsonObject.put("url", "http://www.baidu.com");

        JSONObject data = new JSONObject();

        // 标题
        JSONObject first = new JSONObject();
        first.put("value", "标题");
        first.put("color", "#173177");
        //  keyword1
        JSONObject keyword1 = new JSONObject();
        keyword1.put("value", "关键词1");
        keyword1.put("color", "#173177");

        //  keyword2
        JSONObject keyword2 = new JSONObject();
        keyword2.put("value", "关键词2");
        keyword2.put("color", "#173177");

        //  keyword3
        JSONObject 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";
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值