From 86cbf21d6d659b7580e1f1c90f2558aa6e1df0e6 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 4 Apr 2021 23:28:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=91=E7=89=87=E7=9A=84?= =?UTF-8?q?=E5=8F=91=E9=80=81=E5=9B=9E=E6=89=A7=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/client/dto/SmsReceiveRespDTO.java | 4 ++-- .../controller/sms/SmsCallbackController.java | 7 +++--- .../dal/dataobject/sms/SysSmsLogDO.java | 23 +++++++++++++++---- .../enums/sms/SysSmsReceiveStatusEnum.java | 23 +++++++++++++++++++ .../system/service/sms/SysSmsLogService.java | 14 ++++++++++- .../sms/impl/SysSmsLogServiceImpl.java | 14 ++++++++++- .../service/sms/impl/SysSmsServiceImpl.java | 9 ++++++++ 7 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/enums/sms/SysSmsReceiveStatusEnum.java diff --git a/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/dto/SmsReceiveRespDTO.java b/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/dto/SmsReceiveRespDTO.java index c1ccd1e65..ecfdb045c 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/dto/SmsReceiveRespDTO.java +++ b/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/dto/SmsReceiveRespDTO.java @@ -17,11 +17,11 @@ public class SmsReceiveRespDTO { */ private Boolean success; /** - * API 接收结果编码 + * API 接收结果的编码 */ private String errorCode; /** - * API 接收结果说明 + * API 接收结果的说明 */ private String errorMsg; diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/SmsCallbackController.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/SmsCallbackController.java index f8e845380..d81ecfa4b 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/SmsCallbackController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/SmsCallbackController.java @@ -1,7 +1,7 @@ package cn.iocoder.dashboard.modules.system.controller.sms; import cn.hutool.core.util.URLUtil; -import cn.iocoder.dashboard.common.pojo.CommonResult; +import cn.iocoder.dashboard.framework.logger.operatelog.core.annotations.OperateLog; import cn.iocoder.dashboard.framework.sms.core.enums.SmsChannelEnum; import cn.iocoder.dashboard.modules.system.service.sms.SysSmsService; import io.swagger.annotations.Api; @@ -25,10 +25,11 @@ public class SmsCallbackController { @PostMapping("/sms/yunpian") @ApiOperation(value = "云片短信的回调", notes = "参见 https://www.yunpian.com/official/document/sms/zh_cn/domestic_push_report 文档") @ApiImplicitParam(name = "sms_status", value = "发送状态", required = true, example = "[{具体内容}]", dataTypeClass = Long.class) - public CommonResult receiveYunpianSmsStatus(@RequestParam("sms_status") String smsStatus) throws Throwable { + @OperateLog(enable = false) + public String receiveYunpianSmsStatus(@RequestParam("sms_status") String smsStatus) throws Throwable { String text = URLUtil.decode(smsStatus); // decode 解码参数,因为它被 encode smsService.receiveSmsStatus(SmsChannelEnum.YUN_PIAN.getCode(), text); - return CommonResult.success(true); + return "SUCCESS"; // 约定返回 SUCCESS 为成功 } } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/dal/dataobject/sms/SysSmsLogDO.java b/src/main/java/cn/iocoder/dashboard/modules/system/dal/dataobject/sms/SysSmsLogDO.java index 44d0da639..78e04e117 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/dal/dataobject/sms/SysSmsLogDO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/dal/dataobject/sms/SysSmsLogDO.java @@ -3,6 +3,7 @@ package cn.iocoder.dashboard.modules.system.dal.dataobject.sms; import cn.iocoder.dashboard.common.enums.UserTypeEnum; import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.dashboard.framework.sms.core.enums.SmsFrameworkErrorCodeConstants; +import cn.iocoder.dashboard.modules.system.enums.sms.SysSmsReceiveStatusEnum; import cn.iocoder.dashboard.modules.system.enums.sms.SysSmsSendStatusEnum; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; @@ -150,9 +151,23 @@ public class SysSmsLogDO extends BaseDO { // ========= 接收相关字段 ========= -// /** -// * 是否获取过结果[0否 1是] -// */ -// private Integer gotResult; + /** + * 接收状态 + * + * 枚举 {@link SysSmsReceiveStatusEnum} + */ + private Integer receiveStatus; + /** + * 接收时间 + */ + private Date receiveTime; + /** + * 短信 API 接收结果的编码 + */ + private String apiReceiveCode; + /** + * 短信 API 接收结果的提示 + */ + private String apiReceiveMsg; } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/enums/sms/SysSmsReceiveStatusEnum.java b/src/main/java/cn/iocoder/dashboard/modules/system/enums/sms/SysSmsReceiveStatusEnum.java new file mode 100644 index 000000000..880238822 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/enums/sms/SysSmsReceiveStatusEnum.java @@ -0,0 +1,23 @@ +package cn.iocoder.dashboard.modules.system.enums.sms; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * 短信的接收状态枚举 + * + * @author 芋道源码 + * @date 2021/2/1 13:39 + */ +@Getter +@AllArgsConstructor +public enum SysSmsReceiveStatusEnum { + + INIT(0), // 初始化 + SUCCESS(10), // 接收成功 + FAILURE(20), // 接收失败 + ; + + private final int status; + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsLogService.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsLogService.java index a7a6f8fee..33fb86a36 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsLogService.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsLogService.java @@ -2,6 +2,7 @@ package cn.iocoder.dashboard.modules.system.service.sms; import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO; +import java.util.Date; import java.util.Map; /** @@ -28,7 +29,7 @@ public interface SysSmsLogService { SysSmsTemplateDO template, String templateContent, Map templateParams); /** - * 更新发送日志的结果 + * 更新日志的发送结果 * * @param id 日志编号 * @param sendCode 发送结果的编码 @@ -41,4 +42,15 @@ public interface SysSmsLogService { void updateSmsSendResult(Long id, Integer sendCode, String sendMsg, String apiSendCode, String apiSendMsg, String apiRequestId, String apiSerialNo); + /** + * 更新日志的接收结果 + * + * @param id 日志编号 + * @param success 是否接收成功 + * @param receiveTime 用户接收时间 + * @param apiReceiveCode API 接收结果的编码 + * @param apiReceiveMsg API 接收结果的说明 + */ + void updateSmsReceiveResult(Long id, Boolean success, Date receiveTime, String apiReceiveCode, String apiReceiveMsg); + } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsLogServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsLogServiceImpl.java index 32f4883e0..6dcd9de7c 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsLogServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsLogServiceImpl.java @@ -4,6 +4,7 @@ import cn.iocoder.dashboard.common.pojo.CommonResult; import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsLogDO; import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO; import cn.iocoder.dashboard.modules.system.dal.mysql.sms.SysSmsLogMapper; +import cn.iocoder.dashboard.modules.system.enums.sms.SysSmsReceiveStatusEnum; import cn.iocoder.dashboard.modules.system.enums.sms.SysSmsSendStatusEnum; import cn.iocoder.dashboard.modules.system.service.sms.SysSmsLogService; import lombok.extern.slf4j.Slf4j; @@ -41,6 +42,8 @@ public class SysSmsLogServiceImpl implements SysSmsLogService { logBuilder.templateContent(templateContent).templateParams(templateParams).apiTemplateId(template.getApiTemplateId()); // 设置渠道相关字段 logBuilder.channelId(template.getChannelId()).channelCode(template.getChannelCode()); + // 设置接收相关字段 + logBuilder.receiveStatus(SysSmsReceiveStatusEnum.INIT.getStatus()); // 插入数据库 SysSmsLogDO logDO = logBuilder.build(); @@ -51,10 +54,19 @@ public class SysSmsLogServiceImpl implements SysSmsLogService { @Override public void updateSmsSendResult(Long id, Integer sendCode, String sendMsg, String apiSendCode, String apiSendMsg, String apiRequestId, String apiSerialNo) { - SysSmsSendStatusEnum sendStatus = CommonResult.isSuccess(sendCode) ? SysSmsSendStatusEnum.SUCCESS : SysSmsSendStatusEnum.FAILURE; + SysSmsSendStatusEnum sendStatus = CommonResult.isSuccess(sendCode) ? SysSmsSendStatusEnum.SUCCESS + : SysSmsSendStatusEnum.FAILURE; smsLogMapper.updateById(SysSmsLogDO.builder().id(id).sendStatus(sendStatus.getStatus()).sendTime(new Date()) .sendCode(sendCode).sendMsg(sendMsg).apiSendCode(apiSendCode).apiSendMsg(apiSendMsg) .apiRequestId(apiRequestId).apiSerialNo(apiSerialNo).build()); } + @Override + public void updateSmsReceiveResult(Long id, Boolean success, Date receiveTime, String apiReceiveCode, String apiReceiveMsg) { + SysSmsReceiveStatusEnum receiveStatus = Objects.equals(success, true) ? SysSmsReceiveStatusEnum.SUCCESS + : SysSmsReceiveStatusEnum.FAILURE; + smsLogMapper.updateById(SysSmsLogDO.builder().id(id).receiveStatus(receiveStatus.getStatus()).receiveTime(receiveTime) + .apiReceiveCode(apiReceiveCode).apiReceiveMsg(apiReceiveMsg).build()); + } + } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsServiceImpl.java index 5a3487a5f..c5a12797f 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsServiceImpl.java @@ -1,5 +1,6 @@ package cn.iocoder.dashboard.modules.system.service.sms.impl; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.dashboard.common.core.KeyValue; import cn.iocoder.dashboard.common.enums.CommonStatusEnum; @@ -153,6 +154,14 @@ public class SysSmsServiceImpl implements SysSmsService { Assert.notNull(smsClient, String.format("短信客户端(%s) 不存在", channelCode)); // 解析内容 List receiveResults = smsClient.parseSmsReceiveStatus(text); + if (CollUtil.isEmpty(receiveResults)) { + return; + } + // 更新短信日志的接收结果. 因为量一般不打,所以先使用 for 循环更新 + receiveResults.forEach(result -> { + smsLogService.updateSmsReceiveResult(result.getLogId(), result.getSuccess(), result.getReceiveTime(), + result.getErrorCode(), result.getErrorCode()); + }); } }