| ¶Ô±ÈÐÂÎļþ |
| | |
| | | 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]; |
| | | } |