page.tsx
8.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"use client";
import { useEffect, useState } from "react";
import { ChevronDown, ChevronRight, Link2, RefreshCw } from "lucide-react";
import Perm from "@/components/Perm";
import { Card, GhostButton, SearchInput, Spinner, SysPage } from "@/components/system/kit";
import { listOaSources, syncOaSources, type OaSource } from "@/lib/auth";
import { SourceType, SourceTypeLabel } from "@/lib/oaEnums";
import { cn } from "@/lib/utils";
const TYPE_COLOR: Record<string, string> = {
M: "bg-slate-100 text-slate-600",
C: "bg-sky-100 text-sky-700",
P: "bg-violet-100 text-violet-700",
F: "bg-amber-100 text-amber-700",
A: "bg-emerald-100 text-emerald-700",
};
const TYPE_HINT: Record<string, string> = {
M: "目录:仅分组,无权限点",
C: "菜单:对应一个可访问页面路由",
P: "页面:嵌入式面板(无独立路由)",
F: "按钮:页面内操作权限点",
A: "接口:后端数据权限点 / 系统接口",
};
const FILTER_TYPES = [SourceType.DIR, SourceType.MENU, SourceType.PAGE, SourceType.BUTTON, SourceType.API];
// 列宽:名称适当收窄,把空间让给权限标识与路由列
const GRID = "grid grid-cols-[minmax(0,1.4fr)_5rem_minmax(0,1.3fr)_minmax(0,1.1fr)] items-center gap-2";
/** 单节点是否命中当前筛选(类型 + 关键词,名称/perm/路由/类型名任一包含)。 */
function nodeMatch(node: OaSource, types: Set<string>, q: string): boolean {
if (types.size > 0 && !types.has(node.type)) return false;
const s = q.trim().toLowerCase();
if (!s) return true;
return (
node.name.toLowerCase().includes(s) ||
(node.perm ?? "").toLowerCase().includes(s) ||
(node.path ?? "").toLowerCase().includes(s) ||
(SourceTypeLabel[node.type] ?? "").includes(q.trim())
);
}
/** 子树内是否有命中节点(含自身),用于保留层级结构。 */
function subtreeVisible(node: OaSource, types: Set<string>, q: string): boolean {
if (nodeMatch(node, types, q)) return true;
return (node.children ?? []).some((c) => subtreeVisible(c, types, q));
}
export default function SourcePage() {
const [tree, setTree] = useState<OaSource[]>([]);
const [loading, setLoading] = useState(true);
const [open, setOpen] = useState<Set<string>>(new Set());
const [types, setTypes] = useState<Set<string>>(new Set());
const [q, setQ] = useState("");
const [notice, setNotice] = useState("");
const [syncing, setSyncing] = useState(false);
const filterActive = types.size > 0 || q.trim().length > 0;
const loadTree = () => {
setLoading(true);
listOaSources()
.then((d) => { setTree(d.items); setOpen(new Set(d.items.map((x) => x.id))); })
.finally(() => setLoading(false));
};
useEffect(loadTree, []);
const flash = (m: string) => (setNotice(m), setTimeout(() => setNotice(""), 3500));
const toggle = (id: string) =>
setOpen((s) => { const n = new Set(s); n.has(id) ? n.delete(id) : n.add(id); return n; });
const toggleType = (t: string) =>
setTypes((s) => { const n = new Set(s); n.has(t) ? n.delete(t) : n.add(t); return n; });
const doSync = async () => {
setSyncing(true);
try {
const r = await syncOaSources();
flash(`同步完成:前端资源 + 后端接口共 ${r.count} 个节点(其中接口 ${r.interfaces} 个)`);
loadTree();
} catch (e) {
flash(e instanceof Error ? e.message : "同步失败");
} finally {
setSyncing(false);
}
};
// 筛选/搜索生效时把命中分支的容器节点补进展开集(默认展开),此后用户仍可手动折叠——
// 展开态统一以 open 为准,不再靠 filterActive 强制撑开,故折叠按钮永远可用。
useEffect(() => {
if (!filterActive) return;
const ids = new Set<string>();
const walk = (nodes: OaSource[]) => {
for (const n of nodes) {
const kids = n.children ?? [];
if (kids.length > 0 && subtreeVisible(n, types, q)) ids.add(n.id);
if (kids.length) walk(kids);
}
};
walk(tree);
setOpen((prev) => new Set([...prev, ...ids]));
}, [types, q, tree, filterActive]);
return (
<SysPage
title="资源权限"
subtitle="系统页面 / 按钮 / 数据权限点由代码统一登记;新增功能后点“同步资源”即自动补全"
actions={
<Perm code="oa:source:list">
<GhostButton onClick={doSync} disabled={syncing}>
<RefreshCw className={cn("h-4 w-4", syncing && "animate-spin")} /> 同步资源
</GhostButton>
</Perm>
}
>
{notice && <div className="shrink-0 rounded-lg bg-emerald-50 px-3 py-2 text-sm text-emerald-700">{notice}</div>}
{/* 搜索 + 类型筛选(筛选/搜索后仍按树结构展示) */}
<div className="flex shrink-0 flex-wrap items-center gap-x-4 gap-y-2 rounded-xl border border-slate-200 bg-white px-4 py-3 text-sm text-slate-500">
<SearchInput value={q} onChange={setQ} placeholder="搜索名称 / 类型 / 权限标识 / 路由" className="w-72" />
<span className="text-xs font-semibold text-slate-400">类型:</span>
{FILTER_TYPES.map((t) => (
<button
key={t}
onClick={() => toggleType(t)}
title={TYPE_HINT[t]}
className={cn(
"rounded-lg px-2.5 py-1 text-sm font-medium transition",
types.has(t) ? "bg-blue-600 text-white shadow-sm shadow-blue-500/25" : cn(TYPE_COLOR[t], "hover:opacity-80")
)}
>
{SourceTypeLabel[t]}
</button>
))}
{filterActive && (
<button onClick={() => { setTypes(new Set()); setQ(""); }} className="text-sm text-slate-400 underline-offset-2 hover:text-blue-600 hover:underline">
清除
</button>
)}
</div>
<Card>
<div className={cn(GRID, "shrink-0 border-b border-blue-100 bg-blue-50/60 px-4 py-3 text-sm font-semibold text-slate-600")}>
<span>名称</span>
<span>类型</span>
<span>权限标识(perm)</span>
<span>路由(path)</span>
</div>
<div className="min-h-0 flex-1 overflow-auto pb-4">
{loading ? <Spinner /> : (
<ul>
{tree.map((n) => (
<SourceRow key={n.id} node={n} depth={0} open={open} onToggle={toggle} types={types} q={q} />
))}
</ul>
)}
</div>
</Card>
</SysPage>
);
}
function TypeBadge({ type }: { type: string }) {
return <span className={cn("rounded px-1.5 py-0.5 text-xs", TYPE_COLOR[type])}>{SourceTypeLabel[type]}</span>;
}
function SourceRow({
node, depth, open, onToggle, types, q,
}: {
node: OaSource;
depth: number;
open: Set<string>;
onToggle: (id: string) => void;
types: Set<string>;
q: string;
}) {
const active = types.size > 0 || q.trim().length > 0;
// 筛选/搜索生效时,隐藏整条无命中节点的分支
if (active && !subtreeVisible(node, types, q)) return null;
const kids = node.children ?? [];
const hasChild = kids.length > 0;
const isOpen = open.has(node.id);
const dim = active && !nodeMatch(node, types, q); // 仅作层级支撑的祖先节点淡化
return (
<li>
<div
onClick={() => hasChild && onToggle(node.id)}
className={cn(
GRID,
"border-b border-slate-50 px-4 py-2 text-sm",
hasChild && "cursor-pointer hover:bg-blue-50/40"
)}
>
<span className="flex items-center gap-1" style={{ paddingLeft: depth * 16 }}>
{hasChild ? (isOpen ? <ChevronDown className="h-4 w-4 shrink-0 text-slate-400" /> : <ChevronRight className="h-4 w-4 shrink-0 text-slate-400" />) : <span className="w-4 shrink-0" />}
<span className={cn("truncate font-medium", dim ? "text-slate-400" : "text-slate-700")}>{node.name}</span>
</span>
<span><TypeBadge type={node.type} /></span>
<span className="truncate text-xs text-slate-500">{node.perm ? <code>{node.perm}</code> : <span className="text-slate-300">—</span>}</span>
<span className="truncate text-xs text-blue-500">
{node.path ? <span className="inline-flex items-center gap-1"><Link2 className="h-3 w-3 shrink-0" />{node.path}</span> : ""}
</span>
</div>
{hasChild && isOpen && (
<ul>{kids.map((c) => <SourceRow key={c.id} node={c} depth={depth + 1} open={open} onToggle={onToggle} types={types} q={q} />)}</ul>
)}
</li>
);
}