Ver Fonte

Merge branch 'huangyuan-user' of lift-manager/lift-server into develop

huangyuan há 5 anos atrás
pai
commit
352632546c

+ 6 - 1
lift-common/src/main/java/cn.com.ty.lift.common/constants/WebSocketConstants.java

@@ -7,6 +7,11 @@ package cn.com.ty.lift.common.constants;
  */
 public class WebSocketConstants {
 
+    /**
+     * 消息推送目的地
+     */
+    public static final String PUSH_MESSAGE_TO_PC = "pushMessageToPC";
+
     /**
      * 推送当前用户信息
      */
@@ -44,7 +49,7 @@ public class WebSocketConstants {
         int EMERGENCY_MSG = 4;
 
         /**
-         * 年检
+         * 年检消息
          */
         int INSPECTION_MSG = 5;
 

+ 27 - 4
lift-common/src/main/java/cn.com.ty.lift.common/model/TimeyMessage.java

@@ -1,8 +1,10 @@
 package cn.com.ty.lift.common.model;
 
+import cn.com.ty.lift.common.constants.WebSocketConstants;
 import lombok.Data;
 
-import java.time.LocalDateTime;
+import java.io.Serializable;
+import java.time.LocalDate;
 
 /**
  * @author huangyuan
@@ -10,7 +12,9 @@ import java.time.LocalDateTime;
  * @description 实时推送消息实体类
  */
 @Data
-public class TimeyMessage {
+public class TimeyMessage implements Serializable {
+
+    private static final long serialVersionUID = 4125096758372084309L;
 
     /**
      * 消息id
@@ -18,7 +22,7 @@ public class TimeyMessage {
     private Long id;
 
     /**
-     * 用户id,推送的用户id
+     * 用户id,推送的用户id
      */
     private Long userId;
 
@@ -45,5 +49,24 @@ public class TimeyMessage {
     /**
      * 创建时间
      */
-    private LocalDateTime createTime;
+    private LocalDate createTime;
+
+    /**
+     * @param pushToUserId 要推送的用户id
+     * @param createUserId 申请的用户id
+     * @return 返回推送消息
+     * @description 推送消息创建
+     * @date 2020/4/30 5:13 下午
+     */
+    public static TimeyMessage create(long pushToUserId, long createUserId, int msgType, String content) {
+        TimeyMessage timeyMessage = new TimeyMessage();
+        timeyMessage.setContent(content);
+        timeyMessage.setUserId(pushToUserId);
+        timeyMessage.setType(msgType);
+        timeyMessage.setCreateTime(LocalDate.now());
+        timeyMessage.setCreateUserId(createUserId);
+        timeyMessage.setViewFlag(WebSocketConstants.IS_NOT_VIEW);
+        return timeyMessage;
+    }
+
 }

+ 57 - 0
lift-common/src/main/java/cn.com.ty.lift.common/utils/SendMessageUtil.java

@@ -0,0 +1,57 @@
+package cn.com.ty.lift.common.utils;
+
+import cn.com.ty.lift.common.constants.WebSocketConstants;
+import cn.com.ty.lift.common.model.TimeyMessage;
+import org.springframework.jms.core.JmsMessagingTemplate;
+import org.springframework.messaging.support.GenericMessage;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author huangyuan
+ * @date 2020/5/2
+ * @description 消息推送类
+ */
+public class SendMessageUtil {
+
+
+    /**
+     * @param jmsMessagingTemplate 推送消息模板
+     * @param sendToUserIdList     要推送的用户id
+     * @param content              推送的消息内容
+     * @param createUserId         消息创建人id
+     * @return 是否推送成功 false 失败 true 成功
+     * @description
+     * @date 2020/5/2 4:11 下午
+     */
+    public static boolean sendMessageToPC(JmsMessagingTemplate jmsMessagingTemplate, List<Long> sendToUserIdList,
+                                          String content, long createUserId) {
+        if (sendToUserIdList != null && sendToUserIdList.size() > 0) {
+            //组合消息
+            List<TimeyMessage> timeyMessageList = new ArrayList<>();
+            sendToUserIdList.forEach(sendToUserId -> {
+                timeyMessageList.add(TimeyMessage.create(sendToUserId, createUserId,
+                        WebSocketConstants.MessageType.TEAM_MSG, content));
+            });
+            //推送消息
+            return SendMessageUtil.sendMessageToPC(jmsMessagingTemplate, timeyMessageList);
+        }
+        return false;
+    }
+
+    /**
+     * @param jmsMessagingTemplate 推动模板类
+     * @param timeyMessageList     要推送的消息列表
+     * @return 是否成功 false 失败 true 成功
+     * @description 将消息推送到PC端
+     * @date 2020/5/2 3:38 下午
+     */
+    private static boolean sendMessageToPC(JmsMessagingTemplate jmsMessagingTemplate, List<TimeyMessage> timeyMessageList) {
+        if (timeyMessageList != null && timeyMessageList.size() > 0) {
+            jmsMessagingTemplate.send(WebSocketConstants.PUSH_MESSAGE_TO_PC, new GenericMessage<>(timeyMessageList));
+            return true;
+        }
+        return false;
+    }
+}

+ 22 - 10
lift-push/src/main/java/cn/com/ty/lift/push/app/PushConsumer.java

@@ -1,7 +1,11 @@
 package cn.com.ty.lift.push.app;
 
+import cn.com.ty.lift.common.constants.WebSocketConstants;
 import cn.com.ty.lift.common.model.PushMessage;
+import cn.com.ty.lift.common.model.TimeyMessage;
 import cn.com.ty.lift.common.utils.ValuePool;
+import cn.com.ty.lift.push.websocket.handler.TimeyMessageHandler;
+import cn.com.ty.lift.push.websocket.service.TimeyMessageService;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.jms.annotation.JmsListener;
@@ -23,13 +27,14 @@ import java.util.List;
 @AllArgsConstructor
 public class PushConsumer {
 
-    private XingePush            xingePush;
+    private XingePush xingePush;
     private JmsMessagingTemplate jmsMessagingTemplate;
+    private TimeyMessageService timeyMessageService;
 
     private class Processor {
-        private String      destination;
+        private String destination;
         private PushMessage pushMessage;
-        private boolean     result;
+        private boolean result;
 
         private Processor(String destination, PushMessage pushMessage) {
             this.destination = destination;
@@ -68,39 +73,46 @@ public class PushConsumer {
         }
     }
 
-    private void process(String destination, PushMessage pushMessage){
-        if(pushMessage.needSplit()){
+    private void process(String destination, PushMessage pushMessage) {
+        if (pushMessage.needSplit()) {
             List<ArrayList<String>> doSplit = pushMessage.doSplit(ValuePool.PUSH_MAX_SIZE);
             for (ArrayList<String> list : doSplit) {
                 pushMessage.setToList(list);
                 new Processor(destination, pushMessage).perform().tryAgain();
             }
-        }else{
+        } else {
             new Processor(destination, pushMessage).perform().tryAgain();
         }
     }
 
     @JmsListener(destination = ValuePool.PUSH_QUEUE_ANDROID_TOKEN)
     public void listenAndroidTokenQueue(PushMessage pushMessage) {
-        log.info("listen {} receive message:{}",ValuePool.PUSH_QUEUE_ANDROID_TOKEN, pushMessage);
+        log.info("listen {} receive message:{}", ValuePool.PUSH_QUEUE_ANDROID_TOKEN, pushMessage);
         process(ValuePool.PUSH_QUEUE_ANDROID_TOKEN, pushMessage);
     }
 
     @JmsListener(destination = ValuePool.PUSH_QUEUE_ANDROID_ALL)
     public void listenAndroidAllQueue(PushMessage pushMessage) {
-        log.info("listen {} receive message:{}",ValuePool.PUSH_QUEUE_ANDROID_ALL, pushMessage);
+        log.info("listen {} receive message:{}", ValuePool.PUSH_QUEUE_ANDROID_ALL, pushMessage);
         process(ValuePool.PUSH_QUEUE_ANDROID_ALL, pushMessage);
     }
 
     @JmsListener(destination = ValuePool.PUSH_QUEUE_IOS_TOKEN)
     public void listenIosTokenQueue(PushMessage pushMessage) {
-        log.info("listen {} receive message:{}",ValuePool.PUSH_QUEUE_IOS_TOKEN, pushMessage);
+        log.info("listen {} receive message:{}", ValuePool.PUSH_QUEUE_IOS_TOKEN, pushMessage);
         process(ValuePool.PUSH_QUEUE_IOS_TOKEN, pushMessage);
     }
 
     @JmsListener(destination = ValuePool.PUSH_QUEUE_IOS_ALL)
     public void listenIosAllQueue(PushMessage pushMessage) {
-        log.info("listen {} receive message:{}",ValuePool.PUSH_QUEUE_IOS_ALL, pushMessage);
+        log.info("listen {} receive message:{}", ValuePool.PUSH_QUEUE_IOS_ALL, pushMessage);
         process(ValuePool.PUSH_QUEUE_IOS_ALL, pushMessage);
     }
+
+    @JmsListener(destination = WebSocketConstants.PUSH_MESSAGE_TO_PC)
+    public void listenPCAllQueue(List<TimeyMessage> timeyMessageList) {
+        log.info("listen {} receive message: {}", WebSocketConstants.PUSH_MESSAGE_TO_PC, timeyMessageList);
+        TimeyMessageHandler timeyMessageHandler = new TimeyMessageHandler(timeyMessageService);
+        timeyMessageHandler.saveAndPushMessageToUsers(timeyMessageList);
+    }
 }

+ 20 - 0
lift-push/src/main/java/cn/com/ty/lift/push/websocket/handler/TimeyMessageHandler.java

@@ -53,6 +53,26 @@ public class TimeyMessageHandler implements WebSocketHandler {
 
     }
 
+    /**
+     * @param timeyMessages 推送的消息
+     * @return 是否推送成功
+     * @description 保存用户信息,并将消息推送给多个用户
+     * @date 2020/5/2 4:28 下午
+     */
+    public boolean saveAndPushMessageToUsers(List<TimeyMessage> timeyMessages) {
+        if (timeyMessages != null && timeyMessages.size() > 0) {
+            //先将即使消息保存到数据库
+            if (timeyMessageService.saveBatch(timeyMessages)) {
+                List<Long> userIds = new ArrayList<>();
+                timeyMessages.forEach(timeyMessage -> {
+                    userIds.add(timeyMessage.getUserId());
+                });
+                return pushMessageToUsers(userIds, timeyMessages);
+            }
+        }
+        return false;
+    }
+
     /**
      * @return boolean
      * @Author huangyuan

+ 3 - 1
lift-system-service/src/main/java/cn/com/ty/lift/system/user/controller/LoginController.java

@@ -286,9 +286,11 @@ public class LoginController {
             return RestResponse.fail(ApiConstants.RESULT_ERROR, "授权码为空无法进行操作");
         }
         //通过授权码获取用户openId
-        String openId = WxUtil.getAppOpenIdByAuthCode(authCode);
+        String openId = "";
         if (WxConstants.APPLET_LOGIN.equals(loginStyle)) {
             openId = WxUtil.getAppletOpenIdByAuthCode(authCode);
+        } else {
+            openId = WxUtil.getAppOpenIdByAuthCode(authCode);
         }
         if (StringUtils.isBlank(openId)) {
             return RestResponse.fail(ApiConstants.RESULT_ERROR, "授权码无效无法进行操作");

+ 53 - 8
lift-system-service/src/main/java/cn/com/ty/lift/system/user/service/impl/UserApplicationService.java

@@ -3,9 +3,13 @@ package cn.com.ty.lift.system.user.service.impl;
 import cn.com.ty.lift.common.constants.ApiConstants;
 import cn.com.ty.lift.common.constants.CommonEnum;
 import cn.com.ty.lift.common.utils.ProjectUtils;
+import cn.com.ty.lift.common.utils.SendMessageUtil;
 import cn.com.ty.lift.system.constants.CommonConstants;
+import cn.com.ty.lift.system.settings.dao.entity.MaintenanceCompany;
+import cn.com.ty.lift.system.settings.service.IMaintenanceCompanyService;
 import cn.com.ty.lift.system.user.dao.entity.MtCompanyUser;
 import cn.com.ty.lift.system.user.dao.entity.UserApplication;
+import cn.com.ty.lift.system.user.dao.entity.UserInfo;
 import cn.com.ty.lift.system.user.dao.entity.UserRole;
 import cn.com.ty.lift.system.user.dao.entity.model.*;
 import cn.com.ty.lift.system.user.dao.mapper.UserApplicationMapper;
@@ -17,8 +21,10 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.apache.commons.lang3.StringUtils;
+import org.springframework.jms.core.JmsMessagingTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.transaction.interceptor.TransactionAspectSupport;
 
 import javax.annotation.Resource;
 import java.time.LocalDateTime;
@@ -38,21 +44,34 @@ public class UserApplicationService extends ServiceImpl<UserApplicationMapper, U
     @Resource
     private IMtCompanyUserService mtCompanyUserService;
 
+    @Resource
+    private IMaintenanceCompanyService maintenanceCompanyService;
+
     @Resource
     private IUserRoleService userRoleService;
 
     @Resource
     private IUserService userService;
+
+    @Resource
+    private IUserInfoService userInfoService;
+
     @Resource
     private IProjectService projectService;
 
+    @Resource
+    private JmsMessagingTemplate jmsMessagingTemplate;
+
     @Override
     public RestResponse apply(UserApplyRequest userApplyRequest, int applyTypeTeam) {
+        //申请公司id
         long companyId = userApplyRequest.getCompanyId();
+        //申请人id
+        long applyUserId = userApplyRequest.getUserId();
         //获取用户原有的申请信息
         UserApplication userApplication = this.getOne(new QueryWrapper<UserApplication>()
                 .eq("mt_company_id", companyId)
-                .eq("user_id", userApplyRequest.getUserId())
+                .eq("user_id", applyUserId)
                 //去掉申请未通过的判断
                 .ne("status", ApiConstants.ApplicationConstants.APPLY_FAIL)
         );
@@ -65,7 +84,7 @@ public class UserApplicationService extends ServiceImpl<UserApplicationMapper, U
         //校验通过设置审核信息
         if (userApplication == null) {
             userApplication = new UserApplication();
-            userApplication.setUserId(userApplyRequest.getUserId());
+            userApplication.setUserId(applyUserId);
             userApplication.setMtCompanyId(companyId);
             userApplication.setType(applyTypeTeam);
             //设置审核描述信息
@@ -78,17 +97,16 @@ public class UserApplicationService extends ServiceImpl<UserApplicationMapper, U
         //申请加入项目时需要设置的信息
         userApplication.setPowerType(userApplyRequest.getPowerType());
         boolean result = this.saveOrUpdate(userApplication);
+
         if (result) {
             //申请操作成功,推送消息
             if (ApiConstants.ApplicationConstants.APPLY_TYPE_TEAM == applyTypeTeam) {
-                //申请加入团队成功,给公司所有文员推送信息
-                //1.获取所有文员信息
-                List<Long> clerkIds = userService.getUserIdByCompanyIdAndRoleCode(companyId,
-                        CommonEnum.DefaultRole.CLERK.getCode());
-                if (clerkIds != null && clerkIds.size() > 0) {
-
+                RestResponse pushMessageResponse = applyTeamPushMessage(companyId, applyUserId);
+                if (pushMessageResponse != null) {
+                    return pushMessageResponse;
                 }
             } else {
+                //申请加入团队,将消息推送到app端
 
             }
         } else {
@@ -319,4 +337,31 @@ public class UserApplicationService extends ServiceImpl<UserApplicationMapper, U
         projectService.saveProjectUser(projectUser);
     }
 
+    /**
+     * @param companyId   公司id
+     * @param applyUserId 申请人id
+     * @return 推送信息
+     * @description 申请加入团队将消息推送给pc端文员
+     * @date 2020/5/2 4:18 下午
+     */
+    private RestResponse applyTeamPushMessage(long companyId, long applyUserId) {
+        //组合消息内容
+        UserInfo userInfo = userInfoService.getByUserId(applyUserId);
+        if (userInfo == null) {
+            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
+            return RestResponse.fail(ApiConstants.RESULT_ERROR, "用户信息不存在,无法申请");
+        }
+        MaintenanceCompany maintenanceCompany = maintenanceCompanyService.getById(companyId);
+        if (maintenanceCompany == null) {
+            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
+            return RestResponse.fail(ApiConstants.RESULT_ERROR, "申请加入的团队不存在");
+        }
+        String content = userInfo.getName() + "申请加入" + maintenanceCompany.getName();
+        //申请加入团队成功,给公司所有文员推送信息
+        List<Long> clerkIds = userService.getUserIdByCompanyIdAndRoleCode(companyId,
+                CommonEnum.DefaultRole.CLERK.getCode());
+        SendMessageUtil.sendMessageToPC(jmsMessagingTemplate, clerkIds, content, applyUserId);
+        return null;
+    }
+
 }

+ 21 - 7
lift-system-service/src/main/java/cn/com/ty/lift/system/wechat/constants/WxConstants.java

@@ -17,25 +17,39 @@ public class WxConstants {
      */
     public static final String APP_LOGIN = "appLogin";
 
+
     /**
      * app登录配置常量
      */
     public interface AppConfigConstants {
-        String APP_ID = "wx0f10e6386fb9969e";
-        String APP_SECRET = "d4624c36b6795d1d99dcf0547af5443d";
-        String USER_INFO_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?"
+
+        String OPEN_ID_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?"
                 + "appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
 
+        /**
+         * appId
+         */
+        String APP_ID = "wx0f10e6386fb9969e";
+        /**
+         * appSecret
+         */
+        String APP_SECRET = "d4624c36b6795d1d99dcf0547af5443d";
     }
 
     /**
      * 小程序登录配置常量
      */
     public interface AppletConfigConstants {
-        String APP_ID = "wx9183e5fa00c9614a";//小程序appId
-        String APP_SECRET = "2e4c634a4f1f54a871e79da9d6f1179e";//小程序secret
-        String AUTHORIZED_CODE_STR = "code";
-        String AUTH_URL = "https://api.weixin.qq.comI/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
+
+        String OPEN_ID_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
+        /**
+         * 小程序appId
+         */
+        String APP_ID = "wx9183e5fa00c9614a";
+        /**
+         * 小程序secret
+         */
+        String APP_SECRET = "2e4c634a4f1f54a871e79da9d6f1179e";
     }
 
 }

+ 19 - 12
lift-system-service/src/main/java/cn/com/ty/lift/system/wechat/utils/WxUtil.java

@@ -18,15 +18,9 @@ public class WxUtil {
      * @description app端通过用户授权码获取用户openId
      */
     public static String getAppOpenIdByAuthCode(String authCode) {
-        String url = WxConstants.AppConfigConstants.USER_INFO_ACCESS_TOKEN_URL
-                .replace("APPID", WxConstants.AppConfigConstants.APP_ID)
-                .replace("SECRET", WxConstants.AppConfigConstants.APP_SECRET)
-                .replace("CODE", authCode);
-        JSONObject result = JSONUtil.parseObj(HttpUtil.get(url));
-        if (result.containsKey("openid")) {
-            return result.getStr("openid");
-        }
-        return null;
+        return getOpenIdByAuthCode(authCode, WxConstants.AppConfigConstants.OPEN_ID_URL,
+                WxConstants.AppConfigConstants.APP_ID,
+                WxConstants.AppConfigConstants.APP_SECRET);
     }
 
     /**
@@ -36,9 +30,22 @@ public class WxUtil {
      * @date 2020/4/27 10:36 上午
      */
     public static String getAppletOpenIdByAuthCode(String authCode) {
-        String url = WxConstants.AppletConfigConstants.AUTH_URL
-                .replace("APPID", WxConstants.AppletConfigConstants.APP_ID)
-                .replace("SECRET", WxConstants.AppletConfigConstants.APP_SECRET)
+        return getOpenIdByAuthCode(authCode, WxConstants.AppletConfigConstants.OPEN_ID_URL,
+                WxConstants.AppletConfigConstants.APP_ID,
+                WxConstants.AppletConfigConstants.APP_SECRET);
+    }
+
+    /**
+     * @param authCode  授权码
+     * @param appId     appId
+     * @param appSecret appSecret
+     * @return openId
+     * @description 通过授权码获取微信openId
+     * @date 2020/4/30 2:47 下午
+     */
+    private static String getOpenIdByAuthCode(String authCode, String url, String appId, String appSecret) {
+        url = url.replace("APPID", appId)
+                .replace("SECRET", appSecret)
                 .replace("CODE", authCode);
         JSONObject result = JSONUtil.parseObj(HttpUtil.get(url));
         if (result.containsKey("openid")) {