@svadmin/ui 0.129.0 → 0.131.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/dist/app.css +403 -0
  2. package/dist/app.theme.css +403 -0
  3. package/dist/components/AboutSettings.svelte +1 -1
  4. package/dist/components/AccessRevocationMatrix.svelte +425 -0
  5. package/dist/components/AccessRevocationMatrix.svelte.d.ts +27 -0
  6. package/dist/components/AudioVisualInspector.svelte +458 -0
  7. package/dist/components/AudioVisualInspector.svelte.d.ts +53 -0
  8. package/dist/components/AuditSnapshotCapture.svelte +328 -0
  9. package/dist/components/AuditSnapshotCapture.svelte.d.ts +28 -0
  10. package/dist/components/CannedResponsePicker.svelte +257 -0
  11. package/dist/components/CannedResponsePicker.svelte.d.ts +22 -0
  12. package/dist/components/ContentModerationCenter.svelte +166 -0
  13. package/dist/components/ContentModerationCenter.svelte.d.ts +11 -0
  14. package/dist/components/ContractClauseDiffViewer.svelte +566 -0
  15. package/dist/components/ContractClauseDiffViewer.svelte.d.ts +53 -0
  16. package/dist/components/DataImportPipeline.svelte +577 -0
  17. package/dist/components/DataImportPipeline.svelte.d.ts +35 -0
  18. package/dist/components/DataRetentionPolicyEditor.svelte +455 -0
  19. package/dist/components/DataRetentionPolicyEditor.svelte.d.ts +10 -0
  20. package/dist/components/ElectronicSealInspector.svelte +450 -0
  21. package/dist/components/ElectronicSealInspector.svelte.d.ts +72 -0
  22. package/dist/components/EmployeeOffboardingAuditor.svelte +405 -0
  23. package/dist/components/EmployeeOffboardingAuditor.svelte.d.ts +39 -0
  24. package/dist/components/FeatureFlagStudio.svelte +583 -0
  25. package/dist/components/FeatureFlagStudio.svelte.d.ts +12 -0
  26. package/dist/components/FieldMappingBuilder.svelte +393 -0
  27. package/dist/components/FieldMappingBuilder.svelte.d.ts +39 -0
  28. package/dist/components/GatewayRouteEditor.svelte +493 -0
  29. package/dist/components/GatewayRouteEditor.svelte.d.ts +34 -0
  30. package/dist/components/HotkeysHelpModal.svelte +286 -0
  31. package/dist/components/HotkeysHelpModal.svelte.d.ts +21 -0
  32. package/dist/components/I18nStudio.svelte +635 -0
  33. package/dist/components/I18nStudio.svelte.d.ts +23 -0
  34. package/dist/components/IpAccessRuleEditor.svelte +290 -0
  35. package/dist/components/IpAccessRuleEditor.svelte.d.ts +19 -0
  36. package/dist/components/IpWhitelistDrawer.svelte +94 -0
  37. package/dist/components/IpWhitelistDrawer.svelte.d.ts +10 -0
  38. package/dist/components/MediaAuthenticityAuditCard.svelte +357 -0
  39. package/dist/components/MediaAuthenticityAuditCard.svelte.d.ts +64 -0
  40. package/dist/components/MediaGallery.svelte +575 -0
  41. package/dist/components/MediaGallery.svelte.d.ts +34 -0
  42. package/dist/components/NotificationCenter.svelte +20 -1
  43. package/dist/components/NotificationCenter.svelte.d.ts +2 -0
  44. package/dist/components/NotificationDrawer.svelte +242 -0
  45. package/dist/components/NotificationDrawer.svelte.d.ts +40 -0
  46. package/dist/components/PrintDocument.svelte +95 -0
  47. package/dist/components/PrintDocument.svelte.d.ts +10 -0
  48. package/dist/components/PrintPreviewModal.svelte +52 -0
  49. package/dist/components/PrintPreviewModal.svelte.d.ts +11 -0
  50. package/dist/components/PrintTemplateDesigner.svelte +280 -0
  51. package/dist/components/PrintTemplateDesigner.svelte.d.ts +11 -0
  52. package/dist/components/RateLimitPolicyPanel.svelte +369 -0
  53. package/dist/components/RateLimitPolicyPanel.svelte.d.ts +29 -0
  54. package/dist/components/ResizableSplitter.svelte +246 -0
  55. package/dist/components/ResizableSplitter.svelte.d.ts +18 -0
  56. package/dist/components/SensitiveWordAuditor.svelte +368 -0
  57. package/dist/components/SensitiveWordAuditor.svelte.d.ts +9 -0
  58. package/dist/components/ServiceDeskTicketCard.svelte +297 -0
  59. package/dist/components/ServiceDeskTicketCard.svelte.d.ts +52 -0
  60. package/dist/components/ShiftScheduler.svelte +490 -0
  61. package/dist/components/ShiftScheduler.svelte.d.ts +50 -0
  62. package/dist/components/SslCertificateMonitorCard.svelte +309 -0
  63. package/dist/components/SslCertificateMonitorCard.svelte.d.ts +30 -0
  64. package/dist/components/SsoConnectionStudio.svelte +784 -0
  65. package/dist/components/SsoConnectionStudio.svelte.d.ts +44 -0
  66. package/dist/components/SsoSpMetadataPanel.svelte +44 -0
  67. package/dist/components/SsoSpMetadataPanel.svelte.d.ts +8 -0
  68. package/dist/components/StepUpAuthModal.svelte +448 -0
  69. package/dist/components/StepUpAuthModal.svelte.d.ts +26 -0
  70. package/dist/components/TrafficTopologyInspector.svelte +361 -0
  71. package/dist/components/TrafficTopologyInspector.svelte.d.ts +43 -0
  72. package/dist/components/ip-access-rules.d.ts +24 -0
  73. package/dist/components/ip-access-rules.js +110 -0
  74. package/dist/components/moderation-utils.d.ts +45 -0
  75. package/dist/components/moderation-utils.js +74 -0
  76. package/dist/components/print-document.d.ts +3 -0
  77. package/dist/components/print-document.js +65 -0
  78. package/dist/components/print-utils.d.ts +44 -0
  79. package/dist/components/print-utils.js +68 -0
  80. package/dist/components/retention-utils.d.ts +31 -0
  81. package/dist/components/retention-utils.js +38 -0
  82. package/dist/components/sso-connection-presets.d.ts +2 -0
  83. package/dist/components/sso-connection-presets.js +59 -0
  84. package/dist/components/ui/sheet/sheet.svelte +3 -1
  85. package/dist/components/ui/sheet/sheet.svelte.d.ts +1 -0
  86. package/dist/components/useFeatureFlag.d.svelte.ts +1 -0
  87. package/dist/components/useFeatureFlag.svelte.d.ts +45 -0
  88. package/dist/components/useFeatureFlag.svelte.js +94 -0
  89. package/dist/index.d.ts +35 -0
  90. package/dist/index.js +53 -0
  91. package/package.json +5 -4
