Commit 86cbf21d6d659b7580e1f1c90f2558aa6e1df0e6
1 parent
8ab29d2a
完成云片的发送回执处理
Showing
7 changed files
with
83 additions
and
11 deletions
src/main/java/cn/iocoder/dashboard/framework/sms/core/client/dto/SmsReceiveRespDTO.java
| @@ -17,11 +17,11 @@ public class SmsReceiveRespDTO { | @@ -17,11 +17,11 @@ public class SmsReceiveRespDTO { | ||
| 17 | */ | 17 | */ |
| 18 | private Boolean success; | 18 | private Boolean success; |
| 19 | /** | 19 | /** |
| 20 | - * API 接收结果编码 | 20 | + * API 接收结果的编码 |
| 21 | */ | 21 | */ |
| 22 | private String errorCode; | 22 | private String errorCode; |
| 23 | /** | 23 | /** |
| 24 | - * API 接收结果说明 | 24 | + * API 接收结果的说明 |
| 25 | */ | 25 | */ |
| 26 | private String errorMsg; | 26 | private String errorMsg; |
| 27 | 27 |
src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/SmsCallbackController.java
| 1 | package cn.iocoder.dashboard.modules.system.controller.sms; | 1 | package cn.iocoder.dashboard.modules.system.controller.sms; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.util.URLUtil; | 3 | import cn.hutool.core.util.URLUtil; |
| 4 | -import cn.iocoder.dashboard.common.pojo.CommonResult; | 4 | +import cn.iocoder.dashboard.framework.logger.operatelog.core.annotations.OperateLog; |
| 5 | import cn.iocoder.dashboard.framework.sms.core.enums.SmsChannelEnum; | 5 | import cn.iocoder.dashboard.framework.sms.core.enums.SmsChannelEnum; |
| 6 | import cn.iocoder.dashboard.modules.system.service.sms.SysSmsService; | 6 | import cn.iocoder.dashboard.modules.system.service.sms.SysSmsService; |
| 7 | import io.swagger.annotations.Api; | 7 | import io.swagger.annotations.Api; |
| @@ -25,10 +25,11 @@ public class SmsCallbackController { | @@ -25,10 +25,11 @@ public class SmsCallbackController { | ||
| 25 | @PostMapping("/sms/yunpian") | 25 | @PostMapping("/sms/yunpian") |
| 26 | @ApiOperation(value = "云片短信的回调", notes = "参见 https://www.yunpian.com/official/document/sms/zh_cn/domestic_push_report 文档") | 26 | @ApiOperation(value = "云片短信的回调", notes = "参见 https://www.yunpian.com/official/document/sms/zh_cn/domestic_push_report 文档") |
| 27 | @ApiImplicitParam(name = "sms_status", value = "发送状态", required = true, example = "[{具体内容}]", dataTypeClass = Long.class) | 27 | @ApiImplicitParam(name = "sms_status", value = "发送状态", required = true, example = "[{具体内容}]", dataTypeClass = Long.class) |
| 28 | - public CommonResult<Boolean> receiveYunpianSmsStatus(@RequestParam("sms_status") String smsStatus) throws Throwable { | 28 | + @OperateLog(enable = false) |
| 29 | + public String receiveYunpianSmsStatus(@RequestParam("sms_status") String smsStatus) throws Throwable { | ||
| 29 | String text = URLUtil.decode(smsStatus); // decode 解码参数,因为它被 encode | 30 | String text = URLUtil.decode(smsStatus); // decode 解码参数,因为它被 encode |
| 30 | smsService.receiveSmsStatus(SmsChannelEnum.YUN_PIAN.getCode(), text); | 31 | smsService.receiveSmsStatus(SmsChannelEnum.YUN_PIAN.getCode(), text); |
| 31 | - return CommonResult.success(true); | 32 | + return "SUCCESS"; // 约定返回 SUCCESS 为成功 |
| 32 | } | 33 | } |
| 33 | 34 | ||
| 34 | } | 35 | } |
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; | @@ -3,6 +3,7 @@ package cn.iocoder.dashboard.modules.system.dal.dataobject.sms; | ||
| 3 | import cn.iocoder.dashboard.common.enums.UserTypeEnum; | 3 | import cn.iocoder.dashboard.common.enums.UserTypeEnum; |
| 4 | import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; | 4 | import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; |
| 5 | import cn.iocoder.dashboard.framework.sms.core.enums.SmsFrameworkErrorCodeConstants; | 5 | import cn.iocoder.dashboard.framework.sms.core.enums.SmsFrameworkErrorCodeConstants; |
| 6 | +import cn.iocoder.dashboard.modules.system.enums.sms.SysSmsReceiveStatusEnum; | ||
| 6 | import cn.iocoder.dashboard.modules.system.enums.sms.SysSmsSendStatusEnum; | 7 | import cn.iocoder.dashboard.modules.system.enums.sms.SysSmsSendStatusEnum; |
| 7 | import com.baomidou.mybatisplus.annotation.TableField; | 8 | import com.baomidou.mybatisplus.annotation.TableField; |
| 8 | import com.baomidou.mybatisplus.annotation.TableName; | 9 | import com.baomidou.mybatisplus.annotation.TableName; |
| @@ -150,9 +151,23 @@ public class SysSmsLogDO extends BaseDO { | @@ -150,9 +151,23 @@ public class SysSmsLogDO extends BaseDO { | ||
| 150 | 151 | ||
| 151 | // ========= 接收相关字段 ========= | 152 | // ========= 接收相关字段 ========= |
| 152 | 153 | ||
| 153 | -// /** | ||
| 154 | -// * 是否获取过结果[0否 1是] | ||
| 155 | -// */ | ||
| 156 | -// private Integer gotResult; | 154 | + /** |
| 155 | + * 接收状态 | ||
| 156 | + * | ||
| 157 | + * 枚举 {@link SysSmsReceiveStatusEnum} | ||
| 158 | + */ | ||
| 159 | + private Integer receiveStatus; | ||
| 160 | + /** | ||
| 161 | + * 接收时间 | ||
| 162 | + */ | ||
| 163 | + private Date receiveTime; | ||
| 164 | + /** | ||
| 165 | + * 短信 API 接收结果的编码 | ||
| 166 | + */ | ||
| 167 | + private String apiReceiveCode; | ||
| 168 | + /** | ||
| 169 | + * 短信 API 接收结果的提示 | ||
| 170 | + */ | ||
| 171 | + private String apiReceiveMsg; | ||
| 157 | 172 | ||
| 158 | } | 173 | } |
src/main/java/cn/iocoder/dashboard/modules/system/enums/sms/SysSmsReceiveStatusEnum.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.system.enums.sms; | ||
| 2 | + | ||
| 3 | +import lombok.AllArgsConstructor; | ||
| 4 | +import lombok.Getter; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * 短信的接收状态枚举 | ||
| 8 | + * | ||
| 9 | + * @author 芋道源码 | ||
| 10 | + * @date 2021/2/1 13:39 | ||
| 11 | + */ | ||
| 12 | +@Getter | ||
| 13 | +@AllArgsConstructor | ||
| 14 | +public enum SysSmsReceiveStatusEnum { | ||
| 15 | + | ||
| 16 | + INIT(0), // 初始化 | ||
| 17 | + SUCCESS(10), // 接收成功 | ||
| 18 | + FAILURE(20), // 接收失败 | ||
| 19 | + ; | ||
| 20 | + | ||
| 21 | + private final int status; | ||
| 22 | + | ||
| 23 | +} |
src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsLogService.java
| @@ -2,6 +2,7 @@ package cn.iocoder.dashboard.modules.system.service.sms; | @@ -2,6 +2,7 @@ package cn.iocoder.dashboard.modules.system.service.sms; | ||
| 2 | 2 | ||
| 3 | import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO; | 3 | import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO; |
| 4 | 4 | ||
| 5 | +import java.util.Date; | ||
| 5 | import java.util.Map; | 6 | import java.util.Map; |
| 6 | 7 | ||
| 7 | /** | 8 | /** |
| @@ -28,7 +29,7 @@ public interface SysSmsLogService { | @@ -28,7 +29,7 @@ public interface SysSmsLogService { | ||
| 28 | SysSmsTemplateDO template, String templateContent, Map<String, Object> templateParams); | 29 | SysSmsTemplateDO template, String templateContent, Map<String, Object> templateParams); |
| 29 | 30 | ||
| 30 | /** | 31 | /** |
| 31 | - * 更新发送日志的结果 | 32 | + * 更新日志的发送结果 |
| 32 | * | 33 | * |
| 33 | * @param id 日志编号 | 34 | * @param id 日志编号 |
| 34 | * @param sendCode 发送结果的编码 | 35 | * @param sendCode 发送结果的编码 |
| @@ -41,4 +42,15 @@ public interface SysSmsLogService { | @@ -41,4 +42,15 @@ public interface SysSmsLogService { | ||
| 41 | void updateSmsSendResult(Long id, Integer sendCode, String sendMsg, | 42 | void updateSmsSendResult(Long id, Integer sendCode, String sendMsg, |
| 42 | String apiSendCode, String apiSendMsg, String apiRequestId, String apiSerialNo); | 43 | String apiSendCode, String apiSendMsg, String apiRequestId, String apiSerialNo); |
| 43 | 44 | ||
| 45 | + /** | ||
| 46 | + * 更新日志的接收结果 | ||
| 47 | + * | ||
| 48 | + * @param id 日志编号 | ||
| 49 | + * @param success 是否接收成功 | ||
| 50 | + * @param receiveTime 用户接收时间 | ||
| 51 | + * @param apiReceiveCode API 接收结果的编码 | ||
| 52 | + * @param apiReceiveMsg API 接收结果的说明 | ||
| 53 | + */ | ||
| 54 | + void updateSmsReceiveResult(Long id, Boolean success, Date receiveTime, String apiReceiveCode, String apiReceiveMsg); | ||
| 55 | + | ||
| 44 | } | 56 | } |
src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsLogServiceImpl.java
| @@ -4,6 +4,7 @@ import cn.iocoder.dashboard.common.pojo.CommonResult; | @@ -4,6 +4,7 @@ import cn.iocoder.dashboard.common.pojo.CommonResult; | ||
| 4 | import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsLogDO; | 4 | import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsLogDO; |
| 5 | import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO; | 5 | import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO; |
| 6 | import cn.iocoder.dashboard.modules.system.dal.mysql.sms.SysSmsLogMapper; | 6 | import cn.iocoder.dashboard.modules.system.dal.mysql.sms.SysSmsLogMapper; |
| 7 | +import cn.iocoder.dashboard.modules.system.enums.sms.SysSmsReceiveStatusEnum; | ||
| 7 | import cn.iocoder.dashboard.modules.system.enums.sms.SysSmsSendStatusEnum; | 8 | import cn.iocoder.dashboard.modules.system.enums.sms.SysSmsSendStatusEnum; |
| 8 | import cn.iocoder.dashboard.modules.system.service.sms.SysSmsLogService; | 9 | import cn.iocoder.dashboard.modules.system.service.sms.SysSmsLogService; |
| 9 | import lombok.extern.slf4j.Slf4j; | 10 | import lombok.extern.slf4j.Slf4j; |
| @@ -41,6 +42,8 @@ public class SysSmsLogServiceImpl implements SysSmsLogService { | @@ -41,6 +42,8 @@ public class SysSmsLogServiceImpl implements SysSmsLogService { | ||
| 41 | logBuilder.templateContent(templateContent).templateParams(templateParams).apiTemplateId(template.getApiTemplateId()); | 42 | logBuilder.templateContent(templateContent).templateParams(templateParams).apiTemplateId(template.getApiTemplateId()); |
| 42 | // 设置渠道相关字段 | 43 | // 设置渠道相关字段 |
| 43 | logBuilder.channelId(template.getChannelId()).channelCode(template.getChannelCode()); | 44 | logBuilder.channelId(template.getChannelId()).channelCode(template.getChannelCode()); |
| 45 | + // 设置接收相关字段 | ||
| 46 | + logBuilder.receiveStatus(SysSmsReceiveStatusEnum.INIT.getStatus()); | ||
| 44 | 47 | ||
| 45 | // 插入数据库 | 48 | // 插入数据库 |
| 46 | SysSmsLogDO logDO = logBuilder.build(); | 49 | SysSmsLogDO logDO = logBuilder.build(); |
| @@ -51,10 +54,19 @@ public class SysSmsLogServiceImpl implements SysSmsLogService { | @@ -51,10 +54,19 @@ public class SysSmsLogServiceImpl implements SysSmsLogService { | ||
| 51 | @Override | 54 | @Override |
| 52 | public void updateSmsSendResult(Long id, Integer sendCode, String sendMsg, | 55 | public void updateSmsSendResult(Long id, Integer sendCode, String sendMsg, |
| 53 | String apiSendCode, String apiSendMsg, String apiRequestId, String apiSerialNo) { | 56 | String apiSendCode, String apiSendMsg, String apiRequestId, String apiSerialNo) { |
| 54 | - SysSmsSendStatusEnum sendStatus = CommonResult.isSuccess(sendCode) ? SysSmsSendStatusEnum.SUCCESS : SysSmsSendStatusEnum.FAILURE; | 57 | + SysSmsSendStatusEnum sendStatus = CommonResult.isSuccess(sendCode) ? SysSmsSendStatusEnum.SUCCESS |
| 58 | + : SysSmsSendStatusEnum.FAILURE; | ||
| 55 | smsLogMapper.updateById(SysSmsLogDO.builder().id(id).sendStatus(sendStatus.getStatus()).sendTime(new Date()) | 59 | smsLogMapper.updateById(SysSmsLogDO.builder().id(id).sendStatus(sendStatus.getStatus()).sendTime(new Date()) |
| 56 | .sendCode(sendCode).sendMsg(sendMsg).apiSendCode(apiSendCode).apiSendMsg(apiSendMsg) | 60 | .sendCode(sendCode).sendMsg(sendMsg).apiSendCode(apiSendCode).apiSendMsg(apiSendMsg) |
| 57 | .apiRequestId(apiRequestId).apiSerialNo(apiSerialNo).build()); | 61 | .apiRequestId(apiRequestId).apiSerialNo(apiSerialNo).build()); |
| 58 | } | 62 | } |
| 59 | 63 | ||
| 64 | + @Override | ||
| 65 | + public void updateSmsReceiveResult(Long id, Boolean success, Date receiveTime, String apiReceiveCode, String apiReceiveMsg) { | ||
| 66 | + SysSmsReceiveStatusEnum receiveStatus = Objects.equals(success, true) ? SysSmsReceiveStatusEnum.SUCCESS | ||
| 67 | + : SysSmsReceiveStatusEnum.FAILURE; | ||
| 68 | + smsLogMapper.updateById(SysSmsLogDO.builder().id(id).receiveStatus(receiveStatus.getStatus()).receiveTime(receiveTime) | ||
| 69 | + .apiReceiveCode(apiReceiveCode).apiReceiveMsg(apiReceiveMsg).build()); | ||
| 70 | + } | ||
| 71 | + | ||
| 60 | } | 72 | } |
src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsServiceImpl.java
| 1 | package cn.iocoder.dashboard.modules.system.service.sms.impl; | 1 | package cn.iocoder.dashboard.modules.system.service.sms.impl; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.collection.CollUtil; | ||
| 3 | import cn.hutool.core.util.StrUtil; | 4 | import cn.hutool.core.util.StrUtil; |
| 4 | import cn.iocoder.dashboard.common.core.KeyValue; | 5 | import cn.iocoder.dashboard.common.core.KeyValue; |
| 5 | import cn.iocoder.dashboard.common.enums.CommonStatusEnum; | 6 | import cn.iocoder.dashboard.common.enums.CommonStatusEnum; |
| @@ -153,6 +154,14 @@ public class SysSmsServiceImpl implements SysSmsService { | @@ -153,6 +154,14 @@ public class SysSmsServiceImpl implements SysSmsService { | ||
| 153 | Assert.notNull(smsClient, String.format("短信客户端(%s) 不存在", channelCode)); | 154 | Assert.notNull(smsClient, String.format("短信客户端(%s) 不存在", channelCode)); |
| 154 | // 解析内容 | 155 | // 解析内容 |
| 155 | List<SmsReceiveRespDTO> receiveResults = smsClient.parseSmsReceiveStatus(text); | 156 | List<SmsReceiveRespDTO> receiveResults = smsClient.parseSmsReceiveStatus(text); |
| 157 | + if (CollUtil.isEmpty(receiveResults)) { | ||
| 158 | + return; | ||
| 159 | + } | ||
| 160 | + // 更新短信日志的接收结果. 因为量一般不打,所以先使用 for 循环更新 | ||
| 161 | + receiveResults.forEach(result -> { | ||
| 162 | + smsLogService.updateSmsReceiveResult(result.getLogId(), result.getSuccess(), result.getReceiveTime(), | ||
| 163 | + result.getErrorCode(), result.getErrorCode()); | ||
| 164 | + }); | ||
| 156 | } | 165 | } |
| 157 | 166 | ||
| 158 | } | 167 | } |