CommonListPage.vue 12 KB

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