mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-04 04:08:43 +08:00 
			
		
		
		
	sms:移除 SmsCodeMapping + SmsCommonResult,简化短信的封装
This commit is contained in:
		@@ -1,10 +1,9 @@
 | 
			
		||||
package cn.iocoder.yudao.module.system.dal.dataobject.sms;
 | 
			
		||||
 | 
			
		||||
import cn.iocoder.yudao.module.system.enums.sms.SmsReceiveStatusEnum;
 | 
			
		||||
import cn.iocoder.yudao.module.system.enums.sms.SmsSendStatusEnum;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
 | 
			
		||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
 | 
			
		||||
import cn.iocoder.yudao.framework.sms.core.enums.SmsFrameworkErrorCodeConstants;
 | 
			
		||||
import cn.iocoder.yudao.module.system.enums.sms.SmsReceiveStatusEnum;
 | 
			
		||||
import cn.iocoder.yudao.module.system.enums.sms.SmsSendStatusEnum;
 | 
			
		||||
import com.baomidou.mybatisplus.annotation.KeySequence;
 | 
			
		||||
import com.baomidou.mybatisplus.annotation.TableField;
 | 
			
		||||
import com.baomidou.mybatisplus.annotation.TableName;
 | 
			
		||||
@@ -115,19 +114,20 @@ public class SmsLogDO extends BaseDO {
 | 
			
		||||
     * 发送时间
 | 
			
		||||
     */
 | 
			
		||||
    private LocalDateTime sendTime;
 | 
			
		||||
    /**
 | 
			
		||||
     * 发送结果的编码
 | 
			
		||||
     *
 | 
			
		||||
     * 枚举 {@link SmsFrameworkErrorCodeConstants}
 | 
			
		||||
     */
 | 
			
		||||
    private Integer sendCode;
 | 
			
		||||
    /**
 | 
			
		||||
     * 发送结果的提示
 | 
			
		||||
     *
 | 
			
		||||
     * 一般情况下,使用 {@link SmsFrameworkErrorCodeConstants}
 | 
			
		||||
     * 异常情况下,通过格式化 Exception 的提示存储
 | 
			
		||||
     */
 | 
			
		||||
    private String sendMsg;
 | 
			
		||||
    // TODO 芋艿:短信
 | 
			
		||||
//    /**
 | 
			
		||||
//     * 发送结果的编码
 | 
			
		||||
//     *
 | 
			
		||||
//     * 枚举 {@link SmsFrameworkErrorCodeConstants}
 | 
			
		||||
//     */
 | 
			
		||||
//    private Integer sendCode;
 | 
			
		||||
//    /**
 | 
			
		||||
//     * 发送结果的提示
 | 
			
		||||
//     *
 | 
			
		||||
//     * 一般情况下,使用 {@link SmsFrameworkErrorCodeConstants}
 | 
			
		||||
//     * 异常情况下,通过格式化 Exception 的提示存储
 | 
			
		||||
//     */
 | 
			
		||||
//    private String sendMsg;
 | 
			
		||||
    /**
 | 
			
		||||
     * 短信 API 发送结果的编码
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -37,15 +37,15 @@ public interface SmsLogService {
 | 
			
		||||
     * 更新日志的发送结果
 | 
			
		||||
     *
 | 
			
		||||
     * @param id 日志编号
 | 
			
		||||
     * @param sendCode 发送结果的编码
 | 
			
		||||
     * @param sendMsg 发送结果的提示
 | 
			
		||||
     * @param success 发送是否成功
 | 
			
		||||
     * @param apiSendCode 短信 API 发送结果的编码
 | 
			
		||||
     * @param apiSendMsg 短信 API 发送失败的提示
 | 
			
		||||
     * @param apiRequestId 短信 API 发送返回的唯一请求 ID
 | 
			
		||||
     * @param apiSerialNo 短信 API 发送返回的序号
 | 
			
		||||
     */
 | 
			
		||||
    void updateSmsSendResult(Long id, Integer sendCode, String sendMsg,
 | 
			
		||||
                             String apiSendCode, String apiSendMsg, String apiRequestId, String apiSerialNo);
 | 
			
		||||
    void updateSmsSendResult(Long id, Boolean success,
 | 
			
		||||
                             String apiSendCode, String apiSendMsg,
 | 
			
		||||
                             String apiRequestId, String apiSerialNo);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 更新日志的接收结果
 | 
			
		||||
@@ -56,7 +56,8 @@ public interface SmsLogService {
 | 
			
		||||
     * @param apiReceiveCode API 接收结果的编码
 | 
			
		||||
     * @param apiReceiveMsg API 接收结果的说明
 | 
			
		||||
     */
 | 
			
		||||
    void updateSmsReceiveResult(Long id, Boolean success, LocalDateTime receiveTime, String apiReceiveCode, String apiReceiveMsg);
 | 
			
		||||
    void updateSmsReceiveResult(Long id, Boolean success,
 | 
			
		||||
                                LocalDateTime receiveTime, String apiReceiveCode, String apiReceiveMsg);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 获得短信日志分页
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,11 @@
 | 
			
		||||
package cn.iocoder.yudao.module.system.service.sms;
 | 
			
		||||
 | 
			
		||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
 | 
			
		||||
import cn.iocoder.yudao.module.system.controller.admin.sms.vo.log.SmsLogExportReqVO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.controller.admin.sms.vo.log.SmsLogPageReqVO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsLogDO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsTemplateDO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.dal.mysql.sms.SmsLogMapper;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
 | 
			
		||||
import cn.iocoder.yudao.module.system.enums.sms.SmsReceiveStatusEnum;
 | 
			
		||||
import cn.iocoder.yudao.module.system.enums.sms.SmsSendStatusEnum;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
@@ -55,13 +54,12 @@ public class SmsLogServiceImpl implements SmsLogService {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void updateSmsSendResult(Long id, Integer sendCode, String sendMsg,
 | 
			
		||||
    public void updateSmsSendResult(Long id, Boolean success,
 | 
			
		||||
                                    String apiSendCode, String apiSendMsg,
 | 
			
		||||
                                    String apiRequestId, String apiSerialNo) {
 | 
			
		||||
        SmsSendStatusEnum sendStatus = CommonResult.isSuccess(sendCode) ?
 | 
			
		||||
                SmsSendStatusEnum.SUCCESS : SmsSendStatusEnum.FAILURE;
 | 
			
		||||
        smsLogMapper.updateById(SmsLogDO.builder().id(id).sendStatus(sendStatus.getStatus())
 | 
			
		||||
                .sendTime(LocalDateTime.now()).sendCode(sendCode).sendMsg(sendMsg)
 | 
			
		||||
        SmsSendStatusEnum sendStatus = success ? SmsSendStatusEnum.SUCCESS : SmsSendStatusEnum.FAILURE;
 | 
			
		||||
        smsLogMapper.updateById(SmsLogDO.builder().id(id)
 | 
			
		||||
                .sendStatus(sendStatus.getStatus()).sendTime(LocalDateTime.now())
 | 
			
		||||
                .apiSendCode(apiSendCode).apiSendMsg(apiSendMsg)
 | 
			
		||||
                .apiRequestId(apiRequestId).apiSerialNo(apiSerialNo).build());
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
package cn.iocoder.yudao.module.system.service.sms;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.collection.CollUtil;
 | 
			
		||||
import cn.hutool.core.exceptions.ExceptionUtil;
 | 
			
		||||
import cn.hutool.core.lang.Assert;
 | 
			
		||||
import cn.hutool.core.util.StrUtil;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.core.KeyValue;
 | 
			
		||||
@@ -8,7 +9,6 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
 | 
			
		||||
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
 | 
			
		||||
import cn.iocoder.yudao.framework.sms.core.client.SmsClient;
 | 
			
		||||
import cn.iocoder.yudao.framework.sms.core.client.SmsCommonResult;
 | 
			
		||||
import cn.iocoder.yudao.framework.sms.core.client.dto.SmsReceiveRespDTO;
 | 
			
		||||
import cn.iocoder.yudao.framework.sms.core.client.dto.SmsSendRespDTO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO;
 | 
			
		||||
@@ -19,6 +19,7 @@ import cn.iocoder.yudao.module.system.mq.producer.sms.SmsProducer;
 | 
			
		||||
import cn.iocoder.yudao.module.system.service.member.MemberService;
 | 
			
		||||
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
 | 
			
		||||
import com.google.common.annotations.VisibleForTesting;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Resource;
 | 
			
		||||
@@ -35,6 +36,7 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
 | 
			
		||||
 * @author 芋道源码
 | 
			
		||||
 */
 | 
			
		||||
@Service
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class SmsSendServiceImpl implements SmsSendService {
 | 
			
		||||
 | 
			
		||||
    @Resource
 | 
			
		||||
@@ -158,11 +160,17 @@ public class SmsSendServiceImpl implements SmsSendService {
 | 
			
		||||
        SmsClient smsClient = smsChannelService.getSmsClient(message.getChannelId());
 | 
			
		||||
        Assert.notNull(smsClient, "短信客户端({}) 不存在", message.getChannelId());
 | 
			
		||||
        // 发送短信
 | 
			
		||||
        SmsCommonResult<SmsSendRespDTO> sendResult = smsClient.sendSms(message.getLogId(), message.getMobile(),
 | 
			
		||||
                message.getApiTemplateId(), message.getTemplateParams());
 | 
			
		||||
        smsLogService.updateSmsSendResult(message.getLogId(), sendResult.getCode(), sendResult.getMsg(),
 | 
			
		||||
                sendResult.getApiCode(), sendResult.getApiMsg(), sendResult.getApiRequestId(),
 | 
			
		||||
                sendResult.getData() != null ? sendResult.getData().getSerialNo() : null);
 | 
			
		||||
        try {
 | 
			
		||||
            SmsSendRespDTO sendResponse = smsClient.sendSms(message.getLogId(), message.getMobile(),
 | 
			
		||||
                    message.getApiTemplateId(), message.getTemplateParams());
 | 
			
		||||
            smsLogService.updateSmsSendResult(message.getLogId(), sendResponse.getSuccess(),
 | 
			
		||||
                    sendResponse.getApiCode(), sendResponse.getApiMsg(),
 | 
			
		||||
                    sendResponse.getApiRequestId(), sendResponse.getSerialNo());
 | 
			
		||||
        } catch (Throwable ex) {
 | 
			
		||||
            log.error("[doSendSms][发送短信异常,日志编号({})]", message.getLogId(), ex);
 | 
			
		||||
            smsLogService.updateSmsSendResult(message.getLogId(), false,
 | 
			
		||||
                    "EXCEPTION", ExceptionUtil.getRootCauseMessage(ex), null, null);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,14 @@
 | 
			
		||||
package cn.iocoder.yudao.module.system.service.sms;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.exceptions.ExceptionUtil;
 | 
			
		||||
import cn.hutool.core.lang.Assert;
 | 
			
		||||
import cn.hutool.core.util.ReUtil;
 | 
			
		||||
import cn.hutool.core.util.StrUtil;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
 | 
			
		||||
import cn.iocoder.yudao.framework.sms.core.client.SmsClient;
 | 
			
		||||
import cn.iocoder.yudao.framework.sms.core.client.SmsCommonResult;
 | 
			
		||||
import cn.iocoder.yudao.framework.sms.core.client.dto.SmsTemplateRespDTO;
 | 
			
		||||
import cn.iocoder.yudao.framework.sms.core.enums.SmsTemplateAuditStatusEnum;
 | 
			
		||||
import cn.iocoder.yudao.module.system.controller.admin.sms.vo.template.SmsTemplateCreateReqVO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.controller.admin.sms.vo.template.SmsTemplateExportReqVO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.controller.admin.sms.vo.template.SmsTemplatePageReqVO;
 | 
			
		||||
@@ -21,11 +23,11 @@ import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.cache.annotation.CacheEvict;
 | 
			
		||||
import org.springframework.cache.annotation.Cacheable;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.springframework.util.Assert;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Resource;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
import java.util.regex.Pattern;
 | 
			
		||||
 | 
			
		||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 | 
			
		||||
@@ -171,9 +173,24 @@ public class SmsTemplateServiceImpl implements SmsTemplateService {
 | 
			
		||||
        // 获得短信模板
 | 
			
		||||
        SmsClient smsClient = smsChannelService.getSmsClient(channelId);
 | 
			
		||||
        Assert.notNull(smsClient, String.format("短信客户端(%d) 不存在", channelId));
 | 
			
		||||
        SmsCommonResult<SmsTemplateRespDTO> templateResult = smsClient.getSmsTemplate(apiTemplateId);
 | 
			
		||||
        // 校验短信模板是否正确
 | 
			
		||||
        templateResult.checkError();
 | 
			
		||||
        SmsTemplateRespDTO template;
 | 
			
		||||
        try {
 | 
			
		||||
            template = smsClient.getSmsTemplate(apiTemplateId);
 | 
			
		||||
        } catch (Throwable ex) {
 | 
			
		||||
            throw exception(SMS_TEMPLATE_API_ERROR, ExceptionUtil.getRootCauseMessage(ex));
 | 
			
		||||
        }
 | 
			
		||||
        // 校验短信模版
 | 
			
		||||
        if (template == null) {
 | 
			
		||||
            throw exception(SMS_TEMPLATE_API_NOT_FOUND);
 | 
			
		||||
        }
 | 
			
		||||
        if (Objects.equals(template.getAuditStatus(), SmsTemplateAuditStatusEnum.CHECKING.getStatus())) {
 | 
			
		||||
            throw exception(SMS_TEMPLATE_API_AUDIT_CHECKING);
 | 
			
		||||
        }
 | 
			
		||||
        if (Objects.equals(template.getAuditStatus(), SmsTemplateAuditStatusEnum.FAIL.getStatus())) {
 | 
			
		||||
            throw exception(SMS_TEMPLATE_API_AUDIT_FAIL, template.getAuditReason());
 | 
			
		||||
        }
 | 
			
		||||
        Assert.equals(template.getAuditStatus(), SmsTemplateAuditStatusEnum.SUCCESS.getStatus(),
 | 
			
		||||
                String.format("短信模板(%s) 审核状态(%d) 不正确", apiTemplateId, template.getAuditStatus()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user