package com.ruoyi.promo.service; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.ruoyi.admin.mapper.PromoAdminMapper; import com.ruoyi.admin.service.TtUserService; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.domain.common.constant.UserType; import com.ruoyi.domain.entity.sys.TtUser; import org.springframework.stereotype.Service; /** * 推广后台登录账号校验。 * 点「获取验证码」时先跑一遍(不弹滑块),发短信时再跑一遍兜底,两处必须是同一份规则。 */ @Service public class PromoAccountService { private final TtUserService userService; private final PromoAdminMapper promoAdminMapper; public PromoAccountService(TtUserService userService, PromoAdminMapper promoAdminMapper) { this.userService = userService; this.promoAdminMapper = promoAdminMapper; } /** * @return null 表示有权登录;否则是给用户看的拒绝原因 */ public String checkLoginPermission(String phoneNumber, String parentInvitationCode) { TtUser user = new LambdaQueryChainWrapper<>(userService.getBaseMapper()) .eq(TtUser::getPhoneNumber, phoneNumber) .eq(TtUser::getDelFlag, "0") .eq(TtUser::getStatus, "0") .one(); boolean hasPromoRole = user != null && (UserType.ANCHOR.getCode().equals(user.getUserType()) || UserType.BUSINESS_USER.getCode().equals(user.getUserType()) || UserType.CARD_MERCHANT.getCode().equals(user.getUserType())); if (hasPromoRole) { return null; } // 超级管理员账号(promo_admin.username / phone_number)也可以是手机号,同样允许短信登录 if (promoAdminMapper.findByAccount(phoneNumber) != null) { return null; } // 手机号从未注册过 + 有效招商邀请码:允许发码,登录时自动注册为主播(01)。 // 手机号已被占用(普通用户02 / 机器人 / 白号 / 已停用已删除的老号)时不能走自动注册: // 自动注册会因手机号已存在而跳过,最后只在建 token 时报「用户不存在」,得在这里拦住。 boolean canAutoRegister = !phoneRegistered(phoneNumber) && StringUtils.isNotEmpty(parentInvitationCode) && findBusinessByInvitationCode(parentInvitationCode) != null; if (canAutoRegister) { return null; } // 统一提示「无权限」:能走到这里的要么是本来就没推广后台权限的号(含已注册的普通用户), // 要么是邀请码没填/填错。不区分「已注册」,避免把某个手机号的注册状态暴露出去。 return "该手机号无推广后台登录权限"; } /** * 手机号在 tt_user 中是否已存在(含停用/软删除)。 * 必须与 PromoLoginServiceImpl.autoRegisterAnchorIfNeeded 的存在性口径一致, * 否则两处判断不一致时会出现「发得出验证码但登录报用户不存在」。 */ private boolean phoneRegistered(String phoneNumber) { return new LambdaQueryChainWrapper<>(userService.getBaseMapper()) .eq(TtUser::getPhoneNumber, phoneNumber) .count() > 0; } /** * 按邀请码找招商账号(必须是 04 招商) */ private TtUser findBusinessByInvitationCode(String invitationCode) { TtUser parent = new LambdaQueryChainWrapper<>(userService.getBaseMapper()) .eq(TtUser::getInvitationCode, invitationCode.trim().toUpperCase()) .eq(TtUser::getDelFlag, "0") .eq(TtUser::getStatus, "0") .one(); if (parent == null || !UserType.BUSINESS_USER.getCode().equals(parent.getUserType())) { return null; } return parent; } }