60 lines
1.9 KiB
Java
60 lines
1.9 KiB
Java
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;
|
||
import org.springframework.web.multipart.MultipartFile;
|
||
|
||
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);
|
||
}
|
||
|
||
//文件保存
|
||
public String saveIcoFile(MultipartFile icoFile){
|
||
|
||
return "";
|
||
}
|
||
}
|