liujie
13 小时以前 fddf3d913d47f391f84f9e460cb76d427ef02c44
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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<String, Object> 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<AccountMember> 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();
    }
}