Browse Source

修复: 兼容对象选择器回包并校验变动码本值

1. PC、H5 和移动端对象选择器统一兼容新版 List<Map> 回包,避免下拉框显示对象字面量或取数组下标作为值。

2. loadObjpOpt 对空参数、缺失对象和缺失码本返回 JSON 错误,避免接口 200 但前端 parsererror 或后端空指针。

3. 变动保存前对缓存码本字段做存在性校验,岗位类别等非法码值会返回明确业务错误,不再落到 Oracle 外键异常。

4. 验证:已执行 javac 定向编译 BpmServ/ObjpServ;已执行 node --check 检查三处 JS 文件。
“gaohp” 3 months ago
parent
commit
84dc53c907

+ 23 - 0
src/ss/bpm/BpmServ.java

@@ -7,6 +7,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
@@ -28,6 +29,8 @@ import ss.db.impl.SqPo;
 import ss.AppB;
 import ss.AppC;
 import ss.WebC;
+import ss.cb.CbB;
+import ss.cb.CbU;
 import ss.cmn.file.FileU;
 import ss.cmn.str.StrWeb;
 import ss.cms.CmsRunB;
@@ -819,6 +822,25 @@ public class BpmServ extends Serv {
 		SsLog.print(paramMap.toString());
 	}
 
+	private static void chkChgCbValue(ObjFieldB field, String value) throws Exception {
+		if (field == null || StringUtils.isBlank(field.cbName) || StringUtils.isBlank(value))
+			return;
+
+		CbB cb = AppB.cbMap.get(field.cbName);
+		if (cb == null)
+			throw new Exception("字段“"+ field.desc +"”引用的码本“"+ field.cbName +"”不存在");
+
+		if (!cb.cacheable)
+			return;
+
+		Map<String, String> optMap = new LinkedHashMap<>();
+		if (CbU.loadCbOpt(optMap, null, cb, field.cbParmStr, null))
+			optMap = cb.optMap;
+
+		if (optMap == null || !optMap.containsKey(value))
+			throw new Exception("字段“"+ field.desc +"”的值“"+ value +"”不在码本“"+ cb.desc +"("+ cb.name +")”中,请重新选择后提交");
+	}
+
 	
 // =====
 
