Browse Source

完善请假自动下发海康门禁权限

“gaohp” 6 months ago
parent
commit
1149a8ab41

+ 2 - 0
.gitignore

@@ -43,6 +43,8 @@ tmp_*.log
 *Probe*.class
 *Probe*.class
 Tmp*.java
 Tmp*.java
 Tmp*.class
 Tmp*.class
+/Snipaste_*.png
+/11b7ed3983a07abf85a64075fdf71409.jpg
 docs/*
 docs/*
 !docs/2026-05-26_*_hikvision_leave_auth_*_report.md
 !docs/2026-05-26_*_hikvision_leave_auth_*_report.md
 !docs/2026-05-27_*_hikvision_leave_auth_*_report.md
 !docs/2026-05-27_*_hikvision_leave_auth_*_report.md

+ 31 - 0
docs/2026-05-27_152954_hikvision_leave_auth_template_optional-report.md

@@ -0,0 +1,31 @@
+# 本轮目标
+
+在不影响现有考勤与海康其他链路的前提下,继续完善“班主任请假后自动下发海康门禁权限”的代码,按海康最新联调口径把 `templateId` 从默认必填条件中移除,让请假自动开门可以继续联调和落地。
+
+# 修改内容
+
+- 调整 [`src/biz/kq/HkLeaveAuthServ.java`](/H:/gaohp/feifan_project/PMS_20260508/PMS/src/biz/kq/HkLeaveAuthServ.java):
+  - 2026-05-27 将 `configReady` 从“必须同时具备 `tagId + templateId`”改为“只要求 `tagId`”。
+  - 2026-05-27 保留 `templateId` 探测和显式传参能力,但默认不再把 `templateId` 视为请假自动下发的前置阻塞项。
+  - 2026-05-27 当 `templateId` 为空时,不再向 `special/person/diy` 请求体中写入 `templateConfigInfos`。
+  - 2026-05-27 新增 `downloadMode` 回显字段,区分“纯添加权限配置”与“带模板添加权限配置”。
+  - 2026-05-27 将跳过原因收敛为只缺 `tagId` 时才跳过真实执行。
+- 调整 [`.gitignore`](/H:/gaohp/feifan_project/PMS_20260508/PMS/.gitignore):
+  - 忽略本地联调截图文件,避免提交无关图片。
+
+# 验证结果
+
+- 使用仓库自带依赖做增量编译验证:
+
+```powershell
+javac -encoding UTF-8 -sourcepath src -cp "web/WEB-INF/classes;web/WEB-INF/lib/*" -d tmp_hk_leave_auth_compile src/core/co/haikang/HkProtocolC.java src/core/co/haikang/HkApiClient.java src/biz/kq/HkKqServ.java src/biz/kq/HkLeaveAuthServ.java
+```
+
+- 结果:编译通过。
+- 备注:`HkLeaveAuthServ.java` 仍有既存的 unchecked 警告,但不影响本轮功能编译通过。
+
+# 下一步
+
+- 海康侧若已开启权限自动下发,可直接继续用 `hkkq_leave_auth_execute` 联调真实“添加权限配置”。
+- 若海康侧未开启自动下发,再补“根据出入权限配置快捷下载”接口接入。
+- 如需让平台门禁管理模块可见,再根据现场版本与运维要求评估是否把 `tagId` 调整为 `acs`。

+ 104 - 10
src/biz/kq/HkLeaveAuthServ.java

@@ -1,6 +1,10 @@
 package biz.kq;
 package biz.kq;
 
 
 import base.po.RcPo;
 import base.po.RcPo;
+import com.google.gson.Gson;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
 import core.co.haikang.HkApiClient;
 import core.co.haikang.HkApiClient;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.LogManager;
@@ -28,6 +32,7 @@ public class HkLeaveAuthServ extends Serv {
 
 
     private static final Logger log4j = LogManager.getLogger(HkLeaveAuthServ.class);
     private static final Logger log4j = LogManager.getLogger(HkLeaveAuthServ.class);
     private static final HkApiClient HK_API_CLIENT = new HkApiClient();
     private static final HkApiClient HK_API_CLIENT = new HkApiClient();
+    private static final Gson GSON = new Gson();
 
 
     private static final String CFG_ENABLED = "hk.leaveAuth.enabled";
     private static final String CFG_ENABLED = "hk.leaveAuth.enabled";
     private static final String CFG_EXECUTE = "hk.leaveAuth.execute";
     private static final String CFG_EXECUTE = "hk.leaveAuth.execute";
@@ -89,6 +94,14 @@ public class HkLeaveAuthServ extends Serv {
         return ServC.SERV_notByA;
         return ServC.SERV_notByA;
     }
     }
 
 
+    /**
+     * 2026-05-27:通用海康原始接口探测入口,便于逐个试海康文档候选接口并回收原始响应分析 templateId。
+     */
+    public String probeHkTemplateApi() {
+        super.wrJsonByResp(probeHkTemplateApiResult());
+        return ServC.SERV_notByA;
+    }
+
     /**
     /**
      * 2026-05-26:手工真实下发入口,只在明确传参时执行海康写操作。
      * 2026-05-26:手工真实下发入口,只在明确传参时执行海康写操作。
      */
      */
