CommonListPage.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <template>
  2. <!-- 通用列表页 -->
  3. <view
  4. :class="[
  5. 'common-list-page d-flex flex-column',
  6. hasBg ? 'bg-base' : '',
  7. hasPadding ? 'p-3' : ''
  8. ]"
  9. >
  10. <view v-if="showTab && tabs" class="top-tab bg-base">
  11. <Tabs
  12. :tabs="tabs"
  13. :width="700"
  14. v-model:currentIndex="tabCurrentIndex"
  15. :autoScroll="false"
  16. :defaultItemWidth="180"
  17. :autoItemWidth="tabsScrollable ? false : true"
  18. @click="handleTabClick"
  19. />
  20. </view>
  21. <!-- 搜索 -->
  22. <view v-if="showSearch && showList" class="d-flex flex-col">
  23. <SearchBar
  24. v-model="searchValue"
  25. placeholder="输入关键词搜索"
  26. @search="doSearch"
  27. @cancel="doSearch"
  28. />
  29. </view>
  30. <!-- 下拉框 -->
  31. <view
  32. v-if="dropDownNames.length > 0"
  33. class="d-flex flex-row justify-between align-center mt-2"
  34. :class="[
  35. dropDownVisibleCount >= 3 ? 'justify-around' : ('justify-between')
  36. ]"
  37. >
  38. <template v-for="(drop, k) in dropDownNames" :key="k" >
  39. <SimpleDropDownPicker
  40. v-if="!drop.activeTab || drop.activeTab.includes(tabCurrentId)"
  41. :modelValue="dropDownValues[k]"
  42. :columns="drop.options"
  43. :style="{maxWidth: `${100/dropDownVisibleCount}%`}"
  44. @update:modelValue="(v) => handleChangeDropDownValue(k, v)"
  45. />
  46. </template>
  47. <view
  48. v-if="(showTotal && dropDownVisibleCount < 3 && dropDownVisibleCount != 0)"
  49. class="d-flex flex-row align-center pt-3 pb-3 size-s color-primary text-bold"
  50. >
  51. <text>总共有 {{ listLoader.total }} 个</text>
  52. </view>
  53. </view>
  54. <view
  55. v-if="showTotal && (dropDownVisibleCount >= 3 || dropDownVisibleCount == 0)"
  56. class="d-flex flex-row justify-center align-center mt-3 size-s color-primary text-bold"
  57. >
  58. <text>总共有 {{ listLoader.total }} 个</text>
  59. </view>
  60. <!-- 列表 -->
  61. <slot name="list" :tabId="tabCurrentId" />
  62. <template v-if="showList">
  63. <view class="position-relative d-flex flex-row flex-wrap justify-between align-stretch mt-3">
  64. <view
  65. v-for="(item, i) in listLoader.list.value"
  66. :key="item.id"
  67. :class="[
  68. 'position-relative d-flex flex-grow-1',
  69. itemType.endsWith('-2') ? 'width-1-2' : 'w-100'
  70. ]"
  71. >
  72. <Box2LineLargeImageUserShadow
  73. v-if="itemType.startsWith('image-large')"
  74. class="w-100"
  75. titleColor="title-text"
  76. :classNames="getItemClass(i)"
  77. :image="getImage(item)"
  78. :titleBox="item.titleBox"
  79. :titlePrefix="item.titlePrefix"
  80. :title="item.title"
  81. :desc="item.desc"
  82. :tags="item.bottomTags"
  83. :badge="item.badge"
  84. @click="goDetails(item, item.id)"
  85. />
  86. <Box2LineImageRightShadow
  87. v-else-if="itemType.startsWith('article-common')"
  88. class="w-100"
  89. titleColor="title-text"
  90. :titleBox="item.titleBox"
  91. :titlePrefix="item.titlePrefix"
  92. :classNames="getItemClass(i)"
  93. :image="getImage(item)"
  94. :title="item.title"
  95. :desc="item.desc"
  96. :tags="item.bottomTags"
  97. :badge="item.badge"
  98. :wideImage="true"
  99. @click="goDetails(item, item.id)"
  100. />
  101. <Box2LineImageRightShadow
  102. v-else-if="itemType.startsWith('article-character')"
  103. class="w-100"
  104. :classNames="getItemClass(i)"
  105. :image="getImage(item)"
  106. titleColor="title-text"
  107. :title="item.title"
  108. :titlePrefix="item.titlePrefix"
  109. :titleBox="item.titleBox"
  110. :tags="item.bottomTags || item.keywords"
  111. :desc="item.desc"
  112. :badge="item.badge"
  113. @click="goDetails(item, item.id)"
  114. />
  115. <Box2LineImageRightShadow
  116. v-else-if="itemType.startsWith('simple-text')"
  117. class="w-100"
  118. :classNames="getItemClass(i)"
  119. titleColor="title-text"
  120. :border="false"
  121. :showImage="false"
  122. :title="item.title"
  123. :titlePrefix="item.titlePrefix"
  124. :titleBox="item.titleBox"
  125. :tags="item.bottomTags || item.keywords"
  126. :desc="item.desc"
  127. :badge="item.badge"
  128. @click="goDetails(item, item.id)"
  129. />
  130. <Grid4Item
  131. v-else-if="itemType.startsWith('image-small')"
  132. :title="item.title"
  133. :image="item.image"
  134. :classNames="itemType.endsWith('-2') ? 'half' : 'full'"
  135. @click="goDetails(item, item.id)"
  136. />
  137. <Box2LineRightShadow
  138. v-else
  139. :key="i"
  140. :title="item.title"
  141. :titleBox="item.titleBox"
  142. :desc="item.desc"
  143. :tags="(item.bottomTags as string[])"
  144. @click="goDetails(item, item.id)"
  145. />
  146. </view>
  147. <view v-if="itemType.endsWith('-2') && listLoader.list.value.length % 2 != 0" class="width-1-2" />
  148. </view>
  149. <Empty v-if="listLoader.list.value.length == 0 && listLoader.loadStatus.value !== 'loading'" :description="emptyText" />
  150. <SimplePageListLoader v-else :loader="listLoader" />
  151. </template>
  152. </view>
  153. </template>
  154. <script setup lang="ts">
  155. import { computed, nextTick, onMounted, ref, watch } from 'vue';
  156. import { useSimplePageListLoader } from '@/common/composeabe/SimplePageListLoader';
  157. import { navTo } from '@/components/utils/PageAction';
  158. import SimplePageListLoader from '@/common/components/SimplePageListLoader.vue';
  159. import Box2LineLargeImageUserShadow from '@/pages/parts/Box2LineLargeImageUserShadow.vue';
  160. import Box2LineImageRightShadow from '@/pages/parts/Box2LineImageRightShadow.vue';
  161. import SimpleDropDownPicker, { type SimpleDropDownPickerItem } from '@/common/components/SimpleDropDownPicker.vue';
  162. import Tabs from '@/components/nav/Tabs.vue';
  163. import SearchBar from '@/components/form/SearchBar.vue';
  164. import Empty from '@/components/feedback/Empty.vue';
  165. import { navigateToAutoContent, resolveCommonContentGetPageDetailUrlAuto } from './CommonContent';
  166. import { doDynamicNavDetails, type CommonListPageItemType } from './CommonListPage';
  167. import Grid4Item from '@/pages/parts/Grid4Item.vue';
  168. import Box2LineRightShadow from '@/pages/parts/Box2LineRightShadow.vue';
  169. import { isDynamicScript } from '../data/CommonCategoryScript';
  170. function getImage(item: any) {
  171. return item.thumbnail || item.image
  172. }
  173. function getItemClass(index: number) {
  174. return props.itemType.endsWith('-2') ? (index % 2 != 0 ? 'ml-1' : 'mr-1') : ''
  175. }
  176. export interface DropDownNames {
  177. /**
  178. * 下拉框选项
  179. */
  180. options: SimpleDropDownPickerItem[],
  181. /**
  182. * 默认选中值
  183. */
  184. defaultSelectedValue: number|string,
  185. /**
  186. * 显示Tab的ID
  187. */
  188. activeTab?: number[],
  189. }
  190. export interface CommonListItem extends Record<string, any> {
  191. id: number,
  192. image: string,
  193. title: string,
  194. }
  195. export interface CommonListPageProps {
  196. /**
  197. * 标题
  198. */
  199. title?: string
  200. /**
  201. * 分组标签
  202. */
  203. tabs?: {
  204. id: number,
  205. text: string,
  206. onlyJump?: boolean,
  207. jump?: () => void,
  208. width?: number,
  209. }[] | null
  210. /**
  211. * 标签是否可滚动
  212. * @default false
  213. */
  214. tabsScrollable?: boolean
  215. /**
  216. * 是否显示搜索框
  217. * @default true
  218. */
  219. showSearch?: boolean
  220. /**
  221. * 是否显示Tab
  222. * @default true
  223. */
  224. showTab?: boolean
  225. /**
  226. * 显示总数
  227. * @default false
  228. */
  229. showTotal?: boolean
  230. /**
  231. * 显示列表的Tab ID。默认所有Tab都显示列表。
  232. * @default undefined
  233. */
  234. showListTabIds?: number[]
  235. /**
  236. * 下拉框选项控制
  237. * @default []
  238. */
  239. dropDownNames?: DropDownNames[]
  240. /**
  241. * 列表项类型
  242. * @default 'article-common'
  243. */
  244. itemType?: CommonListPageItemType
  245. /**
  246. * 分页大小
  247. * @default 8
  248. */
  249. pageSize?: number
  250. /**
  251. * 加载数据函数
  252. * @param page 页码,从1开始
  253. * @param pageSize 分页大小
  254. * @param searchText 搜索文本
  255. * @param dropDownValues 下拉框值
  256. */
  257. load: (page: number, pageSize: number, searchText: string, dropDownValues: number[], tabSelect: number) => Promise<{ list: CommonListItem[], total: number }>
  258. /**
  259. * 点击详情跳转页面路径
  260. * 可以是字符串路径,也可以是对象数组,每个对象包含路径和参数
  261. * * 特殊值:byContent 表示根据 detailsPageByContentCallback 函数返回值跳转。
  262. * * 特殊值:disabled 表示不跳转。
  263. * * 特殊值:custom 表示自定义跳转,需要通过 goCustomDetails(item, id) 事件处理。
  264. */
  265. detailsPage?: string | Record<string, string|{
  266. page: string,
  267. params: Record<string, any>,
  268. }>
  269. /**
  270. * 根据内容项返回跳转路径的回调函数
  271. */
  272. detailsPageByContentCallback?: (item: any) => string
  273. /**
  274. * 详情跳转页面参数
  275. */
  276. detailsParams?: Record<string, any>
  277. /**
  278. * 是否有背景
  279. * @default true
  280. */
  281. hasBg?: boolean
  282. /**
  283. * 是否有内边距
  284. * @default true
  285. */
  286. hasPadding?: boolean
  287. /**
  288. * 起始标签索引
  289. * @default 0
  290. */
  291. startTabIndex?: number | undefined
  292. /**
  293. * 挂载时是否加载数据
  294. * @default true
  295. */
  296. loadMounted?: boolean,
  297. /**
  298. * 空数据时显示的文本
  299. * @default '暂无数据'
  300. */
  301. emptyText?: string,
  302. }
  303. const props = withDefaults(defineProps<CommonListPageProps>(), {
  304. title: '',
  305. tabs: null,
  306. tabsScrollable: false,
  307. showSearch: true,
  308. showTab: true,
  309. showTotal: false,
  310. dropDownNames: () => [],
  311. detailsPageByContentCallback: resolveCommonContentGetPageDetailUrlAuto,
  312. itemType: 'article-common',
  313. pageSize: 8,
  314. detailsPage: '/pages/article/details',
  315. detailsParams: () => ({}),
  316. hasBg: true,
  317. hasPadding: true,
  318. startTabIndex: undefined,
  319. loadMounted: true,
  320. emptyText: '暂无数据',
  321. })
  322. const emit = defineEmits([ 'goCustomDetails' ])
  323. const dropDownVisibleCount = computed(() => {
  324. let c = 0;
  325. for (const element of props.dropDownNames) {
  326. if (!element.activeTab || element.activeTab.includes(tabCurrentId.value))
  327. c++;
  328. }
  329. return c;
  330. })
  331. const dropDownValues = ref<any>([]);
  332. const searchValue = ref('');
  333. const listLoader = useSimplePageListLoader(props.pageSize, async (page, pageSize) => {
  334. return await props.load(
  335. page, pageSize,
  336. searchValue.value,
  337. getDropDownValues(),
  338. props.tabs?.[tabCurrentIndex.value]?.id ?? tabCurrentIndex.value,
  339. )
  340. });
  341. const tabCurrentIndex = ref(0)
  342. const tabCurrentId = computed(() => props.tabs?.[tabCurrentIndex.value]?.id ?? -1)
  343. const showList = computed(() => !props.showListTabIds || props.showListTabIds.includes(tabCurrentId.value))
  344. function getDropDownValues() {
  345. const result = [] as number[];
  346. let c = 0;
  347. for (const element of props.dropDownNames) {
  348. if (!element.activeTab || element.activeTab.includes(tabCurrentId.value))
  349. result.push(dropDownValues.value[c]);
  350. c++;
  351. }
  352. return result;
  353. }
  354. function handleChangeDropDownValue(index: number, value: number) {
  355. dropDownValues.value[index] = value;
  356. listLoader.loadData(undefined, true);
  357. }
  358. function handleTabClick(e: any) {
  359. nextTick(() => {
  360. if (e.jump) {
  361. e.jump?.();
  362. tabCurrentIndex.value = 0;
  363. return;
  364. }
  365. listLoader.loadData(undefined, true);
  366. })
  367. }
  368. function doSearch() {
  369. listLoader.loadData(undefined, true);
  370. }
  371. function goDetails(item: any, id: number) {
  372. if (props.detailsPage == 'disabled')
  373. return;
  374. if (props.detailsPage == 'custom') {
  375. emit('goCustomDetails', item, id)
  376. return;
  377. }
  378. if (props.detailsPage == 'byContent') {
  379. handleByContent();
  380. return;
  381. }
  382. if (typeof props.detailsPage === 'string' && isDynamicScript(props.detailsPage)) {
  383. doDynamicNavDetails(props.detailsPage, item, {});
  384. return;
  385. }
  386. function handleByContent() {
  387. if (props.detailsPageByContentCallback) {
  388. const page = props.detailsPageByContentCallback?.(item);
  389. if (page) {
  390. navTo(page, {
  391. ...props.detailsParams,
  392. id
  393. })
  394. }
  395. } else {
  396. navigateToAutoContent(item, {
  397. ...props.detailsParams,
  398. id
  399. })
  400. }
  401. }
  402. const page = typeof props.detailsPage === 'object' ? props.detailsPage[tabCurrentId.value] : undefined;
  403. if (page) {
  404. if (typeof page === 'string') {
  405. if (page == 'byContent') {
  406. handleByContent();
  407. return;
  408. }
  409. navTo(page, {
  410. ...props.detailsParams,
  411. id
  412. })
  413. return;
  414. }
  415. if (typeof page === 'object') {
  416. const item = page as {
  417. page: string,
  418. params: Record<string, any>,
  419. };
  420. if (item.page == 'byContent') {
  421. handleByContent();
  422. return;
  423. }
  424. navTo(item.page, {
  425. ...item.params,
  426. ...props.detailsParams,
  427. id
  428. })
  429. return;
  430. }
  431. } else if (typeof props.detailsPage === 'object') {
  432. handleByContent();
  433. return;
  434. }
  435. navTo(props.detailsPage as string, {
  436. ...props.detailsParams,
  437. id
  438. })
  439. }
  440. function loadDropDownValues() {
  441. dropDownValues.value = [];
  442. for (const element of props.dropDownNames) {
  443. dropDownValues.value.push(element.defaultSelectedValue);
  444. }
  445. }
  446. watch(tabCurrentIndex, () => {
  447. listLoader.loadData(undefined, true);
  448. });
  449. watch(() => props.startTabIndex, () => {
  450. if (props.startTabIndex) {
  451. tabCurrentIndex.value = props.startTabIndex;
  452. }
  453. });
  454. watch(() => props.dropDownNames.length, () => {
  455. loadDropDownValues();
  456. listLoader.loadData(undefined, true);
  457. });
  458. watch(() => props.title, () => {
  459. if (props.title) {
  460. uni.setNavigationBarTitle({ title: props.title })
  461. }
  462. });
  463. defineExpose({
  464. load: () => {
  465. listLoader.loadData(undefined, true);
  466. },
  467. })
  468. onMounted(() => {
  469. if (props.startTabIndex)
  470. tabCurrentIndex.value = props.startTabIndex;
  471. if (props.title)
  472. uni.setNavigationBarTitle({ title: props.title, })
  473. loadDropDownValues();
  474. setTimeout(() => {
  475. if (props.loadMounted && showList.value) {
  476. listLoader.loadData(undefined, true);
  477. }
  478. }, 500);
  479. });
  480. </script>
  481. <style lang="scss">
  482. .common-list-page {
  483. min-height: 100vh;
  484. }
  485. </style>