goupan
2024-04-28 15ceedff707b3ff1c51970eabb36bb032f3d69c4
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
114
115
package cn.stylefeng.guns.modular.business.service.impl;
 
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.stylefeng.guns.modular.business.dto.ImPushDataDTO;
import cn.stylefeng.guns.modular.business.entity.ImNotice;
import cn.stylefeng.guns.modular.business.service.IImNoticeService;
import cn.stylefeng.roses.kernel.customer.api.pojo.CustomerInfo;
import cn.stylefeng.roses.kernel.customer.modular.entity.Customer;
import cn.stylefeng.roses.kernel.customer.modular.service.CustomerService;
import cn.stylefeng.roses.kernel.im.api.ImServerApi;
import cn.stylefeng.roses.kernel.rule.enums.ImUserTypeEnum;
import cn.stylefeng.roses.kernel.rule.enums.PostIdEnum;
import io.rong.messages.BaseMessage;
import io.rong.messages.TxtMessage;
import io.rong.models.response.ResponseResult;
import io.rong.models.response.TokenResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
 
@Slf4j
@Service
public class ImBizService {
 
    @Resource
    private ImServerApi imServerApi;
 
    @Resource
    private IImNoticeService imNoticeService;
 
    @Resource
    private CustomerService customerService;
 
    public String getImToken(Long customerId) {
        // 获取用户的详细信息
        CustomerInfo customerInfo = customerService.getCustomerInfoById(customerId);
        String imToken = customerInfo.getImToken();
        if (StrUtil.isEmpty(imToken)) {
            // IM用户注册
            TokenResult tokenResult = imServerApi.userRegister(customerInfo.getCustomerId().toString(), customerInfo.getNickName(), customerInfo.getAvatarObjectUrl());
            if (tokenResult.getCode() == 200) {
                imToken = tokenResult.getToken();
 
                // 更新用户IM通讯token
                Customer customer = new Customer();
                customer.setCustomerId(customerId);
                customer.setImToken(imToken);
                customerService.updateCustomerRemoveCache(customer);
            }
        }
 
        return imToken;
    }
 
    public <T extends BaseMessage> ResponseResult messageSendSystem(String fromUserId, String[] toUserIds, ImPushDataDTO pushData, ImUserTypeEnum userTypeEnum, PostIdEnum postIdEnum, Boolean isSaveImNotice) {
        // 推送json消息
        String pushDataStr = JSONUtil.toJsonStr(pushData);
        try {
            // 是否保存IM通知
            if (BooleanUtil.isTrue(isSaveImNotice)) {
                for (String userId : toUserIds) {
                    // 保存IM通知
                    imNoticeService.save(
                            ImNotice.builder()
                                    .type(pushData.getType())
                                    .userType(userTypeEnum == null ? null : userTypeEnum.getCode())
                                    .postId(postIdEnum == null ? null : postIdEnum.getCode())
                                    .userId(Long.valueOf(userId))
                                    .title(pushData.getTitle())
                                    .content(pushData.getContent())
                                    .pushJson(pushDataStr)
                                    .businessId(ObjUtil.toString(pushData.getObjId()))
                                    .build()
                    );
                }
            }
        } catch (Exception e) {
            log.error("保存IM通知异常");
            e.printStackTrace();
        }
        if (toUserIds.length > 0) {
            log.info("融云发送群消息:{}", pushDataStr);
            // 融云发送通知
            return imServerApi.messageSendSystem(fromUserId, toUserIds, new TxtMessage(pushDataStr, pushData.getExtra()));
        }
        return null;
    }
 
    public <T extends BaseMessage> ResponseResult messageSendPrivate(String fromUserId, String[] toUserIds, ImPushDataDTO pushData) {
        // 推送json消息
        String pushDataStr = JSONUtil.toJsonStr(pushData);
        if (toUserIds.length > 0) {
            log.info("融云发送单聊消息:{}", pushDataStr);
            // 融云发送单聊消息
            return imServerApi.messageSendPrivate(fromUserId, toUserIds, new TxtMessage(pushDataStr, pushData.getExtra()));
        }
        return null;
    }
 
    public <T extends BaseMessage> ResponseResult messageSendGroup(String fromUserId, String[] toGroupIds, ImPushDataDTO pushData) {
        // 推送json消息
        String pushDataStr = JSONUtil.toJsonStr(pushData);
        if (toGroupIds.length > 0) {
            log.info("融云发送群消息:{}", pushDataStr);
            // 融云发送群消息
            return imServerApi.messageSendGroup(fromUserId, toGroupIds, new TxtMessage(pushDataStr, pushData.getExtra()));
        }
        return null;
    }
 
}