APP
YYC
2026-02-12 8ac3affc79126b0b8ec6f4f7e80b73a6b4a4634b
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.fzzy.igds.app.v1.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.fzzy.common.constant.RespCodeEnum;
import com.fzzy.igds.app.v1.constant.PhoneConstant;
import com.fzzy.igds.app.v1.data.AuthUser;
import com.fzzy.igds.app.v1.data.ParamUser;
import com.fzzy.igds.app.v1.data.PhoneRequest;
import com.fzzy.igds.app.v1.data.PhoneResponse;
import com.fzzy.igds.app.v1.dto.PUserDto;
import com.fzzy.igds.app.v1.service.PhoneService;
 
import com.fzzy.igds.app.v1.util.PhoneRespUtil;
import com.fzzy.igds.app.v1.util.PhoneUtil;
import com.fzzy.igds.service.SysDeptService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.exception.user.*;
import com.ruoyi.common.utils.IpUtils;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.UUID;
import java.util.concurrent.TimeUnit;
 
 
/**
 * 登录接口实现
 *
 * @author chen
 */
@Slf4j
@Service
public class ServiceImpl5001 implements PhoneService {
 
    //RSA-私钥
    private static final String PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJo3SjBOwXnr97QrRjKcGsJnm8xuWw3bEGsqJseNTEwgYOm2+7L6wMVyIQjJfIG3UO27hubhf4r4iadfaocyCw1dNA82tRNlgGr/B9c68uSLcvFIBlKsd2z29FPC3yA+TKRpyoXC5iUS15PVzPji+d3qR8FgR2LoDmXVcah2UWhzAgMBAAECgYEAkyR8CfmrCyzFrMe5c+KUebcn5XfKMz84vDd8xtwf12bxcS4hnFU7B2ls8ZhlztLdSeeqwLKZ4jxgAy28VADAn2WKHI6z5PCezZpkcS4JbhqU4XhSeXw5UwNeBX0nnXzwO1kRgyMfMyNsUx0pDV6w42ZybufFeG3uEGt76J45j0ECQQDHhqq7LNg9nm6G0XnLERB2wz7YVfdXZKRVlwN38H/98wOIGVDhtgz/t4QGZgBBPEUbxXq1vcXhynRr7LP/W6ThAkEAxd2HbOLcWYG+h/c82/9N8BDZzZWVPzEfhq4MHGuNAvQ5R0SssfunQCQAhoAPnL++8Mz4FOKTExG+1DYAmDXj0wJAX91UKpphSznT4PLEkcyhHG6ywz6KmHLCgl0MTZQ/6sPX7ow7UDcoB7Ukue5IMQ/EJJO9YZTMfq/HQBN6aDagAQJBAMF0UpA3TlGE9RBivnIqL71j/E9wzLc4BCbdzKGM1hXpSodPsbli3qdri8f1NU6AeuNcw8FHtZyTYQyCsBohtlkCQB0Lv67uM0Wz628bfxQ1kCREoMMIuIEZPo6nRfTF4ASjnvERQehBhT2ZBWyGaDPKgG1YElOHAdI9jQvs22DtQ+4=";
    @Autowired
    private SysPasswordService passwordService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private SysDeptService deptService;
    @Autowired
    private RedisCache redisUtil;
    @Override
    public String getInterfaceId() {
        return PhoneConstant.API_PHONE_5001;
    }
 
    @SuppressWarnings("unchecked")
    @Override
    public PhoneResponse<Object> execute(PhoneRequest<JSONObject> req, AuthUser auth)
            throws Exception {
        ParamUser param = JSONObject.parseObject(req.getData().toString(), ParamUser.class);
 
        //验证参数
        if (null == param) {
            return PhoneRespUtil.error(RespCodeEnum.CODE_1007, "用户名和密码错误,请重新登录!");
        }
        if (StringUtils.isEmpty(param.getUsername())) {
            return PhoneRespUtil.error(RespCodeEnum.CODE_1007, "用户名和密码错误,请重新登录!");
        }
        if (StringUtils.isEmpty(param.getPassword())) {
            return PhoneRespUtil.error(RespCodeEnum.CODE_1007, "用户名和密码错误,请重新登录!");
        }
        // 用户名或密码为空 错误
        if (StringUtils.isEmpty(param.getUsername()) || StringUtils.isEmpty(param.getPassword())) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(param.getUsername(), Constants.LOGIN_FAIL, MessageUtils.message("not.null")));
            throw new UserNotExistsException();
        }
        // 密码如果不在指定范围内 错误
        if (param.getPassword().length() < UserConstants.PASSWORD_MIN_LENGTH
                || param.getPassword().length() > UserConstants.PASSWORD_MAX_LENGTH) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(param.getUsername(), Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
            throw new UserPasswordNotMatchException();
        }
        // 用户名不在指定范围内 错误
        if (param.getUsername().length() < UserConstants.USERNAME_MIN_LENGTH
                || param.getUsername().length() > UserConstants.USERNAME_MAX_LENGTH) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(param.getUsername(), Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
            throw new UserPasswordNotMatchException();
        }
 
        // 查询用户信息
        SysUser user = userService.selectUserByLoginName(param.getUsername());
 
        // IP黑名单校验
        String blackStr = configService.selectConfigByKey("sys.login.blackIPList", user.getCompanyId());
        if (IpUtils.isMatchedIp(blackStr, ShiroUtils.getIp())) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(param.getUsername(), Constants.LOGIN_FAIL, MessageUtils.message("login.blocked")));
            throw new BlackListException();
        }
        if (user == null) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(param.getUsername(), Constants.LOGIN_FAIL, MessageUtils.message("user.not.exists")));
            throw new UserNotExistsException();
        }
 
        if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(param.getUsername(), Constants.LOGIN_FAIL, MessageUtils.message("user.password.delete")));
            throw new UserDeleteException();
        }
 
        if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(param.getUsername(), Constants.LOGIN_FAIL, MessageUtils.message("user.blocked")));
            throw new UserBlockedException();
        }
 
        passwordService.validate(user, param.getPassword());
 
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(param.getUsername(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
        PUserDto phoneUserDto = new PUserDto();
        //设置用户信息
        phoneUserDto.setCname(user.getUserName());
        phoneUserDto.setMobile(user.getPhonenumber());
        phoneUserDto.setUsername(user.getLoginName());
        phoneUserDto.setTokenAuth(UUID.randomUUID().toString().replaceAll("-", ""));
 
        //设置AuthUser信息到缓存
        AuthUser authUser = new AuthUser();
        authUser.setCompanyId(user.getCompanyId());
        authUser.setUsername(user.getLoginName());
        authUser.setCname(user.getUserName());
        authUser.setEmail(user.getEmail());
        authUser.setMobile(user.getPhonenumber());
        //设置登录用户所属分库信息
        SysDept dept = deptService.getCacheDept(user.getCompanyId(),user.getDeptId().toString());
 
        phoneUserDto.setCompanyId(authUser.getCompanyId());
        phoneUserDto.setDeptId(dept.getDeptId().toString());
        phoneUserDto.setDeptName(dept.getDeptName());
        phoneUserDto.setType(dept.getType());
 
        redisUtil.setCacheObject(PhoneUtil.createKey(phoneUserDto.getTokenAuth()),
                authUser, 60 * 60 * 24, TimeUnit.SECONDS);
        return PhoneRespUtil.success(phoneUserDto, req);
    }
}