mirror of
https://gitee.com/bookshelfplus/bookshelfplus
synced 2025-09-22 01:30:40 +08:00
用户注册功能实现;用户表添加email字段
This commit is contained in:
@@ -10,6 +10,7 @@ public enum BusinessErrorCode implements CommonError {
|
||||
USER_LOGIN_FAILED(20002, "用户手机号或密码不正确"),
|
||||
USER_NOT_LOGIN(20003, "用户还未登录"),
|
||||
USER_TOKEN_EXPIRED(20004, "用户令牌过期"),
|
||||
USER_ALREADY_EXIST(20005, "用户已存在"),
|
||||
|
||||
// 30000开头为权限相关错误定义
|
||||
OPERATION_NOT_ALLOWED(30001, "用户没有此操作的权限");
|
||||
|
@@ -3,11 +3,20 @@ package plus.bookshelf.Controller.Controller;
|
||||
import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import plus.bookshelf.Common.Error.BusinessErrorCode;
|
||||
import plus.bookshelf.Common.Error.BusinessException;
|
||||
import plus.bookshelf.Common.Response.CommonReturnType;
|
||||
import plus.bookshelf.Common.Response.CommonReturnTypeStatus;
|
||||
import plus.bookshelf.Common.SessionManager.RedisSessionManager;
|
||||
import plus.bookshelf.Common.SessionManager.SessionManager;
|
||||
import plus.bookshelf.Service.Model.UserModel;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class BaseController {
|
||||
|
||||
@@ -66,23 +75,23 @@ public class BaseController {
|
||||
// return;
|
||||
}
|
||||
|
||||
// // 定义ExceptionHandler解决未被Controller层吸收的Exception
|
||||
// @ExceptionHandler(Exception.class)
|
||||
// @ResponseStatus(HttpStatus.OK)
|
||||
// @ResponseBody
|
||||
// public Object handlerException(HttpServletRequest request, Exception ex) {
|
||||
// HashMap<Object, Object> responseData = new HashMap<>();
|
||||
//
|
||||
// if (ex instanceof BusinessException) {
|
||||
// BusinessException businessException = (BusinessException) ex;
|
||||
// responseData.put("errCode", businessException.getErrCode());
|
||||
// responseData.put("errMsg", businessException.getErrMsg());
|
||||
// } else {
|
||||
// // 生产环境输出格式化信息
|
||||
// responseData.put("errCode", BusinessErrorCode.UNKNOWN_ERROR.getErrCode());
|
||||
// responseData.put("errMsg", BusinessErrorCode.UNKNOWN_ERROR.getErrMsg());
|
||||
// }
|
||||
//
|
||||
// return CommonReturnType.create(responseData, CommonReturnTypeStatus.FAILED);
|
||||
// }
|
||||
// 定义ExceptionHandler解决未被Controller层吸收的Exception
|
||||
@ExceptionHandler(Exception.class)
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
@ResponseBody
|
||||
public Object handlerException(HttpServletRequest request, Exception ex) {
|
||||
HashMap<Object, Object> responseData = new HashMap<>();
|
||||
|
||||
if (ex instanceof BusinessException) {
|
||||
BusinessException businessException = (BusinessException) ex;
|
||||
responseData.put("errCode", businessException.getErrCode());
|
||||
responseData.put("errMsg", businessException.getErrMsg());
|
||||
} else {
|
||||
// 生产环境输出格式化信息
|
||||
responseData.put("errCode", BusinessErrorCode.UNKNOWN_ERROR.getErrCode());
|
||||
responseData.put("errMsg", BusinessErrorCode.UNKNOWN_ERROR.getErrMsg());
|
||||
}
|
||||
|
||||
return CommonReturnType.create(responseData, CommonReturnTypeStatus.FAILED);
|
||||
}
|
||||
}
|
||||
|
@@ -46,20 +46,22 @@ public class UserController extends BaseController {
|
||||
return CommonReturnType.create(userVO);
|
||||
}
|
||||
|
||||
// @ApiOperation(value = "用户注册", notes = "传入用户名,以及密码明文,后台计算密码SHA1值,进行注册")
|
||||
// @RequestMapping(value = "register", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
|
||||
// @ResponseBody
|
||||
// public CommonReturnType register(@RequestParam(value = "username") String username,
|
||||
// @RequestParam(value = "password") String password) {
|
||||
// if (username == null || password == null) {
|
||||
// return null;
|
||||
// }
|
||||
// String encryptPwd = DigestUtils.sha1Hex(password);
|
||||
//
|
||||
// UserModel userModel = userService.userRegister(username, encryptPwd);
|
||||
// UserVO userVO = convertFromService(userModel);
|
||||
// return CommonReturnType.create(userVO);
|
||||
// }
|
||||
@ApiOperation(value = "用户注册", notes = "传入用户名,以及密码明文,后台计算密码SHA1值,进行注册")
|
||||
@RequestMapping(value = "register", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
|
||||
@ResponseBody
|
||||
public CommonReturnType register(@RequestParam(value = "username") String username,
|
||||
@RequestParam(value = "password") String password) throws BusinessException {
|
||||
if (username == null || password == null) {
|
||||
return null;
|
||||
}
|
||||
String encryptPwd = DigestUtils.sha1Hex(password);
|
||||
|
||||
if(!userService.userRegister(username, encryptPwd)){
|
||||
throw new BusinessException(BusinessErrorCode.UNKNOWN_ERROR, "未知错误,注册失败");
|
||||
}
|
||||
// 注册成功后,进行登录
|
||||
return login(username, password);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "用户登出", notes = "用户退出登录")
|
||||
// @ApiImplicitParams({
|
||||
|
@@ -55,6 +55,15 @@ public class UserDO {
|
||||
*/
|
||||
private String avatar;
|
||||
|
||||
/**
|
||||
*
|
||||
* This field was generated by MyBatis Generator.
|
||||
* This field corresponds to the database column user_info.email
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
private String email;
|
||||
|
||||
/**
|
||||
*
|
||||
* This field was generated by MyBatis Generator.
|
||||
@@ -226,6 +235,30 @@ public class UserDO {
|
||||
this.avatar = avatar == null ? null : avatar.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method returns the value of the database column user_info.email
|
||||
*
|
||||
* @return the value of user_info.email
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method sets the value of the database column user_info.email
|
||||
*
|
||||
* @param email the value for user_info.email
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
public void setEmail(String email) {
|
||||
this.email = email == null ? null : email.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method returns the value of the database column user_info.phone
|
||||
|
@@ -3,7 +3,7 @@ package plus.bookshelf.Dao.Mapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import plus.bookshelf.Dao.DO.CategoryDO;
|
||||
|
||||
@Repository
|
||||
@Repository // 添加这个注解,Autowired的时候idea就不会报错了
|
||||
public interface CategoryDOMapper {
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
|
@@ -3,7 +3,7 @@ package plus.bookshelf.Dao.Mapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import plus.bookshelf.Dao.DO.FileDO;
|
||||
|
||||
@Repository
|
||||
@Repository // 添加这个注解,Autowired的时候idea就不会报错了
|
||||
public interface FileDOMapper {
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
|
@@ -3,7 +3,7 @@ package plus.bookshelf.Dao.Mapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import plus.bookshelf.Dao.DO.FileObjectDO;
|
||||
|
||||
@Repository
|
||||
@Repository // 添加这个注解,Autowired的时候idea就不会报错了
|
||||
public interface FileObjectDOMapper {
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
|
@@ -1,7 +1,9 @@
|
||||
package plus.bookshelf.Dao.Mapper;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import plus.bookshelf.Dao.DO.ThirdPartyUserAuthDO;
|
||||
|
||||
@Repository // 添加这个注解,Autowired的时候idea就不会报错了
|
||||
public interface ThirdPartyUserAuthDOMapper {
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
|
@@ -1,7 +1,9 @@
|
||||
package plus.bookshelf.Dao.Mapper;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import plus.bookshelf.Dao.DO.ThirdPartyUserDO;
|
||||
|
||||
@Repository // 添加这个注解,Autowired的时候idea就不会报错了
|
||||
public interface ThirdPartyUserDOMapper {
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
|
@@ -3,7 +3,7 @@ package plus.bookshelf.Dao.Mapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import plus.bookshelf.Dao.DO.UserDO;
|
||||
|
||||
@Repository
|
||||
@Repository // 添加这个注解,Autowired的时候idea就不会报错了
|
||||
public interface UserDOMapper {
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
@@ -54,4 +54,12 @@ public interface UserDOMapper {
|
||||
int updateByPrimaryKey(UserDO record);
|
||||
|
||||
UserDO selectByUsernameAndEncryptpwd(String username, String encryptPwd);
|
||||
|
||||
/**
|
||||
* 获取是否存在该用户,用于注册时判断
|
||||
* 若用户存在则返回大于0的整数,否则返回0
|
||||
* @param username
|
||||
* @return
|
||||
*/
|
||||
Integer selectCountByUsername(String username);
|
||||
}
|
@@ -3,6 +3,7 @@ package plus.bookshelf.Service.Impl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import plus.bookshelf.Common.Error.BusinessErrorCode;
|
||||
import plus.bookshelf.Common.Error.BusinessException;
|
||||
import plus.bookshelf.Common.SessionManager.RedisSessionManager;
|
||||
@@ -55,6 +56,26 @@ public class UserServiceImpl implements UserService {
|
||||
return userModel;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Boolean userRegister(String username, String encryptPwd) throws BusinessException {
|
||||
if (username == null || "".equals(username)) {
|
||||
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "用户名不能为空");
|
||||
} else if (encryptPwd == null || "".equals(encryptPwd)) {
|
||||
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "密码不能为空");
|
||||
}
|
||||
Integer count = userDOMapper.selectCountByUsername(username);
|
||||
if (count > 0) {
|
||||
throw new BusinessException(BusinessErrorCode.USER_ALREADY_EXIST, "用户已存在");
|
||||
}
|
||||
UserDO userDO = new UserDO();
|
||||
userDO.setUsername(username);
|
||||
userDO.setEncriptPwd(encryptPwd);
|
||||
userDO.setGroup("USER");
|
||||
userDO.setNickname("该用户尚未设置昵称");
|
||||
return userDOMapper.insertSelective(userDO) > 0;
|
||||
}
|
||||
|
||||
private UserModel convertFromDataObject(UserDO userDO) {
|
||||
if (userDO == null) {
|
||||
return null;
|
||||
|
@@ -8,8 +8,8 @@ public interface UserService {
|
||||
/**
|
||||
* 用户登录
|
||||
*
|
||||
* @param username
|
||||
* @param encryptPwd
|
||||
* @param username 用户名
|
||||
* @param encryptPwd 加密后密码
|
||||
*/
|
||||
UserModel userLogin(String username, String encryptPwd);
|
||||
|
||||
@@ -17,7 +17,7 @@ public interface UserService {
|
||||
* 通过用户Id获取用户
|
||||
*
|
||||
* @param id 用户Id
|
||||
* @return
|
||||
* @return UserModel
|
||||
*/
|
||||
UserModel getUserById(Integer id);
|
||||
|
||||
@@ -25,8 +25,17 @@ public interface UserService {
|
||||
* 检查用户令牌是否有效,并返回令牌对应的用户 UserModel
|
||||
* (令牌无效直接抛出异常)
|
||||
*
|
||||
* @param token
|
||||
* @return
|
||||
* @param token 用户令牌
|
||||
* @return UserModel
|
||||
*/
|
||||
UserModel getUserByToken(RedisTemplate redisTemplate, String token) throws BusinessException;
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param encryptPwd 加密后密码
|
||||
* @return 注册成功返回true,否则返回false
|
||||
*/
|
||||
Boolean userRegister(String username, String encryptPwd) throws BusinessException;
|
||||
}
|
||||
|
@@ -12,6 +12,7 @@
|
||||
<result column="nickname" jdbcType="VARCHAR" property="nickname" />
|
||||
<result column="group" jdbcType="VARCHAR" property="group" />
|
||||
<result column="avatar" jdbcType="VARCHAR" property="avatar" />
|
||||
<result column="email" jdbcType="VARCHAR" property="email" />
|
||||
<result column="phone" jdbcType="VARCHAR" property="phone" />
|
||||
<result column="weixin_third_party_auth_code" jdbcType="VARCHAR" property="weixinThirdPartyAuthCode" />
|
||||
<result column="qq_third_party_auth_code" jdbcType="VARCHAR" property="qqThirdPartyAuthCode" />
|
||||
@@ -21,7 +22,7 @@
|
||||
WARNING - @mbg.generated
|
||||
This element is automatically generated by MyBatis Generator, do not modify.
|
||||
-->
|
||||
id, username, encript_pwd, nickname, `group`, avatar, phone, weixin_third_party_auth_code,
|
||||
id, username, encript_pwd, nickname, `group`, avatar, email, phone, weixin_third_party_auth_code,
|
||||
qq_third_party_auth_code
|
||||
</sql>
|
||||
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
|
||||
@@ -49,12 +50,12 @@
|
||||
-->
|
||||
insert into user_info (id, username, encript_pwd,
|
||||
nickname, `group`, avatar,
|
||||
phone, weixin_third_party_auth_code, qq_third_party_auth_code
|
||||
)
|
||||
email, phone, weixin_third_party_auth_code,
|
||||
qq_third_party_auth_code)
|
||||
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{encriptPwd,jdbcType=VARCHAR},
|
||||
#{nickname,jdbcType=VARCHAR}, #{group,jdbcType=VARCHAR}, #{avatar,jdbcType=VARCHAR},
|
||||
#{phone,jdbcType=VARCHAR}, #{weixinThirdPartyAuthCode,jdbcType=VARCHAR}, #{qqThirdPartyAuthCode,jdbcType=VARCHAR}
|
||||
)
|
||||
#{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{weixinThirdPartyAuthCode,jdbcType=VARCHAR},
|
||||
#{qqThirdPartyAuthCode,jdbcType=VARCHAR})
|
||||
</insert>
|
||||
<insert id="insertSelective" parameterType="plus.bookshelf.Dao.DO.UserDO">
|
||||
<!--
|
||||
@@ -81,6 +82,9 @@
|
||||
<if test="avatar != null">
|
||||
avatar,
|
||||
</if>
|
||||
<if test="email != null">
|
||||
email,
|
||||
</if>
|
||||
<if test="phone != null">
|
||||
phone,
|
||||
</if>
|
||||
@@ -110,6 +114,9 @@
|
||||
<if test="avatar != null">
|
||||
#{avatar,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="email != null">
|
||||
#{email,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="phone != null">
|
||||
#{phone,jdbcType=VARCHAR},
|
||||
</if>
|
||||
@@ -143,6 +150,9 @@
|
||||
<if test="avatar != null">
|
||||
avatar = #{avatar,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="email != null">
|
||||
email = #{email,jdbcType=VARCHAR},
|
||||
</if>
|
||||
<if test="phone != null">
|
||||
phone = #{phone,jdbcType=VARCHAR},
|
||||
</if>
|
||||
@@ -166,6 +176,7 @@
|
||||
nickname = #{nickname,jdbcType=VARCHAR},
|
||||
`group` = #{group,jdbcType=VARCHAR},
|
||||
avatar = #{avatar,jdbcType=VARCHAR},
|
||||
email = #{email,jdbcType=VARCHAR},
|
||||
phone = #{phone,jdbcType=VARCHAR},
|
||||
weixin_third_party_auth_code = #{weixinThirdPartyAuthCode,jdbcType=VARCHAR},
|
||||
qq_third_party_auth_code = #{qqThirdPartyAuthCode,jdbcType=VARCHAR}
|
||||
@@ -177,4 +188,10 @@
|
||||
from user_info
|
||||
where username = #{username} and encript_pwd = #{encryptPwd}
|
||||
</select>
|
||||
<select id="selectCountByUsername" resultType="java.lang.Integer">
|
||||
select
|
||||
count(*)
|
||||
from user_info
|
||||
where username = #{username}
|
||||
</select>
|
||||
</mapper>
|
Reference in New Issue
Block a user