Add front user authentication

This commit is contained in:
王鹏
2026-05-22 09:43:30 +08:00
parent ebb529213a
commit 276da1cb20
6 changed files with 263 additions and 43 deletions

View File

@@ -5,9 +5,9 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.generator.domain.front.FrontUser;
import com.ruoyi.generator.domain.front.dto.FrontLoginBody;
import com.ruoyi.generator.domain.front.dto.FrontRegisterBody;
import com.ruoyi.generator.mapper.front.FrontUserMapper;
@@ -18,55 +18,71 @@ public class FrontUserServiceImpl implements IFrontUserService
private FrontUserMapper frontUserMapper;
@Override
public FrontUser login(FrontLoginBody loginBody)
public FrontUser selectByUsername(String username)
{
if (loginBody == null || StringUtils.isEmpty(loginBody.getUsername()) || StringUtils.isEmpty(loginBody.getPassword()))
{
throw new ServiceException("用户名或密码不能为空");
}
FrontUser user = frontUserMapper.selectFrontUserByUsername(loginBody.getUsername());
if (user == null || !StringUtils.equals(user.getPassword(), loginBody.getPassword()))
{
throw new ServiceException("用户名或密码错误");
}
user.setLastLoginTime(new Date());
frontUserMapper.updateFrontUser(user);
return user;
return frontUserMapper.selectFrontUserByUsername(username);
}
@Override
public FrontUser register(FrontRegisterBody registerBody)
{
if (registerBody == null || StringUtils.isEmpty(registerBody.getUsername()) || StringUtils.isEmpty(registerBody.getPassword()))
{
throw new ServiceException("用户名或密码不能为空");
}
if (frontUserMapper.selectFrontUserByUsername(registerBody.getUsername()) != null)
{
throw new ServiceException("用户名已存在");
}
FrontUser user = new FrontUser();
user.setUsername(registerBody.getUsername());
user.setPassword(registerBody.getPassword());
user.setNickname(registerBody.getNickname());
user.setEmail(registerBody.getEmail());
user.setPhone(registerBody.getPhone());
user.setStatus("0");
user.setDelFlag("0");
frontUserMapper.insertFrontUser(user);
return user;
}
@Override
public FrontUser selectFrontUserById(Long userId)
public FrontUser selectById(Long userId)
{
return frontUserMapper.selectFrontUserById(userId);
}
@Override
public int register(FrontRegisterBody body)
{
if (body == null || StringUtils.isEmpty(body.getUsername()))
{
throw new ServiceException("用户名不能为空");
}
if (body.getUsername().length() < 2 || body.getUsername().length() > 50)
{
throw new ServiceException("用户名长度必须在2到50个字符之间");
}
if (StringUtils.isEmpty(body.getPassword()))
{
throw new ServiceException("密码不能为空");
}
if (body.getPassword().length() < 5 || body.getPassword().length() > 50)
{
throw new ServiceException("密码长度必须在5到50个字符之间");
}
if (frontUserMapper.selectFrontUserByUsername(body.getUsername()) != null)
{
throw new ServiceException("前台账号已存在");
}
FrontUser user = new FrontUser();
user.setUsername(body.getUsername());
user.setPassword(SecurityUtils.encryptPassword(body.getPassword()));
user.setNickname(StringUtils.isEmpty(body.getNickname()) ? body.getUsername() : body.getNickname());
user.setEmail(body.getEmail());
user.setPhone(body.getPhone());
user.setStatus("0");
user.setDelFlag("0");
return frontUserMapper.insertFrontUser(user);
}
@Override
public int updateLastLoginTime(Long userId)
{
FrontUser user = new FrontUser();
user.setUserId(userId);
user.setLastLoginTime(new Date());
return frontUserMapper.updateFrontUser(user);
}
@Override
public FrontUser selectFrontUserById(Long userId)
{
return selectById(userId);
}
@Override
public FrontUser selectFrontUserByUsername(String username)
{
return frontUserMapper.selectFrontUserByUsername(username);
return selectByUsername(username);
}
@Override

View File

@@ -2,13 +2,14 @@ package com.ruoyi.generator.service.front;
import java.util.List;
import com.ruoyi.generator.domain.front.FrontUser;
import com.ruoyi.generator.domain.front.dto.FrontLoginBody;
import com.ruoyi.generator.domain.front.dto.FrontRegisterBody;
public interface IFrontUserService
{
public FrontUser login(FrontLoginBody loginBody);
public FrontUser register(FrontRegisterBody registerBody);
public FrontUser selectByUsername(String username);
public FrontUser selectById(Long userId);
public int register(FrontRegisterBody body);
public int updateLastLoginTime(Long userId);
public FrontUser selectFrontUserById(Long userId);
public FrontUser selectFrontUserByUsername(String username);
public List<FrontUser> selectFrontUserList(FrontUser frontUser);