perf:调整消息通知

This commit is contained in:
曾文豪
2023-03-31 14:21:07 +08:00
parent 1c0e63bf98
commit 8d9632bcaa
8 changed files with 145 additions and 70 deletions

View File

@@ -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
///////////////////////////////////////////////////////////////////////////

View File

@@ -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;
}

View File

@@ -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));
}
}

View File

@@ -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();
}

View File

@@ -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 {
}