index.vue 18 KB

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