Skip to content

Commit 0730487

Browse files
committed
feat(system/smsConfig): 短信配置新增设为默认功能
1 parent a2e156a commit 0730487

16 files changed

Lines changed: 85 additions & 26 deletions

File tree

continew-common/src/main/java/top/continew/admin/common/config/properties/CaptchaProperties.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,6 @@ public static class CaptchaSms {
8484
*/
8585
private long expirationInMinutes;
8686

87-
/**
88-
* 模板 ID
89-
*/
90-
private String templateId;
91-
92-
/**
93-
* 短信厂商
94-
*
95-
* @see top.continew.admin.system.model.resp.SmsConfigResp#supplier
96-
*/
97-
private String supplier;
98-
9987
/**
10088
* 验证码字段模板键名
10189
*/

continew-module-system/src/main/java/top/continew/admin/system/model/entity/SmsConfigDO.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ public class SmsConfigDO extends BaseDO {
9494
*/
9595
private String supplierConfig;
9696

97+
/**
98+
* 是否为默认存储
99+
*/
100+
private Boolean isDefault;
101+
97102
/**
98103
* 状态
99104
*/

continew-module-system/src/main/java/top/continew/admin/system/model/resp/SmsConfigResp.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ public class SmsConfigResp extends BaseDetailResp {
125125
@ExcelProperty(value = "各个厂商独立配置")
126126
private String supplierConfig;
127127

128+
/**
129+
* 是否为默认存储
130+
*/
131+
@Schema(description = "是否为默认存储", example = "true")
132+
@ExcelProperty(value = "是否为默认存储")
133+
private Boolean isDefault;
134+
128135
/**
129136
* 状态
130137
*/

continew-module-system/src/main/java/top/continew/admin/system/service/SmsConfigService.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package top.continew.admin.system.service;
1818

19+
import top.continew.admin.system.model.entity.SmsConfigDO;
1920
import top.continew.admin.system.model.query.SmsConfigQuery;
2021
import top.continew.admin.system.model.req.SmsConfigReq;
2122
import top.continew.admin.system.model.resp.SmsConfigResp;
@@ -27,4 +28,19 @@
2728
* @author luoqiz
2829
* @since 2025/03/15 18:41
2930
*/
30-
public interface SmsConfigService extends BaseService<SmsConfigResp, SmsConfigResp, SmsConfigQuery, SmsConfigReq> {}
31+
public interface SmsConfigService extends BaseService<SmsConfigResp, SmsConfigResp, SmsConfigQuery, SmsConfigReq> {
32+
33+
/**
34+
* 设置默认配置
35+
*
36+
* @param id ID
37+
*/
38+
void setDefaultConfig(Long id);
39+
40+
/**
41+
* 获取默认短信配置
42+
*
43+
* @return 默认短信配置
44+
*/
45+
SmsConfigDO getDefaultConfig();
46+
}

continew-module-system/src/main/java/top/continew/admin/system/service/StorageService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public interface StorageService extends BaseService<StorageResp, StorageResp, St
4545
*
4646
* @param id ID
4747
*/
48-
void setDefault(Long id);
48+
void setDefaultStorage(Long id);
4949

5050
/**
5151
* 查询默认存储

continew-module-system/src/main/java/top/continew/admin/system/service/impl/SmsConfigServiceImpl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
import org.dromara.sms4j.core.factory.SmsFactory;
2121
import org.dromara.sms4j.provider.config.BaseConfig;
2222
import org.springframework.stereotype.Service;
23+
import org.springframework.transaction.annotation.Transactional;
24+
import top.continew.admin.common.enums.DisEnableStatusEnum;
2325
import top.continew.admin.system.config.sms.SmsConfigUtil;
2426
import top.continew.admin.system.mapper.SmsConfigMapper;
2527
import top.continew.admin.system.model.entity.SmsConfigDO;
2628
import top.continew.admin.system.model.query.SmsConfigQuery;
2729
import top.continew.admin.system.model.req.SmsConfigReq;
2830
import top.continew.admin.system.model.resp.SmsConfigResp;
2931
import top.continew.admin.system.service.SmsConfigService;
32+
import top.continew.starter.core.validation.CheckUtils;
3033
import top.continew.starter.extension.crud.service.BaseServiceImpl;
3134

3235
import java.util.List;
@@ -62,6 +65,24 @@ public void afterDelete(List<Long> ids) {
6265
}
6366
}
6467

68+
@Override
69+
@Transactional(rollbackFor = Exception.class)
70+
public void setDefaultConfig(Long id) {
71+
SmsConfigDO smsConfig = super.getById(id);
72+
if (Boolean.TRUE.equals(smsConfig.getIsDefault())) {
73+
return;
74+
}
75+
// 启用状态才能设为默认配置
76+
CheckUtils.throwIfNotEqual(DisEnableStatusEnum.ENABLE, smsConfig.getStatus(), "请先启用所选配置");
77+
baseMapper.lambdaUpdate().eq(SmsConfigDO::getIsDefault, true).set(SmsConfigDO::getIsDefault, false).update();
78+
baseMapper.lambdaUpdate().eq(SmsConfigDO::getId, id).set(SmsConfigDO::getIsDefault, true).update();
79+
}
80+
81+
@Override
82+
public SmsConfigDO getDefaultConfig() {
83+
return baseMapper.lambdaQuery().eq(SmsConfigDO::getIsDefault, true).eq(SmsConfigDO::getStatus, DisEnableStatusEnum.ENABLE).one();
84+
}
85+
6586
/**
6687
* 加载配置
6788
*

continew-module-system/src/main/java/top/continew/admin/system/service/impl/StorageServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void updateStatus(CommonStatusUpdateReq req, Long id) {
139139

140140
@Override
141141
@Transactional(rollbackFor = Exception.class)
142-
public void setDefault(Long id) {
142+
public void setDefaultStorage(Long id) {
143143
StorageDO storage = super.getById(id);
144144
if (Boolean.TRUE.equals(storage.getIsDefault())) {
145145
return;
@@ -152,7 +152,7 @@ public void setDefault(Long id) {
152152

153153
@Override
154154
public StorageDO getDefaultStorage() {
155-
return baseMapper.lambdaQuery().eq(StorageDO::getIsDefault, true).one();
155+
return baseMapper.lambdaQuery().eq(StorageDO::getIsDefault, true).eq(StorageDO::getStatus, DisEnableStatusEnum.ENABLE).one();
156156
}
157157

158158
@Override

continew-webapi/src/main/java/top/continew/admin/controller/common/CaptchaController.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747
import top.continew.admin.common.constant.CacheConstants;
4848
import top.continew.admin.common.constant.SysConstants;
4949
import top.continew.admin.system.enums.OptionCategoryEnum;
50+
import top.continew.admin.system.model.entity.SmsConfigDO;
5051
import top.continew.admin.system.service.OptionService;
52+
import top.continew.admin.system.service.SmsConfigService;
5153
import top.continew.starter.cache.redisson.util.RedisUtils;
5254
import top.continew.starter.captcha.graphic.core.GraphicCaptchaService;
5355
import top.continew.starter.core.autoconfigure.project.ProjectProperties;
@@ -87,6 +89,7 @@ public class CaptchaController {
8789
private final CaptchaService behaviorCaptchaService;
8890
private final GraphicCaptchaService graphicCaptchaService;
8991
private final OptionService optionService;
92+
private final SmsConfigService smsConfigService;
9093

9194
@Log(ignore = true)
9295
@Operation(summary = "获取行为验证码", description = "获取行为验证码(Base64编码)")
@@ -201,14 +204,17 @@ public R getSmsCaptcha(@NotBlank(message = "手机号不能为空") @Mobile Stri
201204
CaptchaProperties.CaptchaSms captchaSms = captchaProperties.getSms();
202205
// 生成验证码
203206
String captcha = RandomUtil.randomNumbers(captchaSms.getLength());
204-
// 发送验证码
205207
Long expirationInMinutes = captchaSms.getExpirationInMinutes();
206-
SmsBlend smsBlend = SmsFactory.getBySupplier(captchaSms.getSupplier());
208+
// 获取短信配置
209+
SmsConfigDO smsConfig = smsConfigService.getDefaultConfig();
210+
SmsBlend smsBlend = smsConfig != null
211+
? SmsFactory.getBySupplier(smsConfig.getSupplier())
212+
: SmsFactory.getSmsBlend();
207213
Map<String, String> messageMap = MapUtil.newHashMap(2, true);
208214
messageMap.put(captchaSms.getCodeKey(), captcha);
209215
messageMap.put(captchaSms.getTimeKey(), String.valueOf(expirationInMinutes));
210-
SmsResponse smsResponse = smsBlend.sendMessage(phone, captchaSms
211-
.getTemplateId(), (LinkedHashMap<String, String>)messageMap);
216+
// 发送验证码
217+
SmsResponse smsResponse = smsBlend.sendMessage(phone, (LinkedHashMap<String, String>)messageMap);
212218
CheckUtils.throwIf(!smsResponse.isSuccess(), "验证码发送失败");
213219
// 保存验证码
214220
String captchaKey = CacheConstants.CAPTCHA_KEY_PREFIX + phone;

continew-webapi/src/main/java/top/continew/admin/controller/system/SmsConfigController.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@
1616

1717
package top.continew.admin.controller.system;
1818

19+
import cn.dev33.satoken.annotation.SaCheckPermission;
20+
import io.swagger.v3.oas.annotations.Operation;
21+
import io.swagger.v3.oas.annotations.Parameter;
22+
import io.swagger.v3.oas.annotations.enums.ParameterIn;
1923
import io.swagger.v3.oas.annotations.tags.Tag;
2024
import org.springframework.validation.annotation.Validated;
25+
import org.springframework.web.bind.annotation.PathVariable;
26+
import org.springframework.web.bind.annotation.PutMapping;
2127
import org.springframework.web.bind.annotation.RestController;
2228
import top.continew.admin.common.controller.BaseController;
2329
import top.continew.admin.system.model.query.SmsConfigQuery;
@@ -38,4 +44,13 @@
3844
@Validated
3945
@RestController
4046
@CrudRequestMapping(value = "/system/smsConfig", api = {Api.PAGE, Api.GET, Api.CREATE, Api.UPDATE, Api.DELETE})
41-
public class SmsConfigController extends BaseController<SmsConfigService, SmsConfigResp, SmsConfigResp, SmsConfigQuery, SmsConfigReq> {}
47+
public class SmsConfigController extends BaseController<SmsConfigService, SmsConfigResp, SmsConfigResp, SmsConfigQuery, SmsConfigReq> {
48+
49+
@Operation(summary = "设为默认配置", description = "设为默认配置")
50+
@Parameter(name = "id", description = "ID", example = "1", in = ParameterIn.PATH)
51+
@SaCheckPermission("system:smsConfig:setDefault")
52+
@PutMapping({"/{id}/default"})
53+
public void setDefault(@PathVariable("id") Long id) {
54+
baseService.setDefaultConfig(id);
55+
}
56+
}

continew-webapi/src/main/java/top/continew/admin/controller/system/StorageController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ public void updateStatus(@Validated @RequestBody CommonStatusUpdateReq req, @Pat
6060
@SaCheckPermission("system:storage:setDefault")
6161
@PutMapping({"/{id}/default"})
6262
public void setDefault(@PathVariable("id") Long id) {
63-
baseService.setDefault(id);
63+
baseService.setDefaultStorage(id);
6464
}
6565
}

0 commit comments

Comments
 (0)