refactor(property): 优化授权逻辑
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled

This commit is contained in:
2025-08-01 14:45:28 +08:00
parent 389ba82659
commit c366a30488
6 changed files with 177 additions and 118 deletions

View File

@@ -8,6 +8,7 @@ import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*; import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission; import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.excel.core.ExcelResult; import org.dromara.common.excel.core.ExcelResult;
import org.dromara.property.domain.vo.ResidentPersonImportVo; import org.dromara.property.domain.vo.ResidentPersonImportVo;
import org.dromara.property.listener.ResidentPersonImportListener; import org.dromara.property.listener.ResidentPersonImportListener;
@@ -38,6 +39,7 @@ import org.springframework.web.multipart.MultipartFile;
* @author mocheng * @author mocheng
* @since 2025-06-19 * @since 2025-06-19
*/ */
@Slf4j
@Validated @Validated
@RequiredArgsConstructor @RequiredArgsConstructor
@RestController @RestController
@@ -126,17 +128,17 @@ public class ResidentPersonController extends BaseController {
@SaCheckPermission("property:person:import") @SaCheckPermission("property:person:import")
@PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport, Long unitId) throws Exception { public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport, Long unitId) throws Exception {
ExcelResult<ResidentPersonImportVo> result = ExcelUtil.importExcel(file.getInputStream(), ResidentPersonImportVo.class, new ResidentPersonImportListener(updateSupport, unitId)); asyncImportExcel(StpUtil.getTokenValue(), file, updateSupport, unitId);
return R.ok(result.getAnalysis()); return R.ok("文件上传成功,请等待处理!");
} }
@Async @Async
public void asyncImportExcel(String tokenValue, MultipartFile file, Boolean updateSupport, Long unitId) { public void asyncImportExcel(String tokenValue, MultipartFile file, Boolean updateSupport, Long unitId) {
try { try {
StpUtil.setTokenValueToStorage(tokenValue); StpUtil.setTokenValueToStorage(tokenValue);
ExcelResult<ResidentPersonImportVo> result = ExcelUtil.importExcel(file.getInputStream(), ResidentPersonImportVo.class, new ResidentPersonImportListener(updateSupport, unitId)); ExcelUtil.importExcel(file.getInputStream(), ResidentPersonImportVo.class, new ResidentPersonImportListener(updateSupport, unitId));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); log.info("处理导入入驻员工Excel文件时出错");
} }
} }
@@ -156,7 +158,17 @@ public class ResidentPersonController extends BaseController {
*/ */
@PostMapping(value = "/importFace", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @PostMapping(value = "/importFace", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public R<Void> importFace(@RequestPart("file") MultipartFile file, Long unitId) { public R<Void> importFace(@RequestPart("file") MultipartFile file, Long unitId) {
uploadFaceUtil.processFaceZip(file, unitId); asyncImportFace(StpUtil.getTokenValue(), file, unitId);
return R.ok(); return R.ok("文件上传成功,请等待处理!");
}
@Async
public void asyncImportFace(String tokenValue, MultipartFile file, Long unitId) {
try {
StpUtil.setTokenValueToStorage(tokenValue);
uploadFaceUtil.processFaceZip(file, unitId);
} catch (Exception e) {
log.info("处理人脸压缩包时出错");
}
} }
} }

View File

@@ -126,30 +126,8 @@ public class ResidentPersonServiceImpl implements IResidentPersonService {
add.setAuthGroupId(ruVo.getAuthGroupId()); add.setAuthGroupId(ruVo.getAuthGroupId());
add.setAuthBegDate(ruVo.getAuthBegDate()); add.setAuthBegDate(ruVo.getAuthBegDate());
add.setAuthEndDate(ruVo.getAuthEndDate()); add.setAuthEndDate(ruVo.getAuthEndDate());
boolean flag = baseMapper.insert(add) > 0; boolean flag = baseMapper.insert(add) > 0;
Assert.isTrue(flag, "员工入驻失败!"); Assert.isTrue(flag, "员工入驻失败!");
// 存在图片时,才同步授权
if (flag && add.getImg() != null) {
log.info("开始写入授权记录, {}", bo.getUserName());
RemotePersonAuth personAuth = new RemotePersonAuth();
personAuth.setId(add.getId());
personAuth.setName(bo.getUserName());
personAuth.setSex(bo.getGender().intValue());
personAuth.setPhone(bo.getPhone());
personAuth.setEmail(bo.getEmail());
personAuth.setIdCardNumber(bo.getIdCard());
personAuth.setOssId(bo.getImg());
personAuth.setCarNumber(bo.getCarNumber());
// 使用公司权限组
personAuth.setAuthBegDate(ruVo.getAuthBegDate());
personAuth.setAuthEndDate(ruVo.getAuthEndDate());
personAuth.setAuthGroupId(ruVo.getAuthGroupId());
Boolean auth = remoteSisAuth.personAuth(personAuth);
Assert.isTrue(auth, "新增授权记录失败");
}
return flag; return flag;
} }
@@ -180,22 +158,8 @@ public class ResidentPersonServiceImpl implements IResidentPersonService {
if (flag && e8Id != null) { if (flag && e8Id != null) {
log.info("开始修改授权记录, {}", bo.getUserName()); log.info("开始修改授权记录, {}", bo.getUserName());
RemotePersonAuth personAuth = new RemotePersonAuth(); // 先删除,定时任务增加
personAuth.setId(update.getId()); Boolean auth = remoteSisAuth.deletePersonAuth(List.of(update.getId()), List.of(e8Id));
personAuth.setName(update.getUserName());
personAuth.setSex(update.getGender().intValue());
personAuth.setPhone(update.getPhone());
personAuth.setEmail(update.getEmail());
personAuth.setIdCardNumber(update.getIdCard());
personAuth.setOssId(update.getImg());
personAuth.setCarNumber(update.getCarNumber());
personAuth.setE8Id(e8Id);
personAuth.setAuthGroupId(update.getAuthGroupId());
personAuth.setAuthBegDate(update.getAuthBegDate());
personAuth.setAuthEndDate(update.getAuthEndDate());
Boolean auth = remoteSisAuth.updatePersonAuth(personAuth);
Assert.isTrue(auth, "修改授权记录失败!"); Assert.isTrue(auth, "修改授权记录失败!");
} }
@@ -256,7 +220,6 @@ public class ResidentPersonServiceImpl implements IResidentPersonService {
boolean auth = remoteSisAuth.deletePersonAuth(ids, e8Ids); boolean auth = remoteSisAuth.deletePersonAuth(ids, e8Ids);
Assert.isTrue(auth, "删除授权记录失败!"); Assert.isTrue(auth, "删除授权记录失败!");
} }
} }
return baseMapper.deleteByIds(ids) > 0; return baseMapper.deleteByIds(ids) > 0;
} }

