index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <template>
  2. <custom-header v-if="isHeaderShow" name="农场详情"></custom-header>
  3. <div class="monitor-index" :style="{ height: `calc(100vh - ${tabBarHeight}px)` }">
  4. <!-- 天气遮罩 -->
  5. <div class="weather-mask" v-show="isExpanded" @click="handleMaskClick"></div>
  6. <!-- 天气 -->
  7. <weather-info ref="weatherInfoRef" class="weather-info" @weatherExpanded="weatherExpanded"
  8. @changeGarden="changeGarden" :isGarden="true" :gardenId="defaultGardenId"></weather-info>
  9. <!-- 作物档案 -->
  10. <div class="archives-time-line">
  11. <div class="archives-time-line-header">
  12. <div class="line-title" @click="handleFarmInfoClick">农事记录</div>
  13. <!-- <el-date-picker style="width: 110px" v-model="date" type="year" placeholder="全部日期" /> -->
  14. </div>
  15. <!-- 品种选择 -->
  16. <div class="variety-tabs" v-if="varietyTabs.length > 0">
  17. <div v-for="(v, index) in varietyTabs" :key="index" class="variety-tab"
  18. :class="{ 'variety-tab--active': activeVariety === index }" @click="handleVarietyClick(v, index)">
  19. {{ v.regionName }}
  20. </div>
  21. </div>
  22. <template v-if="!varietyTabs.length">
  23. <div class="lock-img" @click="handleLockClick">
  24. <img src="@/assets/img/home/lock-blue.png" alt="" class="lock-img-item" />
  25. <div class="lock-text">
  26. 专属数字农场,种好卖好
  27. <div>点击解锁一键溯源增产</div>
  28. </div>
  29. <div class="lock-btn">点击解锁</div>
  30. </div>
  31. <img class="example-img" src="@/assets/img/monitor/example.png" alt="">
  32. </template>
  33. <div class="archives-time-line-content">
  34. <archives-farm-time-line key="agri_record" :farmId="farmIdData" :regionId="regionData" :containerId="containerData"
  35. pageType="agri_record"></archives-farm-time-line>
  36. </div>
  37. </div>
  38. </div>
  39. <!-- 勾选区域引导弹窗 -->
  40. <select-region-popup v-model:show="showSelectRegionPopup" :title="titlePopup" @confirm="handleGoSelectRegion"
  41. @skip="handleSkipSelectRegion" />
  42. <agri-execute-popup ref="agriExecutePopupRef" />
  43. <start-interact-popup ref="startInteractPopupRef" />
  44. </template>
  45. <script setup>
  46. import customHeader from "@/components/customHeader.vue";
  47. import { ref, computed, onActivated, onDeactivated, onMounted } from "vue";
  48. import { useStore } from "vuex";
  49. import wx from "weixin-js-sdk";
  50. import weatherInfo from "@/components/weatherInfo.vue";
  51. import { useRouter, useRoute } from "vue-router";
  52. import { ElMessage, ElMessageBox } from "element-plus";
  53. import ArchivesFarmTimeLine from "@/components/pageComponents/ArchivesFarmTimeLine.vue";
  54. import selectRegionPopup from "@/components/popup/selectRegionPopup.vue";
  55. import agriExecutePopup from "@/components/popup/agriExecutePopup.vue";
  56. import startInteractPopup from "@/components/popup/startInteractPopup.vue";
  57. const agriExecutePopupRef = ref(null);
  58. const startInteractPopupRef = ref(null);
  59. const handleFarmInfoClick = () => {
  60. // const query = {
  61. // askInfo: { title: "农场认领", content: "是否分享该链接给好友" },
  62. // shareText: "邀您参与农情互动,获取专家精准指导",
  63. // targetUrl: `home`,
  64. // paramsPage: JSON.stringify({isFarmer:true}),
  65. // imageUrl: 'https://birdseye-img.sysuimars.com/birdseye-look-mini/share-lz-bg.png',
  66. // };
  67. // wx.miniProgram.navigateTo({
  68. // url: `/pages/subPages/share_page/index?pageParams=${JSON.stringify(query)}&type=sharePage`,
  69. // });
  70. }
  71. // 品种选择(作物档案内)- 根据主体ID动态获取分区列表
  72. const varietyTabs = ref([]);
  73. const activeVariety = ref(0);
  74. const getVarietyTabs = async (isShowPopup = true) => {
  75. if (!gardenId.value) {
  76. return;
  77. }
  78. try {
  79. const res = await VE_API.monitor.listRegionsBySubjectId({
  80. subjectId: gardenId.value,
  81. });
  82. varietyTabs.value = res.data || []
  83. if (varietyTabs.value.length > 0) {
  84. handleVarietyClick(varietyTabs.value[activeVariety.value], activeVariety.value)
  85. if(isShowPopup) {
  86. agriExecutePopupRef.value.showPopup(varietyTabs.value[activeVariety.value].farmId);
  87. }
  88. }
  89. } catch (error) {
  90. console.error("获取主体分区列表失败:", error);
  91. }
  92. };
  93. const farmIdData = ref(null);
  94. const containerData = ref(null);
  95. const regionData = ref(null);
  96. const titlePopup = ref("");
  97. const currentVariety = ref(null);
  98. const showSelectRegionPopup = ref(false);
  99. const handleVarietyClick = (tab, index) => {
  100. activeVariety.value = index;
  101. farmIdData.value = tab.farmId;
  102. containerData.value = tab.containerId;
  103. regionData.value = tab.regionId;
  104. currentVariety.value = tab;
  105. if (tab.lastViewTime == null) {
  106. titlePopup.value = `勾选 ${tab.regionName} 区域`;
  107. showSelectRegionPopup.value = true;
  108. }
  109. };
  110. const handleSkipSelectRegion = () => {
  111. VE_API.basic_farm.updateLastViewTime({
  112. regionId: currentVariety.value.regionId,
  113. }).then(() => {
  114. showSelectRegionPopup.value = false;
  115. getVarietyTabs(false);
  116. });
  117. };
  118. const handleGoSelectRegion = () => {
  119. showSelectRegionPopup.value = false;
  120. router.push({
  121. path: "/draw_area",
  122. query: {
  123. subjectId: gardenId.value,
  124. varietyId: currentVariety.value.typeId,
  125. },
  126. });
  127. };
  128. const showFarmPopup = ref(false); // 农场领取成功弹窗
  129. const date = ref(new Date());
  130. const defaultGardenId = ref(null);
  131. const isHeaderShow = ref(false);
  132. const isDefaultFarm = ref(false);
  133. const weatherInfoRef = ref(null);
  134. onActivated(() => {
  135. // 用来接收我的农场跳转过来的农场详情逻辑
  136. if (route.query.isHeaderShow) {
  137. isHeaderShow.value = true;
  138. defaultGardenId.value = route.query.farmId;
  139. // 统一转换为布尔值
  140. isDefaultFarm.value = route.query.defaultFarm === "true" || route.query.defaultFarm === true;
  141. }
  142. });
  143. const receiveFarm = (json) => {
  144. VE_API.monitor
  145. .receiveFarm({
  146. agriculturalStoreId: json.agriculturalStoreId,
  147. farmId: json.farmId,
  148. })
  149. .then((res) => {
  150. if (res.code === 0) {
  151. showFarmPopup.value = true;
  152. defaultGardenId.value = json.farmId;
  153. } else {
  154. ElMessage.warning(res.msg);
  155. }
  156. // 清空路由参数
  157. router.replace({ path: route.path });
  158. });
  159. };
  160. const store = useStore();
  161. const tabBarHeight = computed(() => store.state.home.tabBarHeight);
  162. const router = useRouter();
  163. const route = useRoute();
  164. const farmInfoRef = ref(null);
  165. function toFarmInfo() {
  166. farmInfoRef.value.handleShow();
  167. }
  168. const handleLockClick = () => {
  169. wx.miniProgram.navigateTo({
  170. url: '/pages/subPages/phone_auth/index',
  171. });
  172. // wx.miniProgram.navigateTo({
  173. // url: `/pages/subPages/share_page/index?pageParams=${JSON.stringify(query)}&type=sharePage`,
  174. // });
  175. }
  176. // 实时播报数据
  177. const broadcastList = ref([]);
  178. const loading = ref(false);
  179. const finished = ref(false);
  180. const currentPage = ref(1);
  181. const pageSize = ref(10);
  182. const getBroadcastList = async (page = 1, isLoadMore = false) => {
  183. if (!gardenId.value) {
  184. loading.value = false;
  185. return;
  186. }
  187. // 如果正在加载,直接返回(避免重复请求)
  188. if (loading.value) {
  189. return;
  190. }
  191. loading.value = true;
  192. try {
  193. const res = await VE_API.monitor.broadcastPage({
  194. farmId: gardenId.value,
  195. limit: pageSize.value,
  196. page: page,
  197. });
  198. const newData = res.data || [];
  199. if (isLoadMore) {
  200. broadcastList.value = [...broadcastList.value, ...newData];
  201. } else {
  202. broadcastList.value = newData;
  203. }
  204. // 判断是否还有更多数据
  205. if (newData.length < pageSize.value) {
  206. finished.value = true;
  207. } else {
  208. finished.value = false;
  209. // 如果未完成,页码+1,为下次加载做准备
  210. currentPage.value = page + 1;
  211. }
  212. } catch (error) {
  213. console.error("获取播报列表失败:", error);
  214. finished.value = true;
  215. } finally {
  216. // 确保 loading 状态被正确设置为 false
  217. loading.value = false;
  218. }
  219. };
  220. // 滚动加载更多
  221. const onLoad = async () => {
  222. if (finished.value || loading.value) return;
  223. // 判断是否是首次加载(页码为1)
  224. const isLoadMore = currentPage.value > 1;
  225. const pageToLoad = currentPage.value;
  226. // 加载数据(页码会在 getBroadcastList 成功后自动更新)
  227. await getBroadcastList(pageToLoad, isLoadMore);
  228. };
  229. // 卡片点击事件
  230. const handleCardClick = (card) => {
  231. const params = {
  232. farmId: gardenId.value,
  233. };
  234. router.push({
  235. path: card.route,
  236. query: { ...params, miniJson: JSON.stringify(params) },
  237. });
  238. };
  239. // 播报相关事件
  240. const isSpeaking = ref(false);
  241. const speechSynthesis = window.speechSynthesis;
  242. const handleBroadcast = () => {
  243. if (isSpeaking.value) {
  244. // 如果正在播放,则停止
  245. speechSynthesis.cancel();
  246. isSpeaking.value = false;
  247. return;
  248. }
  249. // 构建播报文本
  250. let broadcastText = "实时播报:";
  251. if (broadcastList.value.length === 0) {
  252. broadcastText += "暂无更多播报";
  253. } else {
  254. broadcastList.value.forEach((item, index) => {
  255. broadcastText += `${index + 1}、${item.title}。${item.content}。`;
  256. });
  257. }
  258. // 创建语音合成对象
  259. const utterance = new SpeechSynthesisUtterance(broadcastText);
  260. // 设置语音参数
  261. utterance.lang = "zh-CN";
  262. utterance.rate = 0.8; // 语速
  263. utterance.pitch = 1; // 音调
  264. utterance.volume = 1; // 音量
  265. // 播放开始事件
  266. utterance.onstart = () => {
  267. isSpeaking.value = true;
  268. };
  269. // 播放结束事件
  270. utterance.onend = () => {
  271. isSpeaking.value = false;
  272. };
  273. // 播放错误事件
  274. utterance.onerror = (event) => {
  275. isSpeaking.value = false;
  276. console.error("播报错误:", event.error);
  277. };
  278. // 开始播报
  279. speechSynthesis.speak(utterance);
  280. };
  281. // 组件卸载时停止语音播放
  282. onDeactivated(() => {
  283. showFarmPopup.value = false;
  284. isDefaultFarm.value = false;
  285. if (isSpeaking.value) {
  286. speechSynthesis.cancel();
  287. isSpeaking.value = false;
  288. }
  289. });
  290. const isExpanded = ref(false);
  291. const weatherExpanded = (isExpandedValue) => {
  292. isExpanded.value = isExpandedValue;
  293. };
  294. // 点击遮罩时收起天气
  295. const handleMaskClick = () => {
  296. if (weatherInfoRef.value && weatherInfoRef.value.toggleExpand) {
  297. weatherInfoRef.value.toggleExpand();
  298. }
  299. };
  300. const gardenId = ref(store.state.home.gardenId);
  301. // 初始化加载数据
  302. onMounted(() => {
  303. if (gardenId.value) {
  304. getVarietyTabs();
  305. }
  306. });
  307. const changeGarden = ({ id }) => {
  308. gardenId.value = id;
  309. // 更新 store 中的状态
  310. store.commit("home/SET_GARDEN_ID", id);
  311. getVarietyTabs();
  312. startInteractPopupRef.value.getPhenologyInitOrConfirmStatus();
  313. };
  314. </script>
  315. <style scoped lang="scss">
  316. .monitor-index {
  317. width: 100%;
  318. height: 100%;
  319. padding: 13px 10px;
  320. box-sizing: border-box;
  321. background: linear-gradient(180deg, #f9f9f9 0%, #f0f8ff 31.47%, #f9f9f9 46.81%, #f9f9f9 69.38%, #f9f9f9 100%);
  322. .weather-mask {
  323. position: fixed;
  324. top: 0;
  325. left: 0;
  326. width: 100%;
  327. height: 100%;
  328. background-color: rgba(0, 0, 0, 0.52);
  329. z-index: 11;
  330. }
  331. .lock-img {
  332. position: fixed;
  333. z-index: 10;
  334. top: 50%;
  335. left: 50%;
  336. transform: translate(-50%, -20%);
  337. width: 100%;
  338. display: flex;
  339. align-items: center;
  340. justify-content: center;
  341. flex-direction: column;
  342. gap: 16px;
  343. .lock-img-item {
  344. width: 57px;
  345. }
  346. .lock-text {
  347. font-size: 14px;
  348. color: #000;
  349. padding: 5px 64px;
  350. line-height: 21px;
  351. background: linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, #FFFFFF 50%, rgba(255, 255, 255, 0) 100%);
  352. }
  353. .lock-btn {
  354. width: 140px;
  355. height: 40px;
  356. line-height: 40px;
  357. text-align: center;
  358. background: linear-gradient(180deg, #76C3FF 0%, #2199F8 100%);
  359. border-radius: 25px;
  360. color: #fff;
  361. font-size: 16px;
  362. }
  363. }
  364. .example-img {
  365. width: 100%;
  366. height: 100%;
  367. object-fit: contain;
  368. }
  369. .weather-info {
  370. width: calc(100% - 20px);
  371. position: absolute;
  372. z-index: 12;
  373. }
  374. .archives-time-line {
  375. position: relative;
  376. margin-top: 96px;
  377. height: calc(100% - 90px);
  378. .archives-time-line-header {
  379. display: flex;
  380. align-items: center;
  381. justify-content: space-between;
  382. .line-title {
  383. position: relative;
  384. padding-left: 14px;
  385. font-size: 16px;
  386. &::before {
  387. content: "";
  388. position: absolute;
  389. left: 5px;
  390. top: 50%;
  391. transform: translateY(-50%);
  392. width: 4px;
  393. height: 15px;
  394. background: #2199f8;
  395. border-radius: 20px;
  396. }
  397. }
  398. }
  399. .variety-tabs {
  400. display: flex;
  401. align-items: center;
  402. gap: 8px;
  403. margin: 10px 0;
  404. overflow-x: auto;
  405. overflow-y: hidden;
  406. flex-wrap: nowrap;
  407. -webkit-overflow-scrolling: touch;
  408. .variety-tab {
  409. padding: 4px 12px;
  410. border-radius: 2px;
  411. color: #767676;
  412. background: #fff;
  413. white-space: nowrap;
  414. flex-shrink: 0;
  415. }
  416. .variety-tab--active {
  417. background: #2199F8;
  418. color: #ffffff;
  419. }
  420. }
  421. .archives-time-line-content {
  422. margin-top: 10px;
  423. height: calc(100% - 70px);
  424. background: #fff;
  425. border-radius: 8px;
  426. padding: 10px;
  427. box-sizing: border-box;
  428. }
  429. }
  430. }
  431. </style>