Parcourir la source

web mqtt 跌倒消息推送

chejianzheng il y a 3 mois
Parent
commit
4d3bb86720

+ 20 - 0
portal-service-common/src/main/java/com/hfln/portal/common/dto/data/user/SendMsgUserDto.java

@@ -0,0 +1,20 @@
+package com.hfln.portal.common.dto.data.user;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+@Data
+public class SendMsgUserDto implements Serializable {
+
+    private Long userId;
+
+    // 发送电话的手机号
+    private String phone;
+
+    // 发送微信服务号的unionId
+    private String unionId;
+
+    // 发送微信服务号的openId
+    private String fwhOpenId;
+}

+ 2 - 1
portal-service-common/src/main/java/com/hfln/portal/common/dto/data/user/UserDto.java

@@ -3,11 +3,12 @@ package com.hfln.portal.common.dto.data.user;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.Data;
 
+import java.io.Serializable;
 import java.time.LocalDateTime;
 
 @Data
 @Schema(description = "用户信息")
-public class UserDto {
+public class UserDto implements Serializable {
 
     @Schema(description = "用户ID")
     private Long userId;

+ 42 - 21
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/mqtt/MqttSubHandle.java

@@ -7,6 +7,7 @@ import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.hfln.portal.common.constant.mqtt.topic.TopicConstants;
 import com.hfln.portal.common.constant.redis.RedisCacheConstant;
+import com.hfln.portal.common.dto.data.user.SendMsgUserDto;
 import com.hfln.portal.domain.customer.util.MsgClient;
 import com.hfln.portal.domain.customer.util.WxOfficeAccountClient;
 import com.hfln.portal.infrastructure.po.*;
@@ -190,22 +191,32 @@ public class MqttSubHandle {
         msg.put("event", event);
         msg.put("msgType", "event");
 
-        // todo 发送socket
+        // 发送socket
         mqttClient.sendMessage(String.format("/mps/%s/event", clientId), msg.toString());
-//        PushMsgWebSocket.sendMessageTo(msg.toString(), dev_id);
 
         // 发送短信
         if (event.equals("fall_confirmed")) {
             log.info("mqttutil--有跌倒事件");
             List<Long> userIds = new ArrayList<>();
-            StringBuffer devName = new StringBuffer("");
-            String devId = "";
-            // 设备拥有者openid
-            if (dev != null) {
-                userIds.add(dev.getUserId());
-                devName.append(dev.getDevName());
-                devId = dev.getClientId();
+            List<SendMsgUserDto> sendList = new ArrayList<>();
+            String devName = dev.getDevName();
+            String devId = dev.getClientId();
+            Long userId = dev.getUserId();
+
+            // 需要发送提示的 有 当前设备拥有者, 被分享者, 以及 对当前设备 具有管理权限的 web管理用户
+            // 1 小程序拥有者
+            UserInfo userInfo = userService.queryById(userId);
+            SendMsgUserDto msgUserDto = new SendMsgUserDto();
+            msgUserDto.setUserId(userId);
+            msgUserDto.setPhone(userInfo.getPhone());
+            msgUserDto.setUnionId(userInfo.getUnionId());
+            WxRelation wxRelation = wxRelationService.queryOneByUnionId(userInfo.getUnionId());
+            if (wxRelation != null) {
+                msgUserDto.setFwhOpenId(wxRelation.getFwhOpenId());
             }
+            sendList.add(msgUserDto);
+
+            // 2 todo 被分享者
 
             // 针对跌倒事件 发送 网页 主题消息提示
             String tenantName = "";
@@ -229,40 +240,50 @@ public class MqttSubHandle {
                 if (!CollectionUtils.isEmpty(adminUserInfos)) {
                     for (AdminUserInfo adminUserInfo : adminUserInfos) {
 
+                        // 3 对当前设备 具有管理权限的 web管理用户
+                        // todo 确定网页 管理用户 是否有 短信 服务号发送 功能
+//                        SendMsgUserDto sendDto = new SendMsgUserDto();
+//                        sendDto.setUserId(adminUserInfo.getUserId());
+//                        sendDto.setPhone(adminUserInfo.getPhone());
+////                        sendDto.setUnionId(adminUserInfo.getUnionId());
+////                        WxRelation wxRelation = wxRelationService.queryOneByUnionId(userInfo.getUnionId());
+////                        if (wxRelation != null) {
+////                            msgUserDto.setFwhOpenId(wxRelation.getFwhOpenId());
+////                        }
+//                        sendList.add(sendDto);
+
                         // 判断当前用户是否登录网页
                         if (redisUtil.sIsMember(RedisCacheConstant.MQTT_CLIENT_USERID, RedisCacheConstant.WEB_USER_PRE + adminUserInfo.getUserId())) {
+                            log.info("发送网页跌倒主题消息:topic:{}, msg:{}", String.format(TopicConstants.TOPIC_MPS_WEB_NOTIC, RedisCacheConstant.WEB_USER_PRE + adminUserInfo.getUserId()), webMsg.toString());
                             mqttClient.sendMessage(String.format(TopicConstants.TOPIC_MPS_WEB_NOTIC, RedisCacheConstant.WEB_USER_PRE + adminUserInfo.getUserId()), webMsg.toString(), 2, false);
                         }
                     }
                 }
             }
 
-            if (!userIds.isEmpty()) {
-                // 拥有者和被分享者phone
-                List<UserInfo> userInfos = userService.listByIds(userIds);
-                for (UserInfo user : userInfos) {
+            if (!CollectionUtils.isEmpty(sendList)) {
+                for (SendMsgUserDto sendDto : sendList) {
                     // 发送跌倒短信
                     log.info("mqttutil--开始发送跌倒消息");
-                    log.info("mqttutil--sendmsg:phone{}, dev_name:{}", user.getPhone(), devName.toString());
-                    msgClient.sendNotifyMsg(user.getPhone(), devName.toString());
+                    log.info("mqttutil--sendmsg:phone{}, dev_name:{}", sendDto.getPhone(), devName.toString());
+                    msgClient.sendNotifyMsg(sendDto.getPhone(), devName.toString());
                     log.info("mqttUtil--消息发完了");
 
                     // 发送微信公众号消息
-                    List<WxRelation> wxRelations = wxRelationService.queryByUnionId(user.getUnionId());
+                    List<WxRelation> wxRelations = wxRelationService.queryByUnionId(sendDto.getUnionId());
                     String fwhOpenId = "";
                     if (wxRelations != null && !wxRelations.isEmpty()) {
                         fwhOpenId = wxRelations.get(0).getFwhOpenId();
                     }
-                    log.info("mqttutil--当前useropenid=" + user.getOpenid() + ", fwhopenId=" + fwhOpenId);
-                    log.info("发送微信公众号信息:devName=" + devName.toString() + ", phoneNo=" + user.getPhone() + "fwhOpenId=" + fwhOpenId);
+//                        log.info("mqttutil--当前useropenid=" + sendDto.getOpenid() + ", fwhopenId=" + fwhOpenId);
+                    log.info("发送微信公众号信息:devName=" + devName.toString() + ", phoneNo=" + sendDto.getPhone() + "fwhOpenId=" + fwhOpenId);
                     // 发送微信公告号消息
-                    wxOfficeAccountClient.sendMsg(devId, devName.toString(), user.getPhone(), fwhOpenId, "设备检测到跌倒,请前往小程序查看详细信息");
+                    wxOfficeAccountClient.sendMsg(devId, devName.toString(), sendDto.getPhone(), fwhOpenId, "设备检测到跌倒,请前往小程序查看详细信息");
                     log.info("发送微信公众号消息发完了");
                 }
-                }
-
             }
         }
+    }
 
     public void subDasAlarmEvent(String topic, String payload) {
 

+ 0 - 6
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/po/UserInfo.java

@@ -19,12 +19,6 @@ public class UserInfo extends BasePO {
     private Long userId;
 
     /**
-     * 租户表主键id
-     */
-    @TableField("tenant_id")
-    private Long tenantId;
-
-    /**
      * 用户openid,唯一标识
      */
     private String openid;

+ 1 - 0
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/WxRelationService.java

@@ -8,5 +8,6 @@ import java.util.List;
 public interface WxRelationService extends IService<WxRelation> {
 
     List<WxRelation> queryByUnionId(String unionId);
+    WxRelation queryOneByUnionId(String unionId);
     List<WxRelation> queryByUnionIdAndOpenId(String unionId, String fwhOpenId);
 }

+ 6 - 0
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/impl/WxRelationServiceImpl.java

@@ -21,6 +21,12 @@ public class WxRelationServiceImpl extends ServiceImpl<WxRelationMapper, WxRelat
     }
 
     @Override
+    public WxRelation queryOneByUnionId(String unionId) {
+        return this.baseMapper.selectOne(new LambdaQueryWrapper<WxRelation>().eq(WxRelation::getUnionId, unionId).eq(WxRelation::getIsDeleted, BasePO.DeleteFlag.NOT_DELETED));
+
+    }
+
+    @Override
     public List<WxRelation> queryByUnionIdAndOpenId(String unionId, String fwhOpenId) {
         return this.baseMapper.selectList(new LambdaQueryWrapper<WxRelation>().eq(WxRelation::getUnionId, unionId)
                 .eq(WxRelation::getFwhOpenId, fwhOpenId)