diff --git a/kicc-platform/kicc-platform-api/kicc-common-api/src/main/java/com/cloud/kicc/commonbiz/api/entity/PushChatMessage.java b/kicc-platform/kicc-platform-api/kicc-common-api/src/main/java/com/cloud/kicc/commonbiz/api/entity/PushChatMessage.java index 4c361fc6..675db551 100644 --- a/kicc-platform/kicc-platform-api/kicc-common-api/src/main/java/com/cloud/kicc/commonbiz/api/entity/PushChatMessage.java +++ b/kicc-platform/kicc-platform-api/kicc-common-api/src/main/java/com/cloud/kicc/commonbiz/api/entity/PushChatMessage.java @@ -33,6 +33,9 @@ public class PushChatMessage extends CommonEntity { @ApiModelProperty("推送方用户id") private String userId; + @ApiModelProperty("关注粉丝状态") + private String concernFanStatus; + @ApiModelProperty("消息类型") private String type; diff --git a/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/controller/PushConcernFanController.java b/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/controller/PushConcernFanController.java index ac8cc4f5..2f63233c 100644 --- a/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/controller/PushConcernFanController.java +++ b/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/controller/PushConcernFanController.java @@ -242,5 +242,13 @@ public class PushConcernFanController { return R.ok(); } + + @PostMapping("/concernFanSend") + @ApiOperation("关注粉丝双向推送消息") + public R concernFanMessageSend(@Valid @RequestBody PushChatMessage pushChatMessage) { + iPushConcernFanService.concernFanMessageSend(pushChatMessage); + return R.ok(true); + } + } diff --git a/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/service/IPushConcernFanService.java b/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/service/IPushConcernFanService.java index 1bb17a9a..d078278b 100644 --- a/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/service/IPushConcernFanService.java +++ b/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/service/IPushConcernFanService.java @@ -2,6 +2,7 @@ package com.cloud.kicc.commonbiz.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; +import com.cloud.kicc.commonbiz.api.entity.PushChatMessage; import com.cloud.kicc.commonbiz.api.entity.PushConcernFan; import java.util.Map; @@ -38,4 +39,11 @@ public interface IPushConcernFanService extends IService { */ IPage> selectPushFriendList(IPage> page, Map map); + /** + * 针对关注粉丝双向消息推送接口 + * @param pushChatMessage 推送消息 + * @return void + */ + void concernFanMessageSend(PushChatMessage pushChatMessage); + } diff --git a/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/service/impl/PushApplicationServiceImpl.java b/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/service/impl/PushApplicationServiceImpl.java index 1d37f7db..39db131e 100644 --- a/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/service/impl/PushApplicationServiceImpl.java +++ b/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/service/impl/PushApplicationServiceImpl.java @@ -99,8 +99,8 @@ public class PushApplicationServiceImpl extends ServiceImpl item.getConcernUserId()).collect(Collectors.joining())); } - // 过滤掉黑名单用户 - List pushBlacklists = iPushBlacklistService.list(Wrappers.lambdaQuery().eq(PushBlacklist::getConcernUserId, pushChatMessage.getUserId())); + // 过滤黑名单,根据谁拉黑了推送方用户进行过滤推送用户 + List pushBlacklists = iPushBlacklistService.list(Wrappers.lambdaQuery().eq(PushBlacklist::getFanUserId, pushChatMessage.getUserId())); pushBlacklists.forEach(item -> pushChatMessage.setAlias(pushChatMessage.getAlias().replaceAll(item.getConcernUserId() + ",|$", ""))); @@ -112,8 +112,8 @@ public class PushApplicationServiceImpl extends ServiceImpl { - Optional pushConcernFanTypeOptional = pushConcernFanTypeList.stream().filter(item -> item.getConcernUserId().equals(concernUserId)).findFirst(); + StrUtil.split(pushChatMessage.getAlias(), ",").forEach(alias -> { + Optional pushConcernFanTypeOptional = pushConcernFanTypeList.stream().filter(item -> item.getFanUserId().equals(alias)).findFirst(); if (pushConcernFanTypeOptional.isPresent()) { PushConcernFanType pushConcernFanType = pushConcernFanTypeOptional.get(); pushChatMessage.setPushTypeId(pushConcernFanType.getTypeId()); @@ -128,7 +128,7 @@ public class PushApplicationServiceImpl extends ServiceImpl @@ -18,8 +40,16 @@ import java.util.Map; * @since 2023-03-24 */ @Service +@RequiredArgsConstructor +@Transactional(rollbackFor = Exception.class) public class PushConcernFanServiceImpl extends ServiceImpl implements IPushConcernFanService { + private final OkHttpClient okHttpClient; + private final IPushConcernFanService iPushConcernFanService; + private final IPushBlacklistService iPushBlacklistService; + private final IPushChatMessageService iPushChatMessageService; + private final IPushConcernFanTypeService iPushConcernFanTypeService; + @Override public IPage> selectPushConcernFanList(IPage> page, Map map) { return baseMapper.selectPushConcernFanList(page, map); @@ -34,4 +64,125 @@ public class PushConcernFanServiceImpl extends ServiceImpl> selectPushFriendList(IPage> page, Map map) { return baseMapper.selectPushFriendList(page, map); } + + @Override + public void concernFanMessageSend(PushChatMessage pushChatMessage) { + + if (StrUtil.isBlank(pushChatMessage.getUserId())) { + throw new CheckedException("当前推送方用户ID必填!"); + } + List pushConcernFanTypeList; + // 是否为关注推送 + if (StrUtil.equals(pushChatMessage.getConcernFanStatus(), "1")) { + if (StrUtil.isBlank(pushChatMessage.getAlias())) throw new CheckedException("当前推送用户别名ID必填!"); + // 查询对方是否有关注推送用户 + PushConcernFan pushConcernFan = iPushConcernFanService.getOne(Wrappers.lambdaQuery() + .eq(PushConcernFan::getConcernUserId, pushChatMessage.getUserId()) + .eq(PushConcernFan::getFanUserId, pushChatMessage.getAlias()) + .eq(PushConcernFan::getStatus, PushAuditStatusEnum.APPROVED)); + // 推送限制关注用户只能推送一条消息,需要双向关注后才能解锁限制 + if (ObjectUtil.isEmpty(pushConcernFan)) { + Optional optionalPushChatMessage = iPushChatMessageService.list(Wrappers.lambdaQuery() + .eq(PushChatMessage::getConcernFanStatus, "1") + .eq(PushChatMessage::getUserId, pushChatMessage.getUserId()) + .eq(PushChatMessage::getAlias, pushChatMessage.getAlias())).stream().findFirst(); + optionalPushChatMessage.ifPresent(value -> { throw new CheckedException("对方还未关注你,你只能发送一条消息,发送失败!"); }); + } + // 查询关注设置的推送类型,如果参数中没有指定则采用默认的推送类型 + pushConcernFanTypeList = iPushConcernFanTypeService.list(Wrappers.lambdaQuery() + .eq(PushConcernFanType::getFanUserId, pushChatMessage.getUserId()) + .eq(StrUtil.isNotBlank(pushChatMessage.getPushTypeId()), PushConcernFanType::getTypeId, pushChatMessage.getPushTypeId()) + .eq(StrUtil.isBlank(pushChatMessage.getPushTypeId()), PushConcernFanType::getDefaultType, "1")); + } else { + // 配置推送用户,针对粉丝支持多人推送 + if (StrUtil.isBlank(pushChatMessage.getAlias())) { + List pushConcernFanList = iPushConcernFanService.list(Wrappers.lambdaQuery() + .eq(PushConcernFan::getConcernUserId, pushChatMessage.getUserId()) + .eq(PushConcernFan::getStatus, PushAuditStatusEnum.APPROVED)); + pushChatMessage.setAlias(pushConcernFanList.stream().map(item -> item.getConcernUserId()).collect(Collectors.joining())); + } + // 查询粉丝设置的推送类型,如果参数中没有指定则采用默认的推送类型 + pushConcernFanTypeList = iPushConcernFanTypeService.list(Wrappers.lambdaQuery() + .eq(PushConcernFanType::getConcernFanId, pushChatMessage.getUserId()) + .eq(StrUtil.isNotBlank(pushChatMessage.getPushTypeId()), PushConcernFanType::getTypeId, pushChatMessage.getPushTypeId()) + .eq(StrUtil.isBlank(pushChatMessage.getPushTypeId()), PushConcernFanType::getDefaultType, "1")); + } + + // 过滤黑名单,根据谁拉黑了推送方用户进行过滤推送用户 + List pushBlacklists = iPushBlacklistService.list(Wrappers.lambdaQuery().eq(PushBlacklist::getFanUserId, pushChatMessage.getUserId())); + pushBlacklists.forEach(item -> pushChatMessage.setAlias(pushChatMessage.getAlias().replaceAll(item.getConcernUserId() + ",|$", ""))); + + // 推送消息 + StrUtil.split(pushChatMessage.getAlias(), ",").forEach(alias -> { + Optional pushConcernFanTypeOptional = pushConcernFanTypeList.stream().filter(item -> + StrUtil.equals(pushChatMessage.getConcernFanStatus(), "1") ? item.getConcernUserId().equals(alias) : item.getFanUserId().equals(alias)).findFirst(); + if (pushConcernFanTypeOptional.isPresent()) { + PushConcernFanType pushConcernFanType = pushConcernFanTypeOptional.get(); + pushChatMessage.setPushTypeId(pushConcernFanType.getTypeId()); + pushChatMessage.setPlayLights(pushConcernFanType.getPlayLights()); + pushChatMessage.setPlaySound(pushConcernFanType.getPlaySound()); + pushChatMessage.setPlayVibrate(pushConcernFanType.getPlayVibrate()); + pushChatMessage.setPlayToText(pushConcernFanType.getPlayToText()); + pushChatMessage.setSound(pushConcernFanType.getOfflineRingtone()); + pushChatMessage.setOnlineRingtone(pushConcernFanType.getOnlineRingtone()); + } + + pushChatMessage.setType("customizedcast"); + pushChatMessage.setDisplayType("notification"); + pushChatMessage.setAliasType(PushClientUtil.uPushUserAliasType); + pushChatMessage.setAlias(alias); + JSONObject custom = new JSONObject(); + custom.put("fanUserId", pushChatMessage.getUserId()); + custom.put("offlineRingtone", pushChatMessage.getSound()); + custom.put("onlineRingtone", pushChatMessage.getOnlineRingtone()); + custom.put("playToText", BooleanUtil.toBoolean(pushChatMessage.getPlayToText())); + custom.put("playSound", BooleanUtil.toBoolean(pushChatMessage.getPlaySound())); + custom.put("playVibrate", BooleanUtil.toBoolean(pushChatMessage.getPlayVibrate())); + custom.put("playLights", BooleanUtil.toBoolean(pushChatMessage.getPlayLights())); + pushChatMessage.setCustom(custom.toString()); + JSONObject jsonObject = new JSONObject(); + jsonObject.put("production_mode", PushClientUtil.PRODUCTION_MODE); + jsonObject.put("type", pushChatMessage.getType()); + jsonObject.put("description", pushChatMessage.getRemarks()); + jsonObject.put("alias_type", pushChatMessage.getAliasType()); + jsonObject.put("alias", pushChatMessage.getAlias()); + JSONObject payload = new JSONObject(); + payload.put("display_type", pushChatMessage.getDisplayType()); + JSONObject body = new JSONObject(); + body.put("ticker", pushChatMessage.getTitle()); + body.put("text", pushChatMessage.getText()); + body.put("title", pushChatMessage.getTitle()); + body.put("sound", pushChatMessage.getSound()); + body.put("after_open", "go_custom"); + body.put("custom", pushChatMessage.getCustom()); + + if (BooleanUtil.toBoolean(pushChatMessage.getPlaySound())) { + body.put("play_sound", true); + } + + if (BooleanUtil.toBoolean(pushChatMessage.getPlayVibrate())) { + body.put("play_vibrate", true); + } + + if (BooleanUtil.toBoolean(pushChatMessage.getPlayLights())) { + body.put("play_lights", true); + } + + payload.put("body", body); + jsonObject.put("payload", payload); + JSONObject policy = new JSONObject(); + policy.put("expire_time", LocalDateTimeUtil.now().plusDays(3).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); + policy.put("out_biz_no", LocalDateTimeUtil.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); + jsonObject.put("policy", policy); + try { + PushClientUtil pushClientUtil = new PushClientUtil(okHttpClient); + pushClientUtil.send(jsonObject); + iPushChatMessageService.save(pushChatMessage); + } catch (Exception e) { + e.printStackTrace(); + } + + }); + } + }