Files
monkey-ai-web/src/pages/Organization/fund/fundFormat.js
T
qiubaishen a57aae3043 sync: 同步 monkey-ai@weiyuan(ddc5b9443) 的 web/ 到 ui-optimization
- 合入 weiyuan 新功能前端:分佣 V2 / 伙伴工作台、智能路由、企业资金与分析、官网设置与预约演示等,
  semi-ui 已全部迁移到 antd 6 / @/components/ui,不重新引入 Semi
- 分佣二级菜单与页头去重、伙伴入口探测(企业伙伴走企业中心「企业分佣」)、超管侧栏入口恢复
- 适配层 DatePicker / TimePicker 统一 onChange(Date|Date[]|null, 文本) 契约;生产构建剔除调试日志
- 图片链接刷新:同任务请求复用、后台刷新不弹全局错误、离页停止批量请求
- 恢复 bun.lock(与 package-lock.json 共存);docs 规范与 CHANGELOG 更新
- 停止跟踪 .env.playwright.local(本地登录凭据,已加入 .gitignore)
2026-11-18 10:22:02 +08:00

134 lines
4.9 KiB
JavaScript

/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
// 与后端 model/org_fund.go 常量一一对应
export const FUND_STATE = { NEVER: 0, ON: 1, OFF: 2 };
export const FUND_ACCOUNT_TYPE = { POOL: 0, UNALLOCATED: 1, DEPT: 2 };
export const FUND_ACCOUNT_STATUS = { ACTIVE: 1, DISABLED: 2, ARCHIVED: 3 };
export const FUND_LEDGER_PENDING = 1;
export const FUND_ENTRY_TYPES = [
'allocate_in',
'allocate_out',
'recall_in',
'recall_out',
'consume',
'consume_adjust',
'recharge',
'platform_add',
'platform_sub',
'baseline',
];
export const fundEntryLabel = (t, type) =>
({
baseline: t('首次开启基线'),
allocate_in: t('分配转入'),
allocate_out: t('分配转出'),
recall_in: t('回收转入'),
recall_out: t('回收转出'),
consume: t('消费'),
consume_adjust: t('消费调账'),
recharge: t('充值入账'),
platform_add: t('平台调增'),
platform_sub: t('平台调减'),
})[type] || type;
// 扣费账户展示值(F-36):企业资金池 / 企业未分配户 / 部门账户 · {名称快照}
export const fundAccountDisplay = (t, accountType, deptName) => {
if (accountType === FUND_ACCOUNT_TYPE.UNALLOCATED) return t('企业未分配户');
if (accountType === FUND_ACCOUNT_TYPE.DEPT)
return `${t('部门账户')} · ${deptName || ''}`;
return t('企业资金池');
};
// 归属部门展示值:未快照(历史待补齐)为 “—”,部门为 0 是“未归属部门”
export const fundDeptDisplay = (t, deptId, deptName) => {
if (deptId === null || deptId === undefined) return '—';
if (!deptId) return t('未归属部门');
return deptName || `#${deptId}`;
};
// 一次分配/回收的幂等键:打开弹窗时生成,重试沿用(F-12)。
// 旧内核(Chromium < 92)没有 crypto.randomUUID,退回 getRandomValues 拼装。
export const newFundOpId = () => {
const c = typeof window !== 'undefined' ? window.crypto : undefined;
if (c?.randomUUID) return c.randomUUID();
const bytes = new Uint8Array(16);
if (c?.getRandomValues) c.getRandomValues(bytes);
else for (let i = 0; i < 16; i++) bytes[i] = Math.floor(Math.random() * 256);
return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
};
// 手动金额校验(F-13):正数、最多两位小数;TOKENS 展示模式只收整数。
// 不接受负数、0、科学计数法、非数字文本。返回 i18n 键,合法返回 ''。
export const validateFundAmountInput = (raw, isTokenUnit) => {
const s = String(raw ?? '').trim();
if (!s) return '请输入金额';
const pattern = isTokenUnit ? /^\d+$/ : /^\d+(\.\d{1,2})?$/;
if (!pattern.test(s) || !(Number(s) > 0))
return '请输入大于 0、最多两位小数的金额';
if (Number(s) > 1e9) return '金额超出可操作范围';
return '';
};
// 按 parent_id 组装树(同级按 sort、id 排序),供 Semi Table 树形展示
export const buildFundTree = (rows, rootParentId = 0) => {
const byParent = new Map();
rows.forEach((r) => {
const p = r.parent_id || 0;
if (!byParent.has(p)) byParent.set(p, []);
byParent.get(p).push(r);
});
const ids = new Set(rows.map((r) => r.department_id));
const sortRows = (list) =>
[...list].sort(
(a, b) =>
(a.sort || 0) - (b.sort || 0) || a.department_id - b.department_id,
);
const build = (parentId, trail) =>
sortRows(byParent.get(parentId) || []).map((r) => {
const path = [...trail, r.name];
const children = build(r.department_id, path);
return {
...r,
key: r.department_id,
pathLabel: path.join(' / '),
children: children.length ? children : undefined,
};
});
// 根:rootParentId 的直接子节点;另外把父节点不在列表里的行也当作根(下级视图里的顶层)
const roots = build(rootParentId, []);
const orphanRoots = rows.filter(
(r) => (r.parent_id || 0) !== rootParentId && !ids.has(r.parent_id),
);
return [
...roots,
...sortRows(orphanRoots).map((r) => {
const children = build(r.department_id, [r.name]);
return {
...r,
key: r.department_id,
pathLabel: r.name,
children: children.length ? children : undefined,
};
}),
];
};