View File

@@ -1,5 +1,6 @@
package org.dromara.sis.service.impl; package org.dromara.sis.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import org.apache.dubbo.config.annotation.DubboReference; import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.domain.TreeNode; import org.dromara.common.core.domain.TreeNode;
@@ -351,7 +352,6 @@ public class SisAuthRecordServiceImpl implements ISisAuthRecordService {
Assert.isTrue(flag, "删除授权记录失败"); Assert.isTrue(flag, "删除授权记录失败");
if (flag) { if (flag) {
List<SisPersonLibImgVo> list = new ArrayList<>(); List<SisPersonLibImgVo> list = new ArrayList<>();
ids.forEach(id -> { ids.forEach(id -> {
SisPersonLibImgVo imgVo = sisPersonLibImgService.queryByPersonId(id); SisPersonLibImgVo imgVo = sisPersonLibImgService.queryByPersonId(id);
@@ -360,10 +360,14 @@ public class SisAuthRecordServiceImpl implements ISisAuthRecordService {
} }
}); });
Collection<Long> imgIds = list.stream().map(SisPersonLibImgVo::getId).toList(); Collection<Long> imgIds = list.stream().map(SisPersonLibImgVo::getId).toList();
flag = sisPersonLibImgService.deleteWithValidByIds(imgIds, false); if (CollUtil.isNotEmpty(imgIds)){
Assert.isTrue(flag, "删除人像库图片失败"); flag = sisPersonLibImgService.deleteWithValidByIds(imgIds, false);
Assert.isTrue(flag, "删除人像库图片失败");
}
e8Ids.forEach(e8PlatformApi::deleteCustomer); if (CollUtil.isNotEmpty(e8Ids)){
e8Ids.forEach(e8PlatformApi::deleteCustomer);
}
} }
return flag; return flag;
} }

View File

