perf:调整消息发送接口和方法

This commit is contained in:
曾文豪
2023-05-22 11:30:20 +08:00
parent e29e9552ac
commit 206a6dc1ff
6 changed files with 55 additions and 25 deletions

View File

@@ -2,7 +2,6 @@ package com.tiesheng.demo.controller;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.TimeInterval; import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import cn.hutool.log.LogFactory; import cn.hutool.log.LogFactory;
import cn.hutool.poi.excel.ExcelUtil; import cn.hutool.poi.excel.ExcelUtil;
@@ -17,15 +16,14 @@ import com.tiesheng.database.config.DbBackupConfig;
import com.tiesheng.demo.pojos.PoiBean; import com.tiesheng.demo.pojos.PoiBean;
import com.tiesheng.demo.pojos.TestFile; import com.tiesheng.demo.pojos.TestFile;
import com.tiesheng.login.config.token.TsTokenConfig; import com.tiesheng.login.config.token.TsTokenConfig;
import com.tiesheng.login.config.token.bean.TokenBean;
import com.tiesheng.message.pojos.MessageReqResp; import com.tiesheng.message.pojos.MessageReqResp;
import com.tiesheng.message.pojos.UserChannel;
import com.tiesheng.util.config.EncryptConfig; import com.tiesheng.util.config.EncryptConfig;
import com.tiesheng.util.config.GlobalConfig; import com.tiesheng.util.config.GlobalConfig;
import com.tiesheng.util.config.Ip2regionConfig; import com.tiesheng.util.config.Ip2regionConfig;
import com.tiesheng.util.pojos.ApiResp; import com.tiesheng.util.pojos.ApiResp;
import com.tiesheng.util.pojos.FileUploadPath; import com.tiesheng.util.pojos.FileUploadPath;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@@ -67,7 +65,7 @@ public class TestController {
@TokenIgnore @TokenIgnore
public void redirect(HttpServletResponse response) { public void redirect(HttpServletResponse response) {
ArrayList<String> strings = CollUtil.newArrayList("11111","22222"); ArrayList<String> strings = CollUtil.newArrayList("11111", "22222");
coreLogService.addProcess("fdfd", strings, new ProcessImportConsumer<String>() { coreLogService.addProcess("fdfd", strings, new ProcessImportConsumer<String>() {
@Override @Override
public int accept(List<String> list) { public int accept(List<String> list) {
@@ -94,8 +92,8 @@ public class TestController {
@TokenIgnore @TokenIgnore
public ApiResp<MessageReqResp> sendMessage() { public ApiResp<MessageReqResp> sendMessage() {
MessageReqResp reqResp = coreMessageService.send("13567116463", "SMS_154950909", MessageReqResp reqResp = coreMessageService.send(new UserChannel("13567116463", "sms"),
JSONUtil.createObj().putOpt("code", "123456"), "sms"); JSONUtil.createObj().putOpt("code", "123456").putOpt("template_code", "SMS_154950909"));
return ApiResp.respOK(reqResp); return ApiResp.respOK(reqResp);
} }

View File

@@ -138,8 +138,8 @@ public class AliyunSmsSender implements TieshengMessageSender {
} }
@Override @Override
public MessageReqResp send(String user, String title, JSONObject body) { public MessageReqResp send(String user, JSONObject body) {
return sendSms(user, title, (JSONObject) body); return sendSms(user, body.getStr("template_code"), body);
} }
@Override @Override

View File

@@ -24,7 +24,6 @@ public class MessageReqResp {
*/ */
private Integer result; private Integer result;
/** /**
* 提示的异常信息 * 提示的异常信息
*/ */

View File

@@ -0,0 +1,32 @@
package com.tiesheng.message.pojos;
public class UserChannel {
private String user;
private String channel;
public UserChannel(String user, String channel) {
this.user = user;
this.channel = channel;
}
///////////////////////////////////////////////////////////////////////////
// setter\getter
///////////////////////////////////////////////////////////////////////////
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getChannel() {
return channel;
}
public void setChannel(String channel) {
this.channel = channel;
}
}

View File

@@ -20,7 +20,7 @@ public interface TieshengMessageSender {
* @param channel 消息通道,如果为空表示所有通道 * @param channel 消息通道,如果为空表示所有通道
* @return 返回的内容,如果为空表示发送成功 * @return 返回的内容,如果为空表示发送成功
*/ */
MessageReqResp send(String user, String title, JSONObject body); MessageReqResp send(String user, JSONObject body);
/** /**

View File

@@ -8,6 +8,7 @@ import cn.hutool.json.JSONObject;
import com.tiesheng.core.mapper.CoreLogMessageMapper; import com.tiesheng.core.mapper.CoreLogMessageMapper;
import com.tiesheng.core.pojos.dao.CoreLogMessage; import com.tiesheng.core.pojos.dao.CoreLogMessage;
import com.tiesheng.message.pojos.MessageReqResp; import com.tiesheng.message.pojos.MessageReqResp;
import com.tiesheng.message.pojos.UserChannel;
import com.tiesheng.message.service.TieshengMessageSender; import com.tiesheng.message.service.TieshengMessageSender;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -30,22 +31,21 @@ public class CoreMessageService {
/** /**
* 发送消息 * 发送消息
* *
* @param user
* @param title * @param title
* @param body * @param body
* @param channels 消息通道如果为all时表示发送全部通道 * @param channels 消息通道如果为all时表示发送全部通道
*/ */
public void multiple(String user, String title, JSONObject body, String... channels) { public void multiple(List<UserChannel> channels, JSONObject body) {
if (StrUtil.isEmpty(user) || ArrayUtil.isEmpty(channels)) { if (ArrayUtil.isEmpty(channels)) {
return; return;
} }
messageSenderList.forEach(sender -> { messageSenderList.stream().filter(TieshengMessageSender::support)
boolean isChannel = ArrayUtil.contains(channels, sender.getChannel()) || ArrayUtil.contains(channels, "all"); .forEach(sender -> {
if (sender.support() && isChannel) { channels.stream().filter(it -> Objects.equals(it.getChannel(), sender.getChannel())).forEach(it -> {
MessageReqResp reqResp = sender.send(user, title, body); MessageReqResp reqResp = sender.send(it.getUser(), body);
coreLogMessageMapper.insert(BeanUtil.copyProperties(reqResp, CoreLogMessage.class)); coreLogMessageMapper.insert(BeanUtil.copyProperties(reqResp, CoreLogMessage.class));
} });
}); });
} }
@@ -53,19 +53,20 @@ public class CoreMessageService {
/** /**
* 发送消息 * 发送消息
* *
* @param user * @param userChannel
* @param title * @param title
* @param body * @param body
*/ */
public MessageReqResp send(String user, String title, JSONObject body, String channel) { public MessageReqResp send(UserChannel userChannel, JSONObject body) {
if (StrUtil.isEmpty(user) || StrUtil.isEmpty(channel)) {
if (StrUtil.isEmpty(userChannel.getUser()) || StrUtil.isEmpty(userChannel.getChannel())) {
return null; return null;
} }
TieshengMessageSender messageSender = CollUtil.findOne(messageSenderList, TieshengMessageSender messageSender = CollUtil.findOne(messageSenderList,
sender -> Objects.equals(sender.getChannel(), channel) && sender.support()); sender -> Objects.equals(sender.getChannel(), userChannel.getChannel()) && sender.support());
if (messageSender != null) { if (messageSender != null) {
MessageReqResp reqResp = messageSender.send(user, title, body); MessageReqResp reqResp = messageSender.send(userChannel.getUser(), body);
coreLogMessageMapper.insert(BeanUtil.copyProperties(reqResp, CoreLogMessage.class)); coreLogMessageMapper.insert(BeanUtil.copyProperties(reqResp, CoreLogMessage.class));
return reqResp; return reqResp;
} }