diff --git a/austin-common/src/main/java/com/java3y/austin/constant/RespStatusEnum.java b/austin-common/src/main/java/com/java3y/austin/constant/RespStatusEnum.java new file mode 100644 index 0000000..c25eb7f --- /dev/null +++ b/austin-common/src/main/java/com/java3y/austin/constant/RespStatusEnum.java @@ -0,0 +1,43 @@ +package com.java3y.austin.constant; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.ToString; + +/** + * 全局响应状态枚举 + * + * @author zzb + * @since 2021.11.17 + **/ +@Getter +@ToString +@AllArgsConstructor +public enum RespStatusEnum { + /** + * OK:操作成功 + */ + SUCCESS("00000", "操作成功"), + FAIL("00001", "操作失败"), + + /** + * 客户端 + */ + CLIENT_BAD_PARAMETERS("A0100", "客户端参数错误"), + + /** + * 系统 + */ + SERVICE_ERROR("B0001", "服务执行异常"), + RESOURCE_NOT_FOUND("B0404", "资源不存在"), + ; + + /** + * 响应状态 + */ + private final String code; + /** + * 响应编码 + */ + private final String msg; +} diff --git a/austin-common/src/main/java/com/java3y/austin/pojo/vo/BasicResultVO.java b/austin-common/src/main/java/com/java3y/austin/pojo/vo/BasicResultVO.java new file mode 100644 index 0000000..052bbff --- /dev/null +++ b/austin-common/src/main/java/com/java3y/austin/pojo/vo/BasicResultVO.java @@ -0,0 +1,119 @@ +package com.java3y.austin.pojo.vo; + +import com.java3y.austin.constant.RespStatusEnum; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; + +/** + * @author zzb + * @since 2021.11.17 + */ + +@Getter +@ToString(callSuper = true) +@AllArgsConstructor +@NoArgsConstructor +public final class BasicResultVO { + + /** + * 响应状态 + */ + private String code; + + /** + * 响应编码 + */ + private String msg; + + /** + * 返回数据 + */ + private T data; + + public BasicResultVO(RespStatusEnum status) { + this(status, null); + } + + public BasicResultVO(RespStatusEnum status, T data) { + this(status, status.getMsg(), data); + } + + public BasicResultVO(RespStatusEnum status, String msg, T data) { + this.code = status.getCode(); + this.msg = msg; + this.data = data; + } + + /** + * @return 默认成功响应 + */ + public static BasicResultVO success() { + return new BasicResultVO<>(RespStatusEnum.SUCCESS); + } + + /** + * 自定义信息的成功响应 + *

通常用作插入成功等并显示具体操作通知如: return BasicResultVO.success("发送信息成功")

+ * + * @param msg 信息 + * @return 自定义信息的成功响应 + */ + public static BasicResultVO success(String msg) { + return new BasicResultVO<>(RespStatusEnum.SUCCESS, msg, null); + } + + /** + * 带数据的成功响应 + * + * @param data 数据 + * @return 带数据的成功响应 + */ + public static BasicResultVO success(T data) { + return new BasicResultVO<>(RespStatusEnum.SUCCESS, data); + } + + /** + * @return 默认失败响应 + */ + public static BasicResultVO fail() { + return new BasicResultVO<>( + RespStatusEnum.FAIL, + RespStatusEnum.FAIL.getMsg(), + null + ); + } + + /** + * 自定义错误信息的失败响应 + * + * @param msg 错误信息 + * @return 自定义错误信息的失败响应 + */ + public static BasicResultVO fail(String msg) { + return fail(RespStatusEnum.FAIL, msg); + } + + /** + * 自定义状态的失败响应 + * + * @param status 状态 + * @return 自定义状态的失败响应 + */ + public static BasicResultVO fail(RespStatusEnum status) { + return fail(status, status.getMsg()); + } + + /** + * 自定义状态和信息的失败响应 + * + * @param status 状态 + * @param msg 信息 + * @return 自定义状态和信息的失败响应 + */ + public static BasicResultVO fail(RespStatusEnum status, String msg) { + return new BasicResultVO<>(status, msg, null); + } + +} diff --git a/austin-handler/src/main/java/com/java3y/austin/handler/SmsHandler.java b/austin-handler/src/main/java/com/java3y/austin/handler/SmsHandler.java index 6c25e9e..5784c03 100644 --- a/austin-handler/src/main/java/com/java3y/austin/handler/SmsHandler.java +++ b/austin-handler/src/main/java/com/java3y/austin/handler/SmsHandler.java @@ -12,30 +12,35 @@ import org.springframework.stereotype.Component; import java.util.List; +/** + * @author 三歪 + */ + @Component public class SmsHandler implements Handler { - @Autowired - private SmsRecordDao smsRecordDao; - @Autowired - private SmsScript smsScript; + @Autowired + private SmsRecordDao smsRecordDao; - @Override - public boolean doHandler(TaskInfo taskInfo) { + @Autowired + private SmsScript smsScript; - SmsParam smsParam = SmsParam.builder() - .phones(taskInfo.getReceiver()) - .content(taskInfo.getContent()) - .messageTemplateId(taskInfo.getMessageTemplateId()) - .supplierId(10) - .supplierName("腾讯云通知类消息渠道").build(); - List recordList = smsScript.send(smsParam); + @Override + public boolean doHandler(TaskInfo taskInfo) { - if (CollUtil.isNotEmpty(recordList)) { - smsRecordDao.saveAll(recordList); - return true; - } + SmsParam smsParam = SmsParam.builder() + .phones(taskInfo.getReceiver()) + .content(taskInfo.getContent()) + .messageTemplateId(taskInfo.getMessageTemplateId()) + .supplierId(10) + .supplierName("腾讯云通知类消息渠道").build(); + List recordList = smsScript.send(smsParam); - return false; + if (CollUtil.isEmpty(recordList)) { + return false; } + + smsRecordDao.saveAll(recordList); + return true; + } } diff --git a/austin-web/src/main/java/com/java3y/austin/controller/SendController.java b/austin-web/src/main/java/com/java3y/austin/controller/SendController.java index 88a1791..6d22b3d 100644 --- a/austin-web/src/main/java/com/java3y/austin/controller/SendController.java +++ b/austin-web/src/main/java/com/java3y/austin/controller/SendController.java @@ -1,38 +1,44 @@ package com.java3y.austin.controller; -import com.alibaba.fastjson.JSON; import com.java3y.austin.handler.SmsHandler; -import com.java3y.austin.pojo.SmsParam; import com.java3y.austin.pojo.TaskInfo; -import com.java3y.austin.script.TencentSmsScript; +import com.java3y.austin.pojo.vo.BasicResultVO; +import java.util.Collections; +import java.util.HashSet; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.Arrays; -import java.util.HashSet; +/** + * @author 三歪 + */ @RestController public class SendController { - @Autowired - private SmsHandler smsHandler; - - /** - * 测试发送短信 - * @param phone 手机号 - * @return - */ - @GetMapping("/sendSms") - public boolean sendSms(String phone,String content,Long messageTemplateId ) { + @Autowired + private SmsHandler smsHandler; - TaskInfo taskInfo = TaskInfo.builder().receiver(new HashSet<>(Arrays.asList(phone))) - .content(content).messageTemplateId(messageTemplateId).build(); - - return smsHandler.doHandler(taskInfo); + /** + * 测试发送短信 + * @param phone 手机号 + * @return BasicResultVO + */ + @GetMapping("/sendSms") + public BasicResultVO sendSms(String phone, String content, Long messageTemplateId) { + TaskInfo taskInfo = TaskInfo.builder().receiver(new HashSet<>( + Collections.singletonList(phone))) + .content(content) + .messageTemplateId(messageTemplateId) + .build(); + if (smsHandler.doHandler(taskInfo)) { + return BasicResultVO.success("发送信息成功"); } + return BasicResultVO.fail(); + } + }