package com.ard.work.sdk.aspect; import com.ard.common.core.constant.SecurityConstants; import com.ard.common.core.domain.R; import com.ard.common.core.exception.ServiceException; import com.ard.common.core.utils.StringUtils; import com.ard.common.core.web.domain.AjaxResult; import com.ard.common.security.utils.DictUtils; import com.ard.system.api.RemoteUserService; import com.ard.system.api.domain.SysUser; import com.ard.system.api.model.LoginUser; import com.ard.work.api.domian.ArdCamera; import com.ard.work.api.domian.CameraCmd; import com.ard.work.device.camera.mapper.ArdCameraMapper; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import jakarta.annotation.Resource; import java.lang.reflect.Method; import java.util.Date; /** * SDK控制处理 * * @author ruoyi */ @Aspect @Component @Slf4j public class SdkOperateAspect { @Resource ArdCameraMapper ardCameraMapper; @Resource RemoteUserService remoteUserService; @Pointcut("@annotation(com.ard.work.sdk.annotation.SdkOperate)") public void dsPointCut() { } @Around("dsPointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { MethodSignature signature = (MethodSignature) point.getSignature(); Method method = signature.getMethod(); Boolean result = controlScopeFilter(point); if (result) { String message = "已获取相机控制权--" + method.getName(); log.debug(message); // 执行目标方法 return point.proceed(); } else { String message = "未获取相机控制权--" + method.getName(); Class returnType = method.getReturnType(); if (R.class.isAssignableFrom(returnType)) { // 这是Feign接口,返回R对象 return R.fail(message); } else if (AjaxResult.class.isAssignableFrom(returnType)) { // 这是普通接口,返回AjaxResult return AjaxResult.error(message); } // 【新增】处理 Boolean 返回类型 else if (Boolean.class.isAssignableFrom(returnType) || boolean.class == returnType) { log.error(message); return false; }else if (String.class.isAssignableFrom(returnType)) { // 【新增】处理 String 返回类型 log.error(message); return ""; } else if (byte[].class.isAssignableFrom(returnType)) { // 【新增】处理 byte[] 返回类型 log.error(message); return new byte[0]; } else if (void.class == returnType || Void.class.isAssignableFrom(returnType)) { // 处理 void 返回类型 log.error(message); return null; } else { // 其他未知类型,为了安全还是返回 null,但最好记录错误 log.error("未知返回类型:{}, 无法构造默认返回值,返回 null (可能导致 NPE)", returnType.getName()); return null; } } } /** * 操控判断逻辑 * * @param joinPoint 切点 */ public Boolean controlScopeFilter(ProceedingJoinPoint joinPoint) { // 获取请求控制相机的命令信息 CameraCmd cmd = (CameraCmd) joinPoint.getArgs()[0]; String operator = cmd.getOperator(); // 申请者 ArdCamera ardCamera = ardCameraMapper.selectArdCameraById(cmd.getCameraId()); // 如果相机不存在,返回 false if (StringUtils.isNull(ardCamera)) { return false; } Date currentExpired = ardCamera.getOperatorExpired(); // 当前相机的过期时间 String currentOperator = ardCamera.getOperatorId(); // 当前相机的操作者 // 如果申请者是当前操作者,或者未配置锁定时间或当前相机控制者为空,任何人可控制 if (StringUtils.isNull(currentOperator) || StringUtils.isNull(currentExpired) || currentOperator.equals(operator)) { updateCameraOperator(ardCamera, operator); return true; } // 判断优先级 int currentLevel = getOperatorPriority(currentOperator); int operatorLevel = getOperatorPriority(operator); // 申请者优先级更高,或者当前操作者的控制时间已过期,允许控制 if (operatorLevel > currentLevel || new Date().after(currentExpired)) { updateCameraOperator(ardCamera, operator); return true; } // 时间未过期,且优先级不高,不可控制 return false; } /** * 更新相机操作者信息 */ private void updateCameraOperator(ArdCamera ardCamera, String operator) { ardCamera.setOperatorId(operator); ardCamera.setOperatorName(getOperatorName(operator)); ardCamera.setOperatorExpired(null); ardCameraMapper.updateArdCamera(ardCamera); } /** * 获取操作者的优先级 */ private int getOperatorPriority(String operator) { String camerasPriority = DictUtils.getDictValue("cameras_priority", operator); if (StringUtils.isNotEmpty(camerasPriority)) { // 系统报警用户的优先级 return Integer.parseInt(camerasPriority); } else { // 普通用户的优先级 R userResult = remoteUserService.getUserInfoByUserId(Long.parseLong(operator), SecurityConstants.INNER); SysUser sysUser = userResult.getData(); if (sysUser == null) { return 0; } return sysUser.getCameraPriority() != null ? Integer.parseInt(sysUser.getCameraPriority()) : 0; } } /** * 获取操控者名称 * 用户显示nick_name * 其他显示优先级字典备注 */ private String getOperatorName(String operator) { String camerasPriority = DictUtils.getDictValue("cameras_priority", operator); if (StringUtils.isNotEmpty(camerasPriority)) { // 系统报警用户的优先级 return DictUtils.getDictRemark("cameras_priority", operator); } else { // 普通用户的优先级 R userResult = remoteUserService.getUserInfoByUserId(Long.parseLong(operator), SecurityConstants.INNER); SysUser sysUser = userResult.getData(); if (sysUser == null) { return ""; } return sysUser.getNickName(); } } }