Java后端接收用户关注/取消公众号或者接收用户发送到公众号的消息

 

 请求的Controller



@Slf4j
@RestController
@RequestMapping("/wxGongZhongHao")
public class UserAttentionGongZhongHaoController {

    @PostMapping(value = "/notifyMsg", produces = "text/plain;charset=utf-8")
    public Object notify(@ApiIgnore @RequestBody String xml) {
        userAttentionGongZhongHaoService.notifyMsg(xml);
        return StringUtils.EMPTY;
    }
}

请求的Service


import cn.hutool.core.util.XmlUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户关注/取消公众号以及接收用户发送到公众号的消息
 * </p>
 *
 * @author 
 * @since 2020-08-20
 */
@Slf4j
@Service
public class UserAttentionGongZhongHaoServiceImpl implements UserAttentionGongZhongHaoService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void notifyMsg(String xml) {

        log.info("后台接收到公众号发来的数据是:\n{}", xml);

        // 把微信发送给后端的xml消息转成WechatNotifyRequestVO对象  start
        Map<String, Object> map = XmlUtil.xmlToMap(xml);
        WechatNotifyRequestVO wechatNotifyRequestVO = JSONUtil.parseFromMap(map).toBean(WechatNotifyRequestVO.class);
        // 把微信发送给后端的xml消息转成WechatNotifyRequestVO对象  end

        // 事件类型,subscribe表示订阅,unsubscribe表示取消订阅, null表示用户发送消息过来
        String event = wechatNotifyRequestVO.getEvent();

        if (StringUtils.equals(event, "subscribe")) {

            // 获取关注公众号的用户的openid start
            String userGongZhongHaoOpenIdStr = wechatNotifyRequestVO.getFromUserName();
            // 获取关注公众号的用户的openid end


            // 获取access_token  start
            String accessTokenStr = "这里填写公众号的access_token"
            // 获取access_token  end


            // 利用微信公众号的openid和公众号的access_token获取用户的基本信息包括unionid  详情见https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html#UinonId  start
            Map<String, Object> unionidParamMap = new HashMap<>(16);
            unionidParamMap.put("access_token", accessTokenStr);
            unionidParamMap.put("openid", userGongZhongHaoOpenIdStr);
            JSONObject JSONObjectResult = HttpsUtils.doGet("https://api.weixin.qq.com/cgi-bin/user/info", unionidParamMap);
            String subscribe = JSONObjectResult.getString("subscribe");                     // 用户是否订阅该公众号标识,值为0时,代表此用户没有关注该公众号,拉取不到其余信息。
            if (!StringUtils.equals(subscribe, "1")) {
                log.info("用户没有关注公众号, 错误信息是:" + JSONObjectResult.toString());
                return;
            }
            String unionidStr = JSONObjectResult.getString("unionid");
            // 利用微信公众号的openid和公众号的access_token获取用户的基本信息包括unionid  详情见https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html#UinonId  end
            
        }

        if (StringUtils.equals(event, "unsubscribe")) {
            log.info("用户取消公众号的订阅了....");
        }

        if (!StringUtils.isBlank(wechatNotifyRequestVO.getContent())) {
            log.info("用户发送消息过来,消息内容是:" + wechatNotifyRequestVO.getContent());
        }
    }
}

 

 

WechatNotifyRequestVO类:

import com.thoughtworks.xstream.annotations.XStreamAlias;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 详情见微信官方文档
 * https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_event_pushes.html
 */

@Data
public class WechatNotifyRequestVO {

    @ApiModelProperty("开发者微信号,微信官方不是驼峰式命名")
    private String ToUserName;

    @ApiModelProperty("发送方帐号(一个OpenID),微信官方不是驼峰式命名")
    private String FromUserName;

    @ApiModelProperty("消息创建时间 (整型),微信官方不是驼峰式命名")
    private String CreateTime;

    @ApiModelProperty("消息类型,微信官方不是驼峰式命名")
    private String MsgType;

    @ApiModelProperty("事件类型,subscribe(订阅)、unsubscribe(取消订阅),微信官方不是驼峰式命名")
    private String Event;

    @ApiModelProperty("消息id,微信官方不是驼峰式命名")
    private String MsgId;

    @ApiModelProperty("消息内容,微信官方不是驼峰式命名")
    private String Content;
}

 

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值