yyy
2026-05-06 b59bca62353f93424818a4dd39068b10948c5267
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.xiaopeng.operation.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaopeng.api.model.account.XpOperationsUser;
import com.xiaopeng.api.model.operation.XpOfficialPartner;
import com.xiaopeng.api.vo.account.LoginOfficialPartnerUser;
import com.xiaopeng.api.vo.account.LoginOperationUser;
import com.xiaopeng.common.core.constant.CacheConstants;
import com.xiaopeng.common.core.exception.ServiceException;
import com.xiaopeng.common.core.utils.StringUtils;
import com.xiaopeng.common.redis.service.RedisService;
import com.xiaopeng.common.security.utils.SecurityUtils;
import com.xiaopeng.operation.mapper.XpOfficialPartnerMapper;
import com.xiaopeng.operation.service.IXpOfficialPartnerService;
import com.xiaopeng.operation.vo.OfficialPartnerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletRequest;
import java.util.Set;
import java.util.concurrent.TimeUnit;
 
@Service
public class XpOfficialPartnerServiceImpl extends ServiceImpl<XpOfficialPartnerMapper, XpOfficialPartner> implements IXpOfficialPartnerService {
 
    @Autowired
    private RedisService redisService;
 
    private int maxRetryCount = CacheConstants.PASSWORD_MAX_RETRY_COUNT;
 
    private Long lockTime = CacheConstants.PASSWORD_LOCK_TIME;
 
 
    /**
     * 添加合作商
     * @param vo
     */
    @Override
    public void addOfficialPartner(OfficialPartnerVo vo) {
        XpOfficialPartner officialPartner = this.getOne(new QueryWrapper<XpOfficialPartner>().eq("code", vo.getCode()).eq("is_deleted", 0));
        if(null != officialPartner){
            throw new ServiceException("合作商已存在");
        }
        officialPartner = new XpOfficialPartner();
        officialPartner.setCode(vo.getCode());
        officialPartner.setName(vo.getName());
        officialPartner.setBrandName(vo.getBrandName());
        officialPartner.setLogoUrl(vo.getLogoUrl());
        officialPartner.setUserName(vo.getUserName());
        officialPartner.setPassword(SecurityUtils.encryptPassword(vo.getPassword()));
        officialPartner.setCreateOrderUrl(vo.getCreateOrderUrl());
        officialPartner.setCancelOrderUrl(vo.getCancelOrderUrl());
        officialPartner.setCloseOrderUrl(vo.getCloseOrderUrl());
        officialPartner.setChangeDestinationUrl(vo.getChangeDestinationUrl());
        this.save(officialPartner);
    }
 
 
    /**
     * 合作商密码登录
     * @param userName
     * @param password
     * @param request
     * @return
     */
    public LoginOfficialPartnerUser officialPartnerPasswordLogin(String userName, String password, HttpServletRequest request) {
        LoginOfficialPartnerUser loginOfficialPartnerUser = new LoginOfficialPartnerUser();
        userName = userName.trim();
        // 查询运维用户信息
        XpOfficialPartner officialPartner = this.lambdaQuery().eq(
                XpOfficialPartner::getUserName, userName
        ).eq(XpOfficialPartner::getDeleted, 0).last("limit 1").one();
        if (StringUtils.isNull(officialPartner)) {
            throw new ServiceException("账号或密码错误!");
        }
        if (1 == officialPartner.getStatus()){
            throw new ServiceException("账号已禁用!");
        }
        // 用户名或密码为空 错误
        if (StringUtils.isAnyBlank(userName, password)) {
            throw new ServiceException("账号密码必须填写");
        }
        validateOperationsUser(officialPartner, password, request);
        loginOfficialPartnerUser.setXpOfficialPartner(officialPartner);
        return loginOfficialPartnerUser;
    }
 
 
    public void validateOperationsUser(XpOfficialPartner officialPartner, String password, HttpServletRequest request) {
        String phoneNumber = officialPartner.getUserName();
 
        Integer retryCount = redisService.getCacheObject(getCacheKey(phoneNumber));
 
        if (retryCount == null)
        {
            retryCount = 0;
        }
 
        if (retryCount >= Integer.valueOf(maxRetryCount).intValue())
        {
            String errMsg = String.format("密码输入错误%s次,帐户锁定%s分钟", maxRetryCount, lockTime);
            throw new ServiceException(errMsg);
        }
 
        if (!matchesOperationsUser(officialPartner, password))
        {
            retryCount = retryCount + 1;
            redisService.setCacheObject(getCacheKey(phoneNumber), retryCount, lockTime, TimeUnit.MINUTES);
            throw new ServiceException("用户名/密码错误");
        }
        else
        {
            clearLoginRecordCache(phoneNumber);
        }
    }
 
    private String getCacheKey(String username)
    {
        return CacheConstants.PWD_ERR_CNT_KEY + username;
    }
 
    public boolean matchesOperationsUser(XpOfficialPartner user, String rawPassword)
    {
        return SecurityUtils.matchesPassword(rawPassword, user.getPassword());
    }
 
    public void clearLoginRecordCache(String loginName)
    {
        if (redisService.hasKey(getCacheKey(loginName)))
        {
            redisService.deleteObject(getCacheKey(loginName));
        }
    }
}