index.vue 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  1. <template>
  2. <view class="dashboard-container">
  3. <!-- 顶部 TopBar -->
  4. <view class="top-bar">
  5. <text class="greeting">农事有序,心里有数</text>
  6. <text class="top-sub-guide">从今天的田间环境开始</text>
  7. </view>
  8. <!-- 田间环境/天气区域 (降级为扁平状态块) -->
  9. <view class="environmental-status-block" @click="fetchWeatherData">
  10. <view class="status-main">
  11. <view class="main-info">
  12. <text :class="weatherData.infos.weather == '' ? 'main-textN' : 'main-text' "> {{
  13. weatherData.infos.weather == ''
  14. ? '请点击获取环境数据'
  15. : `${weatherData.infos.temperature}° · ${weatherData.infos.weather}`
  16. }}</text>
  17. <text class="fixed-subtitle">田间环境概览</text>
  18. </view>
  19. <view class="refresh-btn" :class="{ 'rotating': isLoadingWeather }" @click.stop="handleRefresh">
  20. <!-- <u-icon name="reload" :color="isLoadingWeather ? '#999' : '#3BB44A'" size="20"></u-icon> -->
  21. <!-- <uni-icons type="reload" color='#3BB44A' size="20"></uni-icons> -->
  22. <image src="/static/icons/reload.png" style="width:20px;height:20px;"></image>
  23. </view>
  24. </view>
  25. <view class="metrics-row">
  26. <view class="metric-item">
  27. <text class="label">湿度</text>
  28. <text class="value">{{ weatherData.infos.humidity }}%</text>
  29. </view>
  30. <view class="metric-item">
  31. <text class="label">风力</text>
  32. <text class="value">{{ weatherData.infos.wind_power_v2 }}</text>
  33. </view>
  34. <view class="metric-item">
  35. <text class="label">气压</text>
  36. <text class="value">{{ weatherData.infos.air_pressure }} hPa</text>
  37. </view>
  38. </view>
  39. <view class="update-time" v-if="weatherData.update_time">
  40. <text>更新时间:{{ weatherData.update_time }}</text>
  41. </view>
  42. </view>
  43. <!-- AI 主卡 -->
  44. <view class="card ai-card">
  45. <view class="ai-header">
  46. <view class="ai-pill">AI 农小禹</view>
  47. <text class="ai-subtitle">今天安排从这里开始</text>
  48. </view>
  49. <view class="ai-body">
  50. <text class="ai-title">{{ aiQuestion.title }}</text>
  51. <text class="ai-desc">问问 AI 农小禹,给你具体建议</text>
  52. </view>
  53. <view class="ai-footer">
  54. <view class="ai-cta">
  55. <u-button
  56. type="primary"
  57. color="#2F7D32"
  58. shape="round"
  59. :custom-style="{
  60. height: '60rpx',
  61. width: 'auto',
  62. minWidth: '200rpx',
  63. borderRadius: '30rpx',
  64. padding: '0 32rpx',
  65. fontSize: '28rpx',
  66. fontWeight: '600',
  67. margin: '0'
  68. }"
  69. @click="goToAI"
  70. >
  71. 立即提问
  72. <u-icon name="arrow-right" color="white" size="16" style="margin-left: 8rpx;"></u-icon>
  73. </u-button>
  74. </view>
  75. </view>
  76. <!-- 玻璃感噪点纹理 -->
  77. <view class="ai-texture"></view>
  78. </view>
  79. <!-- 农资精选 -->
  80. <view class="card">
  81. <view class="card-header">
  82. <view class="title-section">
  83. <view class="title-line"></view>
  84. <text class="card-title">农资精选</text>
  85. </view>
  86. <view class="more-btn" @click="goToMall">
  87. <text>去农资</text>
  88. <u-icon name="arrow-right" color="#3BB44A" size="14"></u-icon>
  89. </view>
  90. </view>
  91. <view class="products-grid">
  92. <view
  93. class="product-card"
  94. v-for="(product, index) in products"
  95. :key="index"
  96. @click="showProductDetail(product)"
  97. >
  98. <image
  99. :src="product.image"
  100. mode="aspectFill"
  101. class="product-image"
  102. lazy-load
  103. ></image>
  104. <view class="product-info">
  105. <text class="product-title">{{ product.title }}</text>
  106. <text class="product-desc">{{ product.desc }}</text>
  107. <view class="price-row">
  108. <text class="price">¥{{ product.price }}</text>
  109. <text class="unit">/{{ product.unit }}</text>
  110. </view>
  111. <u-button
  112. type="primary"
  113. color="linear-gradient(135deg, #3BB44A, #66CC6A)"
  114. size="mini"
  115. shape="round"
  116. >
  117. 了解更多
  118. </u-button>
  119. </view>
  120. </view>
  121. </view>
  122. </view>
  123. <!-- 农技推荐 -->
  124. <view class="card">
  125. <view class="card-header">
  126. <view class="title-section">
  127. <view class="title-line"></view>
  128. <text class="card-title">农技推荐</text>
  129. </view>
  130. <view class="more-btn" @click="goToKnowledge">
  131. <text>更多</text>
  132. <u-icon name="arrow-right" color="#3BB44A" size="14"></u-icon>
  133. </view>
  134. </view>
  135. <view class="articles-list">
  136. <view
  137. class="article-card"
  138. v-for="(article, index) in articles"
  139. :key="index"
  140. @click="showArticleDetail(article)"
  141. >
  142. <view class="article-image-wrapper">
  143. <image
  144. :src="article.image"
  145. mode="aspectFill"
  146. class="article-image"
  147. lazy-load
  148. ></image>
  149. <view class="article-tag">文章</view>
  150. </view>
  151. <view class="article-info">
  152. <text class="article-title">{{ article.title }}</text>
  153. <text class="article-source">{{ article.source }}</text>
  154. <view class="article-stats">
  155. <u-icon name="eye" color="#999" size="12"></u-icon>
  156. <text class="read-count">{{ article.readCount }}</text>
  157. </view>
  158. </view>
  159. </view>
  160. </view>
  161. </view>
  162. <!-- 位置权限使用说明弹窗 -->
  163. <!-- #ifndef MP-WEIXIN -->
  164. <u-popup
  165. ref="popup"
  166. :show="showLocationPermissionPopup"
  167. mode="top"
  168. round="20"
  169. :closeOnClickOverlay="false"
  170. :closeable="false"
  171. :safeAreaInsetTop="true"
  172. @change="showChange"
  173. >
  174. <view class="location-permission-popup">
  175. <view class="permission-header">
  176. <view class="permission-icon">
  177. <u-icon name="map" color="#666" size="20"></u-icon>
  178. </view>
  179. <view class="permission-title">位置权限使用说明</view>
  180. </view>
  181. <view class="permission-content">
  182. 为了向您展示当前所在地的天气信息,我们需要获取您的位置信息,用于查询并展示您当前位置的实时天气情况。
  183. </view>
  184. </view>
  185. </u-popup>
  186. <!-- #endif -->
  187. <!-- 天气详情弹窗 -->
  188. <!-- <u-popup
  189. :show="showWeatherPopup"
  190. mode="bottom"
  191. round="20"
  192. @close="showWeatherPopup = false"
  193. >
  194. <view class="popup-content">
  195. <view class="popup-header">
  196. <text class="popup-title">今日农事详情</text>
  197. <u-icon name="close" color="#999" size="20" @click="showWeatherPopup = false"></u-icon>
  198. </view>
  199. <view class="weather-detail">
  200. <view class="detail-item">
  201. <text class="detail-label">天气状况</text>
  202. <text class="detail-value">{{ weatherData.weatherText }}</text>
  203. </view>
  204. <view class="detail-item">
  205. <text class="detail-label">当前温度</text>
  206. <text class="detail-value">{{ weatherData.tempNow }}°C</text>
  207. </view>
  208. <view class="detail-item">
  209. <text class="detail-label">湿度</text>
  210. <text class="detail-value">{{ weatherData.humidity }}%</text>
  211. </view>
  212. <view class="detail-item">
  213. <text class="detail-label">风力</text>
  214. <text class="detail-value">{{ weatherData.windScale }}级</text>
  215. </view>
  216. <view class="detail-item">
  217. <text class="detail-label">降水量</text>
  218. <text class="detail-value">{{ weatherData.precipMm }}mm</text>
  219. </view>
  220. <view class="advice-detail">
  221. <text class="advice-label">今日建议:</text>
  222. <text class="advice-content">{{ weatherData.adviceText }}</text>
  223. </view>
  224. </view>
  225. <u-button
  226. type="primary"
  227. color="linear-gradient(135deg, #3BB44A, #66CC6A)"
  228. shape="round"
  229. @click="askAIAboutWeather"
  230. >
  231. 去问 AI
  232. </u-button>
  233. </view>
  234. </u-popup> -->
  235. <!-- 文章详情弹窗 -->
  236. <!-- <u-popup
  237. :show="showArticlePopup"
  238. mode="bottom"
  239. round="20"
  240. @close="showArticlePopup = false"
  241. >
  242. <view class="popup-content">
  243. <view class="popup-header">
  244. <text class="popup-title">文章详情</text>
  245. <u-icon name="close" color="#999" size="20" @click="showArticlePopup = false"></u-icon>
  246. </view>
  247. <view class="article-detail" v-if="currentArticle">
  248. <text class="article-detail-title">{{ currentArticle.title }}</text>
  249. <text class="article-detail-time">{{ currentArticle.time }}</text>
  250. <view class="article-detail-content">
  251. <text class="content-paragraph">{{ currentArticle.content }}</text>
  252. <text class="content-subtitle">主要要点:</text>
  253. <view class="content-list">
  254. <text class="list-item" v-for="(item, index) in currentArticle.points" :key="index">
  255. • {{ item }}
  256. </text>
  257. </view>
  258. </view>
  259. </view>
  260. <u-button
  261. type="primary"
  262. color="linear-gradient(135deg, #3BB44A, #66CC6A)"
  263. shape="round"
  264. @click="askAIAboutArticle"
  265. >
  266. 去问 AI
  267. </u-button>
  268. </view>
  269. </u-popup> -->
  270. </view>
  271. </template>
  272. <script setup>
  273. import { ref, reactive, onMounted } from 'vue'
  274. import { getWeatherInfo, getLocation } from '@/api/services/weather.js'
  275. import { getMallList } from '@/api/services/mall.js'
  276. import { getTopArticles } from '@/api/services/knowledge.js'
  277. import privacyUtil from '@/utils/privacy.js'
  278. // 天气数据
  279. const weatherData = reactive({
  280. infos: {
  281. weather: "",
  282. temperature: "--",
  283. humidity: "--",
  284. wind_power_v2: "--",
  285. air_pressure: "--"
  286. },
  287. update_time: "" // 更新时间
  288. })
  289. // 加载状态
  290. const isLoadingWeather = ref(false)
  291. const aiQuestions = [
  292. {
  293. title: '叶片发黄该从哪查?',
  294. recommend: '最近阴雨天多,管理要点?'
  295. },
  296. {
  297. title: '水稻分蘖期怎么管理?',
  298. recommend: '如何提高分蘖成穗率?'
  299. },
  300. {
  301. title: '玉米倒伏了怎么办?',
  302. recommend: '预防玉米倒伏的措施?'
  303. }
  304. ]
  305. const aiQuestion = ref(aiQuestions[0])
  306. // 商品列表数据
  307. const products = ref([])
  308. // 文章列表数据
  309. const articles = ref([])
  310. // 弹窗控制
  311. const showWeatherPopup = ref(false)
  312. const showArticlePopup = ref(false)
  313. const currentArticle = ref(null)
  314. const showLocationPermissionPopup = ref(false)
  315. // 方法
  316. const showTip = () => {
  317. uni.showToast({
  318. title: '建议先看今日农事,再去问 AI',
  319. icon: 'none',
  320. duration: 2000
  321. })
  322. }
  323. const showWeatherDetail = () => {
  324. showWeatherPopup.value = true
  325. }
  326. const showArticleDetail = (article) => {
  327. if (!article.id) {
  328. uni.showToast({
  329. title: '文章信息不完整',
  330. icon: 'none',
  331. duration: 2000
  332. })
  333. return;
  334. }
  335. // 跳转到农技详情页
  336. uni.navigateTo({
  337. url: `/pages/knowledge/detail?id=${article.id}`
  338. })
  339. }
  340. const showProductDetail = (product) => {
  341. if (!product.id) {
  342. uni.showToast({
  343. title: '商品信息不完整',
  344. icon: 'none',
  345. duration: 2000
  346. })
  347. return;
  348. }
  349. // 跳转到农资详情页
  350. uni.navigateTo({
  351. url: `/pages/service/mall-detail?id=${product.id}`
  352. })
  353. }
  354. const goToAI = () => {
  355. uni.switchTab({
  356. url: '/pages/knowledge/ai-chat/index'
  357. });
  358. }
  359. const goToMall = () => {
  360. uni.switchTab({
  361. url: '/pages/service/mall'
  362. })
  363. }
  364. const goToKnowledge = () => {
  365. uni.switchTab({
  366. url: '/pages/knowledge/index'
  367. })
  368. }
  369. const askAIAboutWeather = () => {
  370. showWeatherPopup.value = false
  371. uni.navigateTo({
  372. url: '/pages/knowledge/ai-chat/index?question=' + encodeURIComponent('今日天气适宜哪些农事活动?')
  373. })
  374. }
  375. const askAIAboutArticle = () => {
  376. showArticlePopup.value = false
  377. if (currentArticle.value) {
  378. uni.navigateTo({
  379. url: '/pages/knowledge/ai-chat/index?question=' + encodeURIComponent('关于' + currentArticle.value.title + '有什么建议?')
  380. })
  381. }
  382. }
  383. const handleRefresh = () => {
  384. if (isLoadingWeather.value) {
  385. uni.showToast({
  386. title: '正在刷新中...',
  387. icon: 'none',
  388. duration: 1500
  389. });
  390. return;
  391. }
  392. fetchWeatherData(true);
  393. }
  394. /**
  395. * 显示位置权限说明弹窗
  396. */
  397. const showLocationPermissionGuide = () => {
  398. showLocationPermissionPopup.value = true
  399. }
  400. const showChange= (e)=>{
  401. showLocationPermissionPopup.value = e.showLocationPermissionPopup
  402. }
  403. /**
  404. * 关闭位置权限说明弹窗
  405. */
  406. const closeLocationPermissionPopup = () => {
  407. showLocationPermissionPopup.value = false
  408. }
  409. /**
  410. * 检查隐私协议同意状态
  411. * @returns {boolean} 是否已同意隐私协议
  412. */
  413. const checkPrivacyAgreement = () => {
  414. return privacyUtil.checkAndRedirect()
  415. }
  416. /**
  417. * 获取实时天气数据
  418. */
  419. const fetchWeatherData = async (showToast = false) => {
  420. // 首先检查隐私协议同意状态
  421. if (!checkPrivacyAgreement()) {
  422. return;
  423. }
  424. if (isLoadingWeather.value) {
  425. return;
  426. }
  427. // 如果是手动刷新,显示提示
  428. if (showToast) {
  429. uni.showToast({
  430. title: '正在刷新天气...',
  431. icon: 'loading',
  432. duration: 2000
  433. });
  434. }
  435. isLoadingWeather.value = true;
  436. try {
  437. // 1. 显示位置权限说明弹窗
  438. showLocationPermissionGuide();
  439. // 2. 获取当前位置(这会触发系统权限弹窗)
  440. const location = await getLocation();
  441. console.log('获取到位置信息:', location);
  442. // 3. 位置获取成功后关闭权限说明弹窗
  443. closeLocationPermissionPopup();
  444. // 4. 根据位置查询天气
  445. const weatherResult = await getWeatherInfo({
  446. latitude: location.latitude,
  447. longitude: location.longitude,
  448. type: 'now', // 查询实时天气
  449. added_fields: 'air' // 附加空气质量信息
  450. });
  451. console.log('天气查询结果:', weatherResult);
  452. // 5. 更新天气数据
  453. if (weatherResult && weatherResult.result && weatherResult.result.realtime) {
  454. const realtime = weatherResult.result.realtime;
  455. const infos = realtime[0].infos || {};
  456. weatherData.infos = {
  457. weather: infos.weather || '未知',
  458. temperature: infos.temperature !== undefined ? infos.temperature : '--',
  459. humidity: infos.humidity !== undefined ? infos.humidity : '--',
  460. wind_power_v2: infos.wind_power_v2 || '--',
  461. air_pressure: infos.air_pressure !== undefined ? infos.air_pressure : '--'
  462. };
  463. // 更新时间
  464. weatherData.update_time = realtime.update_time || '';
  465. console.log('天气数据更新成功:', weatherData);
  466. // 刷新成功提示
  467. if (showToast) {
  468. uni.showToast({
  469. title: '刷新成功',
  470. icon: 'success',
  471. duration: 1500
  472. });
  473. }
  474. }
  475. } catch (error) {
  476. console.error('获取天气失败:', error);
  477. // 权限被拒绝或其他错误时也要关闭弹窗
  478. closeLocationPermissionPopup();
  479. uni.showToast({
  480. title: `${error}`,
  481. icon: 'none',
  482. duration: 2000
  483. })
  484. // 错误处理
  485. let errorMsg = '获取天气失败';
  486. if (error.errMsg) {
  487. if (error.errMsg.includes('auth deny')) {
  488. errorMsg = '定位权限被拒绝,请在设置中开启定位权限';
  489. } else if (error.errMsg.includes('timeout')) {
  490. errorMsg = '定位超时,请检查网络连接';
  491. }
  492. } else if (error.message) {
  493. errorMsg = error.message;
  494. }
  495. uni.showToast({
  496. title: errorMsg,
  497. icon: 'none',
  498. duration: 2000
  499. });
  500. // 设置默认值
  501. weatherData.infos = {
  502. weather: "暂无数据",
  503. temperature: "--",
  504. humidity: "--",
  505. wind_power_v2: "--",
  506. air_pressure: "--"
  507. };
  508. } finally {
  509. isLoadingWeather.value = false;
  510. }
  511. }
  512. /**
  513. * 获取第一张图片地址
  514. * @param {string} swiperImages - 图片地址字符串,可能包含多个用逗号分隔的地址
  515. * @returns {string} - 返回第一张图片地址
  516. */
  517. const getFirstImage = (swiperImages) => {
  518. if (!swiperImages) {
  519. return '/static/images/products/agriculture-tools.jpg';
  520. }
  521. // 如果包含逗号,说明有多张图片,取第一张
  522. if (swiperImages.includes(',')) {
  523. return swiperImages.split(',')[0].trim();
  524. }
  525. // 否则直接返回
  526. return swiperImages;
  527. }
  528. /**
  529. * 格式化阅读量显示
  530. * @param {number} count - 阅读量数字
  531. * @returns {string} - 格式化后的阅读量(如:1.2k)
  532. */
  533. const formatReadCount = (count) => {
  534. if (!count || count === 0) return '0';
  535. if (count >= 1000) {
  536. return (count / 1000).toFixed(1) + 'k';
  537. }
  538. return count.toString();
  539. }
  540. /**
  541. * 获取推荐商品列表
  542. */
  543. const fetchRecommendedProducts = async () => {
  544. // 首先检查隐私协议同意状态
  545. if (!checkPrivacyAgreement()) {
  546. return;
  547. }
  548. try {
  549. const result = await getMallList({
  550. isRecommended: 1, // 查询推荐商品
  551. pageNum: 1,
  552. pageSize: 4 // 只获取4个推荐商品
  553. });
  554. console.log('推荐商品查询结果:', result);
  555. if (result.data && result.data.rows && result.data.rows.length > 0) {
  556. // 映射后端数据到前端展示格式
  557. products.value = result.data.rows.map(item => ({
  558. id: item.id,
  559. title: item.name || item.title || '商品名称',
  560. desc: item.description || item.desc || '暂无描述',
  561. price: item.price || '0',
  562. unit: item.unit || '件',
  563. image: getFirstImage(item.swiperImages)
  564. }));
  565. console.log('推荐商品数据更新成功:', products.value);
  566. } else {
  567. // 如果没有推荐商品,使用默认数据
  568. console.log('暂无推荐商品,使用默认数据');
  569. setDefaultProducts();
  570. }
  571. } catch (error) {
  572. console.error('获取推荐商品失败:', error);
  573. // 失败时使用默认数据
  574. setDefaultProducts();
  575. }
  576. }
  577. /**
  578. * 设置默认商品数据
  579. */
  580. const setDefaultProducts = () => {
  581. products.value = [
  582. {
  583. title: '优质玉米种子',
  584. desc: '高产抗病品种',
  585. price: '68',
  586. unit: '袋',
  587. image: '/static/images/products/corn-seeds-new.jpg'
  588. },
  589. {
  590. title: '有机肥料',
  591. desc: '纯天然环保',
  592. price: '128',
  593. unit: '袋',
  594. image: '/static/images/products/organic-fertilizer-new.jpg'
  595. },
  596. {
  597. title: '杀虫剂',
  598. desc: '广谱高效',
  599. price: '45',
  600. unit: '瓶',
  601. image: '/static/images/products/insecticide.jpg'
  602. },
  603. {
  604. title: '农用地膜',
  605. desc: '保湿保温',
  606. price: '89',
  607. unit: '卷',
  608. image: '/static/images/products/plastic-film.jpg'
  609. }
  610. ];
  611. }
  612. /**
  613. * 获取热门文章列表(根据阅读量)
  614. */
  615. const fetchTopArticles = async () => {
  616. // 首先检查隐私协议同意状态
  617. if (!checkPrivacyAgreement()) {
  618. return;
  619. }
  620. try {
  621. const result = await getTopArticles();
  622. console.log('热门文章查询结果:', result);
  623. if (result.data && result.data.length > 0) {
  624. // 映射后端数据到前端展示格式
  625. articles.value = result.data.slice(0, 4).map(item => ({
  626. id: item.id,
  627. title: item.title || '文章标题',
  628. source: item.category || item.source || '农技知识',
  629. readCount: formatReadCount(item.viewCount || item.readCount || 0),
  630. image: getFirstImage(item.imageUrl) || '/static/images/rice-farming/rice-field1.jpg',
  631. time: item.createTime || item.publishTime || '',
  632. content: item.content || item.summary || '暂无内容',
  633. points: [] // 后端如果有要点字段可以映射
  634. }));
  635. console.log('热门文章数据更新成功:', articles.value);
  636. } else {
  637. // 如果没有热门文章,使用默认数据
  638. console.log('暂无热门文章,使用默认数据');
  639. setDefaultArticles();
  640. }
  641. } catch (error) {
  642. console.error('获取热门文章失败:', error);
  643. // 失败时使用默认数据
  644. setDefaultArticles();
  645. }
  646. }
  647. /**
  648. * 设置默认文章数据
  649. */
  650. const setDefaultArticles = () => {
  651. articles.value = [
  652. {
  653. title: '春季水稻种植技术要点',
  654. source: '农技知识',
  655. readCount: '1.2k',
  656. image: '/static/images/rice-farming/rice-field1.jpg',
  657. time: '2024-03-15',
  658. content: '春季是水稻种植的关键时期,需要掌握以下技术要点。',
  659. points: []
  660. },
  661. {
  662. title: '玉米病虫害防治指南',
  663. source: '植保技术',
  664. readCount: '856',
  665. image: '/static/images/rice-farming/rice-field2.jpg',
  666. time: '2024-03-14',
  667. content: '玉米病虫害防治是确保产量的重要环节。',
  668. points: []
  669. },
  670. {
  671. title: '现代农业机械化作业规范',
  672. source: '农机管理',
  673. readCount: '2.1k',
  674. image: '/static/images/rice-farming/rice-field3.jpg',
  675. time: '2024-03-13',
  676. content: '提升农业机械化作业水平是现代农业发展的必然要求。',
  677. points: []
  678. },
  679. {
  680. title: '土壤肥力提升与改良方案',
  681. source: '土肥管理',
  682. readCount: '1.5k',
  683. image: '/static/images/rice-farming/rice-field4.jpg',
  684. time: '2024-03-12',
  685. content: '健康的土壤是丰收的基础。',
  686. points: []
  687. }
  688. ];
  689. }
  690. // 页面加载时检查隐私协议状态,只有同意后才获取数据
  691. onMounted(() => {
  692. // 检查隐私协议同意状态
  693. if (checkPrivacyAgreement()) {
  694. // 直接加载数据,天气数据获取时会显示权限说明弹窗(该方案鸿蒙的安卓审核不通过,需要在用户自己点击获取天气的时候显示权限说明弹窗)
  695. // fetchWeatherData()
  696. fetchRecommendedProducts()
  697. fetchTopArticles()
  698. }
  699. })
  700. </script>
  701. <style lang="scss" scoped>
  702. .dashboard-container {
  703. padding: 24rpx;
  704. background-color: #F6FDF9;
  705. min-height: 100vh;
  706. }
  707. // 顶部 TopBar
  708. .top-bar {
  709. display: flex;
  710. flex-direction: column;
  711. justify-content: flex-start;
  712. align-items: flex-start;
  713. margin-bottom: 12rpx;
  714. padding: 0 8rpx;
  715. .greeting {
  716. font-size: 32rpx;
  717. font-weight: 600;
  718. color: #1A3026;
  719. letter-spacing: 1rpx;
  720. line-height: 1.4;
  721. }
  722. .top-sub-guide {
  723. font-size: 24rpx;
  724. color: rgba(0, 0, 0, 0.45);
  725. margin-top: 4rpx;
  726. }
  727. }
  728. // Card 基础样式(复用旧首页)
  729. .card {
  730. background: white;
  731. border-radius: 20rpx;
  732. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.04), 0 1rpx 4rpx rgba(0, 0, 0, 0.02);
  733. margin-bottom: 24rpx;
  734. overflow: hidden;
  735. .card-header {
  736. display: flex;
  737. justify-content: space-between;
  738. align-items: center;
  739. padding: 24rpx 28rpx;
  740. border-bottom: 1px solid rgba(0, 0, 0, 0.03);
  741. .title-section {
  742. display: flex;
  743. align-items: center;
  744. .title-line {
  745. width: 4rpx;
  746. height: 28rpx;
  747. border-radius: 0;
  748. background: linear-gradient(180deg, #3BB44A, #66CC6A);
  749. margin-right: 16rpx;
  750. }
  751. .card-title {
  752. font-size: 28rpx;
  753. font-weight: 600;
  754. color: #2C3E50;
  755. }
  756. }
  757. .tag {
  758. background: linear-gradient(135deg, #3BB44A, #66CC6A);
  759. color: white;
  760. font-size: 22rpx;
  761. padding: 6rpx 16rpx;
  762. border-radius: 16rpx;
  763. }
  764. .more-btn {
  765. display: flex;
  766. align-items: center;
  767. color: #3BB44A;
  768. font-size: 24rpx;
  769. text {
  770. margin-right: 6rpx;
  771. }
  772. }
  773. }
  774. }
  775. // 田间环境状态块 (降级为扁平化背景块)
  776. .environmental-status-block {
  777. position: relative;
  778. padding: 24rpx 28rpx;
  779. background: linear-gradient(135deg, #FFFFFF 0%, #F4FAF7 100%);
  780. border: 1rpx solid rgba(59, 180, 74, 0.08);
  781. border-radius: 24rpx;
  782. margin-bottom: 24rpx;
  783. overflow: hidden;
  784. .status-main {
  785. display: flex;
  786. justify-content: space-between;
  787. // align-items: flex-end;
  788. margin-bottom: 20rpx;
  789. .main-info {
  790. display: flex;
  791. flex-direction: column;
  792. .main-text {
  793. font-size: 48rpx;
  794. font-weight: 800;
  795. color: #1A3026;
  796. line-height: 1;
  797. }
  798. .main-textN{
  799. font-size: 30rpx;
  800. font-weight: 600;
  801. color: #1A3026;
  802. line-height: 1;
  803. }
  804. .fixed-subtitle {
  805. font-size: 24rpx;
  806. color: #7A9086;
  807. margin-top: 8rpx;
  808. }
  809. }
  810. .refresh-btn {
  811. display: flex;
  812. align-items: center;
  813. justify-content: center;
  814. width: 60rpx;
  815. height: 60rpx;
  816. border-radius: 50%;
  817. background: rgba(59, 180, 74, 0.08);
  818. transition: all 0.3s ease;
  819. &:active {
  820. background: rgba(59, 180, 74, 0.15);
  821. transform: scale(0.95);
  822. }
  823. &.rotating {
  824. animation: rotate 1s linear infinite;
  825. }
  826. }
  827. }
  828. @keyframes rotate {
  829. from {
  830. transform: rotate(0deg);
  831. }
  832. to {
  833. transform: rotate(360deg);
  834. }
  835. }
  836. .metrics-row {
  837. display: flex;
  838. justify-content: space-between;
  839. align-items: center;
  840. padding-top: 16rpx;
  841. border-top: 1rpx solid rgba(0, 0, 0, 0.03);
  842. .metric-item {
  843. flex: 1;
  844. display: flex;
  845. align-items: center;
  846. gap: 8rpx;
  847. .label {
  848. font-size: 22rpx;
  849. color: #8EAC9D;
  850. }
  851. .value {
  852. font-size: 24rpx;
  853. font-weight: 600;
  854. color: #2D4035;
  855. }
  856. }
  857. }
  858. .update-time {
  859. margin-top: 12rpx;
  860. text-align: right;
  861. text {
  862. font-size: 20rpx;
  863. color: #999;
  864. }
  865. }
  866. }
  867. // AI 主卡
  868. .ai-card {
  869. position: relative;
  870. background: linear-gradient(135deg, rgba(59, 180, 74, 0.18), rgba(102, 204, 106, 0.10));
  871. border: 1rpx solid rgba(59, 180, 74, 0.08);
  872. border-radius: 20rpx;
  873. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.04), 0 1rpx 4rpx rgba(0, 0, 0, 0.02);
  874. margin-bottom: 24rpx;
  875. overflow: hidden;
  876. transition: transform 0.2s ease, box-shadow 0.2s ease;
  877. &:active {
  878. transform: scale(0.99);
  879. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.03);
  880. }
  881. // 多层背景光斑效果 (底层氛围)
  882. &::before {
  883. content: '';
  884. position: absolute;
  885. top: 0;
  886. left: 0;
  887. right: 0;
  888. bottom: 0;
  889. background:
  890. radial-gradient(circle at 85% 25%, rgba(59, 180, 74, 0.22), transparent 60%),
  891. radial-gradient(circle at 20% 80%, rgba(76, 175, 80, 0.14), transparent 55%);
  892. pointer-events: none;
  893. z-index: 1;
  894. }
  895. // 抽象 AI 设计母题层 (右侧增强设计感)
  896. &::after {
  897. content: '';
  898. position: absolute;
  899. top: -20%;
  900. right: -10%;
  901. width: 80%;
  902. height: 140%;
  903. background:
  904. radial-gradient(circle at 75% 35%, rgba(59, 180, 74, 0.06) 0%, transparent 45%),
  905. radial-gradient(circle at 85% 65%, rgba(76, 175, 80, 0.04) 0%, transparent 40%),
  906. radial-gradient(circle at 60% 85%, rgba(59, 180, 74, 0.03) 0%, transparent 50%);
  907. filter: blur(30rpx);
  908. pointer-events: none;
  909. z-index: 2;
  910. }
  911. .ai-texture {
  912. position: absolute;
  913. top: 0;
  914. left: 0;
  915. right: 0;
  916. bottom: 0;
  917. background: repeating-linear-gradient(
  918. 45deg,
  919. transparent,
  920. transparent 2rpx,
  921. rgba(255, 255, 255, 0.03) 2rpx,
  922. rgba(255, 255, 255, 0.03) 4rpx
  923. );
  924. pointer-events: none;
  925. z-index: 2;
  926. opacity: 0.5;
  927. }
  928. .ai-header {
  929. position: relative;
  930. z-index: 3;
  931. display: flex;
  932. flex-direction: row;
  933. align-items: center;
  934. padding: 32rpx 28rpx 16rpx;
  935. .ai-pill {
  936. background: #FFFFFF;
  937. color: #3BB44A;
  938. font-size: 24rpx;
  939. font-weight: 600;
  940. padding: 8rpx 24rpx;
  941. border-radius: 40rpx;
  942. margin-right: 20rpx;
  943. margin-bottom: 0;
  944. }
  945. .ai-subtitle {
  946. font-size: 28rpx;
  947. color: #5C6A70;
  948. font-weight: 500;
  949. }
  950. }
  951. .ai-body {
  952. position: relative;
  953. z-index: 3;
  954. padding: 12rpx 28rpx 20rpx;
  955. .ai-title {
  956. font-size: 40rpx;
  957. font-weight: 700;
  958. color: #2C3E50;
  959. line-height: 1.3;
  960. margin-bottom: 12rpx;
  961. display: block;
  962. }
  963. .ai-desc {
  964. font-size: 26rpx;
  965. color: rgba(44, 62, 80, 0.6);
  966. line-height: 1.4;
  967. display: block;
  968. }
  969. }
  970. .ai-footer {
  971. position: relative;
  972. z-index: 3;
  973. display: flex;
  974. justify-content: flex-start;
  975. padding: 16rpx 28rpx 24rpx;
  976. .ai-cta {
  977. display: flex;
  978. align-items: center;
  979. }
  980. }
  981. }
  982. // 农资精选
  983. .products-grid {
  984. display: grid;
  985. grid-template-columns: repeat(2, 1fr);
  986. gap: 16rpx;
  987. padding: 16rpx;
  988. .product-card {
  989. background: white;
  990. border-radius: 16rpx;
  991. overflow: hidden;
  992. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.03);
  993. border: 1px solid rgba(0, 0, 0, 0.02);
  994. .product-image {
  995. width: 100%;
  996. height: 160rpx;
  997. background: #f5f5f5;
  998. }
  999. .product-info {
  1000. padding: 16rpx;
  1001. .product-title {
  1002. font-size: 26rpx;
  1003. font-weight: 600;
  1004. color: #2C3E50;
  1005. margin-bottom: 6rpx;
  1006. overflow: hidden;
  1007. text-overflow: ellipsis;
  1008. white-space: nowrap;
  1009. }
  1010. .product-desc {
  1011. font-size: 22rpx;
  1012. color: #8C9396;
  1013. margin-bottom: 12rpx;
  1014. overflow: hidden;
  1015. text-overflow: ellipsis;
  1016. white-space: nowrap;
  1017. }
  1018. .price-row {
  1019. display: flex;
  1020. align-items: baseline;
  1021. margin-bottom: 12rpx;
  1022. .price {
  1023. font-size: 32rpx;
  1024. font-weight: 700;
  1025. color: #3BB44A;
  1026. }
  1027. .unit {
  1028. font-size: 22rpx;
  1029. color: #8C9396;
  1030. margin-left: 4rpx;
  1031. }
  1032. }
  1033. }
  1034. }
  1035. }
  1036. // 农技推荐
  1037. .articles-list {
  1038. padding: 16rpx;
  1039. .article-card {
  1040. display: flex;
  1041. background: white;
  1042. border-radius: 16rpx;
  1043. overflow: hidden;
  1044. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.03);
  1045. border: 1px solid rgba(0, 0, 0, 0.02);
  1046. margin-bottom: 16rpx;
  1047. .article-image-wrapper {
  1048. position: relative;
  1049. width: 200rpx;
  1050. height: 140rpx;
  1051. flex-shrink: 0;
  1052. .article-image {
  1053. width: 100%;
  1054. height: 100%;
  1055. background: #f5f5f5;
  1056. }
  1057. .article-tag {
  1058. position: absolute;
  1059. top: 8rpx;
  1060. left: 8rpx;
  1061. background: #2196F3;
  1062. color: white;
  1063. font-size: 20rpx;
  1064. padding: 4rpx 8rpx;
  1065. border-radius: 6rpx;
  1066. }
  1067. }
  1068. .article-info {
  1069. flex: 1;
  1070. padding: 16rpx;
  1071. display: flex;
  1072. flex-direction: column;
  1073. justify-content: space-between;
  1074. .article-title {
  1075. font-size: 26rpx;
  1076. font-weight: 600;
  1077. color: #2C3E50;
  1078. margin-bottom: 6rpx;
  1079. overflow: hidden;
  1080. text-overflow: ellipsis;
  1081. display: -webkit-box;
  1082. -webkit-line-clamp: 2;
  1083. -webkit-box-orient: vertical;
  1084. }
  1085. .article-source {
  1086. font-size: 22rpx;
  1087. color: #8C9396;
  1088. margin-bottom: 8rpx;
  1089. }
  1090. .article-stats {
  1091. display: flex;
  1092. align-items: center;
  1093. .read-count {
  1094. font-size: 22rpx;
  1095. color: #999;
  1096. margin-left: 6rpx;
  1097. }
  1098. }
  1099. }
  1100. }
  1101. }
  1102. // 弹窗样式
  1103. // 弹窗样式
  1104. .location-permission-popup {
  1105. padding: 40rpx;
  1106. // width: 600rpx;
  1107. // background: white;
  1108. // box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
  1109. // max-width: 90vw;
  1110. .permission-header {
  1111. display: flex;
  1112. align-items: center;
  1113. margin-bottom: 24rpx;
  1114. .permission-icon {
  1115. margin-right: 16rpx;
  1116. flex-shrink: 0;
  1117. }
  1118. .permission-title {
  1119. font-size: 32rpx;
  1120. font-weight: 600;
  1121. color: #333;
  1122. flex: 1;
  1123. }
  1124. }
  1125. .permission-content {
  1126. font-size: 25rpx;
  1127. color: #666;
  1128. line-height: 1.6;
  1129. text-align: left;
  1130. }
  1131. }
  1132. .popup-content {
  1133. padding: 32rpx;
  1134. max-height: 80vh;
  1135. overflow-y: auto;
  1136. .popup-header {
  1137. display: flex;
  1138. justify-content: space-between;
  1139. align-items: center;
  1140. margin-bottom: 24rpx;
  1141. .popup-title {
  1142. font-size: 32rpx;
  1143. font-weight: 600;
  1144. color: #2C3E50;
  1145. }
  1146. }
  1147. .weather-detail {
  1148. margin-bottom: 32rpx;
  1149. .detail-item {
  1150. display: flex;
  1151. justify-content: space-between;
  1152. align-items: center;
  1153. padding: 16rpx 0;
  1154. border-bottom: 1px solid rgba(0, 0, 0, 0.03);
  1155. .detail-label {
  1156. font-size: 26rpx;
  1157. color: #8C9396;
  1158. }
  1159. .detail-value {
  1160. font-size: 26rpx;
  1161. font-weight: 600;
  1162. color: #2C3E50;
  1163. }
  1164. }
  1165. .advice-detail {
  1166. margin-top: 24rpx;
  1167. padding: 20rpx;
  1168. background: rgba(59, 180, 74, 0.05);
  1169. border-radius: 16rpx;
  1170. .advice-label {
  1171. font-size: 26rpx;
  1172. font-weight: 600;
  1173. color: #2C3E50;
  1174. margin-bottom: 8rpx;
  1175. }
  1176. .advice-content {
  1177. font-size: 26rpx;
  1178. color: #2C3E50;
  1179. line-height: 1.4;
  1180. }
  1181. }
  1182. }
  1183. .article-detail {
  1184. margin-bottom: 32rpx;
  1185. .article-detail-title {
  1186. font-size: 32rpx;
  1187. font-weight: 700;
  1188. color: #2C3E50;
  1189. margin-bottom: 12rpx;
  1190. }
  1191. .article-detail-time {
  1192. font-size: 22rpx;
  1193. color: #8C9396;
  1194. margin-bottom: 24rpx;
  1195. }
  1196. .article-detail-content {
  1197. .content-paragraph {
  1198. font-size: 26rpx;
  1199. color: #2C3E50;
  1200. line-height: 1.6;
  1201. margin-bottom: 20rpx;
  1202. }
  1203. .content-subtitle {
  1204. font-size: 28rpx;
  1205. font-weight: 600;
  1206. color: #2C3E50;
  1207. margin-bottom: 12rpx;
  1208. }
  1209. .content-list {
  1210. .list-item {
  1211. font-size: 26rpx;
  1212. color: #2C3E50;
  1213. line-height: 1.5;
  1214. margin-bottom: 8rpx;
  1215. }
  1216. }
  1217. }
  1218. }
  1219. }
  1220. // 旋转动画
  1221. @keyframes rotate {
  1222. from {
  1223. transform: rotate(0deg);
  1224. }
  1225. to {
  1226. transform: rotate(360deg);
  1227. }
  1228. }
  1229. </style>