|
@@ -5,10 +5,15 @@ import cn.com.ty.lift.system.entity.UserAccount;
|
|
|
import cn.com.ty.lift.system.entity.UserInfo;
|
|
|
import cn.com.ty.lift.system.entity.model.UserRequest;
|
|
|
import cn.com.ty.lift.system.user.service.ILoginService;
|
|
|
+import cn.com.ty.lift.system.user.service.IUserAccountService;
|
|
|
+import cn.com.ty.lift.system.user.service.IUserService;
|
|
|
+import cn.com.ty.lift.system.utils.PasswordUtils;
|
|
|
import cn.com.xwy.boot.web.dto.RestResponse;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
import java.util.Date;
|
|
|
|
|
|
/**
|
|
@@ -20,22 +25,68 @@ import java.util.Date;
|
|
|
public class LoginService implements ILoginService {
|
|
|
|
|
|
@Autowired
|
|
|
- private UserService userService;
|
|
|
+ private IUserService userService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IUserAccountService userAccountService;
|
|
|
|
|
|
@Override
|
|
|
- public RestResponse register(UserRequest userRequest) {
|
|
|
+ public RestResponse register(HttpServletRequest request, UserRequest userRequest) {
|
|
|
+ String password = userRequest.getPassword();
|
|
|
+ //判断手机号是否为空
|
|
|
+ if(StringUtils.isBlank(userRequest.getMobile())){
|
|
|
+ return RestResponse.error(ApiConstants.RESULT_ERROR, "手机号为空");
|
|
|
+ }
|
|
|
+ //判断密码是否为空
|
|
|
+ if(StringUtils.isBlank(password)){
|
|
|
+ return RestResponse.error(ApiConstants.RESULT_ERROR, "密码为空");
|
|
|
+ }
|
|
|
UserAccount userAccount = new UserAccount();
|
|
|
userAccount.setMobile(userRequest.getMobile());
|
|
|
userAccount.setCreateDate(new Date());
|
|
|
UserInfo userInfo = new UserInfo();
|
|
|
userInfo.setName(userRequest.getName());
|
|
|
- //密码加密
|
|
|
+
|
|
|
+ //前端传递MD5加密密码,然后进行密码加密
|
|
|
+ //1.生成16位随机码
|
|
|
+ String salt = PasswordUtils.randomCode();
|
|
|
+ //2.对密码进行加盐处理,并对加盐的密码进行哈希加密
|
|
|
+ password = PasswordUtils.generatePassword(password, salt);
|
|
|
+ userAccount.setSalt(salt);
|
|
|
+ userAccount.setPassword(password);
|
|
|
|
|
|
//保存用户信息
|
|
|
boolean registerFlag = userService.register(userInfo, userAccount);
|
|
|
if(registerFlag){
|
|
|
- return RestResponse.ok(null, ApiConstants.RESULT_SUCCESS, "注册成功");
|
|
|
+ //注册成功,将用户信息放入session中
|
|
|
+ request.getSession().setAttribute(ApiConstants.CURRENT_USER, userAccount);
|
|
|
+ return RestResponse.ok(userAccount, ApiConstants.RESULT_SUCCESS, "注册成功");
|
|
|
}
|
|
|
return RestResponse.error(ApiConstants.RESULT_ERROR, "注册失败");
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public RestResponse login(HttpServletRequest request, UserRequest userRequest) {
|
|
|
+ String password = userRequest.getPassword();
|
|
|
+ if(StringUtils.isBlank(userRequest.getMobile())){
|
|
|
+ RestResponse.error(ApiConstants.RESULT_ERROR, "手机号为空无法注册");
|
|
|
+ }
|
|
|
+ if(StringUtils.isBlank(password)){
|
|
|
+ RestResponse.error(ApiConstants.RESULT_ERROR, "用户密码为空无法注册");
|
|
|
+ }
|
|
|
+ //通过手机号获取用户账户信息
|
|
|
+ UserAccount userAccount = userAccountService.getByMobile(userRequest.getMobile());
|
|
|
+ //验证手机号是否存在
|
|
|
+ if(userAccount == null){
|
|
|
+ return RestResponse.error(ApiConstants.RESULT_ERROR, "用户名不存在");
|
|
|
+ }
|
|
|
+ //校验密码是否正确
|
|
|
+ String inputPassword = PasswordUtils.generatePassword(password, userAccount.getSalt());
|
|
|
+ if(!inputPassword.equals(userAccount.getPassword())){
|
|
|
+ return RestResponse.error(ApiConstants.RESULT_ERROR, "密码输入有误");
|
|
|
+ }
|
|
|
+ //验证通过登录成功,将用户信息放入到session中
|
|
|
+ request.getSession().setAttribute(ApiConstants.CURRENT_USER, userAccount);
|
|
|
+ return RestResponse.ok(userAccount, "登录成功");
|
|
|
+ }
|
|
|
}
|