import CommonContent, { GetContentListItem, GetContentListParams } from "@/api/CommonContent"; import { useSimpleDataLoader, type ISimpleDataLoader } from "@/common/composeabe/SimpleDataLoader"; import { navTo } from "@/components/utils/PageAction"; import type { IHomeCommonCategoryListTabListDataSolve } from "../data/CommonCategoryDefine"; import { DateUtils } from "@imengyu/imengyu-utils"; /** * 通用内容首页小列表控制代码组合 */ export interface IHomeCommonCategoryBlock { type: 'CommonCategoryBlock', loader: ISimpleDataLoader; goDetail: (i: GetContentListItem) => void; goList: () => void; } export function navCommonDetail(p: { id: number, title?: string, mainBodyColumnId?: string|number|number[], modelId?: number, }) { navTo('/pages/article/details', { mainBodyColumnId: p.mainBodyColumnId, modelId: p.modelId, id: p.id, }) } export function navCommonList(p: { title?: string, mainBodyColumnId?: string|number|number[], modelId?: number, itemType?: string, detailsPage? : string, }) { navTo('/pages/article/common/list', { title: p.title, mainBodyColumnId: typeof p.mainBodyColumnId == 'object' ? p.mainBodyColumnId.join(',') : p.mainBodyColumnId, modelId: p.modelId, itemType: p.itemType || 'article-common', detailsPage: p.detailsPage || '/pages/article/details', }) } export function resolveCommonContentFormData(item: GetContentListItem[]) { item.forEach(it => { it.bottomTags = it.keywords?.length ? it.keywords as string[] : [ it.mainBodyColumnName ]; }) return item; } export function resolveCommonContentGetPageDetailUrlAuto(item: GetContentListItem) { if (item.type === GetContentListParams.TYPE_VIDEO || item.video) return '/pages/video/details'; if (item.type === GetContentListParams.TYPE_ARCHIVE && item.archive) return '/pages/document/details'; return '/pages/article/details'; } const resolveCommonContentData = { 'none': (item: GetContentListItem[]) => item, 'common': (item: GetContentListItem[]) => { item.forEach(it => { it.bottomTags = it.bottomTags || []; it.bottomTags = (it.bottomTags as string[]).concat(it.keywords?.length ? it.keywords as string[] : [ it.mainBodyColumnName ]); }) return item; }, 'date': (item: GetContentListItem[]) => { item.forEach(p => { p.desc = DateUtils.formatDate(p.publishAt, 'YYYY-MM-dd') + ' ' + (p.desc || ''); }) return item; }, 'form': (item: GetContentListItem[]) => { item.forEach(p => { p.desc = `来源:${p.from || '暂无'}` + ' ' + (p.desc || ''); }) return item; }, 'ich': (item: GetContentListItem[]) => { item.forEach(it => { it.bottomTags = (it.bottomTags as string[] || []).concat([ it.levelText as string, it.ichTypeText as string, it.batchText as string, it.regionText as string, ]); }); return item; }, 'inheritor': (item: GetContentListItem[]) => { item.forEach(it => { it.bottomTags = (it.bottomTags as string[] || []).concat([ it.age as string, ]); }); return item; }, } as Record GetContentListItem[]> export function resolveCommonContentSolveProps(res: GetContentListItem[], dataSolve: IHomeCommonCategoryListTabListDataSolve[]) { for (const solve of dataSolve) res = resolveCommonContentData[solve]?.(res) || res; return res; } export interface HomeCommonCategoryBlockProps { title?: string, type?: '', mainBodyColumnId?: string|number|number[], modelId?: number, itemType?: string, detailsPage: string, count?: number, params?: Record, dataSolve?: IHomeCommonCategoryListTabListDataSolve[], } /** * 专用于通用内容的首页小列表控制代码组合 * @param p * @returns */ export function useHomeCommonCategoryBlock(p: HomeCommonCategoryBlockProps, loadWhenMounted = true) : IHomeCommonCategoryBlock { function goDetail(i: GetContentListItem) { navTo(p.detailsPage === 'byContent' ? resolveCommonContentGetPageDetailUrlAuto(i): p.detailsPage, { mainBodyColumnId: p.mainBodyColumnId, modelId: p.modelId, id: i.id, }) } function goList() { navCommonList({ title: p.title, mainBodyColumnId: typeof p.mainBodyColumnId == 'object' ? p.mainBodyColumnId.join(',') : p.mainBodyColumnId, modelId: p.modelId, itemType: p.itemType, detailsPage: p.detailsPage, }) } const loader = useSimpleDataLoader(async () => { let res = (await CommonContent.getContentList(new GetContentListParams().setSelfValues({ mainBodyColumnId: p.mainBodyColumnId, modelId: p.modelId, ...p.params, }), 1, p.count ?? 4)).list; return resolveCommonContentSolveProps(res, p.dataSolve || []);; }, loadWhenMounted); return { type: 'CommonCategoryBlock', loader, goDetail, goList, } }