通过小程序获取微信用户的公众号openid

 @Value("${wx.appId}")
    private String appId;
    @Value("${wx.secret}")
    private String secret;
    @Resource
    private HPatientMapper hPatientMapper;
    @Resource
    private HSharePersonMapper sharePersonMapper;

    @RabbitListener(queues = "TTL_DOCTOR")
    @RabbitHandler
    public void infoConsumption(String message) {
        log.info("开始消费");
        String urlToke = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + secret;//微信公众号的
        try {
            Response response = OkHttpUtil.get(urlToke);
            if (response.isSuccessful()) {
                String string = response.body().string();
                Map<String, String> map = JSONObject.parseObject(string, Map.class);
                String access_token = map.get("access_token");
                String next_openid = "";
                String urlOpenid = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + access_token + "&next_openid=" + next_openid;
                Response responseOpenId = OkHttpUtil.get(urlOpenid);
                if (responseOpenId.isSuccessful()) {
                    String stringOpenId = responseOpenId.body().string();
                    Map<String, String> mapOpenId = JSONObject.parseObject(stringOpenId, Map.class);
                    Object dataS = mapOpenId.get("data");
                    Map<String, String> mapStr = JSONObject.parseObject(dataS.toString(), Map.class);
                    Object openid = mapStr.get("openid");
                    //log.info("这是{}",mapStr.get("openid"));
                    JSONArray objects = JSONArray.parseArray(openid.toString());
                    for (Object object : objects) {
                        String openId = object.toString();
                        //log.info("openid{}", openId);
                        if ("1".equals(message)) {//患者登录
                            addPatientPublicId(openId,access_token);
                        }else {
                            //分享人登录
                            addSharePublicId(openId,access_token);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void addPatientPublicId(String openId,String access_token){
        try {
            List<HPatient> hPatientList = hPatientMapper.selectAllPatient();
            HPatient hPatient = hPatientMapper.selectPatientByPublicOpenId(openId);
            if (hPatient == null) {
                String urlPerson = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + access_token + "&openid=" + openId + "&lang=zh_CN";
                Response responsePerson = OkHttpUtil.get(urlPerson);
                if (responsePerson.isSuccessful()) {
                    String stringPerson = responsePerson.body().string();
                    Map<String, String> mapPerson = JSONObject.parseObject(stringPerson, Map.class);
                    //log.info("数据{}", mapStr.get("openid"));
                    if (ObjectUtils.isNotEmpty(hPatientList)) {
                        for (HPatient patient : hPatientList) {
                            if (mapPerson.get("unionid").equals(patient.getUnionId())) {
                                patient.setPublicId(mapPerson.get("openid"));
                                hPatientMapper.updateHPatientDevice(patient);
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void addSharePublicId(String openId,String access_token){
        try {
            List<HSharePerson> hSharePersonList = sharePersonMapper.selectAllShare();
            HSharePerson sharePerson = sharePersonMapper.selectShareByPublicOpenId(openId);
            if (sharePerson == null) {
                String urlPerson = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + access_token + "&openid=" + openId + "&lang=zh_CN";
                Response responsePerson = OkHttpUtil.get(urlPerson);
                if (responsePerson.isSuccessful()) {
                    String stringPerson = responsePerson.body().string();
                    Map<String, String> mapPerson = JSONObject.parseObject(stringPerson, Map.class);
                    //log.info("数据{}", mapStr.get("openid"));
                    if (ObjectUtils.isNotEmpty(hSharePersonList)) {
                        for (HSharePerson hSharePerson : hSharePersonList) {
                            if (mapPerson.get("unionid").equals(hSharePerson.getUnionId())) {
                                hSharePerson.setPublicId(mapPerson.get("openid"));
                                sharePersonMapper.updateHSharePerson(hSharePerson);
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

原理:

小程序登录会产生一个unionid,微信公众号和开发平台做绑定后用户关注公众号后也会产生一个unionid,二者的unionid是一致的,相应的就可以找到用户公众号的openid

注意事项:

1:小程序在该公众号下

2:公众号要与微信开放平台做绑定(不绑定的话获取不到用户unionid)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值