@@ -61,7 +61,7 @@ public class AuthSyncTask {
/** /**
* 每两分钟执行一次 * 每两分钟执行一次
*/ */
@Scheduled(cron = "0 */5 * * * ?") // @Scheduled(cron = "0 */5 * * * ?")
public void autoAuth() { public void autoAuth() {
AtomicReference<List<RemoteResidentPersonVo>> unAuthPersonRef = new AtomicReference<>(new ArrayList<>()); AtomicReference<List<RemoteResidentPersonVo>> unAuthPersonRef = new AtomicReference<>(new ArrayList<>());
AtomicReference<byte[]> imgByteRef = new AtomicReference<>(new byte[0]); AtomicReference<byte[]> imgByteRef = new AtomicReference<>(new byte[0]);
@@ -76,18 +76,22 @@ public class AuthSyncTask {
try { try {
if (CollUtil.isNotEmpty(unAuthPerson)) { if (CollUtil.isNotEmpty(unAuthPerson)) {
for (RemoteResidentPersonVo person : unAuthPerson) { for (RemoteResidentPersonVo person : unAuthPerson) {
log.info("开始定时授权:{}", person.getId()); log.info("开始定时授权:{}----{}", person.getName(), person.getId());
// 判断是否已存在授权 // 判断是否已存在授权
SisAuthRecordVo authRecord = sisAuthRecordService.queryByGroupIdAndPersonId(person.getAuthGroupId(), person.getId()); SisAuthRecordVo authRecord = sisAuthRecordService.queryByGroupIdAndPersonId(person.getAuthGroupId(), person.getId());
if (ObjectUtil.isEmpty(authRecord)) { if (ObjectUtil.isEmpty(authRecord)) {
log.info("无授权记录:{}", person.getId()); log.info("无授权记录:{}----{}", person.getName(), person.getId());
this.syncAuthRecord(person); this.syncAuthRecord(person);
imgByteRef.set(remoteFileService.downloadToByteArray(Long.parseLong(person.getOssId()))); imgByteRef.set(remoteFileService.downloadToByteArray(Long.parseLong(person.getOssId())));
// 读取人像 // 读取人像
byte[] imgByte = imgByteRef.get(); byte[] imgByte = imgByteRef.get();
if (imgByte == null) continue; if (imgByte == null) {
log.info("下载图片失败:{}-----{}", person.getName(), person.getId());
sisAuthRecordService.deleteByPersonIds(List.of(person.getId()), new ArrayList<>());
continue;
}
// String nowMd5 = this.calculateMD5(imgByte); // String nowMd5 = this.calculateMD5(imgByte);
// SisPersonLibImgVo imgVo = sisPersonLibImgService.queryByImgMd5(nowMd5); // SisPersonLibImgVo imgVo = sisPersonLibImgService.queryByImgMd5(nowMd5);
@@ -104,20 +108,33 @@ public class AuthSyncTask {
// huaweiId = imgVo.getRemoteImgId(); // huaweiId = imgVo.getRemoteImgId();
// } // }
// } // }
// if (huaweiId == null) continue; //
// if (huaweiId == null){
// log.info("华为盒子人像上传失败:{}-----{}", person.getName(), person.getId());
// sisAuthRecordService.deleteByPersonIds(List.of(person.getId()), new ArrayList<>());
// continue;
// }
// //
// // 更新人像信息huaweiBoxId // // 更新人像信息huaweiBoxId
// update = sisPersonLibImgService.updateByPersonId(person.getId(), huaweiId, nowMd5); // update = sisPersonLibImgService.updateByPersonId(person.getId(), huaweiId, nowMd5);
// if (!update) continue; // if (!update) {
// log.info("更新人像信息失败:{}-----{}", person.getName(), person.getId());
// sisAuthRecordService.deleteByPersonIds(List.of(person.getId()), new ArrayList<>());
// continue;
// }
// 同步E8平台 // 同步E8平台
Long e8Id = syncE8Plat(person, imgByte); Long e8Id = syncE8Plat(person, imgByte);
if (e8Id == null) continue; if (e8Id == null) {
log.info("E8平台授权失败{}-----{}", person.getName(), person.getId());
sisAuthRecordService.deleteByPersonIds(List.of(person.getId()), new ArrayList<>());
continue;
}
// 更新入驻员工E8平台id // 更新入驻员工E8平台id
remoteResidentPersonService.updateE8Id(person.getId(), e8Id); remoteResidentPersonService.updateE8Id(person.getId(), e8Id);
}else{ } else {
log.info("已存在授权记录:{}", person.getId()); log.info("已存在授权记录:{}-----{}", person.getName(), person.getId());
} }
} }
} else { } else {
@@ -158,21 +175,27 @@ public class AuthSyncTask {
* @return Long * @return Long
*/ */
private Long syncHuaweiBox(RemoteResidentPersonVo vo, byte[] imgByte) { private Long syncHuaweiBox(RemoteResidentPersonVo vo, byte[] imgByte) {
log.info("开始写入华为平台");
AddHWPersonReq req = new AddHWPersonReq();
req.setIndex(CodePrefixConstants.PERSON_LIB_IMAGE_CODE_PREFIX + IdUtil.getSnowflakeNextIdStr());
req.setName(vo.getName());
req.setGender(vo.getGender() == 1L ? "0" : vo.getGender() == 2L ? "1" : "-1");
req.setCredentialType("0");
req.setCredentialNumber(vo.getIdCard());
ArrayList<String> pictures = new ArrayList<>(); Long pId;
pictures.add(Base64.getEncoder().encodeToString(imgByte)); try {
req.setPictures(pictures); log.info("开始写入华为平台");
AddHWPersonReq req = new AddHWPersonReq();
req.setIndex(CodePrefixConstants.PERSON_LIB_IMAGE_CODE_PREFIX + IdUtil.getSnowflakeNextIdStr());
req.setName(vo.getName());
req.setGender(vo.getGender() == 1L ? "0" : vo.getGender() == 2L ? "1" : "-1");
req.setCredentialType("0");
req.setCredentialNumber(vo.getIdCard());
Long pId = huaWeiBoxApi.addPerson(List.of(req)); ArrayList<String> pictures = new ArrayList<>();
Assert.notNull(pId, "调用华为盒子新增图片失败"); pictures.add(Base64.getEncoder().encodeToString(imgByte));
log.info("写入华为盒子完成pId={}", pId); req.setPictures(pictures);
pId = huaWeiBoxApi.addPerson(List.of(req));
Assert.notNull(pId, "调用华为盒子新增图片失败");
log.info("写入华为盒子完成pId={}", pId);
} catch (Exception e) {
return null;
}
return pId; return pId;
} }
@@ -184,60 +207,81 @@ public class AuthSyncTask {
* @return Long * @return Long
*/ */
private Long syncE8Plat(RemoteResidentPersonVo vo, byte[] imgByte) { private Long syncE8Plat(RemoteResidentPersonVo vo, byte[] imgByte) {
// 初始化步进器
int count = 0;
Long e8Id = null;
log.info("e8平台上传照片"); try {
String e8ImgUrl = e8PlatformApi.uploadFace(imgByte); log.info("e8平台上传照片");
Assert.notNull(e8ImgUrl, "图片上传E8平台失败"); String e8ImgUrl = e8PlatformApi.uploadFace(imgByte);
log.info("e8平台上传照片完成"); Assert.notNull(e8ImgUrl, "图片上传E8平台失败");
log.info("e8平台上传照片完成");
count++; // 图片上传完成步进器+1
log.info("e8同步新建人员"); log.info("e8同步新建人员");
CustomAddReq req = new CustomAddReq(); CustomAddReq req = new CustomAddReq();
req.setName(vo.getName()); req.setName(vo.getName());
req.setGender(vo.getGender() != 1L ? 0 : 1); req.setGender(vo.getGender() != 1L ? 0 : 1);
req.setIdentityType(0); req.setIdentityType(0);
req.setIdentityNo(vo.getIdCard()); req.setIdentityNo(vo.getIdCard());
Long e8Id = e8PlatformApi.addCustomer(req).getId(); e8Id = e8PlatformApi.addCustomer(req).getId();
Assert.notNull(e8Id, "e8同步新建人员失败"); Assert.notNull(e8Id, "e8同步新建人员失败");
log.info("e8同步新建人员完成"); log.info("e8同步新建人员完成");
count++; // 新增人员完成步进器+1
log.info("e8平台开始发行凭证"); log.info("e8平台开始发行凭证");
IssueVoucherReq voucherReq = new IssueVoucherReq(); IssueVoucherReq voucherReq = new IssueVoucherReq();
voucherReq.setVoucherType(70); voucherReq.setVoucherType(70);
voucherReq.setPersonID(e8Id); voucherReq.setPersonID(e8Id);
voucherReq.setTxtData(e8ImgUrl); voucherReq.setTxtData(e8ImgUrl);
voucherReq.setCardType(34); voucherReq.setCardType(34);
Long voucherId = e8PlatformApi.issueVoucher(voucherReq); Long voucherId = e8PlatformApi.issueVoucher(voucherReq);
Assert.notNull(voucherId, "e8平台发行凭证失败"); Assert.notNull(voucherId, "e8平台发行凭证失败");
log.info("e8平台发行凭证成功"); log.info("e8平台发行凭证成功");
count++; // 发行凭证完成步进器+1
// 获取门禁 // 获取门禁
List<SisAuthGroupRefVo> refVos = sisAuthGroupRefService.queryListByGroupId(vo.getAuthGroupId()); List<SisAuthGroupRefVo> refVos = sisAuthGroupRefService.queryListByGroupId(vo.getAuthGroupId());
Collection<Long> deviceIds = refVos.stream().filter(ref -> ref.getDeviceType() == 1).map(SisAuthGroupRefVo::getDeviceId).toList(); Collection<Long> deviceIds = refVos.stream().filter(ref -> ref.getDeviceType() == 1).map(SisAuthGroupRefVo::getDeviceId).toList();
if (CollUtil.isNotEmpty(deviceIds)) { if (CollUtil.isNotEmpty(deviceIds)) {
// 初始化赋值 // 初始化赋值
CustomerAuthAddReq authReq = new CustomerAuthAddReq(); CustomerAuthAddReq authReq = new CustomerAuthAddReq();
authReq.setPersonIds(List.of(e8Id)); authReq.setPersonIds(List.of(e8Id));
authReq.setStartTime(DateUtil.format(vo.getAuthBegDate(), "yyyy-MM-dd HH:mm:ss")); authReq.setStartTime(DateUtil.format(vo.getAuthBegDate(), "yyyy-MM-dd HH:mm:ss"));
authReq.setEndTime(DateUtil.format(vo.getAuthEndDate(), "yyyy-MM-dd HH:mm:ss")); authReq.setEndTime(DateUtil.format(vo.getAuthEndDate(), "yyyy-MM-dd HH:mm:ss"));
List<CustomerAuthAddReq.AuthGroupData> list = new ArrayList<>(); List<CustomerAuthAddReq.AuthGroupData> list = new ArrayList<>();
SisAccessControlVo accessControlVo; SisAccessControlVo accessControlVo;
for (Long deviceId : deviceIds) { for (Long deviceId : deviceIds) {
accessControlVo = sisAccessControlService.queryById(deviceId); accessControlVo = sisAccessControlService.queryById(deviceId);
CustomerAuthAddReq.AuthGroupData authData = new CustomerAuthAddReq.AuthGroupData(); CustomerAuthAddReq.AuthGroupData authData = new CustomerAuthAddReq.AuthGroupData();
authData.setId(Long.parseLong(accessControlVo.getOutDoorCode())); authData.setId(Long.parseLong(accessControlVo.getOutDoorCode()));
authData.setType(0); authData.setType(0);
authData.setGatewayType(1); authData.setGatewayType(1);
list.add(authData); list.add(authData);
}
authReq.setAuthData(list);
log.info("e8平台开始授权");
Boolean flag = e8PlatformApi.addCustomerAuth(authReq);
Assert.isTrue(flag, "E8平台授权失败");
log.info("E8平台授权完成!");
count++; // 授权完成步进器+1
} }
authReq.setAuthData(list); } catch (Exception e) {
return null;
log.info("e8平台开始授权"); }
Boolean flag = e8PlatformApi.addCustomerAuth(authReq);
Assert.isTrue(flag, "E8平台授权失败"); if (count == 4) {
log.info("E8平台授权完成!"); // 授权完成返回e8平台人员Id
return e8Id;
} else if (count >= 2 && count < 4) {
// 人员新建完成,授权失败,删除人员
e8PlatformApi.deleteCustomer(e8Id);
return null;
} else {
return null;
} }
return e8Id;
} }
/** /**

View File

@@ -69,6 +69,7 @@
<groupId>org.dromara</groupId> <groupId>org.dromara</groupId>
<artifactId>ruoyi-api-system</artifactId> <artifactId>ruoyi-api-system</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.dromara</groupId> <groupId>org.dromara</groupId>
<artifactId>sis-api</artifactId> <artifactId>sis-api</artifactId>
@@ -76,6 +77,12 @@
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>property-api</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -0,0 +1,29 @@
package org.dromara.job.snailjob.sis;
import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.aizuda.snailjob.client.job.core.dto.JobArgs;
import com.aizuda.snailjob.client.model.ExecuteResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.property.api.RemoteResidentPersonService;
import org.dromara.property.api.domain.vo.RemoteResidentPersonVo;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author lsm
* @apiNote AuthSyncTask
* @since 2025/8/1
*/
@Component
@JobExecutor(name = "authSyncTask")
public class AuthSyncTask {
@DubboReference
private RemoteResidentPersonService remoteResidentPersonService;
public ExecuteResult jobExecute(JobArgs jobArgs) throws InterruptedException {
List<RemoteResidentPersonVo> unAuthPerson = remoteResidentPersonService.queryUnAuthPerson();
return ExecuteResult.success(unAuthPerson);
}
}