Browse Source

🚀 消息推送模块

master
wangxiang 2 years ago
parent
commit
6c227aa040
  1. 4
      kicc-platform/kicc-platform-api/kicc-common-api/src/main/java/com/cloud/kicc/commonbiz/api/entity/PushMessage.java
  2. 9
      kicc-platform/kicc-platform-api/kicc-common-api/src/main/java/com/cloud/kicc/commonbiz/api/entity/PushUserManage.java
  3. 70
      kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/service/impl/PushApplicationServiceImpl.java

4
kicc-platform/kicc-platform-api/kicc-common-api/src/main/java/com/cloud/kicc/commonbiz/api/entity/PushMessage.java

@ -27,8 +27,8 @@ public class PushMessage extends CommonEntity {
@ApiModelProperty("主键id") @ApiModelProperty("主键id")
private String id; private String id;
@ApiModelProperty("发送用户id") @ApiModelProperty("发送用户id")
private String sendUserId; private String fromUserId;
@ApiModelProperty("消息类型") @ApiModelProperty("消息类型")
private String type; private String type;

9
kicc-platform/kicc-platform-api/kicc-common-api/src/main/java/com/cloud/kicc/commonbiz/api/entity/PushUserManage.java

@ -31,11 +31,14 @@ public class PushUserManage extends CommonEntity {
@ApiModelProperty("对方用户id") @ApiModelProperty("对方用户id")
private String toUserId; private String toUserId;
@ApiModelProperty("用户昵称") @ApiModelProperty("发送方用户id")
private String nickName; private String fromUserId;
@ApiModelProperty("用户名称")
private String userName;
@ApiModelProperty("状态") @ApiModelProperty("状态")
private String Status; private String status;
@ApiModelProperty("是否播放声音") @ApiModelProperty("是否播放声音")
private String playSound; private String playSound;

70
kicc-platform/kicc-platform-biz/kicc-common-biz/src/main/java/com/cloud/kicc/commonbiz/service/impl/PushApplicationServiceImpl.java

@ -1,6 +1,7 @@
package com.cloud.kicc.commonbiz.service.impl; package com.cloud.kicc.commonbiz.service.impl;
import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil; import cn.hutool.extra.servlet.ServletUtil;
@ -10,20 +11,15 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.kicc.common.core.api.R; import com.cloud.kicc.common.core.api.R;
import com.cloud.kicc.common.core.constant.SecurityConstants; import com.cloud.kicc.common.core.constant.SecurityConstants;
import com.cloud.kicc.common.core.exception.CheckedException; import com.cloud.kicc.common.core.exception.CheckedException;
import com.cloud.kicc.commonbiz.api.entity.PushApplication; import com.cloud.kicc.commonbiz.api.entity.*;
import com.cloud.kicc.commonbiz.api.entity.PushCustomType;
import com.cloud.kicc.commonbiz.api.entity.PushMessage;
import com.cloud.kicc.commonbiz.api.entity.PushUserManage;
import com.cloud.kicc.commonbiz.mapper.PushApplicationMapper; import com.cloud.kicc.commonbiz.mapper.PushApplicationMapper;
import com.cloud.kicc.commonbiz.service.IPushApplicationService; import com.cloud.kicc.commonbiz.service.*;
import com.cloud.kicc.commonbiz.service.IPushCustomTypeService;
import com.cloud.kicc.commonbiz.service.IPushMessageService;
import com.cloud.kicc.commonbiz.service.IPushUserManageService;
import com.cloud.kicc.commonbiz.util.PushClientUtil; import com.cloud.kicc.commonbiz.util.PushClientUtil;
import com.cloud.kicc.system.api.entity.User; import com.cloud.kicc.system.api.entity.User;
import com.cloud.kicc.system.api.feign.RemoteUserService; import com.cloud.kicc.system.api.feign.RemoteUserService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
@ -54,17 +50,19 @@ public class PushApplicationServiceImpl extends ServiceImpl<PushApplicationMappe
private final IPushMessageService iPushMessageService; private final IPushMessageService iPushMessageService;
private final RemoteUserService remoteUserService; private final RemoteUserService remoteUserService;
private final IPushCustomTypeService iPushCustomTypeService; private final IPushCustomTypeService iPushCustomTypeService;
private final IPushPassListService iPushPassListService;
@Override @Override
public void messageSend(PushMessage pushMessage) { public void messageSend(PushMessage pushMessage) {
if (StrUtil.isBlank(pushMessage.getSendUserId()) || StrUtil.isBlank(pushMessage.getMessageSecret())) { if (StrUtil.isBlank(pushMessage.getFromUserId()) || StrUtil.isBlank(pushMessage.getMessageSecret())) {
throw new CheckedException("当前发送用户ID与推送应用密钥必填!"); throw new CheckedException("当前发送方用户ID与推送应用密钥必填!");
}
R<User> result = remoteUserService.selectByUserId(pushMessage.getFromUserId(), SecurityConstants.FROM_IN);
if (result == null || result.getData() == null) {
throw new UsernameNotFoundException("用户不存在");
} }
R<User> user = remoteUserService.selectByUserId(pushMessage.getSendUserId(), SecurityConstants.FROM_IN);
// 第三方发送前较验数据 // 第三方发送前较验数据
if (user.getData().getUserType().equals("9")) { if (result.getData().getUserType().equals("9")) {
PushApplication pushApplication = baseMapper.selectOne(Wrappers.<PushApplication>lambdaQuery() PushApplication pushApplication = baseMapper.selectOne(Wrappers.<PushApplication>lambdaQuery()
.eq(PushApplication::getMessageSecret, pushMessage.getMessageSecret()).eq(PushApplication::getStatus, "0")); .eq(PushApplication::getMessageSecret, pushMessage.getMessageSecret()).eq(PushApplication::getStatus, "0"));
pushApplication = Optional.of(pushApplication).orElseThrow(() -> new CheckedException("你当前没有权限发送消息,请联系管理员!")); pushApplication = Optional.of(pushApplication).orElseThrow(() -> new CheckedException("你当前没有权限发送消息,请联系管理员!"));
@ -77,25 +75,48 @@ public class PushApplicationServiceImpl extends ServiceImpl<PushApplicationMappe
// 查询当前用户设置的自定义快捷方式消息提醒属性 // 查询当前用户设置的自定义快捷方式消息提醒属性
PushCustomType pushCustomType = iPushCustomTypeService.getOne(Wrappers.<PushCustomType>lambdaQuery() PushCustomType pushCustomType = iPushCustomTypeService.getOne(Wrappers.<PushCustomType>lambdaQuery()
.eq(PushCustomType::getId, pushMessage.getCustomTypeId()).eq(PushCustomType::getCreateById, pushMessage.getSendUserId())); .eq(PushCustomType::getId, pushMessage.getCustomTypeId()).eq(PushCustomType::getCreateById, pushMessage.getFromUserId()));
if (ObjectUtil.isNotEmpty(pushCustomType)) { if (ObjectUtil.isNotEmpty(pushCustomType)) {
pushMessage.setPlaySound(pushCustomType.getPlaySound()); pushMessage.setPlaySound(pushCustomType.getPlaySound());
pushMessage.setPlayVibrate(pushCustomType.getPlayVibrate()); pushMessage.setPlayVibrate(pushCustomType.getPlayVibrate());
pushMessage.setPlayLights(pushCustomType.getPlayLights()); pushMessage.setPlayLights(pushCustomType.getPlayLights());
if (StrUtil.isNotBlank(pushCustomType.getCustomPlayFileName())) if (StrUtil.isNotBlank(pushCustomType.getCustomPlayFileName())) pushMessage.setCustomPlayFileName(pushCustomType.getCustomPlayFileName());
pushMessage.setCustomPlayFileName(pushCustomType.getCustomPlayFileName());
} }
List<String> blacklist = iPushPassListService.list(Wrappers.<PushPassList>lambdaQuery()
.eq(PushPassList::getFromPushId, pushMessage.getFromUserId()).eq(PushPassList::getType, "0"))
.stream().map(item -> item.getToPushId()).collect(Collectors.toList());
List<String> whitelist = iPushPassListService.list(Wrappers.<PushPassList>lambdaQuery()
.eq(PushPassList::getFromPushId, pushMessage.getFromUserId()).eq(PushPassList::getType, "1"))
.stream().map(item -> item.getToPushId()).collect(Collectors.toList());
List<PushUserManage> pushList = iPushUserManageService.list(Wrappers.<PushUserManage>lambdaQuery() List<PushUserManage> pushList = iPushUserManageService.list(Wrappers.<PushUserManage>lambdaQuery()
.eq(PushUserManage::getCreateById, pushMessage.getSendUserId()).eq(PushUserManage::getStatus, "1")); .eq(PushUserManage::getFromUserId, pushMessage.getFromUserId()).eq(PushUserManage::getStatus, "1"))
if (ObjectUtil.isNotEmpty(pushList)) { .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());
});
}
private void send(PushMessage pushMessage, String toUserId, String playSound, String playVibrate, String playLights) {
// 设置默认数据 // 设置默认数据
pushMessage.setDisplayType("notification"); pushMessage.setDisplayType("notification");
pushMessage.setAliasType(PushClientUtil.uPushUserAliasType); pushMessage.setAliasType(PushClientUtil.uPushUserAliasType);
pushMessage.setAlias(pushList.stream().map(item -> item.getToUserId()).collect(Collectors.joining(","))); pushMessage.setAlias(toUserId);
JSONObject custom = new JSONObject(); JSONObject custom = new JSONObject();
custom.put("sendUserId", pushMessage.getSendUserId()); custom.put("sendUserId", pushMessage.getFromUserId());
custom.put("customPlayFileName", pushMessage.getCustomPlayFileName()); 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()); pushMessage.setCustom(custom.toString());
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put("production_mode", PushClientUtil.PRODUCTION_MODE); jsonObject.put("production_mode", PushClientUtil.PRODUCTION_MODE);
@ -112,9 +133,9 @@ public class PushApplicationServiceImpl extends ServiceImpl<PushApplicationMappe
body.put("sound", pushMessage.getSound()); body.put("sound", pushMessage.getSound());
body.put("after_open", "go_custom"); body.put("after_open", "go_custom");
body.put("custom", pushMessage.getCustom()); body.put("custom", pushMessage.getCustom());
body.put("play_sound", pushMessage.getPlaySound()); body.put("play_sound", BooleanUtil.toBoolean(playSound) ? false : !BooleanUtil.toBoolean(pushMessage.getPlaySound()));
body.put("play_vibrate", pushMessage.getPlayVibrate()); body.put("play_vibrate", BooleanUtil.toBoolean(playVibrate) ? false : !BooleanUtil.toBoolean(pushMessage.getPlayVibrate()));
body.put("play_lights", pushMessage.getPlayLights()); body.put("play_lights", BooleanUtil.toBoolean(playLights) ? false : !BooleanUtil.toBoolean(pushMessage.getPlayLights()));
payload.put("body", body); payload.put("body", body);
jsonObject.put("payload", payload); jsonObject.put("payload", payload);
JSONObject policy = new JSONObject(); JSONObject policy = new JSONObject();
@ -129,6 +150,5 @@ public class PushApplicationServiceImpl extends ServiceImpl<PushApplicationMappe
e.printStackTrace(); e.printStackTrace();
} }
} }
}
} }

Loading…
Cancel
Save