CommonContent.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import CommonContent, { GetContentListItem, GetContentListParams } from "@/api/CommonContent";
  2. import { useSimpleDataLoader, type ISimpleDataLoader } from "@/common/composeabe/SimpleDataLoader";
  3. import { navTo } from "@/components/utils/PageAction";
  4. import type { IHomeCommonCategoryListTabListDataSolve } from "../data/CommonCategoryDefine";
  5. import { DateUtils } from "@imengyu/imengyu-utils";
  6. /**
  7. * 通用内容首页小列表控制代码组合
  8. */
  9. export interface IHomeCommonCategoryBlock {
  10. type: 'CommonCategoryBlock',
  11. loader: ISimpleDataLoader<GetContentListItem[], any>;
  12. goDetail: (i: GetContentListItem) => void;
  13. goList: () => void;
  14. }
  15. export function navCommonDetail(p: {
  16. id: number,
  17. title?: string,
  18. mainBodyColumnId?: string|number|number[],
  19. modelId?: number,
  20. }) {
  21. navTo('/pages/article/details', {
  22. mainBodyColumnId: p.mainBodyColumnId,
  23. modelId: p.modelId,
  24. id: p.id,
  25. })
  26. }
  27. export function navCommonList(p: {
  28. title?: string,
  29. mainBodyColumnId?: string|number|number[],
  30. modelId?: number,
  31. itemType?: string,
  32. detailsPage? : string,
  33. }) {
  34. navTo('/pages/article/common/list', {
  35. title: p.title,
  36. mainBodyColumnId: typeof p.mainBodyColumnId == 'object' ?
  37. p.mainBodyColumnId.join(',') :
  38. p.mainBodyColumnId,
  39. modelId: p.modelId,
  40. itemType: p.itemType || 'article-common',
  41. detailsPage: p.detailsPage || '/pages/article/details',
  42. })
  43. }
  44. export function resolveCommonContentFormData(item: GetContentListItem[]) {
  45. item.forEach(it => {
  46. it.bottomTags = it.keywords?.length ? it.keywords as string[] : [ it.mainBodyColumnName ];
  47. })
  48. return item;
  49. }
  50. export function resolveCommonContentGetPageDetailUrlAuto(item: GetContentListItem) {
  51. if (item.type === GetContentListParams.TYPE_VIDEO || item.video)
  52. return '/pages/video/details';
  53. if (item.type === GetContentListParams.TYPE_ARCHIVE && item.archive)
  54. return '/pages/document/details';
  55. return '/pages/article/details';
  56. }
  57. const resolveCommonContentData = {
  58. 'none': (item: GetContentListItem[]) => item,
  59. 'common': (item: GetContentListItem[]) => {
  60. item.forEach(it => {
  61. it.bottomTags = it.bottomTags || [];
  62. it.bottomTags = (it.bottomTags as string[]).concat(it.keywords?.length ? it.keywords as string[] : [ it.mainBodyColumnName ]);
  63. })
  64. return item;
  65. },
  66. 'date': (item: GetContentListItem[]) => {
  67. item.forEach(p => {
  68. p.desc = DateUtils.formatDate(p.publishAt, 'YYYY-MM-dd') + ' ' + (p.desc || '');
  69. })
  70. return item;
  71. },
  72. 'form': (item: GetContentListItem[]) => {
  73. item.forEach(p => {
  74. p.desc = `来源:${p.from || '暂无'}` + ' ' + (p.desc || '');
  75. })
  76. return item;
  77. },
  78. 'ich': (item: GetContentListItem[]) => {
  79. item.forEach(it => {
  80. it.bottomTags = (it.bottomTags as string[] || []).concat([
  81. it.levelText as string,
  82. it.ichTypeText as string,
  83. it.batchText as string,
  84. it.regionText as string,
  85. ]);
  86. });
  87. return item;
  88. },
  89. 'inheritor': (item: GetContentListItem[]) => {
  90. item.forEach(it => {
  91. it.bottomTags = (it.bottomTags as string[] || []).concat([
  92. it.age as string,
  93. ]);
  94. });
  95. return item;
  96. },
  97. } as Record<string, (item: GetContentListItem[]) => GetContentListItem[]>
  98. export function resolveCommonContentSolveProps(res: GetContentListItem[], dataSolve: IHomeCommonCategoryListTabListDataSolve[]) {
  99. for (const solve of dataSolve)
  100. res = resolveCommonContentData[solve]?.(res) || res;
  101. return res;
  102. }
  103. export interface HomeCommonCategoryBlockProps {
  104. title?: string,
  105. type?: '',
  106. mainBodyColumnId?: string|number|number[],
  107. modelId?: number,
  108. itemType?: string,
  109. detailsPage: string,
  110. count?: number,
  111. params?: Record<string, any>,
  112. dataSolve?: IHomeCommonCategoryListTabListDataSolve[],
  113. }
  114. /**
  115. * 专用于通用内容的首页小列表控制代码组合
  116. * @param p
  117. * @returns
  118. */
  119. export function useHomeCommonCategoryBlock(p: HomeCommonCategoryBlockProps, loadWhenMounted = true) : IHomeCommonCategoryBlock {
  120. function goDetail(i: GetContentListItem) {
  121. navTo(p.detailsPage === 'byContent' ?
  122. resolveCommonContentGetPageDetailUrlAuto(i):
  123. p.detailsPage,
  124. {
  125. mainBodyColumnId: p.mainBodyColumnId,
  126. modelId: p.modelId,
  127. id: i.id,
  128. })
  129. }
  130. function goList() {
  131. navCommonList({
  132. title: p.title,
  133. mainBodyColumnId: typeof p.mainBodyColumnId == 'object' ?
  134. p.mainBodyColumnId.join(',') :
  135. p.mainBodyColumnId,
  136. modelId: p.modelId,
  137. itemType: p.itemType,
  138. detailsPage: p.detailsPage,
  139. })
  140. }
  141. const loader = useSimpleDataLoader(async () => {
  142. let res = (await CommonContent.getContentList(new GetContentListParams().setSelfValues({
  143. mainBodyColumnId: p.mainBodyColumnId,
  144. modelId: p.modelId,
  145. ...p.params,
  146. }), 1, p.count ?? 4)).list;
  147. return resolveCommonContentSolveProps(res, p.dataSolve || []);;
  148. }, loadWhenMounted);
  149. return {
  150. type: 'CommonCategoryBlock',
  151. loader,
  152. goDetail,
  153. goList,
  154. }
  155. }