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 objectCacheOperatorApi; @Resource private CacheOperatorApi imMessageRoutingDTOCacheOperatorApi; @Resource IImUserStatusService imUserStatusService; @SneakyThrows @ApiOperation(value = "用户在线状态回调") @PostResource(name = "用户在线状态回调", path = RuleConstants.NOT_LOGIN + "/userStatusNotify") public String userStatusNotify(@RequestBody List list) { // 回调地址:http://1.95.0.51:8081/rest/im/notLogin/userStatusNotify log.info("IM-用户在线状态回调:{}", list); list.forEach(o -> { boolean update = imUserStatusService.update(o, Wrappers.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"; } }