jyy
2021-06-19 e522e4d40451b4e46ec79b3d8a3891899196f14f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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));
    }
 
}