|
|
@@ -0,0 +1,128 @@
|
|
|
+package com.cyksj.service.gzh.factory;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.cyksj.mapper.UserMapper;
|
|
|
+import com.cyksj.mapper.WxAppMapper;
|
|
|
+import com.cyksj.mapper.gzh.UserGzhRelationMapper;
|
|
|
+import com.cyksj.model.dto.GzhUnionidUserinfo;
|
|
|
+import com.cyksj.model.dto.MsgEvent;
|
|
|
+import com.cyksj.model.entity.User;
|
|
|
+import com.cyksj.model.entity.UserGzhRelation;
|
|
|
+import com.cyksj.model.entity.WxApp;
|
|
|
+import com.cyksj.redis.RedisService;
|
|
|
+import com.cyksj.service.wechat.WeChatService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+/*
|
|
|
+ *项目名: netflix
|
|
|
+ *文件名: EventMsgService
|
|
|
+ *创建者: JavaZou
|
|
|
+ *创建时间:2023/1/13 18:16
|
|
|
+ */
|
|
|
+@Service("event")
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Slf4j
|
|
|
+public class EventMsgService implements GzhMsgService {
|
|
|
+ private final UserMapper userMapper;
|
|
|
+
|
|
|
+ private final UserGzhRelationMapper userGzhRelationMapper;
|
|
|
+
|
|
|
+ private final WeChatService weChatService;
|
|
|
+
|
|
|
+ private final WxAppMapper wxAppMapper;
|
|
|
+
|
|
|
+ private final RedisService redisService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String handleNotify(MsgEvent msgEvent) {
|
|
|
+ try {
|
|
|
+ if (msgEvent.getEvent() == MsgEvent.Event.subscribe) {
|
|
|
+ if (!saveOrGetUser(msgEvent)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ } else if (msgEvent.getEvent() == MsgEvent.Event.unsubscribe) {
|
|
|
+ UserGzhRelation userGzhRelation = userGzhRelationMapper.selectOne(Wrappers.lambdaQuery(UserGzhRelation.class)
|
|
|
+ .eq(UserGzhRelation::getOpenId, msgEvent.getFromUserName()).last("limit 1"));
|
|
|
+ if (userGzhRelation == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ userGzhRelation.setIsSubscribe(false);
|
|
|
+ userGzhRelationMapper.updateById(userGzhRelation);
|
|
|
+ } else if (msgEvent.getEvent() == MsgEvent.Event.SCAN) {
|
|
|
+ if (!saveOrGetUser(msgEvent)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("接收微信公众:{},报错:{]", msgEvent.getToUserName(), e == null ? "" : e.getMessage());
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public Boolean saveOrGetUser(MsgEvent msgEvent) throws Exception {
|
|
|
+ //扫描带参数二维码 场景值
|
|
|
+ String sceneKey = msgEvent.getEventKey();
|
|
|
+
|
|
|
+ String openId = msgEvent.getFromUserName();
|
|
|
+ if (StrUtil.isEmpty(openId)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ //该用户是否存在于用户表里面
|
|
|
+ User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
|
|
|
+ .eq(User::getOpenId, openId)
|
|
|
+ .select(User::getId)
|
|
|
+ .last("limit 1"));
|
|
|
+ GzhUnionidUserinfo info = weChatService.getUserInfo2(weChatService.getAccessToken(), openId);
|
|
|
+ if (user == null) {
|
|
|
+ user = new User();
|
|
|
+ user.setNickname(info.getNickname())
|
|
|
+ .setSex(info.getSex())
|
|
|
+ .setCountry(info.getCountry())
|
|
|
+ .setProvince(info.getProvince())
|
|
|
+ .setCity(info.getCity())
|
|
|
+ .setHeadimgurl(info.getHeadimgurl());
|
|
|
+ user.setOpenId(openId);
|
|
|
+ user.setUnionid(info.getUnionid());
|
|
|
+ userMapper.insert(user);
|
|
|
+
|
|
|
+ saveGzhUserRelation(msgEvent, user.getId(), openId, info.getUnionid());
|
|
|
+ } else {
|
|
|
+ //之前关注过
|
|
|
+ if (user.getUnionid() == null || !user.getUnionid().equals(info.getUnionid())) {
|
|
|
+ user.setUnionid(info.getUnionid());
|
|
|
+ userMapper.updateById(user);
|
|
|
+ }
|
|
|
+ UserGzhRelation userGzhRelation = userGzhRelationMapper.selectOne(Wrappers.lambdaQuery(UserGzhRelation.class)
|
|
|
+ .eq(UserGzhRelation::getOpenId, msgEvent.getFromUserName()).last("limit 1"));
|
|
|
+ if (userGzhRelation == null) {
|
|
|
+ saveGzhUserRelation(msgEvent, user.getId(), openId, info.getUnionid());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //场景值扫描公众号二维码 登录
|
|
|
+ if (StrUtil.isNotEmpty(sceneKey)) {
|
|
|
+ redisService.set(RedisService.key.WX_GZH_QRCODE_LOGIN.getNameFormat(sceneKey), user.getId(), RedisService.key.WX_GZH_QRCODE_LOGIN.getTimeout());
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveGzhUserRelation(MsgEvent msgEvent, Long userId, String openId, String unionid) {
|
|
|
+ UserGzhRelation userGzhRelation = new UserGzhRelation();
|
|
|
+ userGzhRelation.setUserId(userId);
|
|
|
+
|
|
|
+ WxApp wxApp = wxAppMapper.selectOne(Wrappers.lambdaQuery(WxApp.class)
|
|
|
+ .eq(WxApp::getAppId, msgEvent.getToUserName()).last("limit 1"));
|
|
|
+ userGzhRelation.setWxAppId(wxApp.getId());
|
|
|
+ userGzhRelation.setIsSubscribe(true);
|
|
|
+ userGzhRelation.setEvent(msgEvent.getEvent());
|
|
|
+ userGzhRelation.setOpenId(openId);
|
|
|
+ userGzhRelation.setUnionid(unionid);
|
|
|
+ if (StrUtil.isNotEmpty(msgEvent.getEventKey())) {
|
|
|
+ userGzhRelation.setIsQrScene(true);
|
|
|
+ }
|
|
|
+ userGzhRelationMapper.insert(userGzhRelation);
|
|
|
+ }
|
|
|
+}
|