goupan
2024-04-03 5506e9a45e717ffcb67ec313b5a4e8206d9b3a39
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
package cn.stylefeng.rest.modular.im.controller;
 
import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.modular.business.entity.ImUserStatus;
import cn.stylefeng.guns.modular.business.service.IImUserStatusService;
import cn.stylefeng.roses.kernel.auth.api.expander.AuthConfigExpander;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.im.api.pojo.ImMessageRoutingDTO;
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.util.List;
 
@Slf4j
@Api(tags = "IM接口")
@RestController
@ApiResource(name = "IM接口")
@RequestMapping("/im")
public class ImNotifyController {
 
    @Resource
    private CacheOperatorApi<Object> objectCacheOperatorApi;
 
    @Resource
    private CacheOperatorApi<ImMessageRoutingDTO> imMessageRoutingDTOCacheOperatorApi;
 
    @Resource
    IImUserStatusService imUserStatusService;
 
    @SneakyThrows
    @ApiOperation(value = "用户在线状态回调")
    @PostResource(name = "用户在线状态回调", path = RuleConstants.NOT_LOGIN + "/userStatusNotify")
    public String userStatusNotify(@RequestBody List<ImUserStatus> list) {
        // 回调地址:http://1.95.0.51:8081/rest/im/notLogin/userStatusNotify
        log.info("IM-用户在线状态回调:{}", list);
        list.forEach(o -> {
            boolean update = imUserStatusService.update(o,
                    Wrappers.<ImUserStatus>lambdaUpdate()
                            .eq(ImUserStatus::getUserid, o.getUserid())
                            .eq(ImUserStatus::getOs, o.getOs())
            );
            if (!update) {
                imUserStatusService.save(o);
            }
        });
        return "success";
    }
 
    @SneakyThrows
    @ApiOperation(value = "全量消息路由")
    @PostResource(name = "全量消息路由", path = RuleConstants.NOT_LOGIN + "/messageRouting")
    public String messageRouting(ImMessageRoutingDTO dto) {
        // 全量消息路由Api文档:https://doc.rongcloud.cn/imserver/server/v1/message/sync
        log.info("IM-全量消息路由:{}", dto);
 
        if (dto != null && "GROUP".equals(dto.getChannelType())) {
            // 更新群组消息时间
            objectCacheOperatorApi.put("ImGroupTime:" + dto.getToUserId(), DateUtil.date().toString(), 60L * 60 * 24 * 30);
        }
 
        // IM消息缓存失效时间(秒),0不缓存,-1不过期
        Long timeOutSecond = AuthConfigExpander.getsImMessageCacheTimeOutSecond();
        if (timeOutSecond != 0) {
            // 缓存消息(10天:60L * 60 * 24 * 10)
            imMessageRoutingDTOCacheOperatorApi.put(dto.getChannelType() + ":" + dto.getToUserId() + ":" + dto.getMsgUID(), dto, timeOutSecond);
        }
 
        return "success";
    }
 
}