From 0a2332f9b8eb35a46a8712c689e1e3cdfd4a989b Mon Sep 17 00:00:00 2001 From: haungxing <1203316822@qq.com> Date: Tue, 21 May 2024 19:17:12 +0800 Subject: [PATCH] =?UTF-8?q?add:=E9=81=A5=E6=8E=A7=E9=89=B4=E6=9D=83?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../constants/ProjectRemoteTypeEnum.java | 22 +++++ .../service/impl/AuthenticationService.java | 100 +++++++++++++++++++++ .../service/impl/InteractiveServiceImpl.java | 43 +++------ 3 files changed, 136 insertions(+), 29 deletions(-) create mode 100644 hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/constants/ProjectRemoteTypeEnum.java create mode 100644 hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/AuthenticationService.java diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/constants/ProjectRemoteTypeEnum.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/constants/ProjectRemoteTypeEnum.java new file mode 100644 index 0000000..069572a --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/constants/ProjectRemoteTypeEnum.java @@ -0,0 +1,22 @@ +package com.hnac.hzims.bigmodel.interactive.constants; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * @Author: huangxing + * @Date: 2024/05/21 16:34 + */ +@AllArgsConstructor +public enum ProjectRemoteTypeEnum { + /**不允许发送遥控**/ + NOT_ALLOW(0), + /**运行发送遥控并且需要校验权限**/ + VALID(1), + /**运行发送遥控并免校验权限**/ + ALLOW(2), + ; + @Getter + private Integer ctrlType; + +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/AuthenticationService.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/AuthenticationService.java new file mode 100644 index 0000000..f5b9d5b --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/AuthenticationService.java @@ -0,0 +1,100 @@ +package com.hnac.hzims.bigmodel.interactive.service.impl; + +import com.hnac.hzims.bigmodel.entity.FunctionEntity; +import com.hnac.hzims.bigmodel.function.service.IFunctionService; +import com.hnac.hzims.bigmodel.interactive.constants.ProjectRemoteTypeEnum; +import com.hnac.hzinfo.sdk.core.response.Result; +import com.hnac.hzinfo.sdk.v5.project.ProjectClient; +import com.hnac.hzinfo.sdk.v5.project.vo.ProjectVO; +import lombok.AllArgsConstructor; +import org.springblade.core.log.exception.ServiceException; +import org.springblade.core.tool.api.R; +import org.springblade.core.tool.utils.CollectionUtil; +import org.springblade.core.tool.utils.Func; +import org.springblade.core.tool.utils.StringUtil; +import org.springblade.system.dto.DeptStationDTO; +import org.springblade.system.feign.IDeptClient; +import org.springblade.system.feign.IRemoteClient; +import org.springblade.system.user.feign.IUserClient; +import org.springframework.stereotype.Service; +import org.springframework.util.Assert; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * @Author: huangxing + * @Date: 2024/05/21 16:10 + */ +@Service +@AllArgsConstructor +public class AuthenticationService { + + private final IDeptClient deptClient; + private final IFunctionService functionService; + private final IUserClient userClient; + private final ProjectClient projectClient; + private final IRemoteClient remoteClient; + + /** + * 站点鉴权 + * @param stationId 站点ID + * @param userId 用户ID + */ + public void stationAuthentication(String stationId, String userId) { + List stations = this.getStationPermissionsById(userId).stream().map(DeptStationDTO::getStationId) + .filter(StringUtil::isNotBlank).filter(Func::isNotEmpty).collect(Collectors.toList()); + Assert.isTrue(stations.contains(stationId),() -> { + throw new ServiceException("人员站点鉴权失败!"); + }); + } + + /** + * 菜单鉴权 + * @param stationId 站点ID + * @param userId 用户ID + * @param func 函数编号 + */ + public void menuAuthentication(String stationId, String userId, String func) { + FunctionEntity function = functionService.getFunctionByCode(func); + if(Func.isNotEmpty(function) && Func.isNotEmpty(function.getRoute()) && StringUtil.isNotBlank(function.getRoute())) { + R authenticationR = userClient.permissionMenuById(Long.valueOf(userId), function.getRoute()); + Assert.isTrue(authenticationR.isSuccess() && authenticationR.getData(), () -> { + throw new ServiceException("人员菜单鉴权失败!"); + }); + } + } + + /** + * 遥控鉴权 + * @param stationId 站点ID + * @param userId 用户ID + * @param func 函数编号 + */ + public void remoteAuthentication(String stationId, String userId, String func) { + // 查询数据平台站点是否可被遥控 + Result projectR = projectClient.getProjectByPid(stationId); + Assert.isTrue(projectR.isSuccess() && Func.isNotEmpty(projectR.getData()),() -> { + throw new ServiceException("未查询到站点,鉴权失败!"); + }); + ProjectVO project = projectR.getData(); + if(ProjectRemoteTypeEnum.NOT_ALLOW.getCtrlType().equals(project.getCtrlType())) { + // 不允许发送遥控 + throw new ServiceException("该站点不允许发送遥控指令,校验失败!"); + } + else if(ProjectRemoteTypeEnum.VALID.getCtrlType().equals(project.getCtrlType())) { + // 运行发送遥控并且需要校验权限 + //remoteClient.sendCtrlCheckParam(); + } + + } + + public List getStationPermissionsById(String userId) { + R> deptSattionR = deptClient.getStationPermissionsById(Long.valueOf(userId)); + Assert.isTrue(deptSattionR.isSuccess() && CollectionUtil.isNotEmpty(deptSattionR.getData()),() -> { + throw new ServiceException("获取人员站点权限失败!"); + }); + return deptSattionR.getData(); + } + +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/InteractiveServiceImpl.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/InteractiveServiceImpl.java index f150638..d322d1d 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/InteractiveServiceImpl.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/InteractiveServiceImpl.java @@ -8,6 +8,7 @@ import com.alibaba.fastjson.JSONObject; import com.hnac.hzims.bigmodel.configuration.BigModelInvokeUrl; import com.hnac.hzims.bigmodel.entity.FunctionEntity; import com.hnac.hzims.bigmodel.function.service.IFunctionService; +import com.hnac.hzims.bigmodel.interactive.constants.FuncRouteEnum; import com.hnac.hzims.bigmodel.interactive.req.ModelFunctionReq; import com.hnac.hzims.bigmodel.interactive.service.IInteractiveService; import com.hnac.hzims.bigmodel.interactive.service.IJumpPageService; @@ -57,9 +58,7 @@ public class InteractiveServiceImpl implements IInteractiveService { private final BigModelInvokeUrl bigModelInvokeUrl; - private final IDeptClient deptClient; - - private final IUserClient userClient; + private final AuthenticationService authenticationService; private final RedisTemplate redisTemplate; @@ -98,8 +97,8 @@ public class InteractiveServiceImpl implements IInteractiveService { params.put("id",sessionId); params.put("userid", userId); params.put("query",question); - params.put("stationids",this.getStationPermissionsById(userId).stream().map(DeptStationDTO::getStationId).filter(StringUtil::isNotBlank).filter(Func::isNotEmpty).toArray()); - params.put("projectids",this.getStationPermissionsById(userId).stream().map(DeptStationDTO::getDeptId).filter(Func::isNotEmpty).map(String::valueOf).toArray()); + params.put("stationids",authenticationService.getStationPermissionsById(userId).stream().map(DeptStationDTO::getStationId).filter(StringUtil::isNotBlank).filter(Func::isNotEmpty).toArray()); + params.put("projectids",authenticationService.getStationPermissionsById(userId).stream().map(DeptStationDTO::getDeptId).filter(Func::isNotEmpty).map(String::valueOf).toArray()); HttpResponse response = HttpRequest.post(fdpHost + bigModelInvokeUrl.getAssistantAsk()) .body(JSON.toJSONString(params)).execute(); logger.info("interactive:ask","问答传参为:" + JSON.toJSONString(params) + "结果为:" + response.body()); @@ -132,8 +131,8 @@ public class InteractiveServiceImpl implements IInteractiveService { params.put("id",sessionId); params.put("userid", userId); params.put("extra",extra); - params.put("stationids",this.getStationPermissionsById(userId).stream().map(DeptStationDTO::getStationId).filter(StringUtil::isNotBlank).filter(Func::isNotEmpty).toArray()); - params.put("projectids",this.getStationPermissionsById(userId).stream().map(DeptStationDTO::getDeptId).filter(Func::isNotEmpty).map(String::valueOf).toArray()); + params.put("stationids",authenticationService.getStationPermissionsById(userId).stream().map(DeptStationDTO::getStationId).filter(StringUtil::isNotBlank).filter(Func::isNotEmpty).toArray()); + params.put("projectids",authenticationService.getStationPermissionsById(userId).stream().map(DeptStationDTO::getDeptId).filter(Func::isNotEmpty).map(String::valueOf).toArray()); HttpResponse response = HttpRequest.post(fdpHost + bigModelInvokeUrl.getAssistantSpecialAsk()) .body(JSON.toJSONString(params)).execute(); if(response.getStatus() != HttpServletResponse.SC_OK) { @@ -200,21 +199,15 @@ public class InteractiveServiceImpl implements IInteractiveService { public Boolean authentication(String stationId, String userId, String func) { // 站点鉴权 if(StringUtil.isNotBlank(stationId)) { - List stations = this.getStationPermissionsById(userId).stream().map(DeptStationDTO::getStationId) - .filter(StringUtil::isNotBlank).filter(Func::isNotEmpty).collect(Collectors.toList()); - Assert.isTrue(stations.contains(stationId),() -> { - throw new ServiceException("人员站点鉴权失败!"); - }); + authenticationService.stationAuthentication(stationId,userId); } // 菜单鉴权 if(StringUtil.isNotBlank(func) && StringUtil.isNotBlank(userId)) { - FunctionEntity function = functionService.getFunctionByCode(func); - if(Func.isNotEmpty(function) && Func.isNotEmpty(function.getRoute()) && StringUtil.isNotBlank(function.getRoute())) { - R authenticationR = userClient.permissionMenuById(Long.valueOf(userId), function.getRoute()); - Assert.isTrue(authenticationR.isSuccess() && authenticationR.getData(), () -> { - throw new ServiceException("人员菜单鉴权失败!"); - }); - } + authenticationService.menuAuthentication(stationId,userId,func); + } + // 遥控鉴权 + if(FuncRouteEnum.CONFIRM_YK.getFuncCode().equals(func)) { + authenticationService.remoteAuthentication(stationId,userId,func); } return true; } @@ -256,9 +249,9 @@ public class InteractiveServiceImpl implements IInteractiveService { params.put("userid", userId); params.put("query", question); params.put("knowledge", knowledge); - params.put("stationids", this.getStationPermissionsById(userId).stream().map(DeptStationDTO::getStationId) + params.put("stationids", authenticationService.getStationPermissionsById(userId).stream().map(DeptStationDTO::getStationId) .filter(StringUtil::isNotBlank).filter(Func::isNotEmpty).toArray()); - params.put("projectids", this.getStationPermissionsById(userId).stream().map(DeptStationDTO::getDeptId) + params.put("projectids", authenticationService.getStationPermissionsById(userId).stream().map(DeptStationDTO::getDeptId) .filter(Func::isNotEmpty).map(String::valueOf).toArray()); HttpResponse response = HttpRequest.post(fdpHost + bigModelInvokeUrl.getAssistantKnowledgeAsk()) .body(JSON.toJSONString(params)).execute(); @@ -270,12 +263,4 @@ public class InteractiveServiceImpl implements IInteractiveService { this.addQuestionSessionId(sessionId); return R.success("消息发送成功"); } - - private List getStationPermissionsById(String userId) { - R> deptSattionR = deptClient.getStationPermissionsById(Long.valueOf(userId)); - Assert.isTrue(deptSattionR.isSuccess() && CollectionUtil.isNotEmpty(deptSattionR.getData()),() -> { - throw new ServiceException("获取人员站点权限失败!"); - }); - return deptSattionR.getData(); - } }