@@ -1656,6 +1678,7 @@ public class BpmServ extends Serv {
 
 					String newValue = entry.getValue().get(1);
 					String oldValue = entry.getValue().get(0);
+					chkChgCbValue(f, newValue);
 /* 改为 int 了。Lin
 					if (f.getType().equals(Field.TYPE_HTML)) {
 */					if (f.type == ObjC.FIELD_htmlType)

+ 15 - 0
src/ss/objp/ObjpServ.java

@@ -142,6 +142,12 @@ public class ObjpServ extends Serv {
 			}
 
 			Map<String, String> p = gson.fromJson(param, new TypeToken<Map<String, String>>(){}.getType());
+			if (p == null) {
+				r = new ObjpResultB(new LinkedHashMap<>(), null, null);
+				r.errmsg = "对象选择器参数为空";
+				writer.write(gson.toJson(r));
+				return;
+			}
 
 			String codebooks = p.get("codebook");
 			String viewObjectName = p.get("object");
@@ -156,6 +162,12 @@ public class ObjpServ extends Serv {
 				ViewObject viewobject= ViewObjectUtil
 					.findViewObjectByName(WdLoginUtil.getLoginedAppName(getRequest()), viewObjectName);
 */				ObjB viewobject= AppB.objMap.get(viewObjectName);
+				if (viewobject == null) {
+					r = new ObjpResultB(new LinkedHashMap<>(), null, null);
+					r.errmsg = "没找到对象选择器定义:"+ viewObjectName;
+					writer.write(gson.toJson(r));
+					return;
+				}
 
 				if (type == 1) {	// (type.equals("1"))。"查找数据" -- 打开录入页时是"2"("回显数据"),选择第一个下拉列表的行时是"1"。Lin
 ///???用到再改 -- 原 List<Map<"xbm":"1","mc":"男">,Map<"xbm":"2","mc":"女">>> 改为 List<Map<"1":"男">,Map<"2":"女">>。Lin
@@ -234,6 +246,9 @@ public class ObjpServ extends Serv {
 			CbB codebook = AppB.cbMap.get(codebooks);
 			if (codebook == null) {
 				SsLog.log("XXXXXXXX,没找到码本定义:"+ codebooks);
+				r = new ObjpResultB(new LinkedHashMap<>(), null, null);
+				r.errmsg = "没找到码本定义:"+ codebooks;
+				writer.write(gson.toJson(r));
 				return;
 			}
 

+ 10 - 0
web/js/mp_ss_components/mp-ss-components.js

@@ -936,6 +936,16 @@
             return { n: String(it || ""), v: String(it || "") };
           });
         }
+        if (Array.isArray(raw.result)) {
+          return raw.result
+            .map((it) => {
+              if (!it || typeof it !== "object") return null;
+              const entry = Object.entries(it)[0];
+              if (!entry) return null;
+              return { n: entry[1], v: entry[0] };
+            })
+            .filter(Boolean);
+        }
         if (raw.result && typeof raw.result === "object") {
           return Object.keys(raw.result).map((k) => ({
             n: raw.result[k],

+ 33 - 12
web/js/mp_utils/field-formatter.js

@@ -3,6 +3,32 @@
  * 基于小程序版本改造,适配H5环境
  */
 
+const normalizeObjpResultEntries = (result) => {
+  if (!result) return [];
+  if (Array.isArray(result)) {
+    return result
+      .map((item) => {
+        if (!item || typeof item !== "object") return null;
+        const entry = Object.entries(item)[0];
+        if (!entry) return null;
+        return { value: entry[0], label: entry[1] };
+      })
+      .filter(Boolean);
+  }
+  if (typeof result === "object") {
+    return Object.entries(result).map(([value, label]) => ({ value, label }));
+  }
+  return [];
+};
+
+const findObjpResultLabel = (result, value) => {
+  const targetValue = String(value);
+  const item = normalizeObjpResultEntries(result).find(
+    (entry) => String(entry.value) === targetValue
+  );
+  return item ? item.label : undefined;
+};
+
 /**
  * 获取字典翻译 - H5版本
  * @param {string} cbName - 字典名称
@@ -37,11 +63,10 @@ const getDictTranslation = async (
 
     console.log(`字典翻译 [${cbName}:${value}] API返回:`, result);
 
-    // 处理返回数据格式:{"result":{"622182":"会议室A"}}
+    // 兼容旧版 {"result":{"622182":"会议室A"}} 和新版 {"result":[{"622182":"会议室A"}]}
     let translatedValue = value; // 默认返回原值
     if (result && result.data && result.data.result) {
-      // 从result对象中根据value查找对应的翻译
-      translatedValue = result.data.result[value] || value;
+      translatedValue = findObjpResultLabel(result.data.result, value) || value;
       console.log(`字典翻译结果: ${value} -> ${translatedValue}`);
     }
 
@@ -366,13 +391,9 @@ const formatFieldValueAsync = async (fieldData, dictCache) => {
         }
       );
 
-      if (
-        result &&
-        result.data &&
-        result.data.result &&
-        result.data.result[value]
-      ) {
-        const translatedValue = result.data.result[value];
+      if (result && result.data && result.data.result) {
+        const translatedValue = findObjpResultLabel(result.data.result, value);
+        if (!translatedValue) return value;
         // 缓存结果
         if (dictCache) {
           dictCache.set(cacheKey, translatedValue);
@@ -437,8 +458,8 @@ const getDictOptions = async (cbName, dictCache) => {
     console.log(`字典选项 [${cbName}] API返回:`, result);
 
     if (result && result.data && result.data.result) {
-      const options = Object.entries(result.data.result).map(
-        ([value, label]) => ({
+      const options = normalizeObjpResultEntries(result.data.result).map(
+        ({ value, label }) => ({
           n: label,
           v: value,
         })

+ 31 - 16
web/js/vue/ss-components.js

@@ -996,6 +996,24 @@ import { EVEN_VAR } from "./EventBus.js";
         }
       };
 
+      const normalizeObjpResult = (result) => {
+        const normalized = [];
+        if (!result) return normalized;
+        if (Array.isArray(result)) {
+          result.forEach((item) => {
+            if (!item || typeof item !== "object") return;
+            const entry = Object.entries(item)[0];
+            if (!entry) return;
+            normalized.push({ label: entry[1], value: entry[0] });
+          });
+          return normalized;
+        }
+        Object.keys(result).forEach((key) => {
+          normalized.push({ label: result[key], value: key });
+        });
+        return normalized;
+      };
+
       // 功能说明:封装 modelValue 变化后的联动逻辑,兼容用户点击和桥接 API 赋值 by xu 20260416
       const handleModelValueChange = (newValue) => {
         updateSelectItem();
@@ -1022,10 +1040,10 @@ import { EVEN_VAR } from "./EventBus.js";
                 return;
               }
               if (response.data.result) {
-                const keys = Object.keys(response.data.result);
-                if (keys.length === 1) {
-                  let code = keys[0];
-                  let desc = response.data.result[keys[0]];
+                const normalizedResult = normalizeObjpResult(response.data.result);
+                if (normalizedResult.length === 1) {
+                  let code = normalizedResult[0].value;
+                  let desc = normalizedResult[0].label;
                   props.opt.length = 0;
                   props.opt.push({ label: desc, value: code });
                   updateSelectItem();
@@ -1105,10 +1123,10 @@ import { EVEN_VAR } from "./EventBus.js";
                 }
 
                 if (response.data.result) {
-                  const keys = Object.keys(response.data.result);
-                  if (keys.length === 1) {
-                    let code = keys[0];
-                    let desc = response.data.result[keys[0]];
+                  const normalizedResult = normalizeObjpResult(response.data.result);
+                  if (normalizedResult.length === 1) {
+                    let code = normalizedResult[0].value;
+                    let desc = normalizedResult[0].label;
                     if (props.opt)
                       props.opt.length = 0; //通过修改数组的length属性,直接清空数组元素,内存会被自动释放。这是性能最优的方式
                     else {
@@ -1251,15 +1269,12 @@ import { EVEN_VAR } from "./EventBus.js";
                 }
 
                 if (response.data.result) {
-                  const keys = Object.keys(response.data.result);
+                  const normalizedResult = normalizeObjpResult(response.data.result);
                   // console.log("params:"+params+"@@response.data:"+JSON.stringify(response.data));
-                  if (keys.length > 0) {
-                    for (let k in response.data.result) {
-                      props.opt.push({
-                        label: response.data.result[k],
-                        value: k,
-                      });
-                    }
+                  if (normalizedResult.length > 0) {
+                    normalizedResult.forEach((option) => {
+                      props.opt.push(option);
+                    });
 
                     // console.log('###inpTxt:'+inpTxt+';');
                     if (