@@ -262,6 +275,71 @@ public class HkLeaveAuthServ extends Serv {
         }
         }
     }
     }
 
 
+    /**
+     * 2026-05-27:透传海康接口路径和请求体,返回原始请求与响应,作为 templateId 联调诊断口。
+     */
+    private Map<String, Object> probeHkTemplateApiResult() {
+        Map<String, Object> result = new LinkedHashMap<>();
+        String apiPath = rdReqStrParm("apiPath", "path");
+        String reqBodyStr = rdReqStrParm("reqBody", "body", "json");
+        long rcid = rdReqLongParm("rcid");
+        result.put("ok", false);
+        result.put("apiPath", apiPath);
+        result.put("rcid", rcid);
+
+        if (StringUtils.isBlank(apiPath)) {
+            result.put("msg", "缺少 apiPath 参数。");
+            return result;
+        }
+        if (!StringUtils.startsWith(apiPath, "/artemis/api/")) {
+            result.put("msg", "apiPath 必须以 /artemis/api/ 开头。");
+            return result;
+        }
+
+        Map<String, Object> reqBodyMap;
+        try {
+            reqBodyMap = rdReqBodyMap(reqBodyStr);
+        } catch (Exception e) {
+            result.put("msg", "reqBody 不是有效 JSON Object:" + e.getMessage());
+            return result;
+        }
+        result.put("requestBody", reqBodyMap);
+
+        try {
+            Map<String, Object> hkConnParm = makeHkConnParm();
+            if (hkConnParm == null) {
+                result.put("msg", "缺少海康连接配置,请补齐 hk.host / hk.appKey / hk.appSecret。");
+                return result;
+            }
+            result.put("hkConn", makeSafeHkConnResp(hkConnParm));
+
+            String tagId = rdCfgOrOverride(makeReqOverrideParm(), "tagId", CFG_TAG_ID, "HK_LEAVE_AUTH_TAG_ID", HK_DEF_TAG_ID);
+            if (StringUtils.isNotBlank(tagId))
+                hkConnParm.put("tagId", tagId);
+            result.put("tagId", tagId);
+            result.put("tagIdSource", rdCfgSource(makeReqOverrideParm(), "tagId", CFG_TAG_ID, "HK_LEAVE_AUTH_TAG_ID", HK_DEF_TAG_ID));
+
+            if (rcid > 0) {
+                try (Db gdba = Db.makeConn()) {
+                    LeaveAuthSyncCtx ctx = selLeaveAuthSyncCtx(gdba, rcid);
+                    if (ctx != null)
+                        result.put("leaveCtx", makeLeaveCtxResp(ctx));
+                }
+            }
+
+            Map<String, Object> responseMap = HK_API_CLIENT.postProbeApi(hkConnParm, apiPath, reqBodyMap);
+            result.put("ok", true);
+            result.put("response", responseMap);
+            result.put("templateIdCandidates", extTemplateIdList(responseMap));
+            result.put("msg", "海康接口探测已返回,请把 response 和 templateIdCandidates 一并发我分析。");
+            return result;
+        } catch (Exception e) {
+            log4j.error("HkLeaveAuthServ.probeHkTemplateApi() 异常,apiPath={}", apiPath, e);
+            result.put("msg", e.getMessage());
+            return result;
+        }
+    }
+
     /**
     /**
      * 2026-05-26:手工触发真实下发,便于先做一次人工联调确认海康侧能成功接收。
      * 2026-05-26:手工触发真实下发,便于先做一次人工联调确认海康侧能成功接收。
      */
      */
