CommonListPage.vue 12 KB

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