mirror of
https://gitee.com/coder-xiaomo/flashsale
synced 2025-09-13 23:41:39 +08:00
用户登录功能实现
This commit is contained in:
@@ -6,6 +6,7 @@ import com.cxyxiaomo.flashsale.error.EmBusinessError;
|
||||
import com.cxyxiaomo.flashsale.response.CommonReturnType;
|
||||
import com.cxyxiaomo.flashsale.service.UserService;
|
||||
import com.cxyxiaomo.flashsale.service.model.UserModel;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
@@ -30,6 +31,27 @@ public class UserController extends BaseController {
|
||||
@Autowired
|
||||
private HttpServletRequest httpServletRequest;
|
||||
|
||||
// 用户登录接口
|
||||
@RequestMapping(value = "/login", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
|
||||
@ResponseBody
|
||||
public CommonReturnType login(@RequestParam(name = "telephone") String telephone,
|
||||
@RequestParam(name = "password") String password) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
|
||||
|
||||
// 入参校验
|
||||
if (StringUtils.isEmpty(telephone) || StringUtils.isEmpty(password)) {
|
||||
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
|
||||
}
|
||||
|
||||
// 用户登录服务,用来校验用户登录是否合法
|
||||
UserModel userModel = userService.validateLogin(telephone, this.EncodeByMD5(password));
|
||||
|
||||
// 将登陆凭证加入到用户登录成功的Session内
|
||||
this.httpServletRequest.getSession().setAttribute("IS_LOGIN", true);
|
||||
this.httpServletRequest.getSession().setAttribute("LOGIN_USER", userModel);
|
||||
|
||||
return CommonReturnType.create(null);
|
||||
}
|
||||
|
||||
// 用户注册接口
|
||||
@RequestMapping(value = "/register", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
|
||||
@ResponseBody
|
||||
|
@@ -27,6 +27,8 @@ public interface UserDOMapper {
|
||||
*/
|
||||
int insertSelective(UserDO record);
|
||||
|
||||
UserDO selectByTelephone(String telephone);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table user_info
|
||||
|
@@ -2,11 +2,12 @@ package com.cxyxiaomo.flashsale.error;
|
||||
|
||||
public enum EmBusinessError implements CommonError {
|
||||
// 通用错误类型 00001
|
||||
PARAMETER_VALIDATION_ERROR(10001,"参数不合法"),
|
||||
UNKNOWN_ERROR(10002,"未知错误"),
|
||||
PARAMETER_VALIDATION_ERROR(10001, "参数不合法"),
|
||||
UNKNOWN_ERROR(10002, "未知错误"),
|
||||
|
||||
// 10000开头为用户信息相关错误定义
|
||||
USER_NOT_EXIST(20001, "用户不存在");
|
||||
USER_NOT_EXIST(20001, "用户不存在"),
|
||||
USER_LOGIN_FAILED(20002, "用户手机号或密码不正确");
|
||||
|
||||
private EmBusinessError(int errCode, String errMsg) {
|
||||
this.errCode = errCode;
|
||||
|
@@ -9,4 +9,13 @@ public interface UserService {
|
||||
|
||||
// 用户注册
|
||||
void register(UserModel userModel) throws BusinessException;
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*
|
||||
* @param telephone 用户注册手机
|
||||
* @param encryptPassword 用户加密后的密码
|
||||
* @throws BusinessException
|
||||
*/
|
||||
UserModel validateLogin(String telephone, String encryptPassword) throws BusinessException;
|
||||
}
|
||||
|
@@ -36,6 +36,24 @@ public class UserServiceImpl implements UserService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserModel validateLogin(String telephone, String encryptPassword) throws BusinessException {
|
||||
// 通过用户的手机获取用户信息
|
||||
UserDO userDO = userDOMapper.selectByTelephone(telephone);
|
||||
if (userDO == null) {
|
||||
throw new BusinessException(EmBusinessError.USER_LOGIN_FAILED);
|
||||
}
|
||||
UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userDO.getId());
|
||||
UserModel userModel = convertFromDataObject(userDO, userPasswordDO);
|
||||
|
||||
// 比对用户信息内加密的密码是否和传输进来的密码相匹配
|
||||
if (!StringUtils.equals(encryptPassword, userModel.getEncryptPassword())) {
|
||||
throw new BusinessException(EmBusinessError.USER_LOGIN_FAILED);
|
||||
}
|
||||
|
||||
return userModel;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void register(UserModel userModel) throws BusinessException {
|
||||
|
@@ -23,6 +23,12 @@
|
||||
-->
|
||||
id, name, gender, age, telephone, register_mode, third_party_id
|
||||
</sql>
|
||||
<select id="selectByTelephone" resultMap="BaseResultMap">
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from user_info
|
||||
where telephone = #{telephone,jdbcType=VARCHAR}
|
||||
</select>
|
||||
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
|
||||
<!--
|
||||
WARNING - @mbg.generated
|
||||
|
Reference in New Issue
Block a user