用户关注微信公众号,后台获取到谁关注了公众号。
请先配置微信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();