perf:调整消息通知
This commit is contained in:
@@ -4,10 +4,10 @@ import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import com.tiesheng.annotation.token.TokenIgnore;
|
||||
import com.tiesheng.core.service.CoreMessageService;
|
||||
import com.tiesheng.demo.pojos.TestFile;
|
||||
import com.tiesheng.login.config.token.TsTokenConfig;
|
||||
import com.tiesheng.login.config.token.bean.TokenBean;
|
||||
import com.tiesheng.message.config.aliyun.AliyunSmsConfig;
|
||||
import com.tiesheng.message.pojos.MessageReqResp;
|
||||
import com.tiesheng.util.config.EncryptConfig;
|
||||
import com.tiesheng.util.config.GlobalConfig;
|
||||
@@ -31,13 +31,13 @@ public class TestController {
|
||||
@Autowired
|
||||
GlobalConfig globalConfig;
|
||||
@Autowired
|
||||
AliyunSmsConfig aliyunSmsConfig;
|
||||
@Autowired
|
||||
TsTokenConfig tsTokenConfig;
|
||||
@Autowired
|
||||
Ip2regionConfig ip2regionConfig;
|
||||
@Autowired
|
||||
EncryptConfig encryptConfig;
|
||||
@Autowired
|
||||
CoreMessageService coreMessageService;
|
||||
|
||||
|
||||
@RequestMapping("/index")
|
||||
@@ -58,9 +58,12 @@ public class TestController {
|
||||
|
||||
|
||||
@RequestMapping("/send")
|
||||
@TokenIgnore
|
||||
public ApiResp<MessageReqResp> sendMessage() {
|
||||
MessageReqResp reqResp = aliyunSmsConfig.sendSms("13567116463", "SMS_154950909",
|
||||
JSONUtil.createObj().putOpt("code", "123456"));
|
||||
|
||||
MessageReqResp reqResp = coreMessageService.send("13567116463", "SMS_154950909",
|
||||
JSONUtil.createObj().putOpt("code", "123456"), "sms");
|
||||
|
||||
return ApiResp.respOK(reqResp);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,8 +7,7 @@ import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.tiesheng.message.pojos.MessageReqResp;
|
||||
import com.tiesheng.message.service.TieshengMessageConfigurer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import com.tiesheng.message.service.TieshengMessageSender;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -22,16 +21,13 @@ import java.util.*;
|
||||
*/
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "tiesheng.aliyun")
|
||||
public class AliyunSmsConfig {
|
||||
public class AliyunSmsConfig implements TieshengMessageSender {
|
||||
|
||||
/**
|
||||
* 请求地址
|
||||
*/
|
||||
private static final String ENDPOINT = "https://dysmsapi.aliyuncs.com";
|
||||
|
||||
@Autowired
|
||||
TieshengMessageConfigurer tieshengMessageConfigurer;
|
||||
|
||||
private String accessKeyId;
|
||||
private String accessKeySecret;
|
||||
private String signName;
|
||||
@@ -62,7 +58,7 @@ public class AliyunSmsConfig {
|
||||
*/
|
||||
public MessageReqResp sendSms(String phoneNumbers, String templateCode, JSONObject params) {
|
||||
|
||||
MessageReqResp reqResp = new MessageReqResp();
|
||||
MessageReqResp reqResp = new MessageReqResp("阿里云短信");
|
||||
reqResp.setTarget(phoneNumbers);
|
||||
reqResp.setResult(1);
|
||||
reqResp.setContent(JSONUtil.createObj()
|
||||
@@ -115,11 +111,19 @@ public class AliyunSmsConfig {
|
||||
reqResp.setToast(respObj.getStr("Message"));
|
||||
}
|
||||
|
||||
tieshengMessageConfigurer.onMessageSend("阿里云短信", reqResp);
|
||||
|
||||
return reqResp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageReqResp send(String user, String title, Object body) {
|
||||
return sendSms(user, title, (JSONObject) body);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getChannel() {
|
||||
return "sms";
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// setter\getter
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.tiesheng.message.pojos;
|
||||
|
||||
public class MessageReqResp {
|
||||
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 发送对象
|
||||
*/
|
||||
@@ -28,10 +30,22 @@ public class MessageReqResp {
|
||||
*/
|
||||
private String toast;
|
||||
|
||||
public MessageReqResp(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// setter\getter
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.tiesheng.message.service;
|
||||
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import com.tiesheng.message.pojos.MessageReqResp;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
*/
|
||||
public interface TieshengMessageConfigurer {
|
||||
|
||||
|
||||
/**
|
||||
* 消息发送后
|
||||
*
|
||||
* @param reqResp
|
||||
*/
|
||||
default void onMessageSend(String type, MessageReqResp reqResp) {
|
||||
LogFactory.get().info(JSONUtil.toJsonStr(reqResp));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.tiesheng.message.service;
|
||||
|
||||
|
||||
import com.tiesheng.message.pojos.MessageReqResp;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
*/
|
||||
public interface TieshengMessageSender {
|
||||
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*
|
||||
* @param user
|
||||
* @param title
|
||||
* @param content
|
||||
* @param body
|
||||
* @param channel 消息通道,如果为空表示所有通道
|
||||
* @return 返回的内容,如果为空表示发送成功
|
||||
*/
|
||||
MessageReqResp send(String user, String title, Object body);
|
||||
|
||||
|
||||
/**
|
||||
* 获取通道
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getChannel();
|
||||
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.tiesheng.message.service.impl;
|
||||
|
||||
import com.tiesheng.message.service.TieshengMessageConfigurer;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(value = TieshengMessageConfigurer.class, ignored = DefaultMessageConfigurer.class)
|
||||
public class DefaultMessageConfigurer implements TieshengMessageConfigurer {
|
||||
|
||||
}
|
||||
@@ -10,12 +10,13 @@ import com.tiesheng.core.mapper.CoreLogMessageMapper;
|
||||
import com.tiesheng.core.mapper.CoreLogOperationMapper;
|
||||
import com.tiesheng.core.mapper.CoreLogProcessMapper;
|
||||
import com.tiesheng.core.pojos.RequestUserInfo;
|
||||
import com.tiesheng.core.pojos.dao.*;
|
||||
import com.tiesheng.core.pojos.dao.CoreLogLogin;
|
||||
import com.tiesheng.core.pojos.dao.CoreLogOperation;
|
||||
import com.tiesheng.core.pojos.dao.CoreLogProcess;
|
||||
import com.tiesheng.core.pojos.dao.CorePlatformUnique;
|
||||
import com.tiesheng.core.pojos.vo.ProcessDetailVo;
|
||||
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.service.TieshengMessageConfigurer;
|
||||
import com.tiesheng.util.ServletKit;
|
||||
import com.tiesheng.util.config.Ip2regionConfig;
|
||||
import com.tiesheng.util.exception.ApiException;
|
||||
@@ -28,7 +29,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
* @author hao
|
||||
*/
|
||||
@Service
|
||||
public class CoreLogService extends TsServiceBase<CoreLogOperationMapper, CoreLogOperation> implements TieshengMessageConfigurer {
|
||||
public class CoreLogService extends TsServiceBase<CoreLogOperationMapper, CoreLogOperation> {
|
||||
|
||||
@Autowired
|
||||
TieshengWebConfigurer tieshengWebConfigurer;
|
||||
@@ -144,22 +145,4 @@ public class CoreLogService extends TsServiceBase<CoreLogOperationMapper, CoreLo
|
||||
coreLogLoginMapper.insert(login);
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// 消息日志
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* 添加消息日志
|
||||
*
|
||||
* @param type
|
||||
* @param reqResp
|
||||
*/
|
||||
@Override
|
||||
public void onMessageSend(String type, MessageReqResp reqResp) {
|
||||
CoreLogMessage coreLogMessage = BeanUtil.copyProperties(reqResp, CoreLogMessage.class);
|
||||
coreLogMessage.setType(type);
|
||||
coreLogMessageMapper.insert(coreLogMessage);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.tiesheng.core.service;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.tiesheng.core.mapper.CoreLogMessageMapper;
|
||||
import com.tiesheng.core.pojos.dao.CoreLogMessage;
|
||||
import com.tiesheng.message.pojos.MessageReqResp;
|
||||
import com.tiesheng.message.service.TieshengMessageSender;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
*/
|
||||
@Service
|
||||
public class CoreMessageService {
|
||||
|
||||
@Autowired
|
||||
List<TieshengMessageSender> messageSenderList;
|
||||
@Autowired
|
||||
CoreLogMessageMapper coreLogMessageMapper;
|
||||
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*
|
||||
* @param user
|
||||
* @param title
|
||||
* @param body
|
||||
* @param channels 消息通道,如果为all时,表示发送全部通道
|
||||
*/
|
||||
public void multiple(String user, String title, Object body, String... channels) {
|
||||
if (StrUtil.isEmpty(user) || ArrayUtil.isEmpty(channels)) {
|
||||
return;
|
||||
}
|
||||
|
||||
messageSenderList.forEach(sender -> {
|
||||
if (ArrayUtil.contains(channels, sender.getChannel()) || ArrayUtil.contains(channels, "all")) {
|
||||
MessageReqResp reqResp = sender.send(user, title, body);
|
||||
coreLogMessageMapper.insert(BeanUtil.copyProperties(reqResp, CoreLogMessage.class));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*
|
||||
* @param user
|
||||
* @param title
|
||||
* @param body
|
||||
*/
|
||||
public MessageReqResp send(String user, String title, Object body, String channel) {
|
||||
if (StrUtil.isEmpty(user) || StrUtil.isEmpty(channel)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
TieshengMessageSender messageSender = CollUtil.findOne(messageSenderList,
|
||||
sender -> Objects.equals(sender.getChannel(), channel));
|
||||
if (messageSender != null) {
|
||||
MessageReqResp reqResp = messageSender.send(user, title, body);
|
||||
coreLogMessageMapper.insert(BeanUtil.copyProperties(reqResp, CoreLogMessage.class));
|
||||
return reqResp;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user