Skip to content

Commit 837f679

Browse files
committed
feat: 短信配置
1 parent 1044b68 commit 837f679

38 files changed

Lines changed: 1980 additions & 3 deletions

continew-common/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454
<artifactId>mysql-connector-j</artifactId>
5555
</dependency>
5656

57+
<!-- postgresql Java 驱动 -->
58+
<!-- <dependency>-->
59+
<!-- <groupId>org.postgresql</groupId>-->
60+
<!-- <artifactId>postgresql</artifactId>-->
61+
<!-- </dependency>-->
62+
5763
<!-- ContiNew Starter 扩展模块 - CURD(增删改查) -->
5864
<dependency>
5965
<groupId>top.continew</groupId>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.admin.common.enums;
18+
19+
import lombok.Getter;
20+
import lombok.RequiredArgsConstructor;
21+
import top.continew.starter.core.enums.BaseEnum;
22+
23+
/**
24+
* 短信配置方法类型枚举
25+
*
26+
* @author luoqiz
27+
* @since 2025/03/16 13:38
28+
*/
29+
@Getter
30+
@RequiredArgsConstructor
31+
public enum MethodTypeEnum implements BaseEnum<String> {
32+
33+
/**
34+
* 新增
35+
*/
36+
ADD("add", "新增"),
37+
38+
/**
39+
* 更新
40+
*/
41+
UPDATE("update", "更新"),
42+
43+
/**
44+
* 删除
45+
*/
46+
DELETE("delete", "删除"),
47+
/**
48+
* 删除
49+
*/
50+
SEARCH("search", "查询"),;
51+
52+
private final String value;
53+
private final String description;
54+
55+
}

