feature(austin-common):1.新增:统一响应请求回应

pull/2/head
开发-庄子斌 3 years ago
parent 2e9d26e781
commit 3fa0a63f98

@ -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;
}

@ -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<T> {
/**
*
*/
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<Void> success() {
return new BasicResultVO<>(RespStatusEnum.SUCCESS);
}
/**
*
* <p>: return BasicResultVO.success("发送信息成功")</p>
*
* @param msg
* @return
*/
public static <T> BasicResultVO<T> success(String msg) {
return new BasicResultVO<>(RespStatusEnum.SUCCESS, msg, null);
}
/**
*
*
* @param data
* @return
*/
public static <T> BasicResultVO<T> success(T data) {
return new BasicResultVO<>(RespStatusEnum.SUCCESS, data);
}
/**
* @return
*/
public static <T> BasicResultVO<T> fail() {
return new BasicResultVO<>(
RespStatusEnum.FAIL,
RespStatusEnum.FAIL.getMsg(),
null
);
}
/**
*
*
* @param msg
* @return
*/
public static <T> BasicResultVO<T> fail(String msg) {
return fail(RespStatusEnum.FAIL, msg);
}
/**
*
*
* @param status
* @return
*/
public static <T> BasicResultVO<T> fail(RespStatusEnum status) {
return fail(status, status.getMsg());
}
/**
*
*
* @param status
* @param msg
* @return
*/
public static <T> BasicResultVO<T> fail(RespStatusEnum status, String msg) {
return new BasicResultVO<>(status, msg, null);
}
}

@ -12,8 +12,13 @@ import org.springframework.stereotype.Component;
import java.util.List; import java.util.List;
/**
* @author
*/
@Component @Component
public class SmsHandler implements Handler { public class SmsHandler implements Handler {
@Autowired @Autowired
private SmsRecordDao smsRecordDao; private SmsRecordDao smsRecordDao;
@ -31,11 +36,11 @@ public class SmsHandler implements Handler {
.supplierName("腾讯云通知类消息渠道").build(); .supplierName("腾讯云通知类消息渠道").build();
List<SmsRecord> recordList = smsScript.send(smsParam); List<SmsRecord> recordList = smsScript.send(smsParam);
if (CollUtil.isNotEmpty(recordList)) { if (CollUtil.isEmpty(recordList)) {
smsRecordDao.saveAll(recordList); return false;
return true;
} }
return false; smsRecordDao.saveAll(recordList);
return true;
} }
} }

@ -1,16 +1,17 @@
package com.java3y.austin.controller; package com.java3y.austin.controller;
import com.alibaba.fastjson.JSON;
import com.java3y.austin.handler.SmsHandler; import com.java3y.austin.handler.SmsHandler;
import com.java3y.austin.pojo.SmsParam;
import com.java3y.austin.pojo.TaskInfo; 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.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays; /**
import java.util.HashSet; * @author
*/
@RestController @RestController
public class SendController { public class SendController {
@ -22,17 +23,22 @@ public class SendController {
/** /**
* *
* @param phone * @param phone
* @return * @return BasicResultVO
*/ */
@GetMapping("/sendSms") @GetMapping("/sendSms")
public boolean sendSms(String phone,String content,Long messageTemplateId ) { public BasicResultVO<Void> sendSms(String phone, String content, Long messageTemplateId) {
TaskInfo taskInfo = TaskInfo.builder().receiver(new HashSet<>(Arrays.asList(phone))) TaskInfo taskInfo = TaskInfo.builder().receiver(new HashSet<>(
.content(content).messageTemplateId(messageTemplateId).build(); Collections.singletonList(phone)))
.content(content)
return smsHandler.doHandler(taskInfo); .messageTemplateId(messageTemplateId)
.build();
if (smsHandler.doHandler(taskInfo)) {
return BasicResultVO.success("发送信息成功");
}
return BasicResultVO.fail();
} }
} }

Loading…
Cancel
Save