2026-01-15 10:53:54 +08:00
|
|
|
|
package com.backend.webbackend.Tools;
|
|
|
|
|
|
import com.backend.webbackend.mapper.UserMapper;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
2026-01-20 14:24:29 +08:00
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
2026-01-15 10:53:54 +08:00
|
|
|
|
|
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
|
public class Tools {
|
|
|
|
|
|
|
|
|
|
|
|
private static final Pattern CN_MOBILE_PATTERN = Pattern.compile("^1[3-9]\\d{9}$");
|
|
|
|
|
|
// 定义编码器,解决 PASSWORD_ENCODER 无法解析
|
|
|
|
|
|
private static final BCryptPasswordEncoder PASSWORD_ENCODER = new BCryptPasswordEncoder();
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private final UserMapper userMapper;
|
|
|
|
|
|
|
|
|
|
|
|
public Tools(UserMapper userMapper) {
|
|
|
|
|
|
this.userMapper = userMapper;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//校验用户名唯一性
|
|
|
|
|
|
public boolean checkUsernameUnique(String username) {
|
|
|
|
|
|
return userMapper.countByUsername(username)==0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//校验手机号格式合法性
|
|
|
|
|
|
public boolean checkPhoneUnique(String phone) {
|
|
|
|
|
|
if (phone == null) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
String p = phone.trim();
|
|
|
|
|
|
return CN_MOBILE_PATTERN.matcher(p).matches();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 用户名密码加密处理(BCrypt:加盐慢哈希)
|
|
|
|
|
|
public String encryptPassword(String password) {
|
|
|
|
|
|
if (password == null || password.trim().isEmpty()) {
|
|
|
|
|
|
throw new IllegalArgumentException("密码不能为空");
|
|
|
|
|
|
}
|
|
|
|
|
|
return PASSWORD_ENCODER.encode(password);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 校验明文密码是否匹配已加密密码
|
|
|
|
|
|
public boolean matchesPassword(String rawPassword, String encodedPassword) {
|
|
|
|
|
|
if (rawPassword == null || encodedPassword == null) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return PASSWORD_ENCODER.matches(rawPassword, encodedPassword);
|
|
|
|
|
|
}
|
2026-01-20 14:24:29 +08:00
|
|
|
|
|
|
|
|
|
|
//文件保存
|
|
|
|
|
|
public String saveIcoFile(MultipartFile icoFile){
|
|
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
|
}
|
2026-01-15 10:53:54 +08:00
|
|
|
|
}
|