5 处逐字硬编码 profile?.account==='admin'||userId==='ADMIN'(Topbar/ModelerPage/NewFeatureShell/AiFeatureForm/DesignCenterPage)收口到 auth/token.ts 纯函数 isAdminProfile,口径不变。
顺带:5 个页面测试的 vi.mock('../auth/token') 从整模块替换改为 importOriginal 只覆盖 getProfile(不再 mock 纯函数,修复 mock 漏 isAdminProfile 致的 render 崩)。
验证:新增 token.test.ts 4 测试 + tsc -b 干净 + vitest 18 文件/77 测试全绿。见总览 §10 B4。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
311 lines
15 KiB
TypeScript
311 lines
15 KiB
TypeScript
// 人走五环 · 统一「新建功能」入口。先选页面类型(维护型CRUD / 树形 / 主子表),再进对应向导。
|
||
// 关键:页面类型在进向导前由 React 状态确定,向导步骤按类型在【构建时(JS)】分支(主子表才有「建子表」步
|
||
// 及派生/汇总字段)——不依赖 amis 运行时 step hiddenOn(amis Wizard.normalizeSteps 只在 props.data 变时
|
||
// 重算,向导内选择改的是 store.data 不触发重算,故 step 级动态跳步不可靠;构建时分支才健壮)。
|
||
// 各类型向导:① 建实体(post /api/meta/entity) →【主子表】② 建子表 → ③ 生成页面(post /api/design/recipe/compile)
|
||
// → ④ 发布(post /api/design/publish)。主实体 id/表名经 responseData remap 到 masterEntityId/masterTable。
|
||
// 红线:发布恒人审;standard 写入恒走后端 PageSchemaWriter(前端只调 /api/design/recipe/compile)。
|
||
import { useMemo, useState } from 'react';
|
||
import { useNavigate } from 'react-router-dom';
|
||
import { getProfile, isAdminProfile } from '../auth/token';
|
||
import { AmisRenderer } from '../components/AmisRenderer';
|
||
|
||
const TYPE_OPTIONS = [
|
||
{ label: '文本', value: 'string' },
|
||
{ label: '整数', value: 'integer' },
|
||
{ label: '小数', value: 'number' },
|
||
{ label: '日期', value: 'date' },
|
||
{ label: '日期时间', value: 'datetime' },
|
||
{ label: '布尔', value: 'boolean' },
|
||
{ label: '引用(外键)', value: 'reference' },
|
||
];
|
||
|
||
const RECIPE_CARDS = [
|
||
{ recipe: 'maintenance-crud-v1', testid: 'dc-recipe-maintenance', title: '维护型 CRUD',
|
||
desc: '单表:列表 + 查询 + 增删改。最常见的基础数据/业务维护页。' },
|
||
{ recipe: 'tree-crud-v1', testid: 'dc-recipe-tree', title: '树形 CRUD',
|
||
desc: '自引用层级(需含 PARENTID 列)。组织/分类/菜单等树状数据。' },
|
||
{ recipe: 'master-detail-v1', testid: 'dc-recipe-master-detail', title: '主子表',
|
||
desc: '主表 + 子表 EditGrid,子行随主记录级联保存。支持派生字段(金额=数量×单价)与父级汇总(总额=之和)。' },
|
||
];
|
||
|
||
/** 字段 combo(含引用列:type=引用 → 选 referenceTable,源 /api/meta/entities)。 */
|
||
function attrCombo(name: string, addButtonText: string): Record<string, unknown> {
|
||
return {
|
||
type: 'combo', name, label: '字段', multiple: true, addable: true, removable: true,
|
||
addButtonText, minLength: 1,
|
||
items: [
|
||
{
|
||
type: 'input-text', name: 'column', label: '列名', required: true, placeholder: 'TITLE',
|
||
columnClassName: 'w-sm', validations: { matchRegexp: '/^[A-Z][A-Z0-9_]{0,40}$/' },
|
||
validationErrors: { matchRegexp: '大写字母开头' },
|
||
},
|
||
{ type: 'input-text', name: 'display', label: '显示名', placeholder: '标题' },
|
||
{ type: 'select', name: 'type', label: '类型', value: 'string', options: TYPE_OPTIONS, columnClassName: 'w-xs' },
|
||
{
|
||
type: 'select', name: 'referenceTable', label: '引用目标实体',
|
||
source: 'get:/api/meta/entities', labelField: 'tablename', valueField: 'tablename',
|
||
searchable: true, visibleOn: '${type === "reference"}',
|
||
description: '渲染时显该实体的显示列而非裸 id',
|
||
},
|
||
{ type: 'switch', name: 'mandatory', label: '必填', value: false },
|
||
],
|
||
};
|
||
}
|
||
|
||
/** 按页面类型构建向导 amis schema(类型已由 React 状态固定,步骤构建时 JS 分支)。 */
|
||
function buildWizardSchema(recipe: string): Record<string, unknown> {
|
||
const isMD = recipe === 'master-detail-v1';
|
||
const isTree = recipe === 'tree-crud-v1';
|
||
|
||
const steps: Record<string, unknown>[] = [];
|
||
|
||
// ① 建实体(主子表则为主实体)
|
||
steps.push({
|
||
title: '① 建实体',
|
||
subTitle: isMD ? '主表元数据' : '元数据 + 物理表',
|
||
hint: isTree
|
||
? '树形请在字段里加 PARENTID 列(自引用父节点);标准列后端自动补。'
|
||
: '标准列(主键/审计/版本)后端自动补齐,无需在此添加。',
|
||
api: { method: 'post', url: '/api/meta/entity', responseData: { masterEntityId: '${entityId}', masterTable: '${tablename}' } },
|
||
body: [
|
||
{
|
||
type: 'input-text', name: 'tablename', label: isMD ? '主表名' : '表名', required: true, placeholder: 'OA_DEMO',
|
||
description: '大写字母开头,仅大写字母/数字/下划线,3–41 位',
|
||
validations: { matchRegexp: '/^[A-Z][A-Z0-9_]{2,40}$/' },
|
||
validationErrors: { matchRegexp: '表名格式不合法(大写字母开头,3–41 位)' },
|
||
},
|
||
{ type: 'input-text', name: 'name', label: '显示名', placeholder: '演示(可选)' },
|
||
{
|
||
type: 'select', name: 'entitytype', label: '所属分类', required: true,
|
||
source: 'get:/api/meta/entity-types', labelField: 'name', valueField: 'id',
|
||
searchable: true, clearable: true,
|
||
description: isMD ? '主表与子表共用此分类' : '实体归类(SYS_ENTITYTYPE),决定新功能挂在哪个功能域',
|
||
},
|
||
attrCombo('attributes', '加字段'),
|
||
],
|
||
});
|
||
|
||
// ② 建子表(仅主子表)
|
||
if (isMD) {
|
||
steps.push({
|
||
title: '② 建子表',
|
||
subTitle: '明细',
|
||
hint: '子表自动加一列 MASTERID 指向主表(回链外键),无需手动添加。',
|
||
api: {
|
||
method: 'post', url: '/api/meta/entity',
|
||
data: {
|
||
tablename: '${childTablename}', name: '${childName}', entitytype: '${entitytype}',
|
||
tabletype: 1, masterTable: '${masterTable}', attributes: '${childAttributes}',
|
||
},
|
||
},
|
||
body: [
|
||
{ type: 'static', name: 'masterTable', label: '主表' },
|
||
{
|
||
type: 'input-text', name: 'childTablename', label: '子表名', required: true, placeholder: 'OA_PURCHASE_ITEM',
|
||
validations: { matchRegexp: '/^[A-Z][A-Z0-9_]{2,40}$/' }, validationErrors: { matchRegexp: '表名格式不合法' },
|
||
},
|
||
{ type: 'input-text', name: 'childName', label: '子表显示名', placeholder: '采购明细' },
|
||
attrCombo('childAttributes', '加子表字段'),
|
||
],
|
||
});
|
||
}
|
||
|
||
// ③ 生成页面(主子表多派生/汇总字段)
|
||
const genBody: Record<string, unknown>[] = [
|
||
{ type: 'static', name: 'masterTable', label: isMD ? '主表' : '实体表' },
|
||
];
|
||
if (isMD) {
|
||
genBody.push({ type: 'static', label: '子表', tpl: '${childTablename}' });
|
||
}
|
||
genBody.push(
|
||
{ type: 'input-text', name: 'title', label: '页面标题', required: true, placeholder: '如:客户等级维护' },
|
||
{ type: 'input-text', name: 'displayField', label: '显示字段', placeholder: '可选,默认取实体显示列' },
|
||
);
|
||
if (!isMD) {
|
||
genBody.push({ type: 'input-number', name: 'pageSize', label: '每页条数', placeholder: '可选,默认 20', min: 1, max: 200 });
|
||
}
|
||
if (isMD) {
|
||
genBody.push(
|
||
{
|
||
type: 'static', label: '可用子表列', tpl: '${childAttributes|pick:column|join:"、"}',
|
||
visibleOn: '${childAttributes && childAttributes.length}',
|
||
},
|
||
{
|
||
type: 'combo', name: 'childDerived', label: '派生字段', multiple: true,
|
||
addable: true, removable: true, addButtonText: '加派生字段', minLength: 0,
|
||
description: '可选。如 金额=数量×单价:目标列填 MONEY、公式填 QTY*PRICE。'
|
||
+ '公式仅用子表列名与 + - * / ( ),落库前服务端逐行权威求算,单元格实时预览。',
|
||
items: [
|
||
{ type: 'input-text', name: 'column', label: '目标列', required: true, placeholder: 'MONEY',
|
||
validations: { matchRegexp: '/^[A-Z][A-Z0-9_]{0,40}$/' },
|
||
validationErrors: { matchRegexp: '须为大写字母开头的子表真实列' } },
|
||
{ type: 'input-text', name: 'formula', label: '公式', required: true, placeholder: 'QTY*PRICE' },
|
||
],
|
||
},
|
||
{
|
||
type: 'combo', name: 'childRollup', label: '汇总字段', multiple: true,
|
||
addable: true, removable: true, addButtonText: '加汇总字段', minLength: 0,
|
||
description: '可选。如 申请总额=明细金额之和:主表列填 TOTAL、子表列填 AMOUNT、函数选 SUM。'
|
||
+ '主表列须为主实体真实列、子表列须为子表真实列;落库前服务端权威聚合,主表单只读实时预览。',
|
||
items: [
|
||
{ type: 'input-text', name: 'masterColumn', label: '主表列', required: true, placeholder: 'TOTAL',
|
||
validations: { matchRegexp: '/^[A-Z][A-Z0-9_]{0,40}$/' },
|
||
validationErrors: { matchRegexp: '须为大写字母开头的主表真实列' } },
|
||
{ type: 'input-text', name: 'childColumn', label: '子表列', required: true, placeholder: 'AMOUNT',
|
||
validations: { matchRegexp: '/^[A-Z][A-Z0-9_]{0,40}$/' },
|
||
validationErrors: { matchRegexp: '须为大写字母开头的子表真实列' } },
|
||
{ type: 'select', name: 'agg', label: '函数', value: 'SUM', options: [
|
||
{ label: '求和 SUM', value: 'SUM' }, { label: '计数 COUNT', value: 'COUNT' },
|
||
{ label: '平均 AVG', value: 'AVG' }, { label: '最小 MIN', value: 'MIN' },
|
||
{ label: '最大 MAX', value: 'MAX' }] },
|
||
],
|
||
},
|
||
{
|
||
type: 'combo', name: 'childLinkage', label: '引用联动', multiple: true,
|
||
addable: true, removable: true, addButtonText: '加引用联动', minLength: 0,
|
||
description: '可选。子表「引用列」(类型=引用)选中某条记录时,自动带出该记录的其它列到同行子列。'
|
||
+ '如 选物料→带出单价/规格:引用列填 ITEMID(须为引用类型子列);带出映射 目标子列 ITEMPRICE ← 源列 PRICE。'
|
||
+ '选中时实时填充,提交保存所填值。',
|
||
items: [
|
||
{ type: 'input-text', name: 'refColumn', label: '引用列', required: true, placeholder: 'ITEMID',
|
||
columnClassName: 'w-sm', validations: { matchRegexp: '/^[A-Z][A-Z0-9_]{0,40}$/' },
|
||
validationErrors: { matchRegexp: '须为大写字母开头的引用类型子列' } },
|
||
{
|
||
type: 'combo', name: 'fills', label: '带出映射', multiple: true,
|
||
addable: true, removable: true, addButtonText: '加带出', minLength: 1,
|
||
items: [
|
||
{ type: 'input-text', name: 'target', label: '目标子列', required: true, placeholder: 'ITEMPRICE',
|
||
validations: { matchRegexp: '/^[A-Z][A-Z0-9_]{0,40}$/' },
|
||
validationErrors: { matchRegexp: '须为大写字母开头的子表真实列' } },
|
||
{ type: 'input-text', name: 'source', label: '源列(引用实体)', required: true, placeholder: 'PRICE',
|
||
validations: { matchRegexp: '/^[A-Z][A-Z0-9_]{0,40}$/' },
|
||
validationErrors: { matchRegexp: '须为大写字母开头的引用实体列' } },
|
||
],
|
||
},
|
||
],
|
||
},
|
||
);
|
||
}
|
||
const compileData: Record<string, unknown> = {
|
||
tableName: '${masterTable}', recipe, title: '${title}', displayField: '${displayField}',
|
||
};
|
||
if (isMD) {
|
||
Object.assign(compileData, {
|
||
childTable: '${childTablename}', linkColumn: 'MASTERID', listColumn: 'DETAILS', listLabel: '${childName}',
|
||
childDerived: '${childDerived}', childRollup: '${childRollup}', childLinkage: '${childLinkage}',
|
||
});
|
||
} else {
|
||
compileData.pageSize = '${pageSize}';
|
||
}
|
||
steps.push({
|
||
title: isMD ? '③ 生成主子页' : '③ 生成页面',
|
||
subTitle: '编译落标准层',
|
||
api: { method: 'post', url: '/api/design/recipe/compile', data: compileData, responseData: { templateId: '${templateId}' } },
|
||
body: genBody,
|
||
});
|
||
|
||
// ④ 发布
|
||
steps.push({
|
||
title: '④ 发布',
|
||
subTitle: '五流 + 菜单 + 部署包',
|
||
api: {
|
||
method: 'post', url: '/api/design/publish',
|
||
data: {
|
||
entityId: '${masterEntityId}', tableName: '${masterTable}', templateId: '${templateId}',
|
||
menuName: '${menuName}', generateDeploymentPackage: true,
|
||
},
|
||
},
|
||
body: [
|
||
{ type: 'static', name: 'templateId', label: '已生成模板' },
|
||
{ type: 'input-text', name: 'menuName', label: '菜单名', required: true, value: '${title}' },
|
||
{
|
||
type: 'button', label: '进设计器微调', level: 'enhance', className: 'mb-2',
|
||
actionType: 'url', url: '/editor.html?templateId=${templateId}', blank: true, visibleOn: '${templateId}',
|
||
},
|
||
{
|
||
type: 'alert', level: 'info',
|
||
body: '发布 = 写标准页 + 触发默认服务流 + 注册菜单(当前环境立即可用),并生成生产部署包供人审上线(发布恒人审)。'
|
||
+ (isMD ? '主子表的子行经主表 _ADD 流随主记录级联落库。' : ''),
|
||
},
|
||
],
|
||
});
|
||
|
||
return {
|
||
type: 'page',
|
||
className: 'h-full',
|
||
data: { childDerived: [], childRollup: [], childLinkage: [] },
|
||
body: {
|
||
type: 'wizard',
|
||
mode: 'horizontal',
|
||
actionPrevLabel: '上一步',
|
||
actionNextLabel: '下一步',
|
||
actionNextSaveLabel: '保存并下一步',
|
||
actionFinishLabel: '完成(发布)',
|
||
renderStepIndicator: true,
|
||
steps,
|
||
onEvent: {
|
||
finished: {
|
||
actions: [{ actionType: 'toast', args: { msgType: 'success', msg: '已发布:菜单已注册,可在左侧菜单查看新功能。' } }],
|
||
},
|
||
},
|
||
},
|
||
};
|
||
}
|
||
|
||
/** 页面类型选择卡。 */
|
||
function RecipeChooser({ onPick }: { onPick: (recipe: string) => void }) {
|
||
return (
|
||
<div data-testid="recipe-chooser" style={{ display: 'flex', gap: 16, flexWrap: 'wrap', maxWidth: 980 }}>
|
||
{RECIPE_CARDS.map((c) => (
|
||
<button
|
||
key={c.recipe}
|
||
data-testid={c.testid}
|
||
onClick={() => onPick(c.recipe)}
|
||
style={{
|
||
flex: '1 1 280px', minWidth: 260, textAlign: 'left', cursor: 'pointer',
|
||
background: 'var(--surface)', border: '1px solid var(--line)', borderRadius: 10,
|
||
padding: '18px 20px', display: 'flex', flexDirection: 'column', gap: 8,
|
||
}}
|
||
>
|
||
<span style={{ fontSize: 16, fontWeight: 600, color: 'var(--text)' }}>{c.title}</span>
|
||
<span style={{ fontSize: 13, color: 'var(--text-3)', lineHeight: 1.6 }}>{c.desc}</span>
|
||
</button>
|
||
))}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
export function NewFeatureShell() {
|
||
const navigate = useNavigate();
|
||
const profile = getProfile();
|
||
const isAdmin = isAdminProfile(profile);
|
||
const [recipe, setRecipe] = useState<string | null>(null);
|
||
const schema = useMemo(() => (recipe ? buildWizardSchema(recipe) : null), [recipe]);
|
||
|
||
if (!isAdmin) {
|
||
return <div style={{ padding: 24, color: 'var(--text-3)' }}>新建功能仅管理员可用。</div>;
|
||
}
|
||
|
||
return (
|
||
<div style={{ height: '100%', display: 'flex', flexDirection: 'column' }} data-testid="new-feature-shell">
|
||
<div style={{ display: 'flex', alignItems: 'center', gap: 12, padding: 'var(--pad-lg)', paddingBottom: 8 }}>
|
||
<button className="btn btn-sm" onClick={() => (recipe ? setRecipe(null) : navigate('/design-center'))}>
|
||
{recipe ? '← 重选页面类型' : '← 返回设计中心'}
|
||
</button>
|
||
<h2 style={{ margin: 0, fontSize: 18, fontWeight: 600 }}>新建功能 · 人走五环</h2>
|
||
</div>
|
||
<div style={{ flex: 1, minHeight: 0, overflow: 'auto', padding: '0 var(--pad-lg) var(--pad-lg)' }}>
|
||
{schema ? (
|
||
<AmisRenderer schema={schema} />
|
||
) : (
|
||
<>
|
||
<div className="sub-text" style={{ marginBottom: 14 }}>先选页面类型,再走「建实体 → 生成页面 → 发布」。</div>
|
||
<RecipeChooser onPick={setRecipe} />
|
||
</>
|
||
)}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|