CommonListPage.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. </view>
  131. <view v-if="itemType.endsWith('-2') && listLoader.list.value.length % 2 != 0" class="width-1-2" />
  132. </view>
  133. <Empty v-if="listLoader.list.value.length == 0 && listLoader.loadStatus.value !== 'loading'" :description="emptyText" />
  134. <SimplePageListLoader v-else :loader="listLoader" />
  135. </template>
  136. </view>
  137. </template>
  138. <script setup lang="ts">
  139. import { computed, nextTick, onMounted, ref, watch } from 'vue';
  140. import { useSimplePageListLoader } from '@/common/composeabe/SimplePageListLoader';
  141. import { navTo } from '@/components/utils/PageAction';
  142. import SimplePageListLoader from '@/common/components/SimplePageListLoader.vue';
  143. import Box2LineLargeImageUserShadow from '@/pages/parts/Box2LineLargeImageUserShadow.vue';
  144. import Box2LineImageRightShadow from '@/pages/parts/Box2LineImageRightShadow.vue';
  145. import SimpleDropDownPicker, { type SimpleDropDownPickerItem } from '@/common/components/SimpleDropDownPicker.vue';
  146. import AppCofig from '@/common/config/AppCofig';
  147. import Tabs from '@/components/nav/Tabs.vue';
  148. import SearchBar from '@/components/form/SearchBar.vue';
  149. import Empty from '@/components/feedback/Empty.vue';
  150. import { resolveCommonContentGetPageDetailUrlAuto } from './CommonContent';
  151. import type { CommonListPageItemType } from './CommonListPage';
  152. function getImage(item: any) {
  153. return item.thumbnail || item.image || AppCofig.defaultImage
  154. }
  155. function getItemClass(index: number) {
  156. return props.itemType.endsWith('-2') ? (index % 2 != 0 ? 'ml-1' : 'mr-1') : ''
  157. }
  158. export interface DropDownNames {
  159. /**
  160. * 下拉框选项
  161. */
  162. options: SimpleDropDownPickerItem[],
  163. /**
  164. * 默认选中值
  165. */
  166. defaultSelectedValue: number|string,
  167. /**
  168. * 显示Tab的ID
  169. */
  170. activeTab?: number[],
  171. }
  172. export interface CommonListItem extends Record<string, any> {
  173. id: number,
  174. image: string,
  175. title: string,
  176. }
  177. export interface CommonListPageProps {
  178. /**
  179. * 标题
  180. */
  181. title?: string
  182. /**
  183. * 分组标签
  184. */
  185. tabs?: {
  186. id: number,
  187. text: string,
  188. onlyJump?: boolean,
  189. jump?: () => void,
  190. width?: number,
  191. }[] | null
  192. /**
  193. * 标签是否可滚动
  194. * @default false
  195. */
  196. tabsScrollable?: boolean
  197. /**
  198. * 是否显示搜索框
  199. * @default true
  200. */
  201. showSearch?: boolean
  202. /**
  203. * 是否显示Tab
  204. * @default true
  205. */
  206. showTab?: boolean
  207. /**
  208. * 显示总数
  209. * @default false
  210. */
  211. showTotal?: boolean
  212. /**
  213. * 显示列表的Tab ID。默认所有Tab都显示列表。
  214. * @default undefined
  215. */
  216. showListTabIds?: number[]
  217. /**
  218. * 下拉框选项控制
  219. * @default []
  220. */
  221. dropDownNames?: DropDownNames[]
  222. /**
  223. * 列表项类型
  224. * @default 'article-common'
  225. */
  226. itemType?: CommonListPageItemType
  227. /**
  228. * 分页大小
  229. * @default 8
  230. */
  231. pageSize?: number
  232. /**
  233. * 加载数据函数
  234. * @param page 页码,从1开始
  235. * @param pageSize 分页大小
  236. * @param searchText 搜索文本
  237. * @param dropDownValues 下拉框值
  238. */
  239. load: (page: number, pageSize: number, searchText: string, dropDownValues: number[], tabSelect: number) => Promise<{ list: CommonListItem[], total: number }>
  240. /**
  241. * 点击详情跳转页面路径
  242. * 可以是字符串路径,也可以是对象数组,每个对象包含路径和参数
  243. * * 特殊值:byContent 表示根据 detailsPageByContentCallback 函数返回值跳转。
  244. * * 特殊值:disabled 表示不跳转。
  245. * * 特殊值:custom 表示自定义跳转,需要通过 goCustomDetails(item, id) 事件处理。
  246. */
  247. detailsPage?: string | Record<string, string|{
  248. page: string,
  249. params: Record<string, any>,
  250. }>
  251. /**
  252. * 根据内容项返回跳转路径的回调函数
  253. */
  254. detailsPageByContentCallback?: (item: any) => string
  255. /**
  256. * 详情跳转页面参数
  257. */
  258. detailsParams?: Record<string, any>
  259. /**
  260. * 是否有背景
  261. * @default true
  262. */
  263. hasBg?: boolean
  264. /**
  265. * 是否有内边距
  266. * @default true
  267. */
  268. hasPadding?: boolean
  269. /**
  270. * 起始标签索引
  271. * @default 0
  272. */
  273. startTabIndex?: number | undefined
  274. /**
  275. * 挂载时是否加载数据
  276. * @default true
  277. */
  278. loadMounted?: boolean,
  279. /**
  280. * 空数据时显示的文本
  281. * @default '暂无数据'
  282. */
  283. emptyText?: string,
  284. }
  285. const props = withDefaults(defineProps<CommonListPageProps>(), {
  286. title: '',
  287. tabs: null,
  288. tabsScrollable: false,
  289. showSearch: true,
  290. showTab: true,
  291. showTotal: false,
  292. dropDownNames: () => [],
  293. detailsPageByContentCallback: resolveCommonContentGetPageDetailUrlAuto,
  294. itemType: 'article-common',
  295. pageSize: 8,
  296. detailsPage: '/pages/article/details',
  297. detailsParams: () => ({}),
  298. hasBg: true,
  299. hasPadding: true,
  300. startTabIndex: undefined,
  301. loadMounted: true,
  302. emptyText: '暂无数据',
  303. })
  304. const emit = defineEmits([ 'goCustomDetails' ])
  305. const dropDownVisibleCount = computed(() => {
  306. let c = 0;
  307. for (const element of props.dropDownNames) {
  308. if (!element.activeTab || element.activeTab.includes(tabCurrentId.value))
  309. c++;
  310. }
  311. return c;
  312. })
  313. const dropDownValues = ref<any>([]);
  314. const searchValue = ref('');
  315. const listLoader = useSimplePageListLoader(props.pageSize, async (page, pageSize) => {
  316. return await props.load(
  317. page, pageSize,
  318. searchValue.value,
  319. getDropDownValues(),
  320. props.tabs?.[tabCurrentIndex.value]?.id ?? tabCurrentIndex.value,
  321. )
  322. });
  323. const tabCurrentIndex = ref(0)
  324. const tabCurrentId = computed(() => props.tabs?.[tabCurrentIndex.value]?.id ?? -1)
  325. const showList = computed(() => !props.showListTabIds || props.showListTabIds.includes(tabCurrentId.value))
  326. function getDropDownValues() {
  327. const result = [] as number[];
  328. let c = 0;
  329. for (const element of props.dropDownNames) {
  330. if (!element.activeTab || element.activeTab.includes(tabCurrentId.value))
  331. result.push(dropDownValues.value[c]);
  332. c++;
  333. }
  334. return result;
  335. }
  336. function handleChangeDropDownValue(index: number, value: number) {
  337. dropDownValues.value[index] = value;
  338. listLoader.loadData(undefined, true);
  339. }
  340. function handleTabClick(e: any) {
  341. nextTick(() => {
  342. if (e.jump) {
  343. e.jump?.();
  344. tabCurrentIndex.value = 0;
  345. return;
  346. }
  347. listLoader.loadData(undefined, true);
  348. })
  349. }
  350. function doSearch() {
  351. listLoader.loadData(undefined, true);
  352. }
  353. function goDetails(item: any, id: number) {
  354. if (props.detailsPage == 'disabled')
  355. return;
  356. if (props.detailsPage == 'custom') {
  357. emit('goCustomDetails', item, id)
  358. return;
  359. }
  360. if (props.detailsPage == 'byContent') {
  361. if (handleByContent())
  362. return;
  363. }
  364. function handleByContent() {
  365. const page = props.detailsPageByContentCallback?.(item);
  366. if (page) {
  367. navTo(page, {
  368. ...props.detailsParams,
  369. id
  370. })
  371. return true;
  372. }
  373. return false;
  374. }
  375. const page = typeof props.detailsPage === 'object' ? props.detailsPage[tabCurrentId.value] : undefined;
  376. if (page) {
  377. if (typeof page === 'string') {
  378. if (page == 'byContent' && handleByContent())
  379. return;
  380. navTo(page, {
  381. ...props.detailsParams,
  382. id
  383. })
  384. return;
  385. }
  386. if (typeof page === 'object') {
  387. const item = page as {
  388. page: string,
  389. params: Record<string, any>,
  390. };
  391. if (item.page == 'byContent' && handleByContent())
  392. return;
  393. navTo(item.page, {
  394. ...item.params,
  395. ...props.detailsParams,
  396. id
  397. })
  398. return;
  399. }
  400. } else if (typeof props.detailsPage === 'object') {
  401. if (handleByContent())
  402. return;
  403. navTo('/pages/article/details', {
  404. ...props.detailsParams,
  405. id
  406. })
  407. return;
  408. }
  409. navTo(props.detailsPage as string, {
  410. ...props.detailsParams,
  411. id
  412. })
  413. }
  414. function loadDropDownValues() {
  415. dropDownValues.value = [];
  416. for (const element of props.dropDownNames) {
  417. dropDownValues.value.push(element.defaultSelectedValue);
  418. }
  419. }
  420. watch(tabCurrentIndex, () => {
  421. listLoader.loadData(undefined, true);
  422. });
  423. watch(() => props.startTabIndex, () => {
  424. if (props.startTabIndex) {
  425. tabCurrentIndex.value = props.startTabIndex;
  426. }
  427. });
  428. watch(() => props.dropDownNames.length, () => {
  429. loadDropDownValues();
  430. listLoader.loadData(undefined, true);
  431. });
  432. watch(() => props.title, () => {
  433. if (props.title) {
  434. uni.setNavigationBarTitle({ title: props.title })
  435. }
  436. });
  437. defineExpose({
  438. load: () => {
  439. listLoader.loadData(undefined, true);
  440. },
  441. })
  442. onMounted(() => {
  443. if (props.startTabIndex)
  444. tabCurrentIndex.value = props.startTabIndex;
  445. if (props.title)
  446. uni.setNavigationBarTitle({ title: props.title, })
  447. loadDropDownValues();
  448. setTimeout(() => {
  449. if (props.loadMounted && showList.value) {
  450. listLoader.loadData(undefined, true);
  451. }
  452. }, 500);
  453. });
  454. </script>
  455. <style lang="scss">
  456. .common-list-page {
  457. min-height: 100vh;
  458. }
  459. </style>