92 lines
3.7 KiB
TypeScript
92 lines
3.7 KiB
TypeScript
/**
|
||
* 演示文库与页面模型之间的归一。
|
||
*
|
||
* 状态口径与标签见 ./library,这里只管「演示文库(utils/mock-document-library)
|
||
* ↔ 页面模型(DocumentDepartment)」的字段补齐,不掺展示映射。
|
||
*
|
||
* utils/mock-document-library 刻意只声明自己用到的字段(不反向依赖 views 的类型),
|
||
* 它返回的文书缺了页面模型要求的 category / updatedBy / updatedAt,code 也可能为空。
|
||
* 两种口径不一致会在 loadLibrary / saveDocumentLibrary 处直接报类型错误,
|
||
* 因此在这一层做一次显式补齐——而不是让 utils 反过来依赖 views,
|
||
* 也不是把页面的必填字段放宽成可选(放宽会掩盖真实接口返回缺字段的问题)。
|
||
*
|
||
* 补齐用的值都标注了是演示数据的缺省值,不冒充真实字段:
|
||
* updatedAt 留空串,页面会显示成"—",不会假装有一份真实的更新时间。
|
||
*/
|
||
import type { DocumentRecord } from '@/api/management/types'
|
||
import type { LocalDocument, LocalDepartment } from '@/utils/mock-document-library'
|
||
import { readDocumentLibrary, writeDocumentLibrary } from '@/utils/mock-document-library'
|
||
|
||
import type { DocumentDepartment } from './types'
|
||
|
||
function toDocumentRecord(document: LocalDocument, categoryName: string): DocumentRecord {
|
||
return {
|
||
...document,
|
||
code: document.code ?? '',
|
||
category: categoryName,
|
||
updatedBy: '演示数据',
|
||
updatedAt: document.updatedAt ?? '',
|
||
}
|
||
}
|
||
|
||
export function toDocumentDepartment(department: LocalDepartment): DocumentDepartment {
|
||
return {
|
||
...department,
|
||
categories: department.categories.map((category) => ({
|
||
...category,
|
||
documents: category.documents.map((document) => toDocumentRecord(document, category.name)),
|
||
})),
|
||
}
|
||
}
|
||
|
||
/** 读取演示文书库:优先用本地保存的,没有则用种子(调用方负责再克隆一份使用) */
|
||
export function loadDocumentLibrary(): DocumentDepartment[] {
|
||
return readDocumentLibrary().map(toDocumentDepartment)
|
||
}
|
||
|
||
/**
|
||
* 保存演示文书库。
|
||
* 落本地时要把 category 摘掉(LocalDocument 没有 category 字段,
|
||
* 分类信息在 categories 这一层的 name 上),否则多存一个字段会在下次读取时对不上。
|
||
* updatedBy 同理:它是页面模型为「必须有人维护」补的展示字段,不是演示文库的数据。
|
||
*/
|
||
export function saveDocumentLibrary(library: DocumentDepartment[]): void {
|
||
writeDocumentLibrary(
|
||
library.map((department) => ({
|
||
...department,
|
||
categories: department.categories.map((category) => ({
|
||
name: category.name,
|
||
documents: category.documents.map(toLocalDocument),
|
||
})),
|
||
})),
|
||
)
|
||
}
|
||
|
||
/**
|
||
* 页面模型的文书 → 演示文库存储结构。
|
||
*
|
||
* 逐字段列出而不是解构剔除:解构剔除(`const { category, ...rest }`)会因为
|
||
* 被剔除的变量未使用而触发 lint,且多加一个页面专用字段时容易忘掉同步。
|
||
* 这里明确列出演示文库认识哪些字段,多出来的页面字段(category / updatedBy)自然被丢掉。
|
||
*/
|
||
function toLocalDocument(document: DocumentRecord): LocalDocument {
|
||
return {
|
||
id: document.id,
|
||
name: document.name,
|
||
code: document.code,
|
||
version: document.version,
|
||
status: document.status,
|
||
backendStatus: document.backendStatus,
|
||
description: document.description ?? '',
|
||
versionId: document.versionId,
|
||
departmentId: document.departmentId,
|
||
departmentName: document.departmentName,
|
||
sharedDepartments: document.sharedDepartments,
|
||
campusId: document.campusId,
|
||
campusName: document.campusName,
|
||
updatedAt: document.updatedAt,
|
||
contentHtml: document.contentHtml,
|
||
contentSha256: document.contentSha256,
|
||
}
|
||
}
|