|
|
@ -1,6 +1,8 @@ |
|
|
package com.chenhai.framework.web.service; |
|
|
package com.chenhai.framework.web.service; |
|
|
|
|
|
|
|
|
|
|
|
import com.chenhai.common.utils.*; |
|
|
import jakarta.annotation.Resource; |
|
|
import jakarta.annotation.Resource; |
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.security.authentication.AuthenticationManager; |
|
|
import org.springframework.security.authentication.AuthenticationManager; |
|
|
import org.springframework.security.authentication.BadCredentialsException; |
|
|
import org.springframework.security.authentication.BadCredentialsException; |
|
|
@ -19,9 +21,6 @@ import com.chenhai.common.exception.user.CaptchaException; |
|
|
import com.chenhai.common.exception.user.CaptchaExpireException; |
|
|
import com.chenhai.common.exception.user.CaptchaExpireException; |
|
|
import com.chenhai.common.exception.user.UserNotExistsException; |
|
|
import com.chenhai.common.exception.user.UserNotExistsException; |
|
|
import com.chenhai.common.exception.user.UserPasswordNotMatchException; |
|
|
import com.chenhai.common.exception.user.UserPasswordNotMatchException; |
|
|
import com.chenhai.common.utils.DateUtils; |
|
|
|
|
|
import com.chenhai.common.utils.MessageUtils; |
|
|
|
|
|
import com.chenhai.common.utils.StringUtils; |
|
|
|
|
|
import com.chenhai.common.utils.ip.IpUtils; |
|
|
import com.chenhai.common.utils.ip.IpUtils; |
|
|
import com.chenhai.framework.manager.AsyncManager; |
|
|
import com.chenhai.framework.manager.AsyncManager; |
|
|
import com.chenhai.framework.manager.factory.AsyncFactory; |
|
|
import com.chenhai.framework.manager.factory.AsyncFactory; |
|
|
@ -31,7 +30,6 @@ import com.chenhai.framework.security.context.AuthenticationContextHolder; |
|
|
import com.alibaba.fastjson2.JSONObject; |
|
|
import com.alibaba.fastjson2.JSONObject; |
|
|
import com.chenhai.common.core.domain.entity.SysUser; |
|
|
import com.chenhai.common.core.domain.entity.SysUser; |
|
|
import com.chenhai.common.core.domain.model.*; |
|
|
import com.chenhai.common.core.domain.model.*; |
|
|
import com.chenhai.common.utils.WechatDecryptUtil; |
|
|
|
|
|
import com.chenhai.framework.security.exception.WechatNeedBindException; |
|
|
import com.chenhai.framework.security.exception.WechatNeedBindException; |
|
|
import com.chenhai.framework.security.token.PhoneAuthenticationToken; |
|
|
import com.chenhai.framework.security.token.PhoneAuthenticationToken; |
|
|
import com.chenhai.framework.security.token.WechatAuthenticationToken; |
|
|
import com.chenhai.framework.security.token.WechatAuthenticationToken; |
|
|
@ -50,6 +48,7 @@ import java.util.Map; |
|
|
* |
|
|
* |
|
|
* @author ruoyi |
|
|
* @author ruoyi |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
@Slf4j |
|
|
@Component |
|
|
@Component |
|
|
public class SysLoginService |
|
|
public class SysLoginService |
|
|
{ |
|
|
{ |
|
|
@ -410,4 +409,148 @@ public class SysLoginService |
|
|
// ==================================================================== |
|
|
// ==================================================================== |
|
|
// =================== 多端登录方法结束 =============================== |
|
|
// =================== 多端登录方法结束 =============================== |
|
|
// ==================================================================== |
|
|
// ==================================================================== |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ==================================================================== |
|
|
|
|
|
// =================== 以下是牧户PC端注册方法 ======================== |
|
|
|
|
|
// ==================================================================== |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 牧户PC端注册 |
|
|
|
|
|
* |
|
|
|
|
|
* @param phone 手机号 |
|
|
|
|
|
* @param password 密码 |
|
|
|
|
|
* @param clientType 客户端类型 |
|
|
|
|
|
* @return token |
|
|
|
|
|
*/ |
|
|
|
|
|
@Transactional |
|
|
|
|
|
public String vetRegister(String phone, String password, String clientType) { |
|
|
|
|
|
try { |
|
|
|
|
|
// 1. 验证手机号格式 |
|
|
|
|
|
if (!isValidPhone(phone)) { |
|
|
|
|
|
throw new ServiceException("手机号格式不正确"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 2. 验证密码强度 |
|
|
|
|
|
validatePassword(password); |
|
|
|
|
|
|
|
|
|
|
|
// 3. 检查手机号是否已注册 |
|
|
|
|
|
checkPhoneRegistered(phone); |
|
|
|
|
|
|
|
|
|
|
|
// 4. 创建牧户用户 |
|
|
|
|
|
SysUser user = createVetUser(phone, password, clientType); |
|
|
|
|
|
|
|
|
|
|
|
// 5. 创建登录用户 |
|
|
|
|
|
LoginUser loginUser = new LoginUser( |
|
|
|
|
|
user.getUserId(), |
|
|
|
|
|
user.getDeptId(), |
|
|
|
|
|
user, |
|
|
|
|
|
permissionService.getMenuPermission(user), |
|
|
|
|
|
clientType |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
// 6. 记录登录信息 |
|
|
|
|
|
recordLoginInfo(loginUser.getUserId()); |
|
|
|
|
|
|
|
|
|
|
|
// 7. 生成token |
|
|
|
|
|
return tokenService.createToken(loginUser); |
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
throw new ServiceException("注册失败: " + e.getMessage()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 验证手机号格式 |
|
|
|
|
|
*/ |
|
|
|
|
|
private boolean isValidPhone(String phone) { |
|
|
|
|
|
String regex = "^1[3-9]\\d{9}$"; |
|
|
|
|
|
return phone != null && phone.matches(regex); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 验证密码强度 |
|
|
|
|
|
*/ |
|
|
|
|
|
private void validatePassword(String password) { |
|
|
|
|
|
if (password == null || password.length() < 6 || password.length() > 20) { |
|
|
|
|
|
throw new ServiceException("密码长度需6-20位"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isBlank(password)) { |
|
|
|
|
|
throw new ServiceException("密码不能为空"); |
|
|
|
|
|
} |
|
|
|
|
|
if (!password.matches(Constants.PWD_REGEX)) { |
|
|
|
|
|
throw new ServiceException("长度在 8 到 20 个字符且包含大小写字母、数字以及特殊符号"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 检查手机号是否已注册 |
|
|
|
|
|
*/ |
|
|
|
|
|
private void checkPhoneRegistered(String phone) { |
|
|
|
|
|
// 检查 sys_user 表 |
|
|
|
|
|
SysUser existingUser = userService.selectUserByPhone(phone); |
|
|
|
|
|
if (existingUser != null) { |
|
|
|
|
|
// 检查是否是牧户 |
|
|
|
|
|
if ("02".equals(existingUser.getUserType())) { |
|
|
|
|
|
throw new ServiceException("该手机号已注册为兽医用户"); |
|
|
|
|
|
} else { |
|
|
|
|
|
throw new ServiceException("该手机号已注册为其他类型用户"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 可选:检查 user_auth 表 |
|
|
|
|
|
SysUser authUser = userAuthService.findUserByAuth("phone", phone); |
|
|
|
|
|
if (authUser != null) { |
|
|
|
|
|
throw new ServiceException("该手机号已注册"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 创建牧户用户 |
|
|
|
|
|
*/ |
|
|
|
|
|
private SysUser createVetUser(String phone, String password, String clientType) { |
|
|
|
|
|
SysUser user = new SysUser(); |
|
|
|
|
|
|
|
|
|
|
|
// 生成用户名:muhu_ + 手机后4位 + 4位随机数 |
|
|
|
|
|
String phoneSuffix = phone.length() > 4 ? phone.substring(phone.length() - 4) : phone; |
|
|
|
|
|
String randomSuffix = String.format("%04d", (int)(Math.random() * 10000)); |
|
|
|
|
|
String username = "vet_" + phoneSuffix + "_" + randomSuffix; |
|
|
|
|
|
user.setUserName(username); |
|
|
|
|
|
|
|
|
|
|
|
// 设置昵称:牧户 + 手机后4位 |
|
|
|
|
|
user.setNickName("用户" + phoneSuffix); |
|
|
|
|
|
|
|
|
|
|
|
user.setUserType("01"); // 牧户 |
|
|
|
|
|
user.setEmail(""); |
|
|
|
|
|
user.setPhonenumber(phone); |
|
|
|
|
|
user.setSex("0"); // 未知性别 |
|
|
|
|
|
user.setAvatar(""); // 默认头像 |
|
|
|
|
|
|
|
|
|
|
|
// 加密密码 |
|
|
|
|
|
user.setPassword(com.chenhai.common.utils.SecurityUtils.encryptPassword(password)); |
|
|
|
|
|
|
|
|
|
|
|
user.setStatus("0"); // 正常状态 |
|
|
|
|
|
user.setDelFlag("0"); |
|
|
|
|
|
user.setCreateTime(new Date()); |
|
|
|
|
|
|
|
|
|
|
|
// 这里在最初PC端注册,因为兽医用户涉及审核,因此先加入角色“兽医未审核角色” |
|
|
|
|
|
Long[] roleIds = {6L}; // 兽医未审核角色ID |
|
|
|
|
|
user.setRoleIds(roleIds); |
|
|
|
|
|
|
|
|
|
|
|
// 插入用户 |
|
|
|
|
|
userService.insertUser(user); |
|
|
|
|
|
|
|
|
|
|
|
// 重新查询获取完整用户信息 |
|
|
|
|
|
SysUser savedUser = userService.selectUserByUserName(username); |
|
|
|
|
|
|
|
|
|
|
|
// 可选:创建手机号认证记录 |
|
|
|
|
|
try { |
|
|
|
|
|
userAuthService.bindAuth(savedUser.getUserId(), "phone", phone, null); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
log.warn("创建用户认证记录失败: userId={}, phone={}", savedUser.getUserId(), phone); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return savedUser; |
|
|
|
|
|
} |
|
|
} |
|
|
} |