package com.ruoyi.auth.service; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ruoyi.common.core.constant.SecurityConstants; import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.enums.UserStatus; import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.security.utils.SecurityUtils; import com.ruoyi.system.api.RemoteAccountUserService; import com.ruoyi.system.api.domain.AccountMember; /** * 服务商端小程序登录编排服务 * * 服务商并非独立用户体系,而是「会员(account_user)的另一重身份」: * 登录账号/密码直接复用 account_user.username / password(BCrypt), * 并以 account_user.provider_flag = '1' 判定服务商身份。 * * @author hugray */ @Service public class PartnerLoginService { private static final Logger log = LoggerFactory.getLogger(PartnerLoginService.class); /** 服务商标识:account_user.provider_flag = '1' */ private static final String PROVIDER_FLAG_YES = "1"; @Autowired private RemoteAccountUserService remoteAccountUserService; @Autowired private AccountTokenIssuer accountTokenIssuer; /** * 服务商端账号密码登录 * * @param username 登录账号 * @param password 登录密码(明文) * @return 令牌信息(access_token、expires_in) */ public Map partnerLogin(String username, String password) { // STEP 1: 按账号查询会员;不存在与密码错误统一提示,防账号枚举 AccountMember member = getMemberByUsername(username); if (StringUtils.isNull(member)) { throw new ServiceException("账号或密码错误"); } // STEP 2: 强校验服务商身份(非服务商账号禁止登录服务商端) if (!PROVIDER_FLAG_YES.equals(member.getProviderFlag())) { throw new ServiceException("该账号不是服务商账号"); } // STEP 3: 校验账号状态(停用账号拒绝登录) if (UserStatus.DISABLE.getCode().equals(member.getStatus())) { throw new ServiceException("账号已停用,请联系管理员"); } // STEP 4: BCrypt 校验密码;账号无密码或密码不匹配同样按「账号或密码错误」处理 if (StringUtils.isEmpty(member.getPassword()) || !SecurityUtils.matchesPassword(password, member.getPassword())) { throw new ServiceException("账号或密码错误"); } // STEP 5: 组装登录态并签发令牌 return accountTokenIssuer.issue(member, "partner account-login"); } /** * 按用户名查询会员,调用失败时抛出业务异常 * * @param username 登录账号 * @return 会员信息(不存在时返回 null) */ private AccountMember getMemberByUsername(String username) { R memberResult = remoteAccountUserService.getMemberByUsername(username, SecurityConstants.INNER); if (R.FAIL == memberResult.getCode()) { log.error("[PARTNER_LOGIN] query member failed, username={}, msg={}", username, memberResult.getMsg()); throw new ServiceException("登录失败,请稍后重试"); } return memberResult.getData(); } }