From 769fb543015f1a90d42882a0a9f0592efa45a10e Mon Sep 17 00:00:00 2001
From: yyb <995253665@qq.com>
Date: 星期一, 01 六月 2026 19:33:30 +0800
Subject: [PATCH] Merge branch 'dev_NEW_pro' of http://114.132.189.42:9002/r/product-inventory-management into dev_NEW_pro

---
 src/views/officeProcessAutomation/ApproveManage/approve-template/selectOptionSource.js |  140 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 140 insertions(+), 0 deletions(-)

diff --git a/src/views/officeProcessAutomation/ApproveManage/approve-template/selectOptionSource.js b/src/views/officeProcessAutomation/ApproveManage/approve-template/selectOptionSource.js
new file mode 100644
index 0000000..99706b4
--- /dev/null
+++ b/src/views/officeProcessAutomation/ApproveManage/approve-template/selectOptionSource.js
@@ -0,0 +1,140 @@
+import { deptTreeSelect, userListNoPageByTenantId } from "@/api/system/user.js";
+
+/** 涓嬫媺閫夐」鏉ユ簮锛堝啓鍏� formConfig锛屾彁浜ら〉鎸夋潵婧愭媺鍙栨暟鎹級 */
+export const SELECT_OPTION_SOURCE = {
+  STATIC: "static",
+  USER: "user",
+  DEPT: "dept",
+};
+
+export const SELECT_OPTION_SOURCE_OPTIONS = [
+  { value: SELECT_OPTION_SOURCE.STATIC, label: "鎵嬪姩閰嶇疆", desc: "鍦ㄦā鏉夸腑鑷畾涔夐�夐」鏂囨湰涓庡��" },
+  { value: SELECT_OPTION_SOURCE.USER, label: "浜哄憳鍒楄〃", desc: "浠庣郴缁熺敤鎴蜂腑閫夋嫨锛屽�间负鐢ㄦ埛 ID" },
+  { value: SELECT_OPTION_SOURCE.DEPT, label: "閮ㄩ棬鍒楄〃", desc: "浠庣粍缁囨灦鏋勪腑閫夋嫨锛屽�间负閮ㄩ棬 ID" },
+];
+
+export function selectOptionSourceLabel(source) {
+  return SELECT_OPTION_SOURCE_OPTIONS.find((x) => x.value === source)?.label || "鈥�";
+}
+
+export function isDynamicOptionSource(source) {
+  return source === SELECT_OPTION_SOURCE.USER || source === SELECT_OPTION_SOURCE.DEPT;
+}
+
+function unwrapArray(payload) {
+  if (Array.isArray(payload)) return payload;
+  if (payload?.data && Array.isArray(payload.data)) return payload.data;
+  if (payload?.rows && Array.isArray(payload.rows)) return payload.rows;
+  return [];
+}
+
+function isActiveUser(u) {
+  if (u.delFlag === "2" || u.delFlag === 2) return false;
+  if (u.status == null) return true;
+  return String(u.status) === "0";
+}
+
+/** 鐢ㄦ埛 鈫� 涓嬫媺 option */
+export function mapUserToSelectOption(u) {
+  const value = u.userId ?? u.id;
+  return {
+    label: u.nickName || u.userName || `鐢ㄦ埛${value}`,
+    value,
+  };
+}
+
+/** 閮ㄩ棬鏍戞媿骞充负涓嬫媺 option */
+export function flattenDeptToSelectOptions(nodes, result = []) {
+  (nodes || []).forEach((node) => {
+    const value = node.id ?? node.deptId ?? node.value;
+    if (value != null && value !== "") {
+      result.push({
+        label: node.label ?? node.deptName ?? node.name ?? String(value),
+        value,
+      });
+    }
+    if (node.children?.length) flattenDeptToSelectOptions(node.children, result);
+  });
+  return result;
+}
+
+function filterDisabledDept(deptList) {
+  if (!Array.isArray(deptList)) return [];
+  return deptList.filter((dept) => {
+    if (dept.disabled) return false;
+    if (dept.children?.length) {
+      dept.children = filterDisabledDept(dept.children);
+    }
+    return true;
+  });
+}
+
+/** 鎸夊瓧娈甸厤缃В鏋愪笅鎷� options锛堥渶浼犲叆宸插姞杞界殑缂撳瓨锛� */
+export function resolveFieldSelectOptions(field, caches = {}) {
+  const source = field?.optionSource || SELECT_OPTION_SOURCE.STATIC;
+  if (source === SELECT_OPTION_SOURCE.USER) {
+    return (caches.users || []).map(mapUserToSelectOption);
+  }
+  if (source === SELECT_OPTION_SOURCE.DEPT) {
+    return caches.deptOptions || [];
+  }
+  return (field?.options || []).filter((o) => o.value !== "" && o.value != null);
+}
+
+/** 鏍规嵁宸茶В鏋愮殑 options 鍙嶆煡灞曠ず鏂囨湰 */
+export function resolveSelectDisplayLabel(field, val, caches = {}) {
+  if (val == null || val === "") return "鈥�";
+  const options = resolveFieldSelectOptions(field, caches);
+  const hit = options.find((o) => String(o.value) === String(val));
+  return hit?.label || String(val);
+}
+
+/** 鍔犺浇浜哄憳 / 閮ㄩ棬缂撳瓨锛堝澶勫鐢級 */
+export async function fetchSelectOptionCaches(sources = []) {
+  const needUser = sources.includes(SELECT_OPTION_SOURCE.USER);
+  const needDept = sources.includes(SELECT_OPTION_SOURCE.DEPT);
+  const caches = { users: [], deptOptions: [] };
+
+  if (!needUser && !needDept) return caches;
+
+  const tasks = [];
+  if (needUser) {
+    tasks.push(
+      userListNoPageByTenantId()
+        .then((res) => {
+          caches.users = unwrapArray(res).filter(isActiveUser);
+        })
+        .catch(() => {
+          caches.users = [];
+        })
+    );
+  }
+  if (needDept) {
+    tasks.push(
+      deptTreeSelect()
+        .then((res) => {
+          let tree = unwrapArray(res);
+          tree = tree.length ? filterDisabledDept(JSON.parse(JSON.stringify(tree))) : [];
+          if (!tree.length) tree = unwrapArray(res);
+          caches.deptOptions = flattenDeptToSelectOptions(tree);
+        })
+        .catch(() => {
+          caches.deptOptions = [];
+        })
+    );
+  }
+
+  await Promise.all(tasks);
+  return caches;
+}
+
+/** 浠庡瓧娈靛垪琛ㄦ敹闆嗛渶瑕侀鍔犺浇鐨勫姩鎬佹潵婧� */
+export function collectOptionSourcesFromFields(fields) {
+  const set = new Set();
+  (fields || []).forEach((f) => {
+    if (f?.type === "select" && isDynamicOptionSource(f.optionSource)) {
+      set.add(f.optionSource);
+    }
+  });
+  return [...set];
+}

--
Gitblit v1.9.3