西藏巴青项目

index.vue 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. <template>
  2. <!-- 畜牧资源:参考养殖资讯 — 顶区搜索 + up-tabs(无右侧插槽、无 up-popup);下区虚拟列表 -->
  3. <view :class="pageRootClass" class="tab-page lr-page" :style="pageStyle">
  4. <view class="lr-top">
  5. <up-search
  6. v-model="searchKeyword"
  7. shape="round"
  8. :placeholder="$t('livestockResourcesPage.searchPlaceholder')"
  9. :show-action="false"
  10. :clearabled="true"
  11. bg-color="#f5f2ef"
  12. border-color="#e5ded6"
  13. />
  14. <up-tabs
  15. :current="tabCurrentIndex"
  16. class="lr-tabs"
  17. :list="tabsList"
  18. key-name="name"
  19. :scrollable="false"
  20. line-color="#22C55E"
  21. :active-style="{ color: '#15803d', fontWeight: '600' }"
  22. :inactive-style="{ color: '#78716c' }"
  23. @update:current="tabCurrentIndex = $event"
  24. @change="onTabsChange"
  25. />
  26. </view>
  27. <view class="lr-body">
  28. <view v-if="listLoading && !articles.length" class="lr-empty">
  29. <text class="text-body lr-empty__txt">{{ $t('livestockResourcesPage.loading') }}</text>
  30. </view>
  31. <view v-else-if="!articles.length" class="lr-empty">
  32. <text class="text-body lr-empty__txt">{{ $t('livestockResourcesPage.empty') }}</text>
  33. </view>
  34. <view v-else class="lr-list-wrap">
  35. <up-virtual-list
  36. ref="lrVList"
  37. class="lr-vlist"
  38. :list-data="articles"
  39. :item-height="slotHeightPx"
  40. :height="listHeightPx"
  41. :buffer="6"
  42. key-field="id"
  43. :scroll-top="vScrollTop"
  44. @update:scrollTop="vScrollTop = $event"
  45. @scroll="onVirtualListScroll"
  46. >
  47. <template #default="{ item }">
  48. <view class="lr-row-cell">
  49. <view class="lr-row" :style="{ height: rowBodyPx + 'px' }">
  50. <view class="lr-row__left" role="button" @click="openResourceDetail(item)">
  51. <text class="lr-row__title">{{ item.title }}</text>
  52. <text class="text-body lr-row__summary">{{ item.introduction || $t('livestockResourcesPage.noIntro') }}</text>
  53. <view class="lr-row__meta">
  54. <text class="lr-row__meta-tag text-body">{{ tabTitle(item.type) }}</text>
  55. <text class="lr-row__meta-date text-body">{{ item.publishTime || $t('livestockResourcesPage.noDate') }}</text>
  56. </view>
  57. </view>
  58. <view class="lr-row__right">
  59. <up-lazy-load
  60. class="lr-lazy-cover"
  61. role="button"
  62. :image="articleCover(item)"
  63. height="120"
  64. :border-radius="12"
  65. img-mode="aspectFill"
  66. :threshold="200"
  67. :index="item.id"
  68. @click="() => onPreviewCover(item)"
  69. />
  70. </view>
  71. </view>
  72. </view>
  73. </template>
  74. </up-virtual-list>
  75. </view>
  76. </view>
  77. </view>
  78. </template>
  79. <script>
  80. import USearch from 'uview-plus/components/u-search/u-search.vue'
  81. import UTabs from 'uview-plus/components/u-tabs/u-tabs.vue'
  82. import ULazyLoad from 'uview-plus/components/u-lazy-load/u-lazy-load.vue'
  83. import UVirtualList from 'uview-plus/components/u-virtual-list/u-virtual-list.vue'
  84. import tabPage from '@/mixins/tabPage'
  85. import { resolveResourceUrl } from '@/utils/resourceUrl'
  86. import { listLivestockResource } from '@/api/livestockResource'
  87. /** Tab 与资源类型编码(§1.2) */
  88. const TAB_TYPE_CODES = ['004002', '004006', '004007', '004008']
  89. const TAB_I18N_KEYS = ['t1', 't2', 't3', 't4']
  90. /** 列表分页(接口默认 10,上限 50) */
  91. const LIST_PAGE_SIZE = 20
  92. const ROW_BODY_RPX = 260
  93. const ROW_GAP_RPX = 20
  94. /** lr-body 上下 padding(16 + 24 rpx) */
  95. const LR_BODY_PAD_RPX = 40
  96. const COVER = '/static/ai/hero.png'
  97. const LIVESTOCK_DETAIL_PATH = '/package-a/livestock-resource-detail/index'
  98. export default {
  99. components: {
  100. 'up-search': USearch,
  101. 'up-tabs': UTabs,
  102. 'up-lazy-load': ULazyLoad,
  103. 'up-virtual-list': UVirtualList
  104. },
  105. mixins: [tabPage],
  106. data() {
  107. return {
  108. navTitleKey: 'livestockResourcesPage.navTitle',
  109. searchKeyword: '',
  110. tabCurrentIndex: 0,
  111. articles: [],
  112. listLoading: false,
  113. listLoadingMore: false,
  114. listTotal: 0,
  115. pageNum: 1,
  116. pageSize: LIST_PAGE_SIZE,
  117. _searchTimer: null,
  118. loadMoreTimer: null,
  119. rowBodyPx: 120,
  120. marginPx: 10,
  121. slotHeightPx: 130,
  122. listHeightPx: 400,
  123. pageHeightPx: 0,
  124. lrTopHeightPx: 0,
  125. vScrollTop: 0,
  126. coverSrc: COVER
  127. }
  128. },
  129. computed: {
  130. tabsList() {
  131. return TAB_TYPE_CODES.map((code, i) => ({
  132. id: code,
  133. name: this.$t(`livestockResourcesPage.tabs.${TAB_I18N_KEYS[i]}`)
  134. }))
  135. },
  136. selectedTabId() {
  137. const row = this.tabsList[this.tabCurrentIndex]
  138. return row ? row.id : TAB_TYPE_CODES[0]
  139. },
  140. listNoMore() {
  141. return this.listTotal > 0 && this.articles.length >= this.listTotal
  142. },
  143. pageStyle() {
  144. if (this.pageHeightPx > 0) {
  145. return { height: `${this.pageHeightPx}px` }
  146. }
  147. return {}
  148. }
  149. },
  150. watch: {
  151. searchKeyword() {
  152. this.vScrollTop = 0
  153. clearTimeout(this._searchTimer)
  154. this._searchTimer = setTimeout(() => {
  155. this.loadResourceList(true)
  156. }, 300)
  157. },
  158. tabCurrentIndex() {
  159. this.vScrollTop = 0
  160. this.loadResourceList(true)
  161. }
  162. },
  163. created() {
  164. this.loadResourceList(true)
  165. },
  166. onReady() {
  167. try {
  168. this.marginPx = Math.ceil(uni.upx2px(ROW_GAP_RPX))
  169. this.rowBodyPx = Math.max(100, Math.ceil(uni.upx2px(ROW_BODY_RPX)))
  170. this.slotHeightPx = this.rowBodyPx + this.marginPx
  171. } catch (e) {
  172. this.marginPx = 10
  173. this.rowBodyPx = 120
  174. this.slotHeightPx = 130
  175. }
  176. this.applyListHeightFallback()
  177. this.$nextTick(() => this.calcLayoutHeights())
  178. },
  179. onShow() {
  180. this.$nextTick(() => this.calcLayoutHeights())
  181. },
  182. onUnload() {
  183. if (this.loadMoreTimer) clearTimeout(this.loadMoreTimer)
  184. if (this._searchTimer) clearTimeout(this._searchTimer)
  185. },
  186. methods: {
  187. mapResourceRow(row) {
  188. const resourceId = row.id
  189. const sourceType = row.sourceType
  190. const type = row.type || ''
  191. const publishTime = row.publishTime || ''
  192. const title = row.title || ''
  193. return {
  194. id: `${sourceType}-${resourceId}`,
  195. resourceId,
  196. sourceType,
  197. title,
  198. introduction: row.introduction || '',
  199. type,
  200. coverFileUrl: row.coverFileUrl || '',
  201. contentFileUrl: row.contentFileUrl || '',
  202. publishTime
  203. }
  204. },
  205. loadResourceList(reset = false) {
  206. const type = this.selectedTabId
  207. if (!type) {
  208. this.articles = []
  209. this.listTotal = 0
  210. return Promise.resolve()
  211. }
  212. if (!reset) {
  213. if (this.listLoading || this.listLoadingMore || this.listNoMore) {
  214. return Promise.resolve()
  215. }
  216. this.pageNum += 1
  217. this.listLoadingMore = true
  218. } else {
  219. this.pageNum = 1
  220. this.listLoading = true
  221. }
  222. const params = {
  223. type,
  224. pageNum: this.pageNum,
  225. pageSize: this.pageSize
  226. }
  227. const titleKw = (this.searchKeyword || '').trim()
  228. if (titleKw) {
  229. params.title = titleKw
  230. }
  231. return listLivestockResource(params)
  232. .then((res) => {
  233. const rows = res.rows || []
  234. this.listTotal = res.total != null ? Number(res.total) : 0
  235. const mapped = rows.map((row) => this.mapResourceRow(row))
  236. this.articles = reset ? mapped : this.articles.concat(mapped)
  237. this.$nextTick(() => {
  238. this.calcLayoutHeights()
  239. if (this.articles.length < this.listTotal) {
  240. this.tryAutoLoadMore()
  241. }
  242. })
  243. })
  244. .catch(() => {
  245. if (reset) {
  246. this.articles = []
  247. this.listTotal = 0
  248. } else {
  249. this.pageNum -= 1
  250. }
  251. })
  252. .finally(() => {
  253. if (reset) {
  254. this.listLoading = false
  255. } else {
  256. this.listLoadingMore = false
  257. }
  258. })
  259. },
  260. tryAutoLoadMore() {
  261. if (this.listLoading || this.listLoadingMore || this.listNoMore) return
  262. const totalH = this.articles.length * this.slotHeightPx
  263. const viewH = this.listHeightPx
  264. if (totalH > 0 && totalH <= viewH) {
  265. this.loadResourceList(false)
  266. }
  267. },
  268. onVirtualListScroll(scrollTop) {
  269. if (this.loadMoreTimer) clearTimeout(this.loadMoreTimer)
  270. this.loadMoreTimer = setTimeout(() => {
  271. this.loadMoreTimer = null
  272. this.tryLoadMoreOnScroll(typeof scrollTop === 'number' ? scrollTop : 0)
  273. }, 180)
  274. },
  275. tryLoadMoreOnScroll(scrollTop) {
  276. if (this.listLoading || this.listLoadingMore || this.listNoMore) return
  277. const totalH = this.articles.length * this.slotHeightPx
  278. const viewH = this.listHeightPx
  279. const threshold = Math.max(this.slotHeightPx * 2, 120)
  280. if (totalH <= viewH) {
  281. this.loadResourceList(false)
  282. return
  283. }
  284. if (scrollTop + viewH >= totalH - threshold) {
  285. this.loadResourceList(false)
  286. }
  287. },
  288. /** 系统导航栏高度(状态栏 + 标题栏) */
  289. getNavigationBarHeight() {
  290. const sys = uni.getSystemInfoSync()
  291. const statusBar = sys.statusBarHeight || 0
  292. // #ifdef MP-WEIXIN
  293. try {
  294. const menu = uni.getMenuButtonBoundingClientRect()
  295. if (menu && menu.height) {
  296. return statusBar + (menu.top - statusBar) * 2 + menu.height
  297. }
  298. } catch (e) {
  299. /* noop */
  300. }
  301. // #endif
  302. return statusBar + 44
  303. },
  304. /** 视口可用高度 = 屏幕高度 - 导航栏 */
  305. getViewportContentHeight() {
  306. const sys = uni.getSystemInfoSync()
  307. const navH = this.getNavigationBarHeight()
  308. // #ifdef H5
  309. if (typeof window !== 'undefined' && window.innerHeight) {
  310. return Math.max(320, window.innerHeight - navH)
  311. }
  312. // #endif
  313. const screenH = sys.screenHeight || sys.windowHeight || 600
  314. return Math.max(320, screenH - navH)
  315. },
  316. applyListHeightFallback() {
  317. const contentH = this.getViewportContentHeight()
  318. const lrTopFallback = Math.ceil(uni.upx2px(200))
  319. const bodyPad = Math.ceil(uni.upx2px(LR_BODY_PAD_RPX))
  320. this.pageHeightPx = contentH
  321. this.lrTopHeightPx = lrTopFallback
  322. this.listHeightPx = Math.max(240, contentH - lrTopFallback - bodyPad)
  323. },
  324. calcLayoutHeights() {
  325. const contentH = this.getViewportContentHeight()
  326. const bodyPad = Math.ceil(uni.upx2px(LR_BODY_PAD_RPX))
  327. this.pageHeightPx = contentH
  328. uni.createSelectorQuery()
  329. .in(this)
  330. .select('.lr-top')
  331. .boundingClientRect((topRect) => {
  332. const lrTopH =
  333. topRect && topRect.height > 0 ? Math.ceil(topRect.height) : Math.ceil(uni.upx2px(200))
  334. this.lrTopHeightPx = lrTopH
  335. // 列表高度 = 视口 - 导航栏(已扣) - lr-top - lr-body 内边距
  336. this.listHeightPx = Math.max(240, contentH - lrTopH - bodyPad)
  337. this.$nextTick(() => {
  338. this.$refs.lrVList?.measureContainerHeight?.()
  339. })
  340. })
  341. .exec()
  342. },
  343. tabTitle(typeCode) {
  344. const row = this.tabsList.find((t) => t.id === typeCode)
  345. return row ? row.name : String(typeCode || '')
  346. },
  347. articleCover(item) {
  348. if (item.coverFileUrl) {
  349. return resolveResourceUrl(item.coverFileUrl)
  350. }
  351. return this.coverSrc
  352. },
  353. onPreviewCover(item) {
  354. const url = this.articleCover(item)
  355. if (!url) return
  356. uni.previewImage({ urls: [url], current: 0 })
  357. },
  358. openResourceDetail(item) {
  359. if (!item || item.resourceId == null || !item.sourceType) {
  360. uni.showToast({ title: this.$t('livestockResourceDetailPage.invalidParams'), icon: 'none' })
  361. return
  362. }
  363. const q = [
  364. `id=${encodeURIComponent(String(item.resourceId))}`,
  365. `sourceType=${encodeURIComponent(String(item.sourceType))}`,
  366. `typeCode=${encodeURIComponent(item.type || '')}`
  367. ].join('&')
  368. uni.navigateTo({ url: `${LIVESTOCK_DETAIL_PATH}?${q}` })
  369. },
  370. onTabsChange(_item, index) {
  371. this.tabCurrentIndex = typeof index === 'number' ? index : this.tabCurrentIndex
  372. }
  373. }
  374. }
  375. </script>
  376. <style lang="scss" scoped>
  377. @import '@/styles/morandi.scss';
  378. @import '@/styles/tab-page.scss';
  379. .lr-page {
  380. display: flex;
  381. flex-direction: column;
  382. min-width: 0;
  383. width: 100%;
  384. height: 100%;
  385. min-height: 100%;
  386. overflow: hidden;
  387. box-sizing: border-box;
  388. background: $morandi-bg-page;
  389. }
  390. .lr-top {
  391. flex-shrink: 0;
  392. display: flex;
  393. flex-direction: column;
  394. gap: 20rpx;
  395. min-width: 0;
  396. padding: 20rpx 24rpx 16rpx;
  397. box-sizing: border-box;
  398. background: $morandi-bg-page;
  399. border-bottom: 1rpx solid $morandi-border-soft;
  400. }
  401. .lr-tabs {
  402. width: 100%;
  403. min-width: 0;
  404. }
  405. .lr-body {
  406. flex: 1;
  407. min-height: 0;
  408. height: 0;
  409. min-width: 0;
  410. display: flex;
  411. flex-direction: column;
  412. padding: 16rpx 24rpx 24rpx;
  413. box-sizing: border-box;
  414. overflow: hidden;
  415. }
  416. .lr-list-wrap {
  417. flex: 1;
  418. min-height: 0;
  419. display: flex;
  420. flex-direction: column;
  421. min-width: 0;
  422. }
  423. .lr-vlist {
  424. flex: 1;
  425. min-height: 0;
  426. width: 100%;
  427. height: 100%;
  428. }
  429. .lr-empty {
  430. flex: 1;
  431. display: flex;
  432. align-items: center;
  433. justify-content: center;
  434. min-height: 0;
  435. padding: 48rpx 24rpx;
  436. box-sizing: border-box;
  437. }
  438. .lr-empty__txt {
  439. color: $morandi-text-muted;
  440. text-align: center;
  441. }
  442. .lr-row-cell {
  443. height: 100%;
  444. box-sizing: border-box;
  445. display: flex;
  446. flex-direction: column;
  447. justify-content: flex-start;
  448. }
  449. .lr-row {
  450. display: flex;
  451. flex-direction: row;
  452. align-items: stretch;
  453. gap: 20rpx;
  454. min-width: 0;
  455. padding: 20rpx;
  456. box-sizing: border-box;
  457. border-radius: 16rpx;
  458. background: $morandi-bg-card;
  459. border: 1rpx solid $morandi-border;
  460. flex-shrink: 0;
  461. }
  462. .lr-row__left {
  463. flex: 1;
  464. min-width: 0;
  465. display: flex;
  466. flex-direction: column;
  467. gap: 12rpx;
  468. }
  469. .lr-row__title {
  470. font-size: 32rpx;
  471. font-weight: 600;
  472. line-height: 1.45;
  473. color: $morandi-text;
  474. word-break: break-word;
  475. overflow-wrap: anywhere;
  476. }
  477. .lr-row__summary {
  478. font-size: 24rpx;
  479. line-height: 1.55;
  480. color: $morandi-text-secondary;
  481. word-break: break-word;
  482. overflow-wrap: anywhere;
  483. }
  484. .lr-row__meta {
  485. display: flex;
  486. flex-direction: row;
  487. flex-wrap: wrap;
  488. align-items: center;
  489. gap: 12rpx 20rpx;
  490. min-width: 0;
  491. }
  492. .lr-row__meta-tag {
  493. font-size: 22rpx;
  494. color: $morandi-accent-soft;
  495. }
  496. .lr-row__meta-date {
  497. font-size: 22rpx;
  498. color: $morandi-text-soft;
  499. }
  500. .lr-row__right {
  501. flex-shrink: 0;
  502. align-self: center;
  503. width: 160rpx;
  504. min-width: 0;
  505. }
  506. .lr-lazy-cover {
  507. width: 100%;
  508. display: block;
  509. }
  510. .lr-page.lang-bo {
  511. .lr-row__title {
  512. font-size: 30rpx;
  513. line-height: 1.75;
  514. letter-spacing: 2rpx;
  515. font-family: 'Noto Sans Tibetan', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  516. }
  517. .lr-row__summary {
  518. font-size: 22rpx;
  519. line-height: 1.75;
  520. letter-spacing: 2rpx;
  521. font-family: 'Noto Sans Tibetan', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  522. }
  523. .lr-row__meta-tag,
  524. .lr-row__meta-date {
  525. font-size: 20rpx;
  526. line-height: 1.75;
  527. letter-spacing: 2rpx;
  528. font-family: 'Noto Sans Tibetan', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  529. }
  530. }
  531. </style>