From 7f1fee8f5d2de70a3a0ea267cb4238d3a46758b9 Mon Sep 17 00:00:00 2001 From: wangxiang <1827945911@qq.com> Date: Fri, 31 Mar 2023 16:42:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=81=E4=B8=9A=E6=8E=A8=E9=80=81?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/entity/PushConcernFanType.java | 7 +- .../controller/PushApplicationController.java | 6 +- .../PushConcernFanTypeController.java | 2 +- .../service/IPushApplicationService.java | 8 +- .../impl/PushApplicationServiceImpl.java | 209 +++++++++--------- 5 files changed, 117 insertions(+), 115 deletions(-) diff --git a/kicc-platform/kicc-platform-api/kicc-common-api/src/main/java/com/cloud/kicc/commonbiz/api/entity/PushConcernFanType.java b/kicc-platform/kicc-platform-api/kicc-common-api/src/main/java/com/cloud/kicc/commonbiz/api/entity/PushConcernFanType.java index 982de129..1e5d1502 100644 --- a/kicc-platform/kicc-platform-api/kicc-common-api/src/main/java/com/cloud/kicc/commonbiz/api/entity/PushConcernFanType.java +++ b/kicc-platform/kicc-platform-api/kicc-common-api/src/main/java/com/cloud/kicc/commonbiz/api/entity/PushConcernFanType.java @@ -30,8 +30,11 @@ public class PushConcernFanType extends CommonEntity { @ApiModelProperty("类型名称") private String name; - @ApiModelProperty("用户ID") - private String userId; + @ApiModelProperty("关注用户") + private String concernUserId; + + @ApiModelProperty("粉丝用户") + private String fanUserId; @ApiModelProperty("推送类型ID") private String typeId; diff --git a/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/controller/PushApplicationController.java b/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/controller/PushApplicationController.java index 561102f7..e454cd0b 100644 --- a/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/controller/PushApplicationController.java +++ b/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/controller/PushApplicationController.java @@ -95,9 +95,9 @@ public class PushApplicationController { } @PostMapping("/send") - @ApiOperation(value = "消息发送") - public R mapPresetPointConfirmPushMsgTest(@Valid @RequestBody PushChatMessage pushChatMessage) { - iPushApplicationService.messageSend(pushChatMessage); + @ApiOperation(value = "企业推送消息") + public R enterpriseMessageSend(@Valid @RequestBody PushChatMessage pushChatMessage) { + iPushApplicationService.enterpriseMessageSend(pushChatMessage); return R.ok(true); } diff --git a/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/controller/PushConcernFanTypeController.java b/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/controller/PushConcernFanTypeController.java index 845ae362..8bbd75ef 100644 --- a/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/controller/PushConcernFanTypeController.java +++ b/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/controller/PushConcernFanTypeController.java @@ -35,7 +35,7 @@ public class PushConcernFanTypeController { private LambdaQueryWrapper getQueryWrapper(PushConcernFanType pushConcernFanType) { return new LambdaQueryWrapper() - .eq(PushConcernFanType::getUserId, pushConcernFanType.getUserId()) + .eq(PushConcernFanType::getConcernUserId, pushConcernFanType.getConcernUserId()) .eq(StrUtil.isNotBlank(pushConcernFanType.getName()), PushConcernFanType::getName, pushConcernFanType.getName()); } diff --git a/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/service/IPushApplicationService.java b/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/service/IPushApplicationService.java index c70970ad..839b3ce9 100644 --- a/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/service/IPushApplicationService.java +++ b/kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/service/IPushApplicationService.java @@ -17,10 +17,10 @@ import io.lettuce.core.api.push.PushMessage; public interface IPushApplicationService extends IService { /** - * 消息发送 - * @param - * @return + * 针对企业公开外部消息推送接口 + * @param pushChatMessage 推送消息 + * @return void */ - void messageSend(PushChatMessage pushChatMessage); + void enterpriseMessageSend(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 1bad7e2b..df255b52 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 @@ -1,27 +1,19 @@ package com.cloud.kicc.commonbiz.service.impl; -import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.util.BooleanUtil; -import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.extra.servlet.ServletUtil; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.cloud.kicc.common.core.api.R; -import com.cloud.kicc.common.core.constant.SecurityConstants; import com.cloud.kicc.common.core.exception.CheckedException; import com.cloud.kicc.commonbiz.api.entity.*; import com.cloud.kicc.commonbiz.mapper.PushApplicationMapper; import com.cloud.kicc.commonbiz.service.*; import com.cloud.kicc.commonbiz.util.PushClientUtil; -import com.cloud.kicc.system.api.entity.User; -import com.cloud.kicc.system.api.feign.RemoteUserService; -import io.lettuce.core.api.push.PushMessage; import lombok.RequiredArgsConstructor; import okhttp3.OkHttpClient; -import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.request.RequestContextHolder; @@ -48,110 +40,117 @@ import java.util.stream.Collectors; public class PushApplicationServiceImpl extends ServiceImpl implements IPushApplicationService { private final OkHttpClient okHttpClient; - //private final IPushUserManageService iPushUserManageService; - //private final IPushMessageService iPushMessageService; - private final RemoteUserService remoteUserService; - //private final IPushCustomTypeService iPushCustomTypeService; + private final IPushConcernFanService iPushConcernFanService; + private final IPushBlacklistService iPushBlacklistService; + private final IPushChatMessageService iPushChatMessageService; + private final IPushConcernFanTypeService iPushConcernFanTypeService; @Override - public void messageSend(PushChatMessage pushChatMessage) { - - /*if (StrUtil.isBlank(pushChatMessage.getPushUserId()) || StrUtil.isBlank(pushChatMessage.getMessageSecret())) { - throw new CheckedException("当前发送方用户ID与推送应用密钥必填!"); - }*/ - R result = remoteUserService.selectByUserId(pushChatMessage.getPushTypeId(), SecurityConstants.FROM_IN); - if (result == null || result.getData() == null) { - throw new CheckedException("用户不存在"); + public void enterpriseMessageSend(PushChatMessage pushChatMessage) { + + if (StrUtil.isBlank(pushChatMessage.getUserId()) || StrUtil.isBlank(pushChatMessage.getMessageSecret())) { + throw new CheckedException("当前推送方用户ID与推送应用密钥必填!"); } - // 第三方发送前较验数据 - if (result.getData().getUserType().equals("9")) { - PushApplication pushApplication = baseMapper.selectOne(Wrappers.lambdaQuery() - .eq(PushApplication::getMessageSecret, pushChatMessage.getMessageSecret()).eq(PushApplication::getStatus, "0")); - pushApplication = Optional.of(pushApplication).orElseThrow(() -> new CheckedException("你当前没有权限发送消息,请联系管理员!")); - HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); - // 如果ip地址存在即验证发送方ip - if (StrUtil.isNotBlank(pushApplication.getIgnoreIp()) && !StrUtil.contains(pushApplication.getIgnoreIp(), ServletUtil.getClientIP(request))) { - throw new CheckedException("你的ip地址不正确,请检查消息应用中设置的ip地址!"); - } + PushApplication pushApplication = baseMapper.selectOne(Wrappers.lambdaQuery() + .eq(PushApplication::getMessageSecret, pushChatMessage.getMessageSecret()) + .eq(PushApplication::getCreateById, pushChatMessage.getUserId()) + .eq(PushApplication::getStatus, "0")); + pushApplication = Optional.of(pushApplication).orElseThrow(() -> new CheckedException("你当前没有权限发送消息,请联系管理员!")); + + HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); + // 如果ip地址存在即验证发送方ip + if (StrUtil.isNotBlank(pushApplication.getIgnoreIp()) && !StrUtil.contains(pushApplication.getIgnoreIp(), ServletUtil.getClientIP(request))) { + throw new CheckedException("你的ip地址不正确,请检查消息应用中设置的ip地址!"); } - // 查询当前用户设置的自定义快捷方式消息提醒属性 - /*PushCustomType pushCustomType = iPushCustomTypeService.getOne(Wrappers.lambdaQuery() - .eq(PushCustomType::getId, pushChatMessage.getPushTypeId()).eq(PushCustomType::getCreateById, pushChatMessage.getFromUserId())); - if (ObjectUtil.isNotEmpty(pushCustomType)) { - pushMessage.setPlaySound(pushCustomType.getPlaySound()); - pushMessage.setPlayVibrate(pushCustomType.getPlayVibrate()); - pushMessage.setPlayLights(pushCustomType.getPlayLights()); - if (StrUtil.isNotBlank(pushCustomType.getCustomPlayFileName())) pushMessage.setCustomPlayFileName(pushCustomType.getCustomPlayFileName()); - }*/ - - List blacklist = CollectionUtil.newArrayList(); /*iPushPassListService.list(Wrappers.lambdaQuery() - .eq(PushPassList::getFromPushId, pushMessage.getFromUserId()).eq(PushPassList::getType, "0")) - .stream().map(item -> item.getToPushId()).collect(Collectors.toList());*/ - - List whitelist = CollectionUtil.newArrayList(); /*iPushPassListService.list(Wrappers.lambdaQuery() - .eq(PushPassList::getFromPushId, pushMessage.getFromUserId()).eq(PushPassList::getType, "1")) - .stream().map(item -> item.getToPushId()).collect(Collectors.toList());*/ - - /*List pushList = iPushUserManageService.list(Wrappers.lambdaQuery() - .eq(PushUserManage::getFromUserId, pushMessage.getFromUserId()).eq(PushUserManage::getStatus, "1")) - .stream().filter(item -> !blacklist.contains(item.getToUserId()) || !whitelist.contains(item.getToUserId())).collect(Collectors.toList()); - - pushList.forEach(item -> send(pushMessage, item.getToUserId(), item.getPlaySound(), item.getPlayVibrate(), item.getPlayLights())); - - whitelist.forEach(item -> { - pushMessage.setPlaySound("0"); - pushMessage.setPlayVibrate("0"); - pushMessage.setPlayLights("0"); - send(pushMessage, item, pushMessage.getPlaySound(), pushMessage.getPlayVibrate(), pushMessage.getPlayLights()); - });*/ - } + // 配置推送用户 + if (StrUtil.isBlank(pushChatMessage.getAlias())) { + List pushConcernFanList = iPushConcernFanService.list(Wrappers.lambdaQuery().eq(PushConcernFan::getFanUserId, pushChatMessage.getUserId())); + pushChatMessage.setAlias(pushConcernFanList.stream().map(item -> item.getConcernUserId()).collect(Collectors.joining())); + } + // 过滤掉黑名单用户 + List pushBlacklists = iPushBlacklistService.list(Wrappers.lambdaQuery().eq(PushBlacklist::getFanUserId, pushChatMessage.getUserId())); + pushBlacklists.forEach(item -> pushChatMessage.getAlias().replaceAll(item.getConcernUserId() + ",|$", "")); + + + // 查询推送用户设置的推送类型,如果参数中没有指定则采用默认的推送类型 + List 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")); + + + // 推送消息 + StrUtil.split(pushChatMessage.getAlias(), ",").forEach(concernUserId -> { + Optional pushConcernFanTypeOptional = pushConcernFanTypeList.stream().filter(item -> item.getConcernUserId().equals(concernUserId)).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(concernUserId); + 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(); + } - private void send(PushMessage pushMessage, String toUserId, String playSound, String playVibrate, String playLights) { - /*// 设置默认数据 - pushMessage.setType("customizedcast"); - pushMessage.setDisplayType("notification"); - pushMessage.setAliasType(PushClientUtil.uPushUserAliasType); - pushMessage.setAlias(toUserId); - JSONObject custom = new JSONObject(); - custom.put("sendUserId", pushMessage.getFromUserId()); - custom.put("customPlayFileName", pushMessage.getCustomPlayFileName()); - custom.put("playSound", BooleanUtil.toBoolean(playSound)); - custom.put("playVibrate", BooleanUtil.toBoolean(playVibrate)); - custom.put("playLights", BooleanUtil.toBoolean(playLights)); - pushMessage.setCustom(custom.toString()); - JSONObject jsonObject = new JSONObject(); - jsonObject.put("production_mode", PushClientUtil.PRODUCTION_MODE); - jsonObject.put("type", pushMessage.getType()); - jsonObject.put("description", pushMessage.getRemarks()); - jsonObject.put("alias_type", pushMessage.getAliasType()); - jsonObject.put("alias", pushMessage.getAlias()); - JSONObject payload = new JSONObject(); - payload.put("display_type", pushMessage.getDisplayType()); - JSONObject body = new JSONObject(); - body.put("ticker", pushMessage.getTitle()); - body.put("text", pushMessage.getText()); - body.put("title", pushMessage.getTitle()); - body.put("sound", pushMessage.getSound()); - body.put("after_open", "go_custom"); - body.put("custom", pushMessage.getCustom()); - body.put("play_sound", BooleanUtil.toBoolean(playSound) ? false : !BooleanUtil.toBoolean(pushMessage.getPlaySound())); - body.put("play_vibrate", BooleanUtil.toBoolean(playVibrate) ? false : !BooleanUtil.toBoolean(pushMessage.getPlayVibrate())); - body.put("play_lights", BooleanUtil.toBoolean(playLights) ? false : !BooleanUtil.toBoolean(pushMessage.getPlayLights())); - 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); - iPushMessageService.save(pushMessage); - } catch (Exception e) { - e.printStackTrace(); - }*/ + }); } }