feat(sis): 添加模拟上下文登录功能

This commit is contained in:
zcxlsm 2025-07-27 19:46:18 +08:00
parent 5ad0f652fe
commit 1a900ad3db
2 changed files with 66 additions and 49 deletions

View File

@ -1,5 +1,7 @@
package org.dromara.sis.config.timer;
import cn.dev33.satoken.context.mock.SaTokenContextMockUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
@ -9,12 +11,10 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.constant.CodePrefixConstants;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.property.api.RemoteResidentPersonService;
import org.dromara.property.api.domain.vo.RemoteResidentPersonVo;
import org.dromara.resource.api.RemoteFileService;
import org.dromara.sis.api.domain.RemotePersonAuth;
import org.dromara.sis.domain.bo.SisPersonLibImgBo;
import org.dromara.sis.domain.vo.*;
import org.dromara.sis.sdk.e8.E8PlatformApi;
import org.dromara.sis.sdk.e8.domain.accessControl.req.CustomerAuthAddReq;
@ -26,15 +26,12 @@ import org.dromara.sis.service.ISisAccessControlService;
import org.dromara.sis.service.ISisAuthGroupRefService;
import org.dromara.sis.service.ISisAuthRecordService;
import org.dromara.sis.service.ISisPersonLibImgService;
import org.dromara.sis.service.impl.SisPersonLibImgServiceImpl;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.List;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
/**
* @author lsm
@ -63,47 +60,55 @@ public class AuthTimer {
/**
* 每两分钟执行一次
*/
// @Scheduled(cron = "0 */1 * * * ?")
@Scheduled(cron = "0 */2 * * * ?")
public void autoAuth() {
List<RemoteResidentPersonVo> unAuthPerson = remoteResidentPersonService.queryUnAuthPerson();
// if (CollUtil.isNotEmpty(unAuthPerson)) {
//
// try {
// for (RemoteResidentPersonVo person : unAuthPerson) {
// log.info("开始定时授权:{}", person.getId());
//
// // 判断是否已存在授权
// SisAuthRecordVo authRecord = sisAuthRecordService.queryByGroupIdAndPersonId(person.getAuthGroupId(), person.getId());
// if (ObjectUtil.isEmpty(authRecord)) {
// // 无授权记录时补录
// this.syncAuthRecord(person);
// }
//
// // 读取人像
// byte[] imgByte = remoteFileService.downloadToByteArray(Long.parseLong(person.getOssId()));
// if (imgByte == null) continue;
//
// // 写入华为盒子
// Long huaweiId = syncHuaweiBox(person, imgByte);
// if (huaweiId == null) continue;
//
// // 更新人像信息huaweiBoxId
// Boolean update = sisPersonLibImgService.updateByPersonId(person.getId(), huaweiId);
// if (!update) continue;
//
// // 同步E8平台
// Long e8Id = syncE8Plat(person, imgByte);
// if (e8Id == null) continue;
//
// // 更新入驻员工E8平台id
// remoteResidentPersonService.updateE8Id(person.getId(), e8Id);
//
// }
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
//
// }
AtomicReference<List<RemoteResidentPersonVo>> unAuthPersonRef = new AtomicReference<>(new ArrayList<>());
// 需要先设置模拟上下文
SaTokenContextMockUtil.setMockContext(() -> {
// 模拟登录
StpUtil.login(-8); // 模拟登录
unAuthPersonRef.set(remoteResidentPersonService.queryUnAuthPerson());
});
List<RemoteResidentPersonVo> unAuthPerson = unAuthPersonRef.get();
if (CollUtil.isNotEmpty(unAuthPerson)) {
try {
for (RemoteResidentPersonVo person : unAuthPerson) {
log.info("开始定时授权:{}", person.getId());
// 判断是否已存在授权
SisAuthRecordVo authRecord = sisAuthRecordService.queryByGroupIdAndPersonId(person.getAuthGroupId(), person.getId());
if (ObjectUtil.isEmpty(authRecord)) {
// 无授权记录时补录
this.syncAuthRecord(person);
}
// 读取人像
byte[] imgByte = remoteFileService.downloadToByteArray(Long.parseLong(person.getOssId()));
if (imgByte == null) continue;
// 写入华为盒子
Long huaweiId = syncHuaweiBox(person, imgByte);
if (huaweiId == null) continue;
// 更新人像信息huaweiBoxId
Boolean update = sisPersonLibImgService.updateByPersonId(person.getId(), huaweiId);
if (!update) continue;
// 同步E8平台
Long e8Id = syncE8Plat(person, imgByte);
if (e8Id == null) continue;
// 更新入驻员工E8平台id
remoteResidentPersonService.updateE8Id(person.getId(), e8Id);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
log.info("无待授权人员");
}
}
private void syncAuthRecord(RemoteResidentPersonVo person) {

View File

@ -1,5 +1,7 @@
package org.dromara.sis.runner;
import cn.dev33.satoken.context.mock.SaTokenContextMockUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -20,6 +22,7 @@ import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
@Slf4j
@Component
@ -40,7 +43,7 @@ public class HikDeviceApplicationRunner implements ApplicationRunner {
// 现在查询全部后面要更具需要查询指定厂商的设备
HikDeviceApplicationRunner runner = SpringUtils.getAopProxy(this);
// 梯控登录
// runner.hikElevatorInfoLogin();
runner.hikElevatorInfoLogin();
// 网络摄像头登录
runner.hikNetCameraLogin();
}
@ -52,8 +55,17 @@ public class HikDeviceApplicationRunner implements ApplicationRunner {
sisElevatorInfoVos.forEach(item -> {
// 执行设备登录操作
HikApiService.getInstance().login(item.getControlIp(), item.getControlPort().shortValue(), item.getControlAccount(), item.getControlPwd());
AtomicReference<List<RemoteFloorVo>> floorInfoRef = new AtomicReference<>();
// 模拟上下文
SaTokenContextMockUtil.setMockContext(() -> {
// 模拟登录
StpUtil.login(-8);
floorInfoRef.set(remoteFloorService.queryByUnitId(item.getUnitId()));
});
// 根据单元ID获取楼层信息
List<RemoteFloorVo> floorInfo = remoteFloorService.queryByUnitId(item.getUnitId());
List<RemoteFloorVo> floorInfo = floorInfoRef.get();
// 下发权限
for (int i = 0; i < floorInfo.size(); i++) {