mirror of
https://github.com/201206030/novel-cloud.git
synced 2025-04-27 01:40:50 +00:00
后台数据校验优化
This commit is contained in:
parent
a2b164dc86
commit
296c7616ea
@ -1,10 +1,11 @@
|
|||||||
package com.java2nb.novel.common.exception;
|
package com.java2nb.novel.common.exception;
|
||||||
|
|
||||||
import com.java2nb.novel.common.bean.ResultBean;
|
import com.java2nb.novel.common.bean.ResultBean;
|
||||||
|
import com.java2nb.novel.common.enums.ResponseStatus;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.validation.BindException;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通用的异常处理器
|
* 通用的异常处理器
|
||||||
@ -13,10 +14,18 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|||||||
* @since 2020/5/23
|
* @since 2020/5/23
|
||||||
* */
|
* */
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@ControllerAdvice
|
@RestControllerAdvice
|
||||||
@ResponseBody
|
|
||||||
public class CommonExceptionHandler {
|
public class CommonExceptionHandler {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理后台数据校验异常
|
||||||
|
* */
|
||||||
|
@ExceptionHandler(BindException.class)
|
||||||
|
public ResultBean handlerBindException(BindException e){
|
||||||
|
log.error(e.getMessage(),e);
|
||||||
|
return ResultBean.fail(ResponseStatus.PARAM_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理业务异常
|
* 处理业务异常
|
||||||
* */
|
* */
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.java2nb.novel.common.valid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增数据的校验分组
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
*/
|
||||||
|
public interface AddGroup {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.java2nb.novel.common.valid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新数据的校验分组
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
*/
|
||||||
|
public interface UpdateGroup {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,48 +1,66 @@
|
|||||||
package com.java2nb.novel.user.entity;
|
package com.java2nb.novel.user.entity;
|
||||||
|
|
||||||
|
import com.java2nb.novel.common.valid.AddGroup;
|
||||||
|
import com.java2nb.novel.common.valid.UpdateGroup;
|
||||||
import io.swagger.annotations.ApiModelProperty;
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
|
||||||
import javax.annotation.Generated;
|
import javax.annotation.Generated;
|
||||||
|
import javax.validation.constraints.*;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
public class User implements Serializable {
|
public class User implements Serializable {
|
||||||
|
|
||||||
|
@Null(groups = {AddGroup.class, UpdateGroup.class})
|
||||||
@ApiModelProperty(value = "主键")
|
@ApiModelProperty(value = "主键")
|
||||||
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
|
@NotBlank(groups = {AddGroup.class},message="手机号不能为空!")
|
||||||
|
@Pattern(groups = {AddGroup.class},regexp="^1[3|4|5|6|7|8|9][0-9]{9}$",message="手机号格式不正确!")
|
||||||
@ApiModelProperty(value = "登录名")
|
@ApiModelProperty(value = "登录名")
|
||||||
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
|
@NotBlank(groups = {AddGroup.class},message="密码不能为空!")
|
||||||
|
@Null(groups = {UpdateGroup.class})
|
||||||
@ApiModelProperty(value = "登录密码")
|
@ApiModelProperty(value = "登录密码")
|
||||||
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
|
@Null(groups = {AddGroup.class})
|
||||||
@ApiModelProperty(value = "昵称")
|
@ApiModelProperty(value = "昵称")
|
||||||
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
||||||
private String nickName;
|
private String nickName;
|
||||||
|
|
||||||
|
@Null(groups = {AddGroup.class})
|
||||||
@ApiModelProperty(value = "用户头像")
|
@ApiModelProperty(value = "用户头像")
|
||||||
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
||||||
private String userPhoto;
|
private String userPhoto;
|
||||||
|
|
||||||
|
@Null(groups = {AddGroup.class})
|
||||||
|
@Min(value = 0,groups = {UpdateGroup.class})
|
||||||
|
@Max(value = 1,groups = {UpdateGroup.class})
|
||||||
@ApiModelProperty(value = "用户性别,0:男,1:女")
|
@ApiModelProperty(value = "用户性别,0:男,1:女")
|
||||||
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
||||||
private Byte userSex;
|
private Byte userSex;
|
||||||
|
|
||||||
|
@Null(groups = {AddGroup.class,UpdateGroup.class})
|
||||||
@ApiModelProperty(value = "账户余额")
|
@ApiModelProperty(value = "账户余额")
|
||||||
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
||||||
private Long accountBalance;
|
private Long accountBalance;
|
||||||
|
|
||||||
|
@Null(groups = {AddGroup.class,UpdateGroup.class})
|
||||||
@ApiModelProperty(value = "用户状态,0:正常")
|
@ApiModelProperty(value = "用户状态,0:正常")
|
||||||
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
||||||
private Byte status;
|
private Byte status;
|
||||||
|
|
||||||
|
@Null(groups = {AddGroup.class,UpdateGroup.class})
|
||||||
@ApiModelProperty(value = "创建时间")
|
@ApiModelProperty(value = "创建时间")
|
||||||
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
||||||
private Date createTime;
|
private Date createTime;
|
||||||
|
|
||||||
|
@Null(groups = {AddGroup.class,UpdateGroup.class})
|
||||||
@ApiModelProperty(value = "更新时间")
|
@ApiModelProperty(value = "更新时间")
|
||||||
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
||||||
private Date updateTime;
|
private Date updateTime;
|
||||||
|
@ -9,6 +9,8 @@ import com.java2nb.novel.common.bean.UserDetails;
|
|||||||
import com.java2nb.novel.common.cache.CacheService;
|
import com.java2nb.novel.common.cache.CacheService;
|
||||||
import com.java2nb.novel.common.enums.ResponseStatus;
|
import com.java2nb.novel.common.enums.ResponseStatus;
|
||||||
import com.java2nb.novel.common.utils.RandomValidateCodeUtil;
|
import com.java2nb.novel.common.utils.RandomValidateCodeUtil;
|
||||||
|
import com.java2nb.novel.common.valid.AddGroup;
|
||||||
|
import com.java2nb.novel.common.valid.UpdateGroup;
|
||||||
import com.java2nb.novel.user.entity.User;
|
import com.java2nb.novel.user.entity.User;
|
||||||
import com.java2nb.novel.user.entity.UserFeedback;
|
import com.java2nb.novel.user.entity.UserFeedback;
|
||||||
import com.java2nb.novel.user.feign.BookFeignClient;
|
import com.java2nb.novel.user.feign.BookFeignClient;
|
||||||
@ -22,6 +24,7 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@ -54,13 +57,7 @@ public class UserController extends BaseController {
|
|||||||
*/
|
*/
|
||||||
@ApiOperation("用户登陆接口")
|
@ApiOperation("用户登陆接口")
|
||||||
@GetMapping("login")
|
@GetMapping("login")
|
||||||
public ResultBean<Map<String, Object>> login(@Valid UserForm user, BindingResult result) {
|
public ResultBean login(User user) {
|
||||||
//判断参数是否合法
|
|
||||||
if (result.hasErrors()) {
|
|
||||||
log.info(result.getAllErrors().toString());
|
|
||||||
return ResultBean.fail(ResponseStatus.PARAM_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
//登陆
|
//登陆
|
||||||
UserDetails userDetails = userService.login(user);
|
UserDetails userDetails = userService.login(user);
|
||||||
|
|
||||||
@ -77,13 +74,8 @@ public class UserController extends BaseController {
|
|||||||
*/
|
*/
|
||||||
@ApiOperation("用户注册接口")
|
@ApiOperation("用户注册接口")
|
||||||
@PostMapping("register")
|
@PostMapping("register")
|
||||||
public ResultBean<Map<String, Object>> register(@Valid UserForm user, @RequestParam(value = "velCode", defaultValue = "") String velCode, BindingResult result) {
|
public ResultBean register(@Validated({AddGroup.class}) User user, @RequestParam(value = "velCode", defaultValue = "") String velCode) {
|
||||||
|
|
||||||
//判断参数是否合法
|
|
||||||
if (result.hasErrors()) {
|
|
||||||
log.info(result.getAllErrors().toString());
|
|
||||||
return ResultBean.fail(ResponseStatus.PARAM_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
//判断验证码是否正确
|
//判断验证码是否正确
|
||||||
if (!velCode.equals(cacheService.get(RandomValidateCodeUtil.RANDOM_CODE_KEY))) {
|
if (!velCode.equals(cacheService.get(RandomValidateCodeUtil.RANDOM_CODE_KEY))) {
|
||||||
@ -250,7 +242,7 @@ public class UserController extends BaseController {
|
|||||||
* */
|
* */
|
||||||
@ApiOperation("人信息更新接口")
|
@ApiOperation("人信息更新接口")
|
||||||
@PostMapping("updateUserInfo")
|
@PostMapping("updateUserInfo")
|
||||||
public ResultBean updateUserInfo(User user, HttpServletRequest request) {
|
public ResultBean updateUserInfo(@Validated({UpdateGroup.class}) User user, HttpServletRequest request) {
|
||||||
UserDetails userDetails = getUserDetails(request);
|
UserDetails userDetails = getUserDetails(request);
|
||||||
if (userDetails == null) {
|
if (userDetails == null) {
|
||||||
return ResultBean.fail(ResponseStatus.NO_LOGIN);
|
return ResultBean.fail(ResponseStatus.NO_LOGIN);
|
||||||
|
@ -29,12 +29,18 @@ public interface UserService {
|
|||||||
User queryByUsernameAndPassword(String username, String password);
|
User queryByUsernameAndPassword(String username, String password);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户登陆
|
* 用户注册
|
||||||
*
|
* @param user 用户注册信息类
|
||||||
* @param form 用户登陆提交信息类
|
|
||||||
* @return jwt载体信息类
|
* @return jwt载体信息类
|
||||||
*/
|
* */
|
||||||
UserDetails login(UserForm form);
|
UserDetails register(User user);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户登陆
|
||||||
|
* @param user 用户登陆信息类
|
||||||
|
* @return jwt载体信息类
|
||||||
|
* */
|
||||||
|
UserDetails login(User user);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -45,13 +51,6 @@ public interface UserService {
|
|||||||
*/
|
*/
|
||||||
List<User> queryById(List<Long> ids);
|
List<User> queryById(List<Long> ids);
|
||||||
|
|
||||||
/**
|
|
||||||
* 用户注册
|
|
||||||
*
|
|
||||||
* @param form 用户注册提交信息类
|
|
||||||
* @return jwt载体信息类
|
|
||||||
*/
|
|
||||||
UserDetails register(UserForm form);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询小说是否已加入书架
|
* 查询小说是否已加入书架
|
||||||
|
@ -79,9 +79,9 @@ public class UserServiceImpl implements UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserDetails login(UserForm form) {
|
public UserDetails login(User user) {
|
||||||
//根据用户名密码查询记录
|
//根据用户名密码查询记录
|
||||||
User user = queryByUsernameAndPassword(form.getUsername(), form.getPassword());
|
user = queryByUsernameAndPassword(user.getUsername(), user.getPassword());
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
throw new BusinessException(ResponseStatus.USERNAME_PASS_ERROR);
|
throw new BusinessException(ResponseStatus.USERNAME_PASS_ERROR);
|
||||||
}
|
}
|
||||||
@ -89,7 +89,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
UserDetails userDetails = new UserDetails();
|
UserDetails userDetails = new UserDetails();
|
||||||
userDetails.setId(user.getId());
|
userDetails.setId(user.getId());
|
||||||
userDetails.setNickName(user.getNickName());
|
userDetails.setNickName(user.getNickName());
|
||||||
userDetails.setUsername(form.getUsername());
|
userDetails.setUsername(user.getUsername());
|
||||||
return userDetails;
|
return userDetails;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,11 +104,11 @@ public class UserServiceImpl implements UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserDetails register(UserForm form) {
|
public UserDetails register(User user) {
|
||||||
//查询用户名是否已注册
|
//查询用户名是否已注册
|
||||||
SelectStatementProvider selectStatement = select(count(UserDynamicSqlSupport.id))
|
SelectStatementProvider selectStatement = select(count(UserDynamicSqlSupport.id))
|
||||||
.from(UserDynamicSqlSupport.user)
|
.from(UserDynamicSqlSupport.user)
|
||||||
.where(UserDynamicSqlSupport.username, isEqualTo(form.getUsername()))
|
.where(UserDynamicSqlSupport.username, isEqualTo(user.getUsername()))
|
||||||
.build()
|
.build()
|
||||||
.render(RenderingStrategies.MYBATIS3);
|
.render(RenderingStrategies.MYBATIS3);
|
||||||
long count = userMapper.count(selectStatement);
|
long count = userMapper.count(selectStatement);
|
||||||
@ -117,7 +117,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
throw new BusinessException(ResponseStatus.USERNAME_EXIST);
|
throw new BusinessException(ResponseStatus.USERNAME_EXIST);
|
||||||
}
|
}
|
||||||
User entity = new User();
|
User entity = new User();
|
||||||
BeanUtils.copyProperties(form, entity);
|
BeanUtils.copyProperties(user, entity);
|
||||||
//数据库生成注册记录
|
//数据库生成注册记录
|
||||||
Long id = new IdWorker().nextId();
|
Long id = new IdWorker().nextId();
|
||||||
entity.setId(id);
|
entity.setId(id);
|
||||||
@ -304,12 +304,9 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateUserInfo(Long userId, User user) {
|
public void updateUserInfo(Long userId, User user) {
|
||||||
User updateUser = new User();
|
user.setId(userId);
|
||||||
updateUser.setId(userId);
|
user.setUpdateTime(new Date());
|
||||||
updateUser.setNickName(user.getNickName());
|
userMapper.updateByPrimaryKeySelective(user);
|
||||||
updateUser.setUserSex(user.getUserSex());
|
|
||||||
updateUser.setUpdateTime(new Date());
|
|
||||||
userMapper.updateByPrimaryKeySelective(updateUser);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user