index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. <template>
  2. <view class="page-container">
  3. <!-- 顶部标签导航 -->
  4. <view class="header-tabs">
  5. <view
  6. class="tab-item"
  7. :class="{ active: currentTab === 0 }"
  8. @click="handleTabChange(0)"
  9. >
  10. <text>农技知识</text>
  11. <view class="tab-line" :class="{ active: currentTab === 0 }"></view>
  12. </view>
  13. <view
  14. class="tab-item"
  15. :class="{ active: currentTab === 1 }"
  16. @click="handleTabChange(1)"
  17. >
  18. <text>政策解读</text>
  19. <view class="tab-line" :class="{ active: currentTab === 1 }"></view>
  20. </view>
  21. </view>
  22. <!-- 内容区域 -->
  23. <scroll-view
  24. scroll-y
  25. class="content-container"
  26. @scrolltolower="loadMore"
  27. @refresherrefresh="onRefresh"
  28. refresher-enabled
  29. :refresher-triggered="isRefreshing"
  30. >
  31. <!-- 主内容区域 -->
  32. <view class="cards-section">
  33. <!-- 农技知识内容 -->
  34. <block v-if="currentTab === 0">
  35. <navigator
  36. class="content-card"
  37. v-for="(item, index) in techList"
  38. :key="`tech-${item.id}-${index}`"
  39. :url="`/pages/knowledge/detail?id=${item.id}&type=tech`"
  40. hover-class="card-hover"
  41. >
  42. <view class="content-thumbnail">
  43. <image
  44. :src="item.imageUrl"
  45. mode="aspectFill"
  46. class="thumbnail-image"
  47. lazy-load
  48. />
  49. <!-- 仅视频类型显示时长和播放按钮 -->
  50. <template v-if="item.contentType === 'video'">
  51. <view class="video-duration">{{ item.duration || '00:00' }}</view>
  52. <view class="play-button">
  53. <view class="play-icon"></view>
  54. </view>
  55. <view class="content-type-tag video">视频</view>
  56. </template>
  57. <!-- 文章类型显示标签 -->
  58. <template v-else>
  59. <view class="content-type-tag article">文章</view>
  60. </template>
  61. </view>
  62. <view class="content-info">
  63. <text class="content-title">{{ item.title }}</text>
  64. <view class="info-row">
  65. <text class="source-name">{{ item.source }}</text>
  66. <view class="view-count">
  67. <image src="@/static/icons/read.png" style="width: 30rpx;height: 30rpx;"></image>
  68. <text>{{ item.viewCount }}</text>
  69. </view>
  70. </view>
  71. </view>
  72. </navigator>
  73. </block>
  74. <!-- 政策解读内容 -->
  75. <block v-if="currentTab === 1">
  76. <navigator
  77. class="content-card"
  78. v-for="(item, index) in policyList"
  79. :key="`policy-${item.id}-${index}`"
  80. :url="`/pages/knowledge/detail?id=${item.id}&type=policy`"
  81. hover-class="card-hover"
  82. >
  83. <view class="content-thumbnail">
  84. <image
  85. :src="item.imageUrl"
  86. mode="aspectFill"
  87. class="thumbnail-image"
  88. lazy-load
  89. />
  90. <!-- 仅视频类型显示时长和播放按钮 -->
  91. <template v-if="item.contentType === 'video'">
  92. <view class="video-duration">{{ item.duration || '00:00' }}</view>
  93. <view class="play-button">
  94. <view class="play-icon"></view>
  95. </view>
  96. <view class="content-type-tag video">视频</view>
  97. </template>
  98. <!-- 文章类型显示标签 -->
  99. <template v-else>
  100. <view class="content-type-tag article">文章</view>
  101. </template>
  102. </view>
  103. <view class="content-info">
  104. <text class="content-title">{{ item.title }}</text>
  105. <view class="info-row">
  106. <text class="source-name">{{ item.source }}</text>
  107. <view class="view-count">
  108. <image src="@/static/icons/read.png" style="width: 30rpx;height: 30rpx;"></image>
  109. <text>{{ item.viewCount }}</text>
  110. </view>
  111. </view>
  112. </view>
  113. </navigator>
  114. </block>
  115. <!-- 加载中状态 -->
  116. <view class="loading-state" v-if="loading">
  117. <view class="spinner"></view>
  118. <text class="loading-text">加载中...</text>
  119. </view>
  120. <!-- 无数据状态 -->
  121. <view class="empty-state" v-if="!loading && ((currentTab === 0 && techList.length === 0) || (currentTab === 1 && policyList.length === 0))">
  122. <image src="/static/images/empty.png" mode="aspectFit" class="empty-image"></image>
  123. <text class="empty-text">暂无数据</text>
  124. </view>
  125. </view>
  126. <!-- 底部安全区域 -->
  127. <view style="height: 120rpx;"></view>
  128. </scroll-view>
  129. <!-- AI问答悬浮按钮 -->
  130. <!-- <view class="assistant-btn" @click="navigateToAI" hover-class="btn-hover">
  131. <image src="/static/icons/ai.png" class="assistant-icon" mode="aspectFit"></image>
  132. <text class="btn-text">问农小禹</text>
  133. </view> -->
  134. </view>
  135. </template>
  136. <script setup>
  137. import { ref, reactive, onMounted, onBeforeUnmount } from 'vue'
  138. import { getKnowledgeList } from "@/api/services/knowledge.js"
  139. import { onShow } from '@dcloudio/uni-app'
  140. // 当前标签
  141. const currentTab = ref(0)
  142. // 农技知识列表
  143. const techList = ref([])
  144. // 政策解读列表
  145. const policyList = ref([])
  146. // 分页参数
  147. const techPageParams = reactive({
  148. page: 1,
  149. pageSize: 10,
  150. hasMore: true
  151. })
  152. const policyPageParams = reactive({
  153. page: 1,
  154. pageSize: 10,
  155. hasMore: true
  156. })
  157. // 刷新加载状态
  158. const isRefreshing = ref(false)
  159. const loading = ref(false)
  160. // 处理Tab切换
  161. const handleTabChange = (index) => {
  162. if (currentTab.value === index) return
  163. currentTab.value = index
  164. console.log('Tab changed to:', index)
  165. // 如果切换到的标签没有数据,则加载数据
  166. if (index === 0 && techList.value.length === 0) {
  167. fetchTechKnowledge()
  168. } else if (index === 1 && policyList.value.length === 0) {
  169. fetchPolicyData()
  170. }
  171. }
  172. // 获取农技知识数据
  173. const fetchTechKnowledge = async (refresh = false) => {
  174. if (refresh) {
  175. techPageParams.page = 1
  176. techPageParams.hasMore = true
  177. }
  178. // 如果没有更多数据则不请求
  179. if (!techPageParams.hasMore && !refresh) return
  180. loading.value = true
  181. try {
  182. // 构建请求参数
  183. const params = {
  184. pageNum: techPageParams.page,
  185. pageSize: techPageParams.pageSize,
  186. contentCategory: 'tech'
  187. };
  188. // 发起请求
  189. await getKnowledgeList(params).then(response => {
  190. console.log("response农技", response)
  191. // 解析响应
  192. if (response.data.data && response.data.code === 200) {
  193. const { list, total } = response.data.data
  194. if (refresh) {
  195. techList.value = list || []
  196. } else {
  197. techList.value = [...techList.value, ...(list || [])]
  198. }
  199. console.log("techList", techList.value)
  200. // 更新分页状态
  201. techPageParams.page++
  202. techPageParams.hasMore = techList.value.length < total
  203. } else {
  204. uni.showToast({
  205. title: (response.data && response.data.message) || '获取数据失败',
  206. icon: 'none'
  207. })
  208. }
  209. }).catch(err => [err, null])
  210. } catch (error) {
  211. console.error('获取农技知识数据失败:', error)
  212. uni.showToast({
  213. title: '网络异常,请稍后再试',
  214. icon: 'none'
  215. })
  216. } finally {
  217. loading.value = false
  218. if (refresh) {
  219. isRefreshing.value = false
  220. }
  221. // 如果加载完成后没有更多数据,并且不是刷新操作,显示提示
  222. if (!techPageParams.hasMore && !refresh && techList.value.length > 0) {
  223. uni.showToast({
  224. title: '已加载全部内容',
  225. icon: 'none',
  226. duration: 2000
  227. })
  228. }
  229. }
  230. }
  231. // 获取政策解读数据
  232. const fetchPolicyData = async (refresh = false) => {
  233. if (refresh) {
  234. policyPageParams.page = 1
  235. policyPageParams.hasMore = true
  236. }
  237. // 如果没有更多数据则不请求
  238. if (!policyPageParams.hasMore && !refresh) return
  239. loading.value = true
  240. try {
  241. // 构建请求参数
  242. const params = {
  243. pageNum: policyPageParams.page,
  244. pageSize: policyPageParams.pageSize,
  245. contentCategory: 'policy'
  246. };
  247. // 发起请求
  248. getKnowledgeList(params).then(response => {
  249. console.log("response农技333", response)
  250. // 解析响应
  251. if (response.data.data && response.data.code === 200) {
  252. const { list, total } = response.data.data
  253. if (refresh) {
  254. policyList.value = list || []
  255. } else {
  256. policyList.value = [...policyList.value, ...(list || [])]
  257. }
  258. console.log("policyList", policyList.value)
  259. // 更新分页状态
  260. policyPageParams.page++
  261. policyPageParams.hasMore = policyList.value.length < total
  262. } else {
  263. uni.showToast({
  264. title: (response.data && response.data.message) || '获取数据失败',
  265. icon: 'none'
  266. })
  267. }
  268. }).catch(err => [err, null])
  269. } catch (error) {
  270. console.error('获取政策解读数据失败:', error)
  271. uni.showToast({
  272. title: '网络异常,请稍后再试',
  273. icon: 'none'
  274. })
  275. } finally {
  276. loading.value = false
  277. if (refresh) {
  278. isRefreshing.value = false
  279. }
  280. // 如果加载完成后没有更多数据,并且不是刷新操作,显示提示
  281. if (!policyPageParams.hasMore && !refresh && policyList.value.length > 0) {
  282. uni.showToast({
  283. title: '已加载全部内容',
  284. icon: 'none',
  285. duration: 2000
  286. })
  287. }
  288. }
  289. }
  290. // 下拉刷新
  291. const onRefresh = async () => {
  292. isRefreshing.value = true
  293. if (currentTab.value === 0) {
  294. // 刷新农技知识数据
  295. await fetchTechKnowledge(true)
  296. } else {
  297. // 刷新政策解读数据
  298. await fetchPolicyData(true)
  299. }
  300. }
  301. // 加载更多
  302. const loadMore = () => {
  303. if (loading.value) return
  304. console.log('Loading more content')
  305. if (currentTab.value === 0) {
  306. // 检查是否还有更多农技知识数据
  307. if (!techPageParams.hasMore) {
  308. uni.showToast({
  309. title: '已加载全部内容',
  310. icon: 'none',
  311. duration: 2000
  312. })
  313. return
  314. }
  315. // 加载更多农技知识数据
  316. fetchTechKnowledge()
  317. } else {
  318. // 检查是否还有更多政策解读数据
  319. if (!policyPageParams.hasMore) {
  320. uni.showToast({
  321. title: '已加载全部内容',
  322. icon: 'none',
  323. duration: 2000
  324. })
  325. return
  326. }
  327. // 加载更多政策解读数据
  328. fetchPolicyData()
  329. }
  330. }
  331. // 跳转AI问答
  332. const navigateToAI = () => {
  333. uni.navigateTo({
  334. url: '/pages/knowledge/ai-chat/index'
  335. })
  336. }
  337. // 处理文章阅读量更新
  338. const handleViewCountUpdate = (data) => {
  339. if (!data || !data.id) return
  340. // 更新技术文章列表中的阅读量
  341. if (data.type === 'tech' || !data.type) {
  342. const index = techList.value.findIndex(item => item.id === data.id)
  343. if (index !== -1) {
  344. techList.value[index].viewCount = data.viewCount
  345. }
  346. }
  347. // 更新政策解读列表中的阅读量
  348. if (data.type === 'policy') {
  349. const index = policyList.value.findIndex(item => item.id === data.id)
  350. if (index !== -1) {
  351. policyList.value[index].viewCount = data.viewCount
  352. }
  353. }
  354. }
  355. // 页面加载完成
  356. onMounted(() => {
  357. console.log('Page mounted, current tab:', currentTab.value)
  358. // 初始加载数据
  359. fetchTechKnowledge()
  360. // 监听文章阅读量更新事件
  361. uni.$on('updateArticleViewCount', handleViewCountUpdate)
  362. })
  363. // 页面显示时触发
  364. onShow(() => {
  365. // 获取当前页面路由信息,判断是否是从详情页返回
  366. const pages = getCurrentPages()
  367. if (pages.length > 1) {
  368. const prePage = pages[pages.length - 2]
  369. // 如果前一个页面是详情页,检查是否需要刷新数据
  370. if (prePage && prePage.route && prePage.route.includes('knowledge/detail')) {
  371. console.log('从详情页返回,更新阅读量显示')
  372. // 由于已经通过事件机制更新了列表,这里只需要触发一下视图更新
  373. if (currentTab.value === 0) {
  374. techList.value = [...techList.value]
  375. } else {
  376. policyList.value = [...policyList.value]
  377. }
  378. }
  379. }
  380. })
  381. // 页面销毁前
  382. onBeforeUnmount(() => {
  383. // 移除事件监听
  384. uni.$off('updateArticleViewCount', handleViewCountUpdate)
  385. })
  386. </script>
  387. <style scoped>
  388. /* 引入字体图标 */
  389. @font-face {
  390. font-family: 'iconfont';
  391. src: url('//at.alicdn.com/t/font_3266846_kxscvlm9qpp.woff2') format('woff2');
  392. }
  393. .iconfont {
  394. font-family: "iconfont" !important;
  395. font-size: 22rpx;
  396. font-style: normal;
  397. -webkit-font-smoothing: antialiased;
  398. -moz-osx-font-smoothing: grayscale;
  399. }
  400. .icon-robot:before {
  401. content: "\e64b";
  402. }
  403. .icon-eye:before {
  404. content: "\e614";
  405. }
  406. /* 页面容器 */
  407. .page-container {
  408. min-height: 100vh;
  409. background-color: #f7f8fa;
  410. position: relative;
  411. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  412. }
  413. /* 头部标签样式 */
  414. .header-tabs {
  415. display: flex;
  416. background-color: #fff;
  417. padding: 0 30rpx;
  418. position: relative;
  419. border-bottom: 1rpx solid #f2f2f2;
  420. }
  421. .tab-item {
  422. position: relative;
  423. padding: 20rpx 30rpx;
  424. margin-right: 50rpx;
  425. font-size: 30rpx;
  426. color: #666;
  427. transition: all 0.3s ease;
  428. }
  429. .tab-item.active {
  430. color: #333;
  431. font-weight: 600;
  432. }
  433. .tab-line {
  434. position: absolute;
  435. bottom: 0;
  436. left: 50%;
  437. width: 0;
  438. height: 6rpx;
  439. background-color: #4dc971;
  440. border-radius: 3rpx;
  441. transition: all 0.3s ease;
  442. transform: translateX(-50%);
  443. }
  444. .tab-line.active {
  445. width: 40rpx;
  446. }
  447. /* 内容区域样式 */
  448. .content-container {
  449. height: calc(100vh - 88rpx);
  450. box-sizing: border-box;
  451. }
  452. .cards-section {
  453. padding: 12rpx;
  454. }
  455. /* 内容卡片通用样式 */
  456. .content-card {
  457. background-color: #fff;
  458. border-radius: 16rpx;
  459. margin-bottom: 24rpx;
  460. overflow: hidden;
  461. transition: all 0.2s ease;
  462. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
  463. position: relative;
  464. }
  465. .card-hover {
  466. transform: translateY(-2rpx);
  467. box-shadow: 0 6rpx 16rpx rgba(0, 0, 0, 0.12);
  468. }
  469. /* 缩略图 */
  470. .content-thumbnail {
  471. position: relative;
  472. width: 100%;
  473. height: 300rpx;
  474. background-color: #f0f0f0;
  475. }
  476. .thumbnail-image {
  477. width: 100%;
  478. height: 100%;
  479. object-fit: cover;
  480. }
  481. /* 播放按钮 */
  482. .play-button {
  483. position: absolute;
  484. top: 50%;
  485. left: 50%;
  486. transform: translate(-50%, -50%);
  487. width: 70rpx;
  488. height: 70rpx;
  489. background-color: rgba(0, 0, 0, 0.5);
  490. border-radius: 50%;
  491. display: flex;
  492. align-items: center;
  493. justify-content: center;
  494. z-index: 2;
  495. }
  496. .play-icon {
  497. width: 0;
  498. height: 0;
  499. border-style: solid;
  500. border-width: 9rpx 0 9rpx 16rpx;
  501. border-color: transparent transparent transparent #ffffff;
  502. margin-left: 4rpx;
  503. }
  504. /* 视频时长 */
  505. .video-duration {
  506. position: absolute;
  507. bottom: 10rpx;
  508. right: 10rpx;
  509. padding: 3rpx 8rpx;
  510. background-color: rgba(0, 0, 0, 0.6);
  511. color: #fff;
  512. font-size: 18rpx;
  513. border-radius: 10rpx;
  514. }
  515. /* 内容类型标签 */
  516. .content-type-tag {
  517. position: absolute;
  518. top: 10rpx;
  519. left: 10rpx;
  520. padding: 4rpx 10rpx;
  521. font-size: 20rpx;
  522. border-radius: 6rpx;
  523. z-index: 2;
  524. }
  525. .content-type-tag.video {
  526. background-color: #4dc971;
  527. color: #fff;
  528. }
  529. .content-type-tag.article {
  530. background-color: #3498db;
  531. color: #fff;
  532. }
  533. /* 内容信息 */
  534. .content-info {
  535. padding: 16rpx;
  536. position: relative;
  537. }
  538. .content-title {
  539. font-size: 30rpx;
  540. font-weight: 600;
  541. color: #333;
  542. line-height: 1.4;
  543. margin-bottom: 10rpx;
  544. display: -webkit-box;
  545. -webkit-box-orient: vertical;
  546. -webkit-line-clamp: 2;
  547. overflow: hidden;
  548. }
  549. .source-name {
  550. color: #999;
  551. font-size: 22rpx;
  552. }
  553. .info-row {
  554. display: flex;
  555. justify-content: space-between;
  556. align-items: center;
  557. }
  558. .view-count {
  559. color: #c0c0c0;
  560. font-size: 20rpx;
  561. display: flex;
  562. align-items: center;
  563. }
  564. .view-count .iconfont {
  565. margin-right: 4rpx;
  566. }
  567. /* 空状态 */
  568. .empty-state {
  569. display: flex;
  570. flex-direction: column;
  571. align-items: center;
  572. justify-content: center;
  573. padding: 120rpx 0;
  574. }
  575. .empty-image {
  576. width: 180rpx;
  577. height: 180rpx;
  578. margin-bottom: 16rpx;
  579. }
  580. .empty-text {
  581. font-size: 26rpx;
  582. color: #999;
  583. }
  584. /* 加载中状态 */
  585. .loading-state {
  586. display: flex;
  587. flex-direction: column;
  588. align-items: center;
  589. justify-content: center;
  590. padding: 30rpx 0;
  591. }
  592. .spinner {
  593. width: 60rpx;
  594. height: 60rpx;
  595. border: 4rpx solid rgba(77, 201, 113, 0.2);
  596. border-radius: 50%;
  597. border-top-color: #4dc971;
  598. animation: spin 0.8s linear infinite;
  599. margin-bottom: 16rpx;
  600. }
  601. @keyframes spin {
  602. to {
  603. transform: rotate(360deg);
  604. }
  605. }
  606. .loading-text {
  607. font-size: 24rpx;
  608. color: #999;
  609. }
  610. /* AI按钮 */
  611. .assistant-btn {
  612. position: fixed;
  613. right: 30rpx;
  614. bottom: 100rpx;
  615. height: 80rpx;
  616. border-radius: 40rpx;
  617. background: linear-gradient(135deg, #42b983, #3db160);
  618. display: flex;
  619. align-items: center;
  620. padding: 0 24rpx;
  621. box-shadow: 0 6rpx 20rpx rgba(66, 185, 131, 0.35);
  622. z-index: 99;
  623. transition: all 0.25s ease;
  624. }
  625. .assistant-icon {
  626. width: 68rpx;
  627. height: 68rpx;
  628. margin-right: 10rpx;
  629. }
  630. .btn-hover {
  631. transform: scale(1.05);
  632. box-shadow: 0 8rpx 24rpx rgba(66, 185, 131, 0.45);
  633. }
  634. .btn-text {
  635. font-size: 28rpx;
  636. color: #fff;
  637. font-weight: 500;
  638. }
  639. </style>