2025-04-27 16:49:30 +08:00
|
|
|
|
package com.jdc.jdcproject.service.impl;
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
import com.jdc.jdcproject.entity.Users;
|
|
|
|
|
import com.jdc.jdcproject.exceptionhandler.JdcException;
|
|
|
|
|
import com.jdc.jdcproject.mapper.UsersMapper;
|
|
|
|
|
import com.jdc.jdcproject.service.IUsersService;
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
import com.jdc.jdcproject.utils.JwtUtils;
|
|
|
|
|
import com.jdc.jdcproject.utils.MD5;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
2025-04-28 00:04:18 +08:00
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
|
2025-04-27 16:49:30 +08:00
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
|
* 服务实现类
|
|
|
|
|
* </p>
|
|
|
|
|
*
|
|
|
|
|
* @author haoyanlu
|
|
|
|
|
* @since 2025-04-26
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
|
|
|
|
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String login(Users user) {
|
|
|
|
|
System.out.println(user.toString());
|
|
|
|
|
String password = user.getPassword();
|
|
|
|
|
String name = user.getUsername();
|
|
|
|
|
String phone = user.getUsername();
|
2025-04-28 00:04:18 +08:00
|
|
|
|
|
|
|
|
|
if (user.getUsername().matches("^\\d{11}$")) {
|
2025-04-27 16:49:30 +08:00
|
|
|
|
phone = user.getUsername();
|
2025-04-28 00:04:18 +08:00
|
|
|
|
} else {
|
2025-04-27 16:49:30 +08:00
|
|
|
|
name = user.getUsername();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((StringUtils.isEmpty(phone) ^ StringUtils.isEmpty(name)) || StringUtils.isEmpty(password)) {
|
|
|
|
|
throw new JdcException(20001, "登陆失败");
|
|
|
|
|
}
|
2025-04-28 00:04:18 +08:00
|
|
|
|
|
2025-04-27 16:49:30 +08:00
|
|
|
|
QueryWrapper<Users> wrapper = new QueryWrapper<>();
|
|
|
|
|
wrapper.eq("username", name).or().eq("tel", phone);
|
|
|
|
|
Users users = baseMapper.selectOne(wrapper);
|
|
|
|
|
|
|
|
|
|
if (users == null) {
|
|
|
|
|
throw new JdcException(20001, "登陆失败");
|
|
|
|
|
}
|
2025-04-28 00:04:18 +08:00
|
|
|
|
|
|
|
|
|
// 增加锁定判断
|
|
|
|
|
if (users.getLockdatetime() != null && users.getLockdatetime().isAfter(LocalDateTime.now())) {
|
|
|
|
|
throw new JdcException(20001, "账号已锁定,请稍后再试");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 校验密码
|
2025-04-27 16:49:30 +08:00
|
|
|
|
if (!MD5.encrypt(password).equals(users.getPassword())) {
|
2025-04-28 00:04:18 +08:00
|
|
|
|
// 密码错误,增加错误次数
|
|
|
|
|
Integer errorCount = users.getErrlogincount() == null ? 0 : users.getErrlogincount();
|
|
|
|
|
errorCount++;
|
|
|
|
|
|
|
|
|
|
users.setErrlogincount(errorCount);
|
|
|
|
|
|
|
|
|
|
// 如果错误次数达到5次,锁定账户
|
|
|
|
|
if (errorCount >= 5) {
|
|
|
|
|
users.setLockdatetime(LocalDateTime.now().plusMinutes(30));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
baseMapper.updateById(users); // 保存用户状态
|
|
|
|
|
|
2025-04-27 16:49:30 +08:00
|
|
|
|
throw new JdcException(20001, "登陆失败");
|
|
|
|
|
}
|
2025-04-28 00:04:18 +08:00
|
|
|
|
|
|
|
|
|
// 登录成功,清除错误次数和锁定时间
|
|
|
|
|
users.setErrlogincount(0);
|
|
|
|
|
users.setLockdatetime(null);
|
|
|
|
|
baseMapper.updateById(users);
|
|
|
|
|
|
2025-04-27 16:49:30 +08:00
|
|
|
|
String token = JwtUtils.getJwtToken(users.getId(), users.getUsername());
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
}
|