@@ -435,7 +513,7 @@ public class HkLeaveAuthServ extends Serv {
             if (StringUtils.isNotBlank(tagId))
             if (StringUtils.isNotBlank(tagId))
                 hkConnParm.put("tagId", tagId);
                 hkConnParm.put("tagId", tagId);
 
 
-            // 2026-05-26:真实下发优先使用显式配置的 templateId,避免把诊断探测结果当成业务前置条件。
+            // 2026-05-27:当前请假自动开门按“添加权限配置”口径执行,templateId 只在海康侧明确要求时才附带。
             Map<String, Object> singleSearchReq = null;
             Map<String, Object> singleSearchReq = null;
             Map<String, Object> singleSearchResp = null;
             Map<String, Object> singleSearchResp = null;
             String probedTemplateId = null;
             String probedTemplateId = null;
@@ -459,31 +537,31 @@ public class HkLeaveAuthServ extends Serv {
             String effectiveTemplateId = StringUtils.defaultIfBlank(templateId, probedTemplateId);
             String effectiveTemplateId = StringUtils.defaultIfBlank(templateId, probedTemplateId);
             result.put("templateIdSource", templateIdSource);
             result.put("templateIdSource", templateIdSource);
             result.put("templateId", effectiveTemplateId);
             result.put("templateId", effectiveTemplateId);
+            result.put("downloadMode", StringUtils.isBlank(effectiveTemplateId) ? "addPermissionConfig" : "addPermissionConfigWithTemplate");
             result.put("templateDimension", templateDimension);
             result.put("templateDimension", templateDimension);
             result.put("tagId", tagId);
             result.put("tagId", tagId);
             result.put("tagIdSource", tagIdSource);
             result.put("tagIdSource", tagIdSource);
             result.put("callbackUrl", callbackUrl);
             result.put("callbackUrl", callbackUrl);
-            result.put("configReady", StringUtils.isNotBlank(tagId) && StringUtils.isNotBlank(effectiveTemplateId));
+            result.put("configReady", StringUtils.isNotBlank(tagId));
 
 
             List<String> missingCfgList = new ArrayList<>();
             List<String> missingCfgList = new ArrayList<>();
             if (StringUtils.isBlank(tagId))
             if (StringUtils.isBlank(tagId))
                 missingCfgList.add("tagId");
                 missingCfgList.add("tagId");
-            if (StringUtils.isBlank(effectiveTemplateId))
-                missingCfgList.add("templateId");
             result.put("missingConfig", missingCfgList);
             result.put("missingConfig", missingCfgList);
             result.put("nextSteps", makeNextStepList(missingCfgList));
             result.put("nextSteps", makeNextStepList(missingCfgList));
             result.put("tagIdAdvice", "当前项目只做接口下发时,tagId 只要海康侧提供可用值即可;若还要求平台门禁管理可见,再评估是否改成 acs。");
             result.put("tagIdAdvice", "当前项目只做接口下发时,tagId 只要海康侧提供可用值即可;若还要求平台门禁管理可见,再评估是否改成 acs。");
-            result.put("templateIdAdvice", "templateId 需要海康侧在门禁权限模板中提供,当前 single/search 不能稳定自动探测。");
+            result.put("templateIdAdvice", "templateId 仅在海康侧明确要求模板时再补;当前请假自动开门默认可不传。");
             result.put("executeApiPath", "/artemis/api/acps/v1/authDownload/special/person/diy");
             result.put("executeApiPath", "/artemis/api/acps/v1/authDownload/special/person/diy");
             result.put("resultApiPath", "/artemis/api/acps/v1/authDownload/special/person/diy/result");
             result.put("resultApiPath", "/artemis/api/acps/v1/authDownload/special/person/diy/result");
 
 
+            // 2026-05-27:templateId 为空时,不再把 templateConfigInfos 强塞进请求体,避免把新增权限配置卡死。
             Map<String, Object> payload = makeSpecialAuthPayload(
             Map<String, Object> payload = makeSpecialAuthPayload(
                     ctx, personInfo, personId, resourceInfoList, tagId, callbackUrl, effectiveTemplateId, templateDimension);
                     ctx, personInfo, personId, resourceInfoList, tagId, callbackUrl, effectiveTemplateId, templateDimension);
             result.put("payload", payload);
             result.put("payload", payload);
             result.put("authDownloadResultQueryReq", makeAuthDownloadResultQueryReq(personId, resourceInfoList));
             result.put("authDownloadResultQueryReq", makeAuthDownloadResultQueryReq(personId, resourceInfoList));
 
 
-            if (StringUtils.isBlank(tagId) || StringUtils.isBlank(effectiveTemplateId)) {
-                result.put("skipReason", "缺少海康下发配置,暂不执行 special/person/diy。");
+            if (StringUtils.isBlank(tagId)) {
+                result.put("skipReason", "缺少海康下发配置 tagId,暂不执行添加权限配置。");
                 result.put("mode", "preview");
                 result.put("mode", "preview");
                 return result;
                 return result;
             }
             }
@@ -637,7 +715,8 @@ public class HkLeaveAuthServ extends Serv {
             payload.put("callbackUrl", callbackUrl);
             payload.put("callbackUrl", callbackUrl);
         payload.put("resourceInfos", resourceInfoList);
         payload.put("resourceInfos", resourceInfoList);
         payload.put("personInfos", List.of(makePersonInfoPayload(ctx, personInfo, personId)));
         payload.put("personInfos", List.of(makePersonInfoPayload(ctx, personInfo, personId)));
-        payload.put("templateConfigInfos", makeTemplateConfigInfos(templateId, templateDimension));
+        if (StringUtils.isNotBlank(templateId))
+            payload.put("templateConfigInfos", makeTemplateConfigInfos(templateId, templateDimension));
         return payload;
         return payload;
     }
     }
 
 
@@ -695,8 +774,6 @@ public class HkLeaveAuthServ extends Serv {
         }
         }
         if (missingCfgList.contains("tagId"))
         if (missingCfgList.contains("tagId"))
             nextStepList.add("联系海康侧提供当前合作方账号可用的 tagId。");
             nextStepList.add("联系海康侧提供当前合作方账号可用的 tagId。");
