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);
|
}
|
}
|