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,30 +12,35 @@ 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
private SmsRecordDao smsRecordDao;
@Autowired @Autowired
private SmsScript smsScript; private SmsRecordDao smsRecordDao;
@Override @Autowired
public boolean doHandler(TaskInfo taskInfo) { private SmsScript smsScript;
SmsParam smsParam = SmsParam.builder() @Override
.phones(taskInfo.getReceiver()) public boolean doHandler(TaskInfo taskInfo) {
.content(taskInfo.getContent())
.messageTemplateId(taskInfo.getMessageTemplateId())
.supplierId(10)
.supplierName("腾讯云通知类消息渠道").build();
List<SmsRecord> recordList = smsScript.send(smsParam);
if (CollUtil.isNotEmpty(recordList)) { SmsParam smsParam = SmsParam.builder()
smsRecordDao.saveAll(recordList); .phones(taskInfo.getReceiver())
return true; .content(taskInfo.getContent())
} .messageTemplateId(taskInfo.getMessageTemplateId())
.supplierId(10)
.supplierName("腾讯云通知类消息渠道").build();
List<SmsRecord> recordList = smsScript.send(smsParam);
return false; if (CollUtil.isEmpty(recordList)) {
return false;
} }
smsRecordDao.saveAll(recordList);
return true;
}
} }

@ -1,38 +1,44 @@
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 {
@Autowired @Autowired
private SmsHandler smsHandler; private SmsHandler smsHandler;
/**
*
* @param phone
* @return
*/
@GetMapping("/sendSms")
public boolean sendSms(String phone,String content,Long messageTemplateId ) {
TaskInfo taskInfo = TaskInfo.builder().receiver(new HashSet<>(Arrays.asList(phone))) /**
.content(content).messageTemplateId(messageTemplateId).build(); *
* @param phone
return smsHandler.doHandler(taskInfo); * @return BasicResultVO
*/
@GetMapping("/sendSms")
public BasicResultVO<Void> 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();
}
} }

Loading…
Cancel
Save