-        if (missingCfgList.contains("templateId"))
-            nextStepList.add("联系海康侧提供门禁权限模板 templateId,或在平台门禁权限模板中确认后填写。");
         nextStepList.add("配置补齐前可继续使用 hkkq_leave_auth_preview / hkkq_leave_auth_diag 做联调。");
         nextStepList.add("配置补齐前可继续使用 hkkq_leave_auth_preview / hkkq_leave_auth_diag 做联调。");
         nextStepList.add("配置补齐后先调用 hkkq_leave_auth_execute,再调用 hkkq_leave_auth_result 回查。");
         nextStepList.add("配置补齐后先调用 hkkq_leave_auth_execute,再调用 hkkq_leave_auth_result 回查。");
         return nextStepList;
         return nextStepList;
@@ -1061,6 +1138,23 @@ public class HkLeaveAuthServ extends Serv {
         return row;
         return row;
     }
     }
 
 
+    /**
+     * 2026-05-27:把请求里的 JSON 字符串解析成 Map,供海康原始探测接口透传。
+     */
+    private static Map<String, Object> rdReqBodyMap(String reqBodyStr) {
+        if (StringUtils.isBlank(reqBodyStr))
+            return new LinkedHashMap<>();
+
+        JsonElement jsonElement = JsonParser.parseString(reqBodyStr);
+        if (!jsonElement.isJsonObject())
+            throw new IllegalArgumentException("仅支持 JSON Object。");
+
+        JsonObject jsonObject = jsonElement.getAsJsonObject();
+        @SuppressWarnings("unchecked")
+        Map<String, Object> row = GSON.fromJson(jsonObject, Map.class);
+        return row == null ? new LinkedHashMap<>() : row;
+    }
+
     /**
     /**
      * 2026-05-26:把列表字段转成长度统计,日志只看数量不看全文。
      * 2026-05-26:把列表字段转成长度统计,日志只看数量不看全文。
      */
      */

