package com.matrix.component.websoket;
|
|
import cn.hutool.crypto.SecureUtil;
|
import net.sf.json.JSONObject;
|
import org.springframework.web.socket.CloseStatus;
|
import org.springframework.web.socket.TextMessage;
|
import org.springframework.web.socket.WebSocketSession;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 扫码登录soket处理类
|
* @author jyy
|
*/
|
public class WebSoketScanQrCodeLoginObserver implements WebSoketMessageObserver {
|
|
|
private static Map<String,Long> scanCash=new HashMap<>();
|
|
|
@Override
|
public void userConnection(WebSocketSession session) {
|
|
}
|
|
@Override
|
public void handleTextMessage(WebSocketSession session, TextMessage message) {
|
|
|
ScanQrCodeLoginDto commonMessage = (ScanQrCodeLoginDto) JSONObject.toBean(JSONObject.fromObject(message.getPayload()), ScanQrCodeLoginDto.class);
|
|
if(ScanQrCodeLoginDto.MSG_TYPE_LOGIN.equals(commonMessage.getMsgType())){
|
//todo 添加安全校验代码
|
scanCash.put(SecureUtil.md5(commonMessage.getLoginQrCodeKey()),commonMessage.getAppUserId());
|
JSONObject jsonObject=new JSONObject();
|
jsonObject.put("loginOperation",commonMessage.getLoginOperation());
|
jsonObject.put("msgType",commonMessage.getMsgType());
|
WebSocketPushHandler.sendMessageToUser(commonMessage.getWebClientId(), new TextMessage(jsonObject.toString()));
|
}else if(ScanQrCodeLoginDto.MSG_TYPE_SCAN.equals(commonMessage.getMsgType())){
|
JSONObject jsonObject=new JSONObject();
|
jsonObject.put("msgType",commonMessage.getMsgType());
|
WebSocketPushHandler.sendMessageToUser(commonMessage.getWebClientId(), new TextMessage(jsonObject.toString()));
|
}
|
|
|
}
|
|
@Override
|
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
|
|
}
|
|
/**
|
* 获取扫码的用户id
|
* @param webClientId
|
* @return
|
*/
|
public static Long getScanCashValue(String webClientId){
|
return scanCash.remove(SecureUtil.md5(webClientId));
|
}
|
|
}
|