| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- 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<GetContentListItem[], any>;
- 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<string, (item: GetContentListItem[]) => 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<string, any>,
- 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,
- }
- }
|