| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.tourguide.common.exception.BusinessException; |
| | | import com.tourguide.modules.system.service.AdminPermissionService; |
| | | import com.tourguide.modules.user.domain.ClientType; |
| | | import com.tourguide.modules.user.domain.UserRole; |
| | | import com.tourguide.modules.user.dto.AuthResponse; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.List; |
| | | import java.util.UUID; |
| | | |
| | | /** 统一编排登录流程,但后台账号与小程序账号始终从不同数据表读取。 */ |
| | | @Service |
| | | public class AuthService { |
| | | public static final int GUIDE_ACCOUNT_DISABLED_CODE = 40301; |
| | | |
| | | private final AdminUserService adminUserService; |
| | | private final AdminPermissionService adminPermissionService; |
| | | private final MiniUserService miniUserService; |
| | | private final GuideUserService guideUserService; |
| | | private final GuideAutoDisableService guideAutoDisableService; |
| | | private final MiniUserMapper miniUserMapper; |
| | | private final PasswordEncoder passwordEncoder; |
| | | private final JwtTokenService jwtTokenService; |
| | | private final TokenSessionService tokenSessionService; |
| | | |
| | | public AuthService(AdminUserService adminUserService, MiniUserService miniUserService, GuideUserService guideUserService, |
| | | public AuthService(AdminUserService adminUserService, AdminPermissionService adminPermissionService, |
| | | MiniUserService miniUserService, |
| | | GuideUserService guideUserService, GuideAutoDisableService guideAutoDisableService, |
| | | MiniUserMapper miniUserMapper, |
| | | PasswordEncoder passwordEncoder, JwtTokenService jwtTokenService, TokenSessionService tokenSessionService) { |
| | | this.adminUserService = adminUserService; |
| | | this.adminPermissionService = adminPermissionService; |
| | | this.miniUserService = miniUserService; |
| | | this.guideUserService = guideUserService; |
| | | this.guideAutoDisableService = guideAutoDisableService; |
| | | this.miniUserMapper = miniUserMapper; |
| | | this.passwordEncoder = passwordEncoder; |
| | | this.jwtTokenService = jwtTokenService; |
| | |
| | | |
| | | public AuthResponse<UserResponse> loginAdmin(LoginRequest request) { |
| | | AdminUser user = adminUserService.findByUsername(request.getUsername()); |
| | | if (user == null || !passwordEncoder.matches(request.getPassword(), user.getPassword())) { |
| | | if (user == null) { |
| | | throw new BusinessException(401, "用户名或密码错误"); |
| | | } |
| | | if (user.getStatus() == null || user.getStatus() != 1) { |
| | | throw new BusinessException(403, "账号已被禁用,请联系管理员"); |
| | | } |
| | | if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) { |
| | | throw new BusinessException(401, "用户名或密码错误"); |
| | | } |
| | | List<String> menuCodes = adminPermissionService.getMenuCodes(user.getRoleCode()); |
| | | String token = issueToken(user.getId(), user.getUsername(), user.getRoleCode(), ClientType.ADMIN); |
| | | return new AuthResponse<UserResponse>(token, jwtTokenService.getExpirationSeconds(), UserResponse.from(user)); |
| | | return new AuthResponse<UserResponse>(token, jwtTokenService.getExpirationSeconds(), |
| | | UserResponse.from(user, menuCodes)); |
| | | } |
| | | |
| | | public AuthResponse<MiniUserResponse> loginMini(LoginRequest request) { |
| | |
| | | |
| | | public AuthResponse<GuideUserResponse> loginGuide(LoginRequest request) { |
| | | GuideUser user = guideUserService.findByUsername(request.getUsername()); |
| | | if (user == null || user.getStatus() == null || user.getStatus() != 1 || !passwordEncoder.matches(request.getPassword(), user.getPassword())) { |
| | | validateGuideLoginStatus(user, "用户名不存在"); |
| | | if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) { |
| | | throw new BusinessException(401, "用户名或密码错误"); |
| | | } |
| | | return issueGuideToken(user); |
| | | guideAutoDisableService.disableGuideIfEligible(user.getId()); |
| | | GuideUser currentUser = guideUserService.findByUsername(request.getUsername()); |
| | | validateGuideLoginStatus(currentUser, "用户名不存在"); |
| | | return issueGuideToken(currentUser); |
| | | } |
| | | |
| | | public AuthResponse<GuideUserResponse> loginGuideByPhone(String phone) { |
| | |
| | | |
| | | private GuideUser findLoginableGuideByPhone(String phone) { |
| | | GuideUser user = guideUserService.findByPhone(phone); |
| | | if (user == null || user.getStatus() == null || user.getStatus() != 1) { |
| | | throw new BusinessException(401, "手机号未注册或账号已停用"); |
| | | validateGuideLoginStatus(user, "手机号未注册"); |
| | | guideAutoDisableService.disableGuideIfEligible(user.getId()); |
| | | GuideUser currentUser = guideUserService.findByPhone(phone); |
| | | validateGuideLoginStatus(currentUser, "手机号未注册"); |
| | | return currentUser; |
| | | } |
| | | |
| | | private void validateGuideLoginStatus(GuideUser user, String notFoundMessage) { |
| | | if (user == null) { |
| | | throw new BusinessException(401, notFoundMessage); |
| | | } |
| | | return user; |
| | | if (Integer.valueOf(GuideUserService.STATUS_DISABLED).equals(user.getStatus())) { |
| | | throw new BusinessException(GUIDE_ACCOUNT_DISABLED_CODE, "账号已被禁用,请联系后台工作人员"); |
| | | } |
| | | if (Integer.valueOf(GuideUserService.STATUS_CANCELLED).equals(user.getStatus())) { |
| | | throw new BusinessException(403, "账号已注销,无法登录"); |
| | | } |
| | | if (!Integer.valueOf(GuideUserService.STATUS_ENABLED).equals(user.getStatus()) |
| | | && !Integer.valueOf(GuideUserService.STATUS_PENDING).equals(user.getStatus())) { |
| | | throw new BusinessException(403, "账号状态异常,请联系后台工作人员"); |
| | | } |
| | | } |
| | | |
| | | private AuthResponse<GuideUserResponse> issueGuideToken(GuideUser user) { |