@@ -0,0 +1,280 @@
1
+ <script lang="ts">
2
+ import { untrack } from 'svelte';
3
+ import type { PrintTemplateConfig } from './print-utils.js';
4
+ import { printSampleItems } from './print-utils.js';
5
+ import { printDocument } from './print-document.js';
6
+ import PrintDocument from './PrintDocument.svelte';
7
+
8
+ export interface PrintTemplateDesignerProps {
9
+ config?: Partial<PrintTemplateConfig>;
10
+ sampleData?: Record<string, unknown>;
11
+ class?: string;
12
+ onSave?: (config: PrintTemplateConfig) => void;
13
+ onPreview?: (config: PrintTemplateConfig) => void;
14
+ }
15
+
16
+ const {
17
+ config: initialConfig = {},
18
+ sampleData = {},
19
+ class: className = '',
20
+ onSave,
21
+ onPreview,
22
+ }: PrintTemplateDesignerProps = $props();
23
+
24
+ // Reactive state
25
+ let config = $state<PrintTemplateConfig>(untrack(() => ({
26
+ id: initialConfig.id || 'template-receipt-standard',
27
+ name: initialConfig.name || '标准销售出库发货单',
28
+ title: initialConfig.title || '商品发货单 / 出库凭证',
29
+ subTitle: initialConfig.subTitle || 'SHIPPING & WAREHOUSE DISPATCH NOTE',
30
+ companyName: initialConfig.companyName || '极速智能科技 (北京) 有限责任公司',
31
+ paperSize: initialConfig.paperSize || 'A4',
32
+ showBarcode: initialConfig.showBarcode ?? true,
33
+ showQrCode: initialConfig.showQrCode ?? true,
34
+ showWatermark: initialConfig.showWatermark ?? false,
35
+ watermarkText: initialConfig.watermarkText || '正品保证 · 内部凭据',
36
+ showAmountInWords: initialConfig.showAmountInWords ?? true,
37
+ currencySymbol: initialConfig.currencySymbol || '¥',
38
+ metaFields: initialConfig.metaFields || [
39
+ { id: 'f-1', label: '单据编号', field: 'order_no', value: 'CK-20261115-0089' },
40
+ { id: 'f-2', label: '出库日期', field: 'dispatch_date', value: '2026-11-15' },
41
+ { id: 'f-3', label: '客户名称', field: 'customer_name', value: '未来科技有限公司' },
42
+ { id: 'f-4', label: '出库仓库', field: 'warehouse', value: '华东一号中心仓 (A-02)' },
43
+ { id: 'f-5', label: '联系电话', field: 'phone', value: '138-0000-8888' },
44
+ { id: 'f-6', label: '结算方式', field: 'payment_method', value: '银行对公转账 / 月结' },
45
+ ],
46
+ columns: initialConfig.columns || [
47
+ { id: 'c-1', label: '序号', field: 'index', width: '40px', align: 'center', visible: true },
48
+ { id: 'c-2', label: '商品编号', field: 'sku', width: '90px', align: 'left', visible: true },
49
+ { id: 'c-3', label: '商品/物料名称', field: 'name', width: 'auto', align: 'left', visible: true },
50
+ { id: 'c-4', label: '规格型号', field: 'spec', width: '80px', align: 'center', visible: true },
51
+ { id: 'c-5', label: '数量', field: 'quantity', width: '60px', align: 'right', visible: true },
52
+ { id: 'c-6', label: '单价', field: 'unit_price', width: '70px', align: 'right', visible: true },
53
+ { id: 'c-7', label: '金额 (元)', field: 'total_amount', width: '80px', align: 'right', visible: true },
54
+ { id: 'c-8', label: '备注', field: 'remark', width: '80px', align: 'left', visible: false },
55
+ ],
56
+ signers: initialConfig.signers || ['制单人: 张三', '仓管员: 李四', '复核人: 王五', '提货人(签字): _______'],
57
+ copies: initialConfig.copies || ['第一联: 存根联 (白)', '第二联: 客户联 (红)', '第三联: 财务联 (黄)'],
58
+ footerNotes: initialConfig.footerNotes || '特别提示: 货物送达后请现场当面验收并在提货联签字确认,如有异议请在24小时内联系客服。',
59
+ })));
60
+
61
+ // Active view tabs
62
+ let activeTab = $state<'DESIGN' | 'PREVIEW'>('PREVIEW');
63
+ let activeCopyIndex = $state<number>(0);
64
+
65
+ const sampleItems = $derived(printSampleItems(sampleData));
66
+ let pages = $state<HTMLDivElement>();
67
+ let printing = $state(false);
68
+ let printError = $state('');
69
+
70
+ async function handlePrint() {
71
+ if (printing || !pages) return;
72
+ printing = true;
73
+ printError = '';
74
+ try { await printDocument(pages, config.paperSize); }
75
+ catch { printError = '打印失败,请检查浏览器打印权限后重试'; }
76
+ finally { printing = false; }
77
+ }
78
+
79
+ function handleSave() {
80
+ onSave?.($state.snapshot(config));
81
+ }
82
+
83
+ function handlePreview() {
84
+ onPreview?.($state.snapshot(config));
85
+ }
86
+
87
+ function addColumn() {
88
+ const id = `col-${Date.now()}`;
89
+ config.columns = [
90
+ ...config.columns,
91
+ { id, label: '新增列', field: 'custom_field', width: '80px', align: 'left', visible: true },
92
+ ];
93
+ }
94
+
95
+ function removeColumn(id: string) {
96
+ config.columns = config.columns.filter(c => c.id !== id);
97
+ }
98
+ </script>
99
+
100
+ <div
101
+ class="w-full flex flex-col gap-6 p-6 bg-background text-foreground rounded-xl border border-border shadow-sm {className}"
102
+ data-svadmin-component="PrintTemplateDesigner"
103
+ data-svadmin-resource="print_templates"
104
+ >
105
+ <!-- Top Navigation & Action Controls -->
106
+ <div class="flex flex-col md:flex-row md:items-center justify-between gap-4 pb-4 border-b border-border">
107
+ <div class="flex flex-col gap-1">
108
+ <div class="flex items-center gap-2">
109
+ <h2 class="text-xl font-bold tracking-tight text-foreground">单据套打与模板打印设计器 (Print Studio)</h2>
110
+ <span class="text-xs px-2 py-0.5 rounded-full font-medium bg-primary/10 text-primary border border-primary/20">
111
+ Enterprise Print
112
+ </span>
113
+ </div>
114
+ <p class="text-sm text-muted-foreground">
115
+ 定制出入库单、仓储面单、财务三联单标准规范,内置大写金额自动转换与高保真分页防截断打印。
116
+ </p>
117
+ </div>
118
+
119
+ <div class="flex items-center gap-2">
120
+ <!-- Tabs -->
121
+ <div class="inline-flex rounded-lg border border-border p-1 bg-muted/40">
122
+ <button
123
+ type="button"
124
+ onclick={() => (activeTab = 'PREVIEW')}
125
+ class="text-xs px-3 py-1.5 rounded-md font-medium transition-colors {activeTab === 'PREVIEW' ? 'bg-card text-foreground shadow-sm' : 'text-muted-foreground hover:text-foreground'}"
126
+ >
127
+ 单据实效预览
128
+ </button>
129
+ <button
130
+ type="button"
131
+ onclick={() => (activeTab = 'DESIGN')}
132
+ class="text-xs px-3 py-1.5 rounded-md font-medium transition-colors {activeTab === 'DESIGN' ? 'bg-card text-foreground shadow-sm' : 'text-muted-foreground hover:text-foreground'}"
133
+ >
134
+ 属性与版式配置
135
+ </button>
136
+ </div>
137
+
138
+ <button
139
+ type="button"
140
+ onclick={handlePrint}
141
+ disabled={printing}
142
+ class="inline-flex items-center justify-center text-xs font-semibold px-3 py-2 rounded-lg border border-border bg-card text-card-foreground hover:bg-muted transition-colors shadow-sm"
143
+ >
144
+ <svg class="w-3.5 h-3.5 mr-1.5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
145
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z" />
146
+ </svg>
147
+ 调起系统打印
148
+ </button>
149
+
150
+ <button
151
+ type="button"
152
+ onclick={handlePreview}
153
+ class="inline-flex items-center justify-center text-xs font-semibold px-3 py-2 rounded-lg border border-border bg-card text-card-foreground hover:bg-muted transition-colors shadow-sm"
154
+ >
155
+ 预览模板
156
+ </button>
157
+
158
+ <button
159
+ type="button"
160
+ onclick={handleSave}
161
+ class="inline-flex items-center justify-center text-xs font-semibold px-4 py-2 rounded-lg bg-primary text-primary-foreground hover:bg-primary/90 transition-colors shadow-sm"
162
+ >
163
+ 保存模板
164
+ </button>
165
+ </div>
166
+ </div>
167
+
168
+ <!-- Main Work Area -->
169
+ <div class="flex flex-col lg:flex-row gap-6">
170
+ <!-- Left: Designer Configuration Panel (Shown when DESIGN or on side) -->
171
+ {#if activeTab === 'DESIGN'}
172
+ <div class="w-full lg:w-96 flex flex-col gap-4 p-4 rounded-xl border border-border bg-card text-xs">
173
+ <h3 class="font-bold text-sm text-foreground border-b border-border pb-2">版式与基本参数</h3>
174
+
175
+ <!-- Paper Size -->
176
+ <div class="flex flex-col gap-1.5">
177
+ <label for="paper-size-select" class="font-semibold text-foreground">纸张规格</label>
178
+ <select
179
+ id="paper-size-select"
180
+ bind:value={config.paperSize}
181
+ class="px-2.5 py-1.5 rounded-lg border border-border bg-background text-foreground"
182
+ >
183
+ <option value="A4">A4 纵向 (210 × 297 mm)</option>
184
+ <option value="A4_LANDSCAPE">A4 横向 (297 × 210 mm)</option>
185
+ <option value="TRIPLICATE_241X140">针式三联打孔单 (241 × 140 mm)</option>
186
+ <option value="SHIPPING_100X180">仓储物流面单 (100 × 180 mm)</option>
187
+ <option value="RECEIPT_80MM">热敏小票 (80 mm)</option>
188
+ </select>
189
+ </div>
190
+
191
+ <!-- Titles -->
192
+ <div class="flex flex-col gap-1.5">
193
+ <label for="company-name-input" class="font-semibold text-foreground">企业抬头名称</label>
194
+ <input
195
+ id="company-name-input"
196
+ type="text"
197
+ bind:value={config.companyName}
198
+ class="px-2.5 py-1.5 rounded-lg border border-border bg-background text-foreground"
199
+ />
200
+ </div>
201
+
202
+ <div class="flex flex-col gap-1.5">
203
+ <label for="main-title-input" class="font-semibold text-foreground">单据大标题</label>
204
+ <input
205
+ id="main-title-input"
206
+ type="text"
207
+ bind:value={config.title}
208
+ class="px-2.5 py-1.5 rounded-lg border border-border bg-background text-foreground"
209
+ />
210
+ </div>
211
+
212
+ <!-- Options toggles -->
213
+ <div class="flex flex-col gap-2 pt-2 border-t border-border">
214
+ <span class="font-semibold text-foreground">单据功能组件</span>
215
+ <label class="flex items-center gap-2 cursor-pointer">
216
+ <input type="checkbox" bind:checked={config.showBarcode} class="rounded border-border" />
217
+ <span>显示流水单号条形码 (Code 128)</span>
218
+ </label>
219
+ <label class="flex items-center gap-2 cursor-pointer">
220
+ <input type="checkbox" bind:checked={config.showQrCode} class="rounded border-border" />
221
+ <span>显示溯源验真二维码</span>
222
+ </label>
223
+ <label class="flex items-center gap-2 cursor-pointer">
224
+ <input type="checkbox" bind:checked={config.showAmountInWords} class="rounded border-border" />
225
+ <span>自动生成中文人民币大写合计</span>
226
+ </label>
227
+ <label class="flex items-center gap-2 cursor-pointer">
228
+ <input type="checkbox" bind:checked={config.showWatermark} class="rounded border-border" />
229
+ <span>开启背景防伪水印</span>
230
+ </label>
231
+ </div>
232
+
233
+ <!-- Column Settings -->
234
+ <div class="flex flex-col gap-2 pt-2 border-t border-border">
235
+ <div class="flex items-center justify-between">
236
+ <span class="font-semibold text-foreground">明细表格列显隐</span>
237
+ <button type="button" onclick={addColumn} class="text-[11px] text-primary hover:underline">+ 增列</button>
238
+ </div>
239
+ <div class="flex flex-col gap-1.5 max-h-52 overflow-y-auto">
240
+ {#each config.columns as col (col.id)}
241
+ <div class="flex items-center justify-between p-1.5 rounded bg-muted/40 border border-border">
242
+ <label class="flex items-center gap-1.5 cursor-pointer">
243
+ <input type="checkbox" bind:checked={col.visible} class="rounded border-border" />
244
+ <input type="text" bind:value={col.label} class="w-24 text-xs bg-transparent border-b border-border/50 focus:outline-none" />
245
+ </label>
246
+ <button type="button" onclick={() => removeColumn(col.id)} class="text-muted-foreground hover:text-destructive px-1">✕</button>
247
+ </div>
248
+ {/each}
249
+ </div>
250
+ </div>
251
+ </div>
252
+ {/if}
253
+
254
+ <!-- Right: High-Fidelity Paper Preview -->
255
+ <div class="flex-1 flex flex-col items-center justify-start p-6 rounded-xl border border-border bg-muted/20 overflow-x-auto">
256
+ <!-- Copy multi-tabs switcher -->
257
+ {#if config.copies.length > 1}
258
+ <div class="flex items-center gap-2 mb-4">
259
+ <span class="text-xs text-muted-foreground font-medium">预览联次:</span>
260
+ {#each config.copies as copyName, index (copyName)}
261
+ <button
262
+ type="button"
263
+ onclick={() => (activeCopyIndex = index)}
264
+ class="text-xs px-3 py-1 rounded-full border transition-colors {activeCopyIndex === index ? 'bg-primary text-primary-foreground border-primary font-semibold' : 'bg-card text-muted-foreground border-border hover:bg-muted'}"
265
+ >
266
+ {copyName}
267
+ </button>
268
+ {/each}
269
+ </div>
270
+ {/if}
271
+
272
+ {#if printError}<p role="alert">{printError}</p>{/if}
273
+ <PrintDocument {config} data={sampleData} items={sampleItems}
274
+ copies={[config.copies[activeCopyIndex] ?? '打印联']} />
275
+ <div bind:this={pages} class="print-pages" hidden>
276
+ <PrintDocument {config} data={sampleData} items={sampleItems} />
277
+ </div>
278
+ </div>
279
+ </div>
280
+ </div>
@@ -0,0 +1,11 @@
1
+ import type { PrintTemplateConfig } from './print-utils.js';
2
+ export interface PrintTemplateDesignerProps {
3
+ config?: Partial<PrintTemplateConfig>;
4
+ sampleData?: Record<string, unknown>;
5
+ class?: string;
6
+ onSave?: (config: PrintTemplateConfig) => void;
7
+ onPreview?: (config: PrintTemplateConfig) => void;
8
+ }
9
+ declare const PrintTemplateDesigner: import("svelte").Component<PrintTemplateDesignerProps, {}, "">;
10
+ type PrintTemplateDesigner = ReturnType<typeof PrintTemplateDesigner>;
11
+ export default PrintTemplateDesigner;
@@ -0,0 +1,369 @@
1
+ <script lang="ts">
2
+ import { untrack } from 'svelte';
3
+ import {
4
+ Activity,
5
+ ShieldOff,
6
+ ShieldCheck,
7
+ Gauge,
8
+ RotateCcw,
9
+ AlertOctagon,
10
+ ServerCrash,
11
+ Check,
12
+ Save,
13
+ } from '@lucide/svelte';
14
+
15
+ export type CircuitBreakerState = 'closed' | 'open' | 'half_open';
16
+ export type RateLimitDimension = 'client_ip' | 'user_id' | 'api_key' | 'tenant_id';
17
+ export type FallbackStrategy = 'mock_json' | 'static_status' | 'secondary_upstream';
18
+
19
+ export interface RateLimitPolicyConfig {
20
+ id: string;
21
+ name: string;
22
+ enabled: boolean;
23
+ qps: number;
24
+ burst: number;
25
+ dimension: RateLimitDimension;
26
+ limitExceededStatusCode: number;
27
+ circuitBreakerState: CircuitBreakerState;
28
+ failureRateThreshold: number; // e.g. 50 (%)
29
+ slowCallDurationMs: number; // e.g. 1000 (ms)
30
+ minRequestVolume: number; // e.g. 20 (reqs)
31
+ recoveryTimeoutSeconds: number; // e.g. 30 (s)
32
+ fallbackStrategy: FallbackStrategy;
33
+ fallbackMockPayload: string;
34
+ }
35
+
36
+ interface Props {
37
+ config?: Partial<RateLimitPolicyConfig>;
38
+ title?: string;
39
+ subtitle?: string;
40
+ onsave?: (config: RateLimitPolicyConfig) => void | Promise<void>;
41
+ onresetCircuitBreaker?: () => void;
42
+ }
43
+
44
+ const defaultConfig: RateLimitPolicyConfig = {
45
+ id: 'policy-default-ratelimit',
46
+ name: '全局对外开放 API 限流与熔断自愈策略',
47
+ enabled: true,
48
+ qps: 500,
49
+ burst: 1000,
50
+ dimension: 'client_ip',
51
+ limitExceededStatusCode: 429,
52
+ circuitBreakerState: 'closed',
53
+ failureRateThreshold: 50,
54
+ slowCallDurationMs: 1200,
55
+ minRequestVolume: 30,
56
+ recoveryTimeoutSeconds: 45,
57
+ fallbackStrategy: 'mock_json',
58
+ fallbackMockPayload: '{"code": 42901, "message": "Service traffic throttled, please retry later"}',
59
+ };
60
+
61
+ let {
62
+ config = {},
63
+ title = 'API 限流熔断与自愈治理看板',
64
+ subtitle = '集成令牌桶 QPS 限流算法、熔断器三态健康流转机及降级兜底响应策略',
65
+ onsave,
66
+ onresetCircuitBreaker,
67
+ }: Props = $props();
68
+
69
+ let policy = $state<RateLimitPolicyConfig>(untrack(() => ({ ...defaultConfig, ...config })));
70
+ let savedSnapshot = $state<string | null>(null);
71
+ const saveSuccess = $derived(savedSnapshot !== null && savedSnapshot === JSON.stringify(policy));
72
+ let isSaving = $state(false);
73
+ let saveError = $state('');
74
+
75
+ $effect(() => {
76
+ policy = { ...defaultConfig, ...config };
77
+ savedSnapshot = null;
78
+ saveError = '';
79
+ });
80
+
81
+ function handleResetCircuitBreaker() {
82
+ policy.circuitBreakerState = 'closed';
83
+ if (onresetCircuitBreaker) {
84
+ onresetCircuitBreaker();
85
+ }
86
+ }
87
+
88
+ async function handleSave() {
89
+ if (!onsave || isSaving) return;
90
+ isSaving = true;
91
+ savedSnapshot = null;
92
+ saveError = '';
93
+ try {
94
+ const payload = { ...policy };
95
+ const snapshot = JSON.stringify(payload);
96
+ await onsave(payload);
97
+ savedSnapshot = snapshot;
98
+ } catch {
99
+ saveError = '保存失败,请重试。';
100
+ } finally {
101
+ isSaving = false;
102
+ }
103
+ }
104
+ </script>
105
+
106
+ <div
107
+ class="relative rounded-xl border border-border bg-card p-6 shadow-lg transition-all"
108
+ data-svadmin-component="RateLimitPolicyPanel"
109
+ >
110
+ <!-- Header Banner -->
111
+ <div class="flex flex-wrap items-center justify-between gap-4 pb-5 border-b border-border">
112
+ <div class="flex items-center gap-3">
113
+ <div class="flex h-11 w-11 shrink-0 items-center justify-center rounded-xl bg-primary/10 text-primary border border-primary/20">
114
+ <Activity class="h-6 w-6" />
115
+ </div>
116
+ <div>
117
+ <div class="flex items-center gap-2">
118
+ <h2 class="text-base font-semibold text-foreground tracking-tight">{title}</h2>
119
+ <span class="inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-semibold
120
+ {policy.enabled ? 'bg-success/15 text-success border border-success/30' : 'bg-muted text-muted-foreground'}">
121
+ {policy.enabled ? '策略在线防护' : '已旁路关闭'}
122
+ </span>
123
+ </div>
124
+ <p class="text-xs text-muted-foreground">{subtitle}</p>
125
+ </div>
126
+ </div>
127
+
128
+ <!-- Actions -->
129
+ <div class="flex items-center gap-3">
130
+ <button
131
+ type="button"
132
+ class="inline-flex items-center gap-1.5 rounded-lg border border-input bg-background px-3 py-1.5 text-xs font-medium text-foreground hover:bg-muted transition-colors shadow-sm"
133
+ onclick={handleResetCircuitBreaker}
134
+ >
135
+ <RotateCcw class="h-3.5 w-3.5 text-muted-foreground" />
136
+ <span>复位熔断器状态</span>
137
+ </button>
138
+
139
+ <button
140
+ type="button"
141
+ class="inline-flex items-center gap-1.5 rounded-lg bg-primary px-4 py-1.5 text-xs font-medium text-primary-foreground shadow hover:bg-primary/90 transition-colors"
142
+ onclick={handleSave}
143
+ disabled={isSaving || !onsave}
144
+ >
145
+ {#if saveSuccess}
146
+ <Check class="h-3.5 w-3.5" />
147
+ <span>策略已同步</span>
148
+ {:else}
149
+ <Save class="h-3.5 w-3.5" />
150
+ <span>保存限流策略</span>
151
+ {/if}
152
+ </button>
153
+ </div>
154
+ </div>
155
+
156
+ {#if saveError}
157
+ <p role="alert" class="pt-3 text-xs text-destructive">{saveError}</p>
158
+ {/if}
159
+
160
+ <!-- Real-time Circuit Breaker Health Status Capsule -->
161
+ <div class="my-5 rounded-lg border border-border bg-muted/20 p-4">
162
+ <div class="flex flex-wrap items-center justify-between gap-4">
163
+ <div class="flex items-center gap-3">
164
+ {#if policy.circuitBreakerState === 'closed'}
165
+ <div class="flex h-9 w-9 items-center justify-center rounded-lg bg-success/15 text-success border border-success/30">
166
+ <ShieldCheck class="h-5 w-5" />
167
+ </div>
168
+ <div>
169
+ <div class="text-xs font-semibold text-success flex items-center gap-1.5">
170
+ <span>熔断器状态:CLOSED (闭合通行)</span>
171
+ <span class="inline-block h-2 w-2 rounded-full bg-success"></span>
172
+ </div>
173
+ <div class="text-[11px] text-muted-foreground">服务链路健康,指标未达到触发熔断阈值</div>
174
+ </div>
175
+ {:else if policy.circuitBreakerState === 'open'}
176
+ <div class="flex h-9 w-9 items-center justify-center rounded-lg bg-destructive/15 text-destructive border border-destructive/30 animate-pulse">
177
+ <ShieldOff class="h-5 w-5" />
178
+ </div>
179
+ <div>
180
+ <div class="text-xs font-semibold text-destructive flex items-center gap-1.5">
181
+ <span>熔断器状态:OPEN (阻断降级中)</span>
182
+ <span class="inline-block h-2 w-2 rounded-full bg-destructive animate-ping"></span>
183
+ </div>
184
+ <div class="text-[11px] text-muted-foreground">错误率超限,所有上游请求已由降级兜底逻辑直接接管</div>
185
+ </div>
186
+ {:else}
187
+ <div class="flex h-9 w-9 items-center justify-center rounded-lg bg-warning/15 text-warning border border-warning/30">
188
+ <AlertOctagon class="h-5 w-5" />
189
+ </div>
190
+ <div>
191
+ <div class="text-xs font-semibold text-warning flex items-center gap-1.5">
192
+ <span>熔断器状态:HALF-OPEN (半开试探)</span>
193
+ <span class="inline-block h-2 w-2 rounded-full bg-warning"></span>
194
+ </div>
195
+ <div class="text-[11px] text-muted-foreground">正在注入灰度探针流量,若连续成功将自愈恢复正常</div>
196
+ </div>
197
+ {/if}
198
+ </div>
199
+
200
+ <div class="flex items-center gap-6 text-xs font-mono">
201
+ <div>
202
+ <span class="text-muted-foreground">当前 QPS:</span>
203
+ <strong class="text-foreground ml-1">{policy.qps} / s</strong>
204
+ </div>
205
+ <div>
206
+ <span class="text-muted-foreground">突发缓冲池:</span>
207
+ <strong class="text-primary ml-1">{policy.burst} req</strong>
208
+ </div>
209
+ <div>
210
+ <span class="text-muted-foreground">熔断阈值:</span>
211
+ <strong class="text-destructive ml-1">{policy.failureRateThreshold}%</strong>
212
+ </div>
213
+ </div>
214
+ </div>
215
+ </div>
216
+
217
+ <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
218
+ <!-- Left Section: Token Bucket Rate Limiting -->
219
+ <div class="space-y-4 rounded-xl border border-border bg-card p-5">
220
+ <div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3">
221
+ <Gauge class="h-4 w-4 text-primary" />
222
+ <span>令牌桶速率限制 (Token Bucket Rate Limiting)</span>
223
+ </div>
224
+
225
+ <div class="space-y-3 text-xs">
226
+ <div>
227
+ <label for="policy-qps-input" class="flex justify-between font-medium text-muted-foreground mb-1">
228
+ <span>基准请求速率限制 (QPS)</span>
229
+ <span class="font-mono text-foreground font-semibold">{policy.qps} 次/秒</span>
230
+ </label>
231
+ <input
232
+ id="policy-qps-input"
233
+ type="range"
234
+ min="10"
235
+ max="5000"
236
+ step="10"
237
+ class="w-full accent-primary cursor-pointer"
238
+ bind:value={policy.qps}
239
+ />
240
+ </div>
241
+
242
+ <div>
243
+ <label for="policy-burst-input" class="flex justify-between font-medium text-muted-foreground mb-1">
244
+ <span>最大突发峰值并发容纳量 (Burst Capacity)</span>
245
+ <span class="font-mono text-primary font-semibold">{policy.burst} 并发令牌</span>
246
+ </label>
247
+ <input
248
+ id="policy-burst-input"
249
+ type="range"
250
+ min="50"
251
+ max="10000"
252
+ step="50"
253
+ class="w-full accent-primary cursor-pointer"
254
+ bind:value={policy.burst}
255
+ />
256
+ </div>
257
+
258
+ <div class="grid grid-cols-2 gap-3 pt-2">
259
+ <div>
260
+ <label for="policy-dimension-select" class="block font-medium text-muted-foreground mb-1">限流粒度维度 (Dimension)</label>
261
+ <select
262
+ id="policy-dimension-select"
263
+ class="w-full rounded-lg border border-input bg-background px-3 py-1.5 text-xs text-foreground focus:outline-none focus:ring-1 focus:ring-ring"
264
+ bind:value={policy.dimension}
265
+ >
266
+ <option value="client_ip">客户端 IP 地址 (Client IP)</option>
267
+ <option value="user_id">已登录用户 UID (User ID)</option>
268
+ <option value="api_key">API 鉴权密钥 (API Key)</option>
269
+ <option value="tenant_id">多租户 ID (Tenant ID)</option>
270
+ </select>
271
+ </div>
272
+
273
+ <div>
274
+ <label for="policy-status-code" class="block font-medium text-muted-foreground mb-1">超限拦截状态码</label>
275
+ <input
276
+ id="policy-status-code"
277
+ type="number"
278
+ class="w-full rounded-lg border border-input bg-background px-3 py-1.5 text-xs font-mono text-foreground focus:outline-none focus:ring-1 focus:ring-ring"
279
+ bind:value={policy.limitExceededStatusCode}
280
+ />
281
+ </div>
282
+ </div>
283
+ </div>
284
+ </div>
285
+
286
+ <!-- Right Section: Circuit Breaker Parameters & Fallback -->
287
+ <div class="space-y-4 rounded-xl border border-border bg-card p-5">
288
+ <div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3">
289
+ <ServerCrash class="h-4 w-4 text-destructive" />
290
+ <span>熔断器阈值与降级兜底策略</span>
291
+ </div>
292
+
293
+ <div class="space-y-3 text-xs">
294
+ <div class="grid grid-cols-2 gap-3">
295
+ <div>
296
+ <label for="policy-failure-threshold" class="block font-medium text-muted-foreground mb-1">
297
+ 异常错误率触发阈值 (%)
298
+ </label>
299
+ <input
300
+ id="policy-failure-threshold"
301
+ type="number"
302
+ min="1"
303
+ max="100"
304
+ class="w-full rounded-lg border border-input bg-background px-3 py-1.5 text-xs font-mono text-foreground focus:outline-none focus:ring-1 focus:ring-ring"
305
+ bind:value={policy.failureRateThreshold}
306
+ />
307
+ </div>
308
+
309
+ <div>
310
+ <label for="policy-slow-duration" class="block font-medium text-muted-foreground mb-1">慢调用超时判定 (ms)</label>
311
+ <input
312
+ id="policy-slow-duration"
313
+ type="number"
314
+ min="100"
315
+ max="30000"
316
+ class="w-full rounded-lg border border-input bg-background px-3 py-1.5 text-xs font-mono text-foreground focus:outline-none focus:ring-1 focus:ring-ring"
317
+ bind:value={policy.slowCallDurationMs}
318
+ />
319
+ </div>
320
+ </div>
321
+
322
+ <div class="grid grid-cols-2 gap-3">
323
+ <div>
324
+ <label for="policy-min-requests" class="block font-medium text-muted-foreground mb-1">最小统计请求采样数</label>
325
+ <input
326
+ id="policy-min-requests"
327
+ type="number"
328
+ min="5"
329
+ class="w-full rounded-lg border border-input bg-background px-3 py-1.5 text-xs font-mono text-foreground focus:outline-none focus:ring-1 focus:ring-ring"
330
+ bind:value={policy.minRequestVolume}
331
+ />
332
+ </div>
333
+
334
+ <div>
335
+ <label for="policy-recovery-timeout" class="block font-medium text-muted-foreground mb-1">熔断持续自愈等待 (秒)</label>
336
+ <input
337
+ id="policy-recovery-timeout"
338
+ type="number"
339
+ min="5"
340
+ class="w-full rounded-lg border border-input bg-background px-3 py-1.5 text-xs font-mono text-foreground focus:outline-none focus:ring-1 focus:ring-ring"
341
+ bind:value={policy.recoveryTimeoutSeconds}
342
+ />
343
+ </div>
344
+ </div>
345
+
346
+ <!-- Fallback Strategy -->
347
+ <div class="pt-1">
348
+ <label for="policy-fallback-strategy" class="block font-medium text-muted-foreground mb-1">降级接管策略 (Fallback Action)</label>
349
+ <select
350
+ id="policy-fallback-strategy"
351
+ class="w-full rounded-lg border border-input bg-background px-3 py-1.5 text-xs text-foreground focus:outline-none focus:ring-1 focus:ring-ring mb-2"
352
+ bind:value={policy.fallbackStrategy}
353
+ >
354
+ <option value="mock_json">返回 Mock 静态 JSON 结构体</option>
355
+ <option value="static_status">返回标准 503 快速失败</option>
356
+ <option value="secondary_upstream">透明转发至二级备用灾备集群</option>
357
+ </select>
358
+
359
+ {#if policy.fallbackStrategy === 'mock_json'}
360
+ <textarea
361
+ class="w-full h-16 rounded-lg border border-input bg-background p-2 text-xs font-mono text-foreground focus:outline-none focus:ring-1 focus:ring-ring"
362
+ bind:value={policy.fallbackMockPayload}
363
+ ></textarea>
364
+ {/if}
365
+ </div>
366
+ </div>
367
+ </div>
368
+ </div>
369
+ </div>