/** * 【录像服务器管理】配置文件 — 基于 newcamara 页面模板 v2 架构 * * ===== 核心设计 ===== * 1. FIELD_DICT — 全局字段预设表,每个字段拥有唯一翻译(label)和完整元数据 * 2. VALUE_MAPS — 值翻译映射表(select options 和 tag 翻译共用) * 3. TAG_RESOLVERS — 标签颜色解析器(集中管理 tag 颜色逻辑) * 4. 各标签页通过 key 引用所需字段(scanBoxFieldKeys / tableColumnKeys) * 5. resolveTabConfig() — 加载时将 key 列表展开为完整配置 */ import type { Component } from 'vue' import DetailPanel from './components/DetailPanel.vue' import LogPanel from './components/LogPanel.vue' import ServerFormDialog from '../pages/ServerFormDialog.vue' // ============================================================ // 〇、全局页面配置 // ============================================================ export const PAGE_CONFIG = { /** 是否显示顶部多标签栏 */ showTabArea: false, /** 单页模式:不显示标签栏,仅一个标签页内容 */ singlePage: true, /** 是否启用详情面板(底部分栏 + 显示/隐藏详情按钮),false 时表格占满全高 */ enableDetailPanel: false } // ============================================================ // 〇-A、组件注册表 — 所有可复用组件在此注册 // ============================================================ export const COMPONENT_REGISTRY: Record = { DetailPanel, LogPanel, ServerFormDialog } // ============================================================ // 〇-B、SCANBOX_GROUPS — ScanBox 分组名称 // ============================================================ export const SCANBOX_GROUPS = { basic: '基础信息', status: '状态筛选' } as const // ============================================================ // 一、FIELD_DICT — 全局字段预设表(唯一真相来源) // ============================================================ export interface FieldDefinition { key: string label: string scanBox: { type: 'text' | 'select' | 'daterange' | 'number' group?: string options?: { label: string; value: any }[] placeholder?: string operators?: string[] defaultOperator?: string } | null table: { minWidth?: number width?: number fixed?: 'left' | 'right' | boolean align?: 'left' | 'center' | 'right' sortable?: boolean showOverflowTooltip?: boolean copyAction?: 'copy' | 'copyQr' copyValue?: (row: any) => any } | null slot: { type: 'tag' | 'text' | 'default' | 'operation' tagType?: string | Record tagResolver?: string translateField?: string format?: 'datetime' } | null detailLabel?: string filterParamMap?: Record } // ============================================================ // VALUE_MAPS — 值翻译映射(select options 和 tag 翻译共用) // ============================================================ export const VALUE_MAPS: Record> = { serverStatus: { '1': '启用', '0': '禁用', '启用': '启用', '禁用': '禁用' } } /** 从 VALUE_MAPS 衍生 options(优先取中文值作为 option value) */ export function getOptions(field: string): { label: string; value: string }[] { const map = VALUE_MAPS[field] if (!map) return [] const seen = new Set() const opts: { label: string; value: string }[] = [] const chineseKeys: [string, string][] = [] const otherKeys: [string, string][] = [] for (const [v, l] of Object.entries(map)) { if (/^[\u4e00-\u9fa5]/.test(v)) { chineseKeys.push([v, l]) } else { otherKeys.push([v, l]) } } for (const [v, l] of [...chineseKeys, ...otherKeys]) { if (!seen.has(l)) { seen.add(l) opts.push({ label: l, value: v }) } } return opts } /** 翻译 value → label */ export function translate(field: string, value: any): string { const map = VALUE_MAPS[field] if (!map) return String(value ?? '—') return map[String(value)] ?? String(value ?? '—') } /** 格式化时间戳为 YYYY-MM-DD HH:mm:ss */ export function formatTime(value: any): string { if (!value) return '—' try { const d = new Date(value) if (isNaN(d.getTime())) return String(value) const pad = (n: number) => String(n).padStart(2, '0') return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}` } catch { return String(value) } } // ============================================================ // TAG_RESOLVERS — 标签颜色解析器 // ============================================================ export const TAG_RESOLVERS: Record string> = { resolveServerStatusTag(v) { if (v === 1 || v === '1' || v === '启用') return 'success' return 'info' } } // ============================================================ // 二、FIELD_DICT 实例 — 录像服务器页面字段 // ============================================================ export const FIELD_DICT: Record = { /** 关键词搜索(虚拟字段,仅用于 ScanBox) */ quickWhere: { key: 'quickWhere', label: '关键词', scanBox: { type: 'text', group: SCANBOX_GROUPS.basic, placeholder: '服务器名称/IP', operators: ['contains'], defaultOperator: 'contains' }, table: null, slot: null, detailLabel: '关键词' }, // id: { // key: 'id', // label: 'ID', // scanBox: null, // table: { width: 80, sortable: true }, // slot: null, // detailLabel: 'ID' // }, name: { key: 'name', label: '服务器名称', scanBox: { type: 'text', group: SCANBOX_GROUPS.basic, placeholder: '请输入名称', operators: ['contains'], defaultOperator: 'contains' }, table: { minWidth: 160, showOverflowTooltip: true }, slot: null, detailLabel: '服务器名称' }, brand: { key: 'brand', label: '品牌', scanBox: { type: 'select', group: SCANBOX_GROUPS.basic, options: getOptions('brand') }, table: { width: 120, align: 'center' }, slot: { type: 'tag', tagResolver: 'resolveBrandTag', translateField: 'brand' }, detailLabel: '品牌' }, public_ip: { key: 'public_ip', label: '公网IP', scanBox: null, table: { width: 150, showOverflowTooltip: true }, slot: null, detailLabel: '公网IP' }, internal_ip: { key: 'internal_ip', label: '内网IP', scanBox: null, table: { width: 150, showOverflowTooltip: true }, slot: null, detailLabel: '内网IP' }, port: { key: 'port', label: '端口', scanBox: null, table: null, slot: null, detailLabel: '端口' }, public_port: { key: 'public_port', label: '公网端口', scanBox: null, table: { width: 90, align: 'center' }, slot: null, detailLabel: '公网端口' }, internal_port: { key: 'internal_port', label: '内网端口', scanBox: null, table: { width: 90, align: 'center' }, slot: null, detailLabel: '内网端口' }, username: { key: 'username', label: '用户名', scanBox: null, table: { width: 120 }, slot: null, detailLabel: '用户名' }, status: { key: 'status', label: '状态', scanBox: { type: 'select', group: SCANBOX_GROUPS.status, options: getOptions('serverStatus') }, table: { width: 90, align: 'center' }, slot: { type: 'tag', tagResolver: 'resolveServerStatusTag', translateField: 'serverStatus' }, detailLabel: '状态', filterParamMap: { '启用': '1', '禁用': '0' } }, remark: { key: 'remark', label: '备注', scanBox: null, table: { minWidth: 160, showOverflowTooltip: true }, slot: null, detailLabel: '备注' }, connectivity: { key: 'connectivity', label: '连通', scanBox: null, table: { width: 110, align: 'center' }, slot: { type: 'default' }, detailLabel: '连通状态' }, created_at: { key: 'created_at', label: '创建时间', scanBox: null, table: { minWidth: 170, sortable: true }, slot: { type: 'text', format: 'datetime' }, detailLabel: '创建时间' }, updated_at: { key: 'updated_at', label: '更新时间', scanBox: null, table: { minWidth: 170, sortable: true }, slot: { type: 'text', format: 'datetime' }, detailLabel: '更新时间' }, /** 操作列(虚拟字段,仅用于表格列渲染) */ operation: { key: 'operation', label: '操作', scanBox: null, table: { width: 150, fixed: 'right', align: 'center' }, slot: { type: 'operation' } } } // ============================================================ // 三、ColumnSlotConfig — 列单元格渲染配置 // ============================================================ export interface ColumnSlotConfig { type: 'tag' | 'text' | 'default' | 'operation' tagType?: string | Record tagResolver?: string translateField?: string format?: 'datetime' } // ============================================================ // 三-A、OperationButtonConfig — 操作列按钮配置 // ============================================================ export interface OperationButtonConfig { key: string label: string type?: 'primary' | 'success' | 'warning' | 'danger' | 'info' | '' actionType: 'form' | 'action' formKey?: string formTitle?: string formWidth?: string paramMap?: Record extraParams?: Record disabledOnField?: string confirmMessage?: string confirmTitle?: string confirmType?: 'warning' | 'danger' | 'info' confirmButtonText?: string } // ============================================================ // 四、DetailFieldConfig / DetailSectionConfig — 明细面板配置 // ============================================================ export interface DetailFieldConfig { key: string label?: string span?: number renderType?: 'text' | 'tag' | 'pre' tagType?: string | Record tagResolver?: string translateField?: string } export interface DetailSectionConfig { title: string columnCount: number fields: DetailFieldConfig[] } export interface DetailTabConfig { name: string label: string componentKey: string component?: Component } // ============================================================ // 五、TabPageApiConfig — 标签页接口插槽配置 // ============================================================ export type ApiMethod = (...args: any[]) => Promise export type ApiSource = 'api' | 'list' export type ComponentApiConfig = { source: 'list' } | ApiMethod export interface TabPageApiConfig { /** 列表查询接口 */ list: ApiMethod /** 明细面板接口(可选,不配则用 row 数据) */ detail?: ComponentApiConfig /** 操作日志接口(可选) */ log?: ComponentApiConfig /** 导出接口(可选) */ export?: ApiMethod /** 导出进度查询接口(可选) */ exportProgress?: ApiMethod /** ScanBox 下拉选项数据源接口(可选) */ scanBoxOptions?: Record /** 工具栏按钮 → API 方法映射(可选) */ toolbar?: Record /** 列表响应解析器 */ parseListResponse?: (res: any) => { list: any[]; total: number } /** 构建列表请求参数 */ buildListParams?: (params: { pagination: { page: number; pageSize: number } conditions: Record sort?: { prop: string; order: string } }) => Record } // ============================================================ // 六、TabPageRawConfig — 标签页声明配置(仅用 key 引用) // ============================================================ export interface TabPageRawConfig { name: string label: string scanBoxCacheKey: string /** ScanBox 需要的字段 key 列表(从 FIELD_DICT 中查找) */ scanBoxFieldKeys: string[] /** 表格需要的列 key 列表(从 FIELD_DICT 中查找) */ tableColumnKeys: string[] upperTableDefaults: { title: string enableColumnConfig?: boolean showIndex?: boolean showSelection?: boolean emptyText?: string } /** 明细面板标签页配置 */ detailTabs?: { name: string; label: string; componentKey: string }[] /** 明细面板分区配置(字段 key 列表,resolveTabConfig 展开) */ detailSections: { title: string columnCount: number fieldKeys: string[] }[] /** 接口配置(有 api 时启用真实接口) */ api?: TabPageApiConfig /** 操作列按钮配置(需 tableColumnKeys 包含 'operation') */ operationButtons?: OperationButtonConfig[] } // ============================================================ // 七、TabPageConfig — 展开后的完整配置(给 index.vue 使用) // ============================================================ export interface TabPageConfig { name: string label: string scanBoxCacheKey: string scanBoxFields: Record[] upperTable: { title: string enableColumnConfig: boolean showIndex: boolean showSelection: boolean emptyText: string columns: Record[] } columnSlots: Record detailConfig: DetailSectionConfig[] detailTabs: DetailTabConfig[] api?: TabPageApiConfig operationButtons?: OperationButtonConfig[] } // ============================================================ // 八、resolveTabConfig — 加载时将 RawConfig 展开为完整配置 // ============================================================ export function resolveTabConfig(raw: TabPageRawConfig): TabPageConfig { // 展开 ScanBox 字段 const scanBoxFields: Record[] = [] for (const key of raw.scanBoxFieldKeys) { const def = FIELD_DICT[key] if (!def || !def.scanBox) continue scanBoxFields.push({ label: def.label, value: def.key, type: def.scanBox.type, options: def.scanBox.options ? [...def.scanBox.options] : undefined, group: def.scanBox.group, placeholder: def.scanBox.placeholder, operators: def.scanBox.operators, defaultOperator: def.scanBox.defaultOperator, required: (def.scanBox as any).required }) } // 展开表格列 const columns: Record[] = [] for (const key of raw.tableColumnKeys) { const def = FIELD_DICT[key] if (!def || !def.table) continue columns.push({ prop: def.key, label: def.label, ...def.table }) } // 展开列插槽 const columnSlots: Record = {} for (const key of raw.tableColumnKeys) { const def = FIELD_DICT[key] if (!def || !def.slot) continue columnSlots[def.key] = { ...def.slot } } // 展开明细面板配置 const detailConfig: DetailSectionConfig[] = raw.detailSections.map((sec) => ({ title: sec.title, columnCount: sec.columnCount, fields: sec.fieldKeys .map((key): DetailFieldConfig | null => { const def = FIELD_DICT[key] if (!def) return null return { key: def.key, label: def.detailLabel ?? def.label, translateField: def.slot?.translateField, tagResolver: def.slot?.tagResolver, tagType: def.slot?.tagType, renderType: def.slot?.type === 'tag' ? 'tag' : 'text' } }) .filter((f): f is DetailFieldConfig => f !== null) })) // 展开明细标签页(解析 componentKey → Component) const detailTabs: DetailTabConfig[] = raw.detailTabs ? raw.detailTabs.map((tab) => ({ name: tab.name, label: tab.label, componentKey: tab.componentKey, component: COMPONENT_REGISTRY[tab.componentKey] })) : [{ name: 'detail', label: '明细', componentKey: 'DetailPanel', component: COMPONENT_REGISTRY['DetailPanel'] }] return { name: raw.name, label: raw.label, scanBoxCacheKey: raw.scanBoxCacheKey, scanBoxFields, upperTable: { title: raw.upperTableDefaults.title, enableColumnConfig: raw.upperTableDefaults.enableColumnConfig ?? true, showIndex: raw.upperTableDefaults.showIndex ?? true, showSelection: raw.upperTableDefaults.showSelection ?? false, emptyText: raw.upperTableDefaults.emptyText ?? '暂无录像服务器数据', columns }, columnSlots, detailConfig, detailTabs, api: raw.api, operationButtons: raw.operationButtons } } // ============================================================ // 九、buildWhereConditions — 将 ScanBox 条件转为 API where 参数 // ============================================================ export function buildWhereConditions(rawConditions: Record): Record { const where: Record = {} for (const [key, cond] of Object.entries(rawConditions || {})) { let value: any = undefined if (cond && typeof cond === 'object' && 'operator' in cond) { value = (cond as any).value } else if (cond !== undefined && cond !== null && cond !== '') { value = Array.isArray(cond) ? cond[0] ?? '' : cond } if (value === undefined || value === null || value === '' || (Array.isArray(value) && value.length === 0)) { continue } // quickWhere 特殊处理 if (key === 'quickWhere') { where.quickWhere = String(value) continue } const def = FIELD_DICT[key] if (def?.filterParamMap) { const mapped = def.filterParamMap[String(value)] if (mapped !== undefined) { where[key] = mapped continue } } where[key] = value } return where } // ============================================================ // 十、工具栏按钮系统 // ============================================================ export interface ToolbarButtonConfig { key: string label: string category: 'refresh' | 'setting' | 'action' | 'cancel' size?: '' | 'default' | 'small' | 'large' type?: '' | 'primary' | 'success' | 'warning' | 'info' | 'danger' plain?: boolean loadingKey?: string disabledKey?: string action: string dynamicLabelKey?: string onlyForTabs?: string[] buttonType?: 'default' | 'dropdown' dropdownOptions?: Array<{ label: string; action: string; divided?: boolean }> formKey?: string } /** ★ 录像服务器工具栏按钮 */ export const TOOLBAR_BUTTONS: Record = { // ===== 刷新 ===== refresh: { key: 'refresh', label: '刷新', category: 'refresh', type: 'primary', size: 'small', loadingKey: 'loading', action: 'loadData' }, // ===== 设置 ===== columnConfig: { key: 'columnConfig', label: '字段设置', category: 'setting', size: 'small', action: 'openColumnConfigPanel' }, // ===== 操作 ===== addServer: { key: 'addServer', label: '新增录像服务器', category: 'action', size: 'small', action: 'openAddServerDialog', formKey: 'ServerFormDialog' }, } /** 按标签页名称获取工具栏按钮列表(自动根据 PAGE_CONFIG 过滤) */ export function getTabToolbarButtons(tabName: string): ToolbarButtonConfig[] { return Object.values(TOOLBAR_BUTTONS).filter((btn) => { if (btn.onlyForTabs && !btn.onlyForTabs.includes(tabName)) return false return true }) } // ============================================================ // 十一、批量操作配置(录像服务器暂无需批量操作) // ============================================================ export interface BatchOperationConfig { action: string title: string confirmText: string confirmType?: 'warning' | 'error' | 'info' | 'success' confirmButtonText: string successText: string failText: string apiMethod: (...args: any[]) => Promise payloadKey?: string idField?: string | string[] payloadBuilder?: (rows: Record[]) => Record useProgress?: boolean progressApiMethod?: (...args: any[]) => Promise } export const BATCH_OPERATION_CONFIGS: Record = {} // ============================================================ // 十二、标签页注册表 — 录像服务器管理 // ============================================================ import { list, create, update, del } from '@/api/camera_server/camera_server' export const tabPages: TabPageRawConfig[] = [ { name: 'server', label: '录像服务器管理', scanBoxCacheKey: 'video-camera-server', scanBoxFieldKeys: ['quickWhere', 'name', 'status'], tableColumnKeys: [ 'id', 'name', 'brand', 'public_ip', 'internal_ip', 'public_port', 'internal_port', 'username', 'status', 'remark', 'created_at', 'updated_at', 'operation' ], upperTableDefaults: { title: '录像服务器列表', showIndex: true, showSelection: false, emptyText: '暂无录像服务器数据,可调整左侧筛选条件' }, detailTabs: [ { name: 'detail', label: '明细', componentKey: 'DetailPanel' } ], detailSections: [ { title: '基础信息', columnCount: 3, fieldKeys: ['id', 'name', 'brand', 'public_ip', 'internal_ip', 'public_port', 'internal_port', 'username', 'status', 'remark'], }, { title: '时间信息', columnCount: 2, fieldKeys: ['created_at', 'updated_at'] } ], api: { list, detail: { source: 'list' }, parseListResponse(res) { return { list: res?.list ?? [], total: Number(res?.pagination?.total ?? res?.total ?? 0) } }, buildListParams({ pagination, conditions }) { return { pagination: { page: pagination.page, pageSize: pagination.pageSize }, where: buildWhereConditions(conditions) } }, toolbar: { delete: del, create, update } }, operationButtons: [ { key: 'edit', label: '编辑', actionType: 'action', type: 'primary' }, { key: 'delete', label: '删除', actionType: 'action', type: 'danger', confirmTitle: '提示', confirmMessage: '确认删除该录像服务器吗?删除后关联的摄像头将不可用', confirmType: 'warning', confirmButtonText: '确认删除' }, { key: 'testConnection', label: '连通测试', actionType: 'action', type: 'warning' } ] } ] // ============================================================ // 十三、向后兼容导出 // ============================================================ export const templateTabs = tabPages.map((t) => ({ name: t.name, label: t.label, scanBoxCacheKey: t.scanBoxCacheKey }))