1.3 公众号订阅通知

用户关注微信公众号,后台获取到谁关注了公众号。

请先配置微信sdk

一、配置处理类



import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class WxMpMessageSubscribeHandler implements WxMpMessageHandler {

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) throws WxErrorException {
        log.info("微信订阅通知:{}",JSONObject.toJSONString(wxMessage) );
        WxMpUser wxMpUser = wxMpService.getUserService().userInfo(wxMessage.getFromUser());
        log.info("用户信息", JSONObject.toJSONString(wxMpUser));
        return null;
    }
}

二、配置路由




import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

@Configuration
public class WxUnionConfig {

    @Autowired
    private WxUnionParam wxUnionParam;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Bean
    public WxMpService initWxMpService(){
        WxMpService  wxMpService = new WxMpServiceImpl();
        WxMpDefaultConfigImpl config = new WxMpRedisConfigImpl(new RedisTemplateWxRedisOps(stringRedisTemplate),"wx");
        config.setAppId(wxUnionParam.getAppId());
        config.setSecret(wxUnionParam.getSecret());
        config.setToken(wxUnionParam.getToken());
        wxMpService.setWxMpConfigStorage(config);
        return wxMpService;
    }

    @Bean
    public WxMpMessageRouter router(WxMpService wxMpService,
                                    WxMpMessageSubscribeHandler wxMpMessageSubscribeHandler) {
        //参考地址 https://github.com/Wechat-Group/WxJava/wiki/MP_%E5%BE%AE%E4%BF%A1%E6%B6%88%E6%81%AF%E8%B7%AF%E7%94%B1%E5%99%A8
        WxMpMessageRouter router = new WxMpMessageRouter(wxMpService);
        router
                // 4个条件必须全部匹配的路由规则
                //公众号订阅事件推送
                .rule()
                .msgType("event")
                .event("subscribe")
                .handler(wxMpMessageSubscribeHandler)
                .end()
                //公众号点击事件推送
                .rule()
                .msgType("event")
                .event("CLICK")
                .handler(wxMpMessageSubscribeHandler)
                .end();
        return router;
    }

}

当用户进行关注以后访问微信后台配置的接口,后台就会获取到用户订阅通知会自动

//注意此接口可以根据微信传递过来的用户openID获取用户的详情信息保存至数据库,还可以获取用户的UnionId,unionId是公众号与各小程序之间通讯的唯一标识,后续可能会用到。
WxMpUser wxMpUser =wxMpService.getUserService().userInfo(wxMessage.getFromUser();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值