封装请求参数以及结果
This commit is contained in:
parent
3c9e7f0b20
commit
6f7262ca3e
@ -74,7 +74,7 @@ public class LogAspect {
|
||||
arguments[i] = args[i];
|
||||
}
|
||||
// 排除字段,敏感字段或太长的字段不显示:身份证、手机号、邮箱、密码等
|
||||
String[] excludeProperties = {"mobile"};
|
||||
String[] excludeProperties = {};
|
||||
PropertyPreFilters filters = new PropertyPreFilters();
|
||||
PropertyPreFilters.MySimplePropertyPreFilter excludefilter = filters.addFilter();
|
||||
excludefilter.addExcludes(excludeProperties);
|
||||
|
@ -0,0 +1,34 @@
|
||||
package com.yovinchen.train.common.exception;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* ClassName: BusinessException
|
||||
* Package: com.yovinchen.train.common.exception
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2024/1/20 16:37
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
//@AllArgsConstructor //生成全参数构造函数
|
||||
public class BusinessException extends RuntimeException {
|
||||
|
||||
private BusinessExceptionEnum e;
|
||||
|
||||
public BusinessException(BusinessExceptionEnum e) {
|
||||
this.e = e;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 不写入堆栈信息,提高性能
|
||||
*/
|
||||
@Override
|
||||
public Throwable fillInStackTrace() {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.yovinchen.train.common.exception;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* ClassName: BusinessExceptionEnum
|
||||
* Package: com.yovinchen.train.common.exception
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2024/1/20 16:36
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor //生成全参数构造函数
|
||||
public enum BusinessExceptionEnum {
|
||||
|
||||
MEMBER_MOBILE_EXIST("手机号已注册"),
|
||||
MEMBER_MOBILE_NOT_EXIST("请先获取短信验证码"),
|
||||
MEMBER_MOBILE_CODE_ERROR("短信验证码错误"),
|
||||
|
||||
BUSINESS_STATION_NAME_UNIQUE_ERROR("车站已存在"),
|
||||
BUSINESS_TRAIN_CODE_UNIQUE_ERROR("车次编号已存在"),
|
||||
BUSINESS_TRAIN_STATION_INDEX_UNIQUE_ERROR("同车次站序已存在"),
|
||||
BUSINESS_TRAIN_STATION_NAME_UNIQUE_ERROR("同车次站名已存在"),
|
||||
BUSINESS_TRAIN_CARRIAGE_INDEX_UNIQUE_ERROR("同车次厢号已存在");
|
||||
|
||||
private String desc;
|
||||
}
|
||||
|
@ -0,0 +1,23 @@
|
||||
package com.yovinchen.train.common.req;
|
||||
|
||||
import jakarta.validation.constraints.Max;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* ClassName: PageReq
|
||||
* Package: com.yovinchen.train.common.req
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2024/1/20 13:40
|
||||
*/
|
||||
@Data
|
||||
public class PageReq {
|
||||
|
||||
@NotNull(message = "【页码】不能为空")
|
||||
private Integer page;
|
||||
|
||||
@NotNull(message = "【每页条数】不能为空")
|
||||
@Max(value = 100, message = "【每页条数】不能超过100")
|
||||
private Integer size;
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.yovinchen.train.common.resp;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* ClassName: CommonResp
|
||||
* Package: com.yovinchen.train.common.resp
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2024/1/19 22:52
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor //生成全参数构造函数
|
||||
@NoArgsConstructor//生成无参构造函数
|
||||
@Builder
|
||||
public class CommonResp<T> {
|
||||
|
||||
/**
|
||||
* 业务上的成功或失败
|
||||
*/
|
||||
private boolean success = true;
|
||||
|
||||
/**
|
||||
* 返回信息
|
||||
*/
|
||||
private String message;
|
||||
|
||||
/**
|
||||
* 返回泛型数据,自定义类型
|
||||
*/
|
||||
private T content;
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.yovinchen.train.common.resp;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ClassName: PageResp
|
||||
* Package: com.yovinchen.train.common.resp
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2024/1/20 13:37
|
||||
*/
|
||||
@Data
|
||||
public class PageResp<T> implements Serializable {
|
||||
/**
|
||||
* 总条数
|
||||
*/
|
||||
private Long total;
|
||||
|
||||
/**
|
||||
* 当前页的列表
|
||||
*/
|
||||
private List<T> list;
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.yovinchen.train.common.util;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
|
||||
/**
|
||||
* ClassName: SnowUtil
|
||||
* Package: com.yovinchen.train.common.util
|
||||
* 封装 hutool 雪花算法
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2024/1/20 16:43
|
||||
*/
|
||||
|
||||
public class SnowUtil {
|
||||
//数据中心
|
||||
private static final long dataCenterId = 1;
|
||||
|
||||
//机器标识
|
||||
private static final long workerId = 1;
|
||||
|
||||
public static long getSnowflakeNextId() {
|
||||
return IdUtil.getSnowflake(workerId, dataCenterId)
|
||||
.nextId();
|
||||
}
|
||||
|
||||
public static String getSnowflakeNextIdStr() {
|
||||
return IdUtil.getSnowflake(workerId, dataCenterId)
|
||||
.nextIdStr();
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ public class MemberApplication {
|
||||
Environment env = app.run(args)
|
||||
.getEnvironment();
|
||||
LOG.info("启动成功!!");
|
||||
LOG.info("测试地址: \thttp://127.0.0.1:{}{}/test/test", env.getProperty("server.port"), env.getProperty("server.servlet.context-path"));
|
||||
LOG.info("测试地址: \thttp://127.0.0.1:{}{}/doc.html", env.getProperty("server.port"), env.getProperty("server.servlet.context-path"));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,15 @@
|
||||
package com.yovinchen.train.member.controller;
|
||||
|
||||
import com.yovinchen.train.member.entity.Member;
|
||||
import com.yovinchen.train.common.resp.CommonResp;
|
||||
import com.yovinchen.train.member.req.MemberRegisterReq;
|
||||
import com.yovinchen.train.member.service.MemberService;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 会员 前端控制器
|
||||
@ -24,9 +25,11 @@ public class MemberController {
|
||||
@Autowired
|
||||
MemberService memberService;
|
||||
|
||||
@GetMapping("/test")
|
||||
public List<Member> test() {
|
||||
return memberService.list();
|
||||
@PostMapping("/register")
|
||||
public CommonResp<Long> register(@Valid @RequestBody MemberRegisterReq req) {
|
||||
long register = memberService.register(req);
|
||||
CommonResp<Long> longCommonResp = new CommonResp<>();
|
||||
longCommonResp.setContent(register);
|
||||
return longCommonResp;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,8 +6,7 @@ import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -17,8 +16,7 @@ import lombok.Setter;
|
||||
* @author yovinchen
|
||||
* @since 2024-01-19 09:53:20
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Data
|
||||
@TableName("member")
|
||||
@ApiModel(value = "Member对象", description = "会员")
|
||||
public class Member {
|
||||
|
@ -6,8 +6,7 @@ import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@ -19,8 +18,7 @@ import java.util.Date;
|
||||
* @author yovinchen
|
||||
* @since 2024-01-19 09:53:20
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Data
|
||||
@TableName("passenger")
|
||||
@ApiModel(value = "Passenger对象", description = "乘车人")
|
||||
public class Passenger {
|
||||
|
@ -0,0 +1,29 @@
|
||||
package com.yovinchen.train.member.req;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* ClassName: MemberLoginReq
|
||||
* Package: com.yovinchen.train.member.req
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2024/1/19 22:55
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor //生成全参数构造函数
|
||||
@NoArgsConstructor//生成无参构造函数
|
||||
@Builder
|
||||
public class MemberLoginReq {
|
||||
|
||||
@NotBlank(message = "【手机号】不能为空")
|
||||
@Pattern(regexp = "^1\\d{10}$", message = "手机号码格式错误")
|
||||
private String mobile;
|
||||
|
||||
@NotBlank(message = "【短信验证码】不能为空")
|
||||
private String code;
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.yovinchen.train.member.req;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* ClassName: MemberRegisterReq
|
||||
* Package: com.yovinchen.train.member.req
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2024/1/19 22:55
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor //生成全参数构造函数
|
||||
@NoArgsConstructor//生成无参构造函数
|
||||
@Builder
|
||||
public class MemberRegisterReq {
|
||||
|
||||
@NotBlank(message = "【手机号】不能为空")
|
||||
@ApiModelProperty("手机号")
|
||||
private String mobile;
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.yovinchen.train.member.req;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* ClassName: MemberSendCodeReq
|
||||
* Package: com.yovinchen.train.member.req
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2024/1/20 11:57
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor //生成全参数构造函数
|
||||
@NoArgsConstructor//生成无参构造函数
|
||||
@Builder
|
||||
public class MemberSendCodeReq {
|
||||
|
||||
@NotBlank(message = "【手机号】不能为空")
|
||||
@Pattern(regexp = "^1\\d{10}$", message = "手机号码格式错误")
|
||||
private String mobile;
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.yovinchen.train.member.req;
|
||||
|
||||
import com.yovinchen.train.common.req.PageReq;
|
||||
import lombok.*;
|
||||
|
||||
/**
|
||||
* ClassName: PassengerQueryReq
|
||||
* Package: com.yovinchen.train.member.req
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2024/1/20 11:58
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@AllArgsConstructor //生成全参数构造函数
|
||||
@NoArgsConstructor//生成无参构造函数
|
||||
@Builder
|
||||
public class PassengerQueryReq extends PageReq {
|
||||
private Long memberId;
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.yovinchen.train.member.req;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* ClassName: PassengerSaveReq
|
||||
* Package: com.yovinchen.train.member.req
|
||||
*
|
||||
* @author yovinchen
|
||||
* @Create 2024/1/20 13:40
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor //生成全参数构造函数
|
||||
@NoArgsConstructor//生成无参构造函数
|
||||
@Builder
|
||||
public class PassengerSaveReq {
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 会员id
|
||||
*/
|
||||
private Long memberId;
|
||||
|
||||
/**
|
||||
* 姓名
|
||||
*/
|
||||
@NotBlank(message = "【姓名】不能为空")
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 身份证
|
||||
*/
|
||||
@NotBlank(message = "【身份证】不能为空")
|
||||
private String idCard;
|
||||
|
||||
/**
|
||||
* 旅客类型|枚举[PassengerTypeEnum]
|
||||
*/
|
||||
@NotBlank(message = "【旅客类型】不能为空")
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 新增时间
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 修改时间
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||
private Date updateTime;
|
||||
}
|
@ -2,6 +2,7 @@ package com.yovinchen.train.member.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.yovinchen.train.member.entity.Member;
|
||||
import com.yovinchen.train.member.req.MemberRegisterReq;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -13,4 +14,5 @@ import com.yovinchen.train.member.entity.Member;
|
||||
*/
|
||||
public interface MemberService extends IService<Member> {
|
||||
|
||||
long register(MemberRegisterReq req);
|
||||
}
|
||||
|
@ -1,8 +1,13 @@
|
||||
package com.yovinchen.train.member.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.yovinchen.train.common.exception.BusinessException;
|
||||
import com.yovinchen.train.common.exception.BusinessExceptionEnum;
|
||||
import com.yovinchen.train.common.util.SnowUtil;
|
||||
import com.yovinchen.train.member.entity.Member;
|
||||
import com.yovinchen.train.member.mapper.MemberMapper;
|
||||
import com.yovinchen.train.member.req.MemberRegisterReq;
|
||||
import com.yovinchen.train.member.service.MemberService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -17,4 +22,19 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
|
||||
|
||||
@Override
|
||||
public long register(MemberRegisterReq req) {
|
||||
String mobile = req.getMobile();
|
||||
LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(Member::getMobile, mobile);
|
||||
if (baseMapper.selectCount(queryWrapper) != 0) {
|
||||
throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
|
||||
}
|
||||
|
||||
Member newMember = new Member();
|
||||
newMember.setId(SnowUtil.getSnowflakeNextId());
|
||||
newMember.setMobile(mobile);
|
||||
baseMapper.insert(newMember);
|
||||
return newMember.getId();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user