+ 9 - 0
src/core/co/haikang/HkApiClient.java

@@ -324,6 +324,15 @@ public final class HkApiClient {
         return postHkApiMap(queryParm, HkProtocolC.HK_SPECIAL_PERSON_AUTH_RESULT_API_PATH, reqBodyMap);
         return postHkApiMap(queryParm, HkProtocolC.HK_SPECIAL_PERSON_AUTH_RESULT_API_PATH, reqBodyMap);
     }
     }
 
 
+    /**
+     * 2026-05-27:开放通用海康 POST 探测能力,便于按文档接口路径逐个试探 templateId 所在接口。
+     */
+    public Map<String, Object> postProbeApi(Map<String, Object> queryParm,
+                                            String apiPath,
+                                            Map<String, Object> reqBodyMap) throws Exception {
+        return postHkApiMap(queryParm, apiPath, reqBodyMap);
+    }
+
     /**
     /**
      * 为返回前端的 `query` 字段生成安全回显。
      * 为返回前端的 `query` 字段生成安全回显。
      * 这里保留“查了什么”,去掉“敏感密钥原文”。
      * 这里保留“查了什么”,去掉“敏感密钥原文”。

+ 1 - 0
web/cfg/biz/kq/hkkq.serv.xml

@@ -15,6 +15,7 @@
 	<serv name="hkkq_leave_auth_preview" desc="海康请假权限预览" clazz="biz.kq.HkLeaveAuthServ" func="previewLeaveAuthSync" auth="true"/>
 	<serv name="hkkq_leave_auth_preview" desc="海康请假权限预览" clazz="biz.kq.HkLeaveAuthServ" func="previewLeaveAuthSync" auth="true"/>
 	<serv name="hkkq_leave_auth_diag" desc="海康权限条目诊断" clazz="biz.kq.HkLeaveAuthServ" func="diagLeaveAuthItem" auth="true"/>
 	<serv name="hkkq_leave_auth_diag" desc="海康权限条目诊断" clazz="biz.kq.HkLeaveAuthServ" func="diagLeaveAuthItem" auth="true"/>
 	<serv name="hkkq_leave_auth_diag_template" desc="海康请假模板探测" clazz="biz.kq.HkLeaveAuthServ" func="diagLeaveAuthTemplateId" auth="true"/>
 	<serv name="hkkq_leave_auth_diag_template" desc="海康请假模板探测" clazz="biz.kq.HkLeaveAuthServ" func="diagLeaveAuthTemplateId" auth="true"/>
+	<serv name="hkkq_leave_auth_probe_api" desc="海康模板原始接口探测" clazz="biz.kq.HkLeaveAuthServ" func="probeHkTemplateApi" auth="true"/>
 	<serv name="hkkq_leave_auth_execute" desc="海康请假权限真实下发" clazz="biz.kq.HkLeaveAuthServ" func="executeLeaveAuthSync" auth="true"/>
 	<serv name="hkkq_leave_auth_execute" desc="海康请假权限真实下发" clazz="biz.kq.HkLeaveAuthServ" func="executeLeaveAuthSync" auth="true"/>
 	<serv name="hkkq_leave_auth_result" desc="海康请假权限下发结果查询" clazz="biz.kq.HkLeaveAuthServ" func="viewLeaveAuthSyncResult" auth="true"/>
 	<serv name="hkkq_leave_auth_result" desc="海康请假权限下发结果查询" clazz="biz.kq.HkLeaveAuthServ" func="viewLeaveAuthSyncResult" auth="true"/>
 	<serv name="hkkq_event_push" desc="海康事件订阅回调" clazz="biz.kq.HkKqServ" func="recvHkEventPush" pub="true"/>
 	<serv name="hkkq_event_push" desc="海康事件订阅回调" clazz="biz.kq.HkKqServ" func="recvHkEventPush" pub="true"/>