PC
2024-03-31 8c9ba6667b89cc0494d05b5da4355dde205b8d4a
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
package com.qianwen.smartman.modules.auth.service;
 
import java.util.Objects;
 
import org.springframework.stereotype.Service;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qianwen.core.log.exception.ServiceException;
import com.qianwen.core.tool.api.R;
import com.qianwen.core.tool.support.Kv;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.smartman.common.utils.MessageUtils;
import com.qianwen.smartman.modules.auth.utils.TokenUtil;
import com.qianwen.smartman.modules.cps.entity.Employee;
import com.qianwen.smartman.modules.cps.service.IEmployeeService;
import com.qianwen.smartman.modules.sync.constant.DingConstant;
import com.qianwen.smartman.modules.sync.constant.QyWechatConstant;
import com.qianwen.smartman.modules.sync.entity.QyUser;
import com.qianwen.smartman.modules.sync.enums.SseEventEnum;
import com.qianwen.smartman.modules.sync.message.sse.SseEmitterServer;
import com.qianwen.smartman.modules.sync.service.impl.WechatSyncServiceImpl;
import com.qianwen.smartman.modules.sync.util.CodeUtil;
import com.qianwen.smartman.modules.system.entity.UserInfo;
import com.qianwen.smartman.modules.system.entity.UserOauth;
import com.qianwen.smartman.modules.system.service.IUserOauthService;
import com.qianwen.smartman.modules.system.service.IUserService;
 
import cn.hutool.core.util.StrUtil;
 
@Service
public class OuterAppScanService {
    private final IUserOauthService userOauthService;
    private final IEmployeeService employeeService;
    private final IUserService userServices;
    private final WechatSyncServiceImpl wechatSyncService;
    private final SseEmitterServer sseEmitterServer;
 
 
    public OuterAppScanService(IUserOauthService userOauthService, IEmployeeService employeeService, IUserService userServices, WechatSyncServiceImpl wechatSyncService, SseEmitterServer sseEmitterServer) {
        this.userOauthService = userOauthService;
        this.employeeService = employeeService;
        this.userServices = userServices;
        this.wechatSyncService = wechatSyncService;
        this.sseEmitterServer = sseEmitterServer;
    }
 
    public R<Kv> loginByOuterUserId(String userId) {
        ;
        UserOauth userOauth = this.userOauthService.getOne(Wrappers.<UserOauth>lambdaQuery().eq(UserOauth::getUuid, userId).eq(UserOauth::getSource, DingConstant.DING_AUTH_KEY));
        if (Func.isEmpty(userOauth))
          throw new ServiceException(MessageUtils.message("sys.outer.user.not.exist", new Object[0])); 
        if (Func.isEmpty(userOauth.getUserId()))
          throw new ServiceException(MessageUtils.message("sys.outer.user.not.bind.employee", new Object[0])); 
        
        Employee employee = this.employeeService.getOne(Wrappers.<Employee>lambdaQuery().eq(Employee::getId, userOauth.getUserId()));
        if (Func.isEmpty(employee)) {
          throw new ServiceException(MessageUtils.message("sys.outer.user.not.bind.employee", new Object[0])); 
        }
        if (Func.isEmpty(employee.getUserId())) {
          throw new ServiceException(MessageUtils.message("sys.outer.user.not.bind.account", new Object[0])); 
        }
        UserInfo userInfo = this.userServices.userInfo(Long.valueOf(employee.getUserId()));
        if (Func.isEmpty(userInfo)) {
          throw new ServiceException(MessageUtils.message("sys.outer.user.not.bind.account", new Object[0])); 
        }
        return R.data(TokenUtil.createAuthInfo(userInfo));
        /*
        UserOauth userOauth = (UserOauth) this.userOauthService.getOne((Wrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getUuid();
        }, userId)).eq((v0) -> {
            return v0.getSource();
        }, DingConstant.DING_AUTH_KEY));
        if (Func.isEmpty(userOauth)) {
            throw new ServiceException(MessageUtils.message("sys.outer.user.not.exist", new Object[0]));
        }
        if (Func.isEmpty(userOauth.getUserId())) {
            throw new ServiceException(MessageUtils.message("sys.outer.user.not.bind.employee", new Object[0]));
        }
        Employee employee = (Employee) this.employeeService.getOne((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getId();
        }, userOauth.getUserId()));
        if (Func.isEmpty(employee)) {
            throw new ServiceException(MessageUtils.message("sys.outer.user.not.bind.employee", new Object[0]));
        }
        if (Func.isEmpty(employee.getUserId())) {
            throw new ServiceException(MessageUtils.message("sys.outer.user.not.bind.account", new Object[0]));
        }
        UserInfo userInfo = this.userServices.userInfo(Long.valueOf(employee.getUserId()));
        if (Func.isEmpty(userInfo)) {
            throw new ServiceException(MessageUtils.message("sys.outer.user.not.bind.account", new Object[0]));
        }
        return R.data(TokenUtil.createAuthInfo(userInfo));
        */
    }
 
    public R<Kv> qyScanLogin(String clientId, String code) {
        
        if (CodeUtil.verifyLoginCodeExpire(clientId).booleanValue()) {
            String codeFailMessage = MessageUtils.message("sync.qy.login.code.failure", new Object[0]);
            this.sseEmitterServer.sendMsg(clientId, codeFailMessage, SseEventEnum.ERROR_MSG_ID.getEventId());
            throw new ServiceException(codeFailMessage);
        }
        QyUser scanUser = this.wechatSyncService.getScanUser(code, clientId);
        String appUserMsg = MessageUtils.message("sys.outer.user.not.exist", new Object[0]);
        if (Objects.isNull(scanUser)) {
            this.sseEmitterServer.sendMsg(clientId, appUserMsg, SseEventEnum.ERROR_MSG_ID.getEventId());
            throw new ServiceException(appUserMsg);
        }
        UserOauth userOauth = this.userOauthService.getOne(Wrappers.<UserOauth>lambdaQuery().eq(UserOauth::getUuid, scanUser.getUserid()).eq(UserOauth::getSource,QyWechatConstant.QY_SOURCE));
        
        if (Func.isEmpty(userOauth)) {
            this.sseEmitterServer.sendMsg(clientId, appUserMsg, SseEventEnum.ERROR_MSG_ID.getEventId());
            throw new ServiceException(appUserMsg);
        }
        Employee employee = this.wechatSyncService.createOrModifyEmployee(scanUser, userOauth);
        String userBingMsg = MessageUtils.message("sys.outer.user.not.bind.account", new Object[0]);
        if (StrUtil.isEmpty(employee.getUserId())) {
            this.sseEmitterServer.sendMsg(clientId, userBingMsg, SseEventEnum.ERROR_MSG_ID.getEventId());
            throw new ServiceException(userBingMsg);
        }
        UserInfo userInfo = this.userServices.userInfo(Long.valueOf(employee.getUserId()));
        if (Func.isEmpty(userInfo)) {
            this.sseEmitterServer.sendMsg(clientId, userBingMsg, SseEventEnum.ERROR_MSG_ID.getEventId());
            throw new ServiceException(userBingMsg);
        }
        Kv authInfo = TokenUtil.createAuthInfo(userInfo);
        this.sseEmitterServer.sendMsg(clientId, authInfo, SseEventEnum.LOGIN_SUCCESS.getEventId());
        String cacheKey = CodeUtil.getLoginCodeCacheKey(clientId);
        CodeUtil.failureCode(cacheKey);
        return R.data(authInfo);
    }
}