continew-module-system/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,12 @@
1818
<groupId>top.continew</groupId>
1919
<artifactId>continew-common</artifactId>
2020
</dependency>
21+
22+
<dependency>
23+
<groupId>org.dromara.sms4j</groupId>
24+
<artifactId>sms4j-spring-boot-starter</artifactId>
25+
<version>3.3.4</version>
26+
</dependency>
27+
2128
</dependencies>
2229
</project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.admin.system.config.sms;
18+
19+
import jakarta.annotation.Resource;
20+
import lombok.extern.slf4j.Slf4j;
21+
import org.dromara.sms4j.core.factory.SmsFactory;
22+
import org.dromara.sms4j.core.proxy.SmsProxyFactory;
23+
import org.springframework.context.event.ContextRefreshedEvent;
24+
import org.springframework.context.event.EventListener;
25+
import org.springframework.stereotype.Component;
26+
27+
@Slf4j
28+
@Component
29+
public class SmsBlendConfig {
30+
31+
@Resource
32+
SmsJdbcReadConfig config;
33+
34+
@Resource
35+
private SmsRecordProcessor smsRecordProcessor;
36+
37+
@EventListener
38+
public void init(ContextRefreshedEvent event) {
39+
log.info("初始化短信配置");
40+
// 创建SmsBlend 短信实例
41+
SmsFactory.createSmsBlend(config);
42+
SmsProxyFactory.addPreProcessor(smsRecordProcessor);
43+
log.info("初始化短信配置完成");
44+
}
45+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.admin.system.config.sms;
18+
19+
import jakarta.annotation.Resource;
20+
import org.dromara.sms4j.core.datainterface.SmsReadConfig;
21+
import org.dromara.sms4j.provider.config.BaseConfig;
22+
import org.springframework.stereotype.Component;
23+
import top.continew.admin.system.config.sms.utils.SmsConvertUtils;
24+
import top.continew.admin.system.model.query.SmsConfigQuery;
25+
import top.continew.admin.system.model.resp.SmsConfigDetailResp;
26+
import top.continew.admin.system.model.resp.SmsConfigResp;
27+
import top.continew.admin.system.service.SmsConfigService;
28+
import top.continew.starter.extension.crud.model.query.SortQuery;
29+
30+
import java.util.ArrayList;
31+
import java.util.List;
32+
33+
@Component
34+
public class SmsJdbcReadConfig implements SmsReadConfig {
35+
36+
@Resource
37+
private SmsConfigService smsConfigService;
38+
39+
@Override
40+
public BaseConfig getSupplierConfig(String configId) {
41+
Long id = Long.parseLong(configId);
42+
SmsConfigDetailResp smsConfig = smsConfigService.get(id);
43+
if (smsConfig == null || !smsConfig.getIsEnable()) {
44+
return null;
45+
}
46+
return SmsConvertUtils.smsConfig2BaseConfig(smsConfig);
47+
}
48+
49+
@Override
50+
public List<BaseConfig> getSupplierConfigList() {
51+
SmsConfigQuery smsConfigQuery = new SmsConfigQuery();
52+
smsConfigQuery.setIsEnable(true);
53+
SortQuery sortQuery = new SortQuery();
54+
sortQuery.setSort(new String[] {"id", "desc"});
55+
List<SmsConfigResp> smsConfigList = smsConfigService.list(smsConfigQuery, sortQuery);
56+
List<BaseConfig> baseConfigList = new ArrayList<>();
57+
for (SmsConfigResp smsConfigResp : smsConfigList) {
58+
baseConfigList.add(SmsConvertUtils.smsConfig2BaseConfig(smsConfigResp));
59+
}
60+
return baseConfigList;
61+
}
62+
63+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.admin.system.config.sms;
18+
19+
import cn.hutool.json.JSONUtil;
20+
import jakarta.annotation.Resource;
21+
import lombok.extern.slf4j.Slf4j;
22+
import org.dromara.sms4j.api.entity.SmsResponse;
23+
import org.dromara.sms4j.api.proxy.CoreMethodProcessor;
24+
import org.springframework.stereotype.Component;
25+
import top.continew.admin.system.model.req.SmsRecordReq;
26+
import top.continew.admin.system.service.SmsRecordService;
27+
28+
import java.lang.reflect.Method;
29+
import java.util.LinkedHashMap;
30+
import java.util.List;
31+
32+
@Slf4j
33+
@Component
34+
public class SmsRecordProcessor implements CoreMethodProcessor {
35+
36+
@Resource
37+
private SmsRecordService smsRecordService;
38+
39+
@Override
40+
public Object[] preProcessor(Method method, Object source, Object[] param) {
41+
return CoreMethodProcessor.super.preProcessor(method, source, param);
42+
}
43+
44+
@Override
45+
public void sendMessagePreProcess(String phone, Object message) {
46+
System.out.println("发送短信前处理" + phone + message);
47+
}
48+
49+
@Override
50+
public void sendMessageByTemplatePreProcess(String phone,
51+
String templateId,
52+
LinkedHashMap<String, String> messages) {
53+
log.debug("发送短信前处理 sendMessageByTemplatePreProcess " + phone + templateId + JSONUtil.toJsonPrettyStr(messages));
54+
}
55+
56+
@Override
57+
public void massTextingPreProcess(List<String> phones, String message) {
58+
log.debug("发送短信前处理 massTextingPreProcess " + JSONUtil.toJsonPrettyStr(phones) + message);
59+
}
60+
61+
@Override
62+
public void massTextingByTemplatePreProcess(List<String> phones,
63+
String templateId,
64+
LinkedHashMap<String, String> messages) {
65+
log.debug("发送短信前处理 massTextingByTemplatePreProcess " + JSONUtil.toJsonPrettyStr(phones) + JSONUtil
66+
.toJsonPrettyStr(messages));
67+
}
68+
69+
@Override
70+
public Object postProcessor(SmsResponse result, Object[] param) {
71+
SmsRecordReq record = new SmsRecordReq();
72+
record.setConfigId(Long.parseLong(result.getConfigId()));
73+
record.setPhone(param[0].toString());
74+
record.setParams(JSONUtil.toJsonPrettyStr(param[1]));
75+
record.setStatus(result.isSuccess());
76+
record.setResMsg(JSONUtil.toJsonPrettyStr(result.getData()));
77+
smsRecordService.add(record);
78+
return CoreMethodProcessor.super.postProcessor(result, param);
79+
}
80+
81+
@Override
82+
public void exceptionHandleProcessor(Method method,
83+
Object source,
84+
Object[] param,
85+
Exception exception) throws RuntimeException {
86+
CoreMethodProcessor.super.exceptionHandleProcessor(method, source, param, exception);
87+
}
88+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.admin.system.config.sms.event;
18+
19+
import lombok.Data;
20+
import top.continew.admin.common.enums.MethodTypeEnum;
21+
22+
import java.io.Serializable;
23+
24+
@Data
25+
public class SmsEventMessage implements Serializable {
26+
27+
private static final long serialVersionUID = 1L;
28+
29+
private final MethodTypeEnum type;
30+
private final String configId;
31+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.admin.system.config.sms.event;
18+
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.data.redis.connection.RedisConnectionFactory;
22+
import org.springframework.data.redis.listener.ChannelTopic;
23+
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
24+
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
25+
26+
@Configuration
27+
public class SmsRedisConfig {
28+
29+
public static final String SysSmsChannel = "system:sms:topic";
30+
31+
@Bean
32+
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
33+
MessageListenerAdapter listenerAdapter) {
34+
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
35+
container.setConnectionFactory(connectionFactory);
36+
container.addMessageListener(listenerAdapter, new ChannelTopic(SysSmsChannel));
37+
return container;
38+
}
39+
40+
@Bean
41+
MessageListenerAdapter listenerAdapter(SmsRedisMessageSubscriber subscriber) {
42+
return new MessageListenerAdapter(subscriber, "onMessage");
43+
}
44+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.admin.system.config.sms.event;
18+
19+
import jakarta.annotation.Resource;
20+
import lombok.extern.slf4j.Slf4j;
21+
import org.springframework.data.redis.core.RedisTemplate;
22+
import org.springframework.stereotype.Component;
23+
24+
@Slf4j
25+
@Component
26+
public class SmsRedisMessagePublisher {
27+
28+
@Resource
29+
private RedisTemplate redisTemplate;
30+
31+
public void publish(String channel, SmsEventMessage message) {
32+
redisTemplate.convertAndSend(channel, message);
33+
log.debug("Message published to Redis channel [" + channel + "]: " + message);
34+
}
35+
}

0 commit comments

Comments
 (0)