登录部分

This commit is contained in:
wangw 2024-06-05 09:25:53 +08:00
parent fefb4c3a85
commit b7ce764bdb
4 changed files with 28 additions and 10 deletions

View File

@ -108,6 +108,9 @@ public class LoginFilter implements Filter {
}else if(environment.equals("wx")){ }else if(environment.equals("wx")){
//获取当前登录人的用户id //获取当前登录人的用户id
String openId = TokenUtil.parseParamFromToken(token).getString("openId"); String openId = TokenUtil.parseParamFromToken(token).getString("openId");
if(StringUtils.isBlank(openId)){
openId = TokenUtil.parseParamFromToken(token).getString("userId");
}
tokenKey=RedisCst.ONLINE_USER.concat(openId); tokenKey=RedisCst.ONLINE_USER.concat(openId);
} }
message = redisUtil.getMessage(tokenKey); message = redisUtil.getMessage(tokenKey);

View File

@ -98,7 +98,9 @@ public class LoginService {
} }
} }
else { else {
if(!userInfo.getSearchWord().contains("微信用户")){
userInfo.setSearchWord(userInfo.getSearchWord() + "||微信用户"); userInfo.setSearchWord(userInfo.getSearchWord() + "||微信用户");
}
userInfo.setMiniAppOpenId(openId); userInfo.setMiniAppOpenId(openId);
userInfo.setUpdatedAt(System.currentTimeMillis()); userInfo.setUpdatedAt(System.currentTimeMillis());
tbUserInfoMapper.updateByPrimaryKeySelective(userInfo); tbUserInfoMapper.updateByPrimaryKeySelective(userInfo);
@ -232,7 +234,7 @@ public class LoginService {
//生成token 信息 //生成token 信息
String token = null; String token = null;
try { try {
token = TokenUtil.generateToken(userInfo.getId(), null, userInfo.getTelephone(), userInfo.getNickName()); token = TokenUtil.generateToken(userInfo.getId(), userInfo.getMiniAppOpenId(), userInfo.getTelephone(), userInfo.getNickName());
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -240,7 +242,12 @@ public class LoginService {
try { try {
map.put("token", token); map.put("token", token);
map.put("userInfo", userInfo); map.put("userInfo", userInfo);
redisUtil.saveMessage(RedisCst.ONLINE_APP_USER.concat(userInfo.getId() + ""), JSON.toJSONString(map)); if(StringUtils.isNotBlank(userInfo.getMiniAppOpenId())){
redisUtil.saveMessage(RedisCst.ONLINE_USER.concat(userInfo.getMiniAppOpenId()), JSON.toJSONString(map),60*60*24*30L);
}else {
redisUtil.saveMessage(RedisCst.ONLINE_USER.concat(userInfo.getId() + ""), JSON.toJSONString(map),60*60*24*30L);
}
// redisUtil.saveMessage(RedisCst.ONLINE_APP_USER.concat(userInfo.getId() + ""), JSON.toJSONString(map),60*60*24*30L);
return Result.success(CodeEnum.SUCCESS, map); return Result.success(CodeEnum.SUCCESS, map);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -90,7 +90,7 @@ public class AppWebSocketServer {
String shopId = queryParams.get("shopId"); String shopId = queryParams.get("shopId");
String userId = queryParams.get("userId"); String userId = queryParams.get("userId");
log.info("建立连接参数 tableId:{} shopId:{} userId:{}",tableId,shopId,userId); log.info("建立连接参数 tableId:{} shopId:{} userId:{} sessionId:{}",tableId,shopId,userId,session.getId());
this.tableId = tableId; this.tableId = tableId;
this.shopId = shopId; this.shopId = shopId;
this.userId = userId; this.userId = userId;
@ -162,7 +162,7 @@ public class AppWebSocketServer {
@OnClose @OnClose
public void onClose() throws IOException { public void onClose() throws IOException {
String key=tableId + "-" + shopId; String key=tableId + "-" + shopId;
log.info("触发关闭操作 key为{} userId为{}",key,userId); log.info("触发关闭操作 key为{} userId为{} sessionId:{}",key,userId,session.getId());
if (webSocketMap.containsKey(key)) { if (webSocketMap.containsKey(key)) {
ConcurrentHashMap<String, AppWebSocketServer> userSocketMap = webSocketMap.get(key); ConcurrentHashMap<String, AppWebSocketServer> userSocketMap = webSocketMap.get(key);
// 在放置新条目之前检查并清除同名userId的数据 // 在放置新条目之前检查并清除同名userId的数据
@ -176,6 +176,7 @@ public class AppWebSocketServer {
// userMap.remove(tableId + "-" + shopId); // userMap.remove(tableId + "-" + shopId);
// } // }
} }
session.close();
} }
/** /**
@ -185,7 +186,7 @@ public class AppWebSocketServer {
*/ */
@OnMessage @OnMessage
public void onMessage(String message, Session session) { public void onMessage(String message, Session session) {
log.info("接收消息 tableId:{} shopId:{} userId:{} message:{}",this.tableId,this.shopId,this.userId,message); log.info("接收消息 tableId:{} shopId:{} userId:{} message:{} sessionId:{}",this.tableId,this.shopId,this.userId,message,session.getId());
//可以群发消息 //可以群发消息
//消息保存到数据库redis //消息保存到数据库redis
if (StringUtils.isNotBlank(message) && !message.equals("undefined")) { if (StringUtils.isNotBlank(message) && !message.equals("undefined")) {
@ -207,12 +208,18 @@ public class AppWebSocketServer {
if (webSocketMap.containsKey(tableId+"-"+shopId)) { if (webSocketMap.containsKey(tableId+"-"+shopId)) {
ConcurrentHashMap<String, AppWebSocketServer> userSocketMap = webSocketMap.get(tableId+"-"+shopId); ConcurrentHashMap<String, AppWebSocketServer> userSocketMap = webSocketMap.get(tableId+"-"+shopId);
// 在放置新条目之前检查并清除同名userId的数据 // 在放置新条目之前检查并清除同名userId的数据
if(userSocketMap.get(userId)!=null && userSocketMap.get(userId).session!=null){
this.session=userSocketMap.get(userId).session;
userSocketMap.put(userId,this); userSocketMap.put(userId,this);
}
} else { } else {
ConcurrentHashMap<String, AppWebSocketServer> userSocketMap=new ConcurrentHashMap<>(); ConcurrentHashMap<String, AppWebSocketServer> userSocketMap=new ConcurrentHashMap<>();
if(userSocketMap.get(userId)!=null && userSocketMap.get(userId).session!=null){
this.session=userSocketMap.get(userId).session;
userSocketMap.put(userId,this); userSocketMap.put(userId,this);
webSocketMap.put(tableId+"-"+shopId,userSocketMap); webSocketMap.put(tableId+"-"+shopId,userSocketMap);
} }
}
//这里采用责任链模式每一个处理器对应一个前段发过来的请这里还可以用工厂模式来生成对象 //这里采用责任链模式每一个处理器对应一个前段发过来的请这里还可以用工厂模式来生成对象
// ChangeHandler changeHandler = new ChangeHandler(); // ChangeHandler changeHandler = new ChangeHandler();
// CreateOrderHandler createOrderHandler = new CreateOrderHandler(); // CreateOrderHandler createOrderHandler = new CreateOrderHandler();
@ -270,6 +277,7 @@ public class AppWebSocketServer {
//加入线程锁 //加入线程锁
synchronized (session) { synchronized (session) {
try { try {
log.info("发送消息 session:{}",session.getId());
//同步发送信息 //同步发送信息
this.session.getBasicRemote().sendObject(message); this.session.getBasicRemote().sendObject(message);
} catch (Exception e) { } catch (Exception e) {

View File

@ -114,7 +114,7 @@ public class TokenUtil {
}catch (Exception e){ }catch (Exception e){
jsonObject.put("status","-4"); jsonObject.put("status","-4");
jsonObject.put("message","token解析失败了"); jsonObject.put("message","token解析失败了");
log.info("token解析失败{}",e.getMessage()); log.info("token解析失败token为{} message{}",token,e.getMessage());
e.printStackTrace(); e.printStackTrace();
} }
return jsonObject; return jsonObject;