基于mall4j产品的二开项目后端
lee
2024-12-18 efdb99f8cecc4afb592afad79c761081d5d5cf22
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.yami.shop.im.client;
 
import java.net.URI;
 
import javax.annotation.PostConstruct;
import javax.websocket.ClientEndpoint;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
 
import org.springframework.stereotype.Component;
 
import com.yami.shop.bean.im.MsgAction;
import com.yami.shop.bean.im.MsgBody;
import com.yami.shop.bean.im.MsgType;
import com.yami.shop.bean.im.RecipientType;
import com.yami.shop.bean.im.SystemMsgType;
import com.yami.shop.bean.im.security.MsgAuth;
import com.yami.shop.im.client.constant.LuckIMClientConstant;
 
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
 
/**
 * 幸运集社im客户端
 */
@Slf4j
@Component
@ClientEndpoint
public class LuckIMClient {
    
    private Session session;
    
    @PostConstruct
    public void init() {
        try {
            String url = LuckIMClientConstant.LUCK_IM_MSG_URL.replace("{auth}", MsgAuth.genTodaySystemAuthCode()).replace("{clientId}", IdUtil.fastSimpleUUID());
            WebSocketContainer container = javax.websocket.ContainerProvider.getWebSocketContainer();
            session = container.connectToServer(LuckIMClient.class, URI.create(url));
        } catch (Exception e) {
            log.error("LuckIMClient init Exception: " + ExceptionUtil.stacktraceToOneLineString(e));
        }
    }
    
    @OnOpen
    public void onOpen(Session session) {
        log.info("Luck Im Server connected.");
        heartBeat();
    }
 
    @OnMessage
    public void onMessage(String message) {
        log.info("Luck Im Server onMessage: " + message);
    }
    
    @OnClose
    public void onClose(Session session) {
        log.info("Luck Im Server closed");
    }
    
    /**
     * 心跳处理
     */
    private void heartBeat() {
        while(session != null && session.isOpen()) {
            try {
                session.getBasicRemote().sendText(MsgBody.getHeartBeatMsgBody().toString());
                Thread.sleep(1000 * 30);//每30秒发送心跳
            } catch (Exception e) {
                log.error("heartBeat Exception: " + ExceptionUtil.stacktraceToString(e));
            }
        }
    }
 
    
    /**
     * 发送系统消息给用户
     * @param userId 用户id
     * @param systemMsgType 系统消息类型
     * @param content 内容
     */
    public void sendSystemMsgToUser(String userId,SystemMsgType systemMsgType,Object content) {
        if(ObjUtil.isAllEmpty(userId,systemMsgType,content)) {
            throw new IllegalArgumentException();
        }
        if(session == null || !session.isOpen()) {
            log.error("系统im初始化失败或链接已断开");
            return;
        }
        
        MsgBody msgBody = new MsgBody();
        msgBody.setAction(MsgAction.SEND_SYSTEM_MSG.name());
        msgBody.setContent(content);
        msgBody.setRecipientType(RecipientType.USER.name());
        msgBody.setFromId(MsgBody.getSystemId());
        msgBody.setMsgType(MsgType.SYSTEM.name());
        msgBody.setSystemType(systemMsgType.getValue());
        msgBody.setToId(userId);
        msgBody.setMsgId(MsgBody.randomMessageId());
        msgBody.setTimestamp(System.currentTimeMillis());
        try {
            session.getBasicRemote().sendText(msgBody.toString());
        } catch (Exception e) {
            log.error("sendSystemMsgToUser Exception: " + ExceptionUtil.stacktraceToString(e));
        }
    }
    
}