西藏巴青项目

index.vue 16KB

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