|
|
@@ -1,9 +1,8 @@
|
|
|
package cn.iocoder.yudao.module.system.service.appnotify;
|
|
|
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
-import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
|
|
-import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyTemplateDO;
|
|
|
-import cn.iocoder.yudao.module.system.service.notify.NotifyTemplateService;
|
|
|
+import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyMessageDO;
|
|
|
+import cn.iocoder.yudao.module.system.service.notify.NotifyMessageService;
|
|
|
+import cn.iocoder.yudao.module.system.service.notify.NotifySendService;
|
|
|
import com.aliyun.auth.credentials.utils.ParameterHelper;
|
|
|
import com.aliyuncs.DefaultAcsClient;
|
|
|
import com.aliyuncs.IAcsClient;
|
|
|
@@ -12,7 +11,6 @@ import com.aliyuncs.exceptions.ServerException;
|
|
|
import com.aliyuncs.profile.DefaultProfile;
|
|
|
import com.aliyuncs.push.model.v20160801.PushRequest;
|
|
|
import com.aliyuncs.push.model.v20160801.PushResponse;
|
|
|
-import com.google.common.annotations.VisibleForTesting;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
@@ -21,11 +19,6 @@ import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
import java.util.Date;
|
|
|
import java.util.Map;
|
|
|
-import java.util.Objects;
|
|
|
-
|
|
|
-import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
-import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND;
|
|
|
-import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTIFY_SEND_TEMPLATE_PARAM_MISS;
|
|
|
|
|
|
/**
|
|
|
* App消息推送服务实现类(阿里云推送)
|
|
|
@@ -96,7 +89,9 @@ public class AppNotifySendServiceImpl implements AppNotifySendService {
|
|
|
*/
|
|
|
|
|
|
@Autowired
|
|
|
- private NotifyTemplateService notifyTemplateService;
|
|
|
+ private NotifySendService notifySendService;
|
|
|
+ @Autowired
|
|
|
+ private NotifyMessageService notifyMessageService;
|
|
|
|
|
|
|
|
|
@Override
|
|
|
@@ -110,14 +105,15 @@ public class AppNotifySendServiceImpl implements AppNotifySendService {
|
|
|
}
|
|
|
|
|
|
// 2. 第二步:通过notifyId获取站内信已经发送的消息,进行消息复制发送
|
|
|
- String content = getMessageByNotify(userId, templateCode, templateParams);
|
|
|
+ NotifyMessageDO notifyMessage = getMessageByNotify(userId, templateCode, templateParams);
|
|
|
|
|
|
try {
|
|
|
// 3. 第三步:创建阿里云ACS客户端(修复Push类找不到的兼容方案)
|
|
|
IAcsClient client = createAcsClient();
|
|
|
|
|
|
// 4. 第四步:构建单设备推送请求(封装Android/iOS双端配置)
|
|
|
- PushRequest pushRequest = buildSingleDevicePushRequest(deviceId, "博士安全", content);
|
|
|
+ // PushRequest pushRequest = buildSingleDevicePushRequest(deviceId, "博士安全", notifyMessage.getTemplateContent());
|
|
|
+ PushRequest pushRequest = buildAllDevicePushRequest("博士安全", notifyMessage.getTemplateContent());
|
|
|
|
|
|
// 5. 第五步:发送推送请求,调用阿里云Push接口
|
|
|
PushResponse response = client.getAcsResponse(pushRequest);
|
|
|
@@ -145,7 +141,7 @@ public class AppNotifySendServiceImpl implements AppNotifySendService {
|
|
|
*
|
|
|
* @return 推送结果:true-成功,false-失败
|
|
|
*/
|
|
|
- public Boolean pushNotify() {
|
|
|
+ public Boolean pushAllNotify() {
|
|
|
// 前置校验:AppKey未配置直接返回失败
|
|
|
if (appKey == null || appKey == 0) {
|
|
|
log.error("App全量推送失败:未配置阿里云Push的appKey,请检查application.yml配置");
|
|
|
@@ -209,8 +205,8 @@ public class AppNotifySendServiceImpl implements AppNotifySendService {
|
|
|
|
|
|
// ========== 基础推送配置 ==========
|
|
|
request.setAppKey(appKey); // 关联的阿里云Push应用ID
|
|
|
- request.setTarget("DEVICE"); // 推送目标类型:DEVICE-按设备ID,ACCOUNT-按账号,TAG-按标签
|
|
|
- request.setTargetValue(deviceId); // 目标值:对应DEVICE类型的设备ID
|
|
|
+ request.setTarget("DEVICE"); // 推送目标类型:DEVICE-按设备ID,ACCOUNT-按账号,TAG-按标签,ALL-全部
|
|
|
+ request.setTargetValue(deviceId); // 目标值:对应DEVICE类型的设备ID,all-全量
|
|
|
request.setDeviceType("ALL"); // 推送设备类型:ALL-全部,ANDROID-安卓,IOS-iOS
|
|
|
request.setPushType("NOTICE"); // 推送类型:NOTICE-通知,MESSAGE-透传消息
|
|
|
|
|
|
@@ -330,45 +326,15 @@ public class AppNotifySendServiceImpl implements AppNotifySendService {
|
|
|
* 通过站内信获取相同发送信息
|
|
|
* @return
|
|
|
*/
|
|
|
- private String getMessageByNotify(Long userId, String templateCode, Map<String, Object> templateParams) {
|
|
|
- // 校验模版
|
|
|
- NotifyTemplateDO template = validateNotifyTemplate(templateCode);
|
|
|
- if (Objects.equals(template.getStatus(), CommonStatusEnum.DISABLE.getStatus())) {
|
|
|
- log.info("[sendSingleNotify][模版({})已经关闭,无法给用户({})发送]", templateCode, userId);
|
|
|
- return null;
|
|
|
- }
|
|
|
- // 校验参数
|
|
|
- validateTemplateParams(template, templateParams);
|
|
|
-
|
|
|
+ private NotifyMessageDO getMessageByNotify(Long userId, String templateCode, Map<String, Object> templateParams) {
|
|
|
// 发送站内信
|
|
|
- return StrUtil.format(template.getContent(), templateParams);
|
|
|
- }
|
|
|
-
|
|
|
- @VisibleForTesting
|
|
|
- public NotifyTemplateDO validateNotifyTemplate(String templateCode) {
|
|
|
- // 获得站内信模板。考虑到效率,从缓存中获取
|
|
|
- NotifyTemplateDO template = notifyTemplateService.getNotifyTemplateByCodeFromCache(templateCode);
|
|
|
- // 站内信模板不存在
|
|
|
- if (template == null) {
|
|
|
- throw exception(NOTICE_NOT_FOUND);
|
|
|
- }
|
|
|
- return template;
|
|
|
+ Long id = notifySendService.sendSingleNotifyToAdmin(userId,
|
|
|
+ templateCode, templateParams, 1);
|
|
|
+ NotifyMessageDO notifyMessage = notifyMessageService.getNotifyMessage(id);
|
|
|
+ // NotifyMessageDO notifyMessage = new NotifyMessageDO();
|
|
|
+ // notifyMessage.setTemplateContent("消息主体内容");
|
|
|
+ return notifyMessage;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 校验站内信模版参数是否确实
|
|
|
- *
|
|
|
- * @param template 邮箱模板
|
|
|
- * @param templateParams 参数列表
|
|
|
- */
|
|
|
- @VisibleForTesting
|
|
|
- public void validateTemplateParams(NotifyTemplateDO template, Map<String, Object> templateParams) {
|
|
|
- template.getParams().forEach(key -> {
|
|
|
- Object value = templateParams.get(key);
|
|
|
- if (value == null) {
|
|
|
- throw exception(NOTIFY_SEND_TEMPLATE_PARAM_MISS, key);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
|
|
|
}
|