瀏覽代碼

推送消息

humuyu 3 年之前
父節點
當前提交
fdacacb863

+ 12 - 3
eladmin-security/src/main/java/me/zhengjie/security/service/OnlineUserService.java

@@ -39,7 +39,7 @@ import java.util.*;
  * @author Zheng Jie
  * @date 2019年10月26日21:56:27
  */
-@Service
+//@Service
 @Slf4j
 public class OnlineUserService {
 
@@ -322,14 +322,14 @@ public class OnlineUserService {
 	 * 
 	 * @param username /
 	 */
-	@Async
+	//@Async
 	public void kickOutForUsername(String username) throws Exception {
 		List<OnlineUserDto> onlineUsers = getAll(username, OnlineUserConstant.SEARCH_USER_WITH_USERNAME);
 		for (OnlineUserDto onlineUser : onlineUsers) {
 			if (onlineUser.getUsername().equals(username)) {
 				String token = EncryptUtils.desDecrypt(onlineUser.getKey());
 				kickOut(token);
-
+				kickSendMsg(username);
 			}
 		}
 	}
@@ -338,6 +338,15 @@ public class OnlineUserService {
 	 * 
 	 * @param username /
 	 */
+	
+	public void kickSendMsg(String username) throws Exception {
+		
+	}
+	/**
+	 * 根据用户名强退用户
+	 * 
+	 * @param username /
+	 */
 	@Async
 	public void kickOutForPhoneUsername(String loginPhone) {
 		Map<String, String> map = getAppAllLoginName();

+ 39 - 5
eladmin-system/src/main/java/me/zhengjie/application/bank/service/impl/BankLoginServiceImpl.java

@@ -1,20 +1,31 @@
 package me.zhengjie.application.bank.service.impl;
 
 import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.corundumstudio.socketio.SocketIOClient;
+
 import lombok.RequiredArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
 import me.zhengjie.application.bank.controller.vo.BankLoginVO;
 import me.zhengjie.application.bank.service.BankLoginService;
 import me.zhengjie.application.bank.service.SysUserService;
 import me.zhengjie.base.ResponseDTO;
 import me.zhengjie.base.ResultCode;
+import me.zhengjie.base.websocket.AppPcBindSocketIo;
+import me.zhengjie.base.websocket.AppSocketClientCache;
+import me.zhengjie.dao.mybatis.entity.ContractOrderEntity;
 import me.zhengjie.dao.mybatis.entity.SysUserEntity;
+import me.zhengjie.dao.mybatis.mapper.ContractOrderMapper;
 import me.zhengjie.exception.AccountNoActivationException;
 import me.zhengjie.security.security.TokenProvider;
 import me.zhengjie.security.service.OnlineUserService;
-
-import java.util.Map;
-
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.UUID;
+import java.util.stream.Collectors;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.collections4.MapUtils;
 import org.springframework.stereotype.Service;
 
 /**
@@ -24,12 +35,15 @@ import org.springframework.stereotype.Service;
  *
  */
 @Service
-@Slf4j
+
 @RequiredArgsConstructor
 public class BankLoginServiceImpl implements BankLoginService {
 	private final SysUserService userService;
 	private final TokenProvider tokenProvider;
 	private final OnlineUserService onlineUserService;
+	private final ContractOrderMapper order;
+
+	private final AppPcBindSocketIo appPcBindSocketIo;
 
 	@Override
 	public ResponseDTO<BankLoginVO> login(BankLoginVO loginVo) {
@@ -43,6 +57,25 @@ public class BankLoginServiceImpl implements BankLoginService {
 			}
 			// 先移除已经登录的账号
 			onlineUserService.kickOutForPhoneUsername(loginVo.getPhone());
+			// 查询这个人的订单
+			QueryWrapper<ContractOrderEntity> qw = new QueryWrapper<>();
+			qw.select("business_no").eq("customer_id", sysUser.getUserId());
+			List<String> orderlist = order.selectList(qw).stream().map(ContractOrderEntity::getBusinessNo)
+					.collect(Collectors.toList());
+			List<String> keys = appPcBindSocketIo.getAllBindBusinessNo();
+			if (CollectionUtils.isNotEmpty(orderlist) && CollectionUtils.isNotEmpty(keys)) {
+				Collection<String> intersection = CollectionUtils.intersection(orderlist, keys);
+				if (CollectionUtils.isNotEmpty(keys)) {
+					intersection.forEach(key -> {
+						HashMap<UUID, SocketIOClient> userClient = AppSocketClientCache.getAppUserClient(key);
+						if (MapUtils.isNotEmpty(userClient)) {
+							userClient.forEach((uuid, socketIOClient) -> {
+								socketIOClient.sendEvent("logoutEvent", "logout");
+							});
+						}
+					});
+				}
+			}
 			String token = tokenProvider.appToken(loginVo.getPhone());
 			tokenProvider.setTokenTime(token, JSONObject.toJSONString(sysUser));
 			BankLoginVO bankLoginVO = new BankLoginVO();
@@ -56,4 +89,5 @@ public class BankLoginServiceImpl implements BankLoginService {
 	public void logout(String authToken) {
 		tokenProvider.delToken(authToken);
 	}
+	
 }

+ 13 - 6
eladmin-system/src/main/java/me/zhengjie/base/websocket/AppPcBindSocketIo.java

@@ -19,7 +19,6 @@ import com.alibaba.fastjson.JSONObject;
 import com.corundumstudio.socketio.SocketIOClient;
 
 import lombok.extern.slf4j.Slf4j;
-import me.zhengjie.application.admin.controller.OrderRoomIdController;
 import me.zhengjie.utils.RedisUtils;
 
 /**
@@ -31,6 +30,7 @@ import me.zhengjie.utils.RedisUtils;
 @Slf4j
 public class AppPcBindSocketIo {
 	public final static String PREFIX_BUSINESS_NO = "business-no-";
+	public final static String PREFIX_SOCKET_IO = "socket-io-";
 	@Autowired
 	RedisUtils redisUtils;
 
@@ -124,7 +124,7 @@ public class AppPcBindSocketIo {
 			// 直接删除这条数据
 			delBusinessNo(businessNo);
 			// TDOO 发送给app。取消了视频通话
-			log.info("推送给app消息 {} 当前公证员 {}", businessNo,username);
+			log.info("推送给app消息 {} 当前公证员 {}", businessNo, username);
 			sendAppMsg(businessNo);
 		} else {
 			notarys.remove(username);
@@ -133,7 +133,10 @@ public class AppPcBindSocketIo {
 		}
 
 	}
-
+	public List<String> getAllBindBusinessNo() {
+		List<String> keys = redisUtils.scan(PREFIX_BUSINESS_NO + "*");
+		return keys;
+	}
 	public Set<String> getAllBindNotary() {
 		List<String> keys = redisUtils.scan(PREFIX_BUSINESS_NO + "*");
 		Set<String> set = new HashSet<>();
@@ -148,11 +151,12 @@ public class AppPcBindSocketIo {
 		return set;
 	}
 
+	
 	public Set<String> disconnectNotary(String notary) {
 		List<String> keys = redisUtils.scan(PREFIX_BUSINESS_NO + "*");
 		Set<String> set = new HashSet<>();
 		// 先查询出业务编号
-		String businessNo = "";
+		String businessNo = null;
 		for (String key : keys) {
 			JSONArray json = JSON.parseArray((String) redisUtils.get(key));
 			for (int i = 0; i < json.size(); i++) {
@@ -162,6 +166,9 @@ public class AppPcBindSocketIo {
 					businessNo = key;
 			}
 		}
+		if (StringUtils.isEmpty(businessNo)) {
+			return set;
+		}
 		log.info("app失去连接:{} ", businessNo);
 		removeNotary(getBindNo(businessNo), notary);
 //		String key = (String) redisUtils.get(PREFIX_BUSINESS_NO + businessNo);
@@ -178,11 +185,11 @@ public class AppPcBindSocketIo {
 	}
 
 	public void socketIoTime(String socketId, String json) {
-		redisUtils.set(socketId, json, 60 * 5);
+		redisUtils.set(PREFIX_SOCKET_IO + socketId, json, 60 * 5);
 	}
 
 	public List<JSONObject> socketIoTime(String socketId) {
-		String json = (String) redisUtils.get(socketId);
+		String json = (String) redisUtils.get(PREFIX_SOCKET_IO + socketId);
 		List<JSONObject> jsonObj = JSONObject.parseArray(json, JSONObject.class);
 		return jsonObj;
 	}

+ 38 - 0
eladmin-system/src/main/java/me/zhengjie/base/websocket/PcOnlineUserService.java

@@ -0,0 +1,38 @@
+package me.zhengjie.base.websocket;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+import org.apache.commons.collections4.MapUtils;
+import org.springframework.stereotype.Service;
+
+import com.corundumstudio.socketio.SocketIOClient;
+
+import me.zhengjie.security.config.bean.SecurityProperties;
+import me.zhengjie.security.service.OnlineUserService;
+import me.zhengjie.utils.RedisUtils;
+
+@Service
+public class PcOnlineUserService extends OnlineUserService {
+
+	public PcOnlineUserService(SecurityProperties properties, RedisUtils redisUtils) {
+		super(properties, redisUtils);
+
+	}
+
+	/**
+	 * 根据用户名强退用户
+	 * 
+	 * @param username /
+	 */
+
+	public void kickSendMsg(String username) throws Exception {
+		Map<UUID, SocketIOClient> userClient = PcSocketClientCache.socketIOClient(username);
+		if (MapUtils.isNotEmpty(userClient)) {
+			userClient.forEach((uuid, socketIOClient) -> {
+				socketIOClient.sendEvent("logoutEvent", "close");
+			});
+		}
+	}
+}

+ 0 - 1
eladmin-system/src/main/java/me/zhengjie/base/websocket/SocketEventListenner.java

@@ -101,7 +101,6 @@ public class SocketEventListenner {
 
 	@OnEvent("pcRegisterEvent")
 	public void pcRegisterEvent(SocketIOClient client, AckRequest request, Map<String, String> map) throws Exception {
-		SpringContextHolder.getBean("onlineUserService");
 		// 在新增之前,先判断当前人是否
 		HandshakeData shakeData = client.getHandshakeData();
 		Map<String, List<String>> param = shakeData.getUrlParams();