index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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">作物档案</div>
  13. <el-date-picker style="width: 110px" v-model="date" type="year" placeholder="全部日期" />
  14. </div>
  15. <div class="archives-time-line-content">
  16. <div class="report-box">
  17. <div class="box-content">
  18. <div class="box-title">
  19. <span>农情互动报告</span>
  20. <el-icon>
  21. <CaretRight />
  22. </el-icon>
  23. </div>
  24. <span class="box-text">当前处于蒂蛀虫高发期,请及时采集</span>
  25. </div>
  26. <img src="@/assets/img/monitor/report-icon.png" alt="" class="report-icon" />
  27. </div>
  28. <div class="time-line">
  29. <archives-farm-time-line :farmId="gardenId"></archives-farm-time-line>
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34. <tip-popup v-model:show="showFarmPopup" type="success" text="农场领取成功"
  35. :overlay-style="{ 'backdrop-filter': 'blur(4px)' }" :closeOnClickOverlay="false" :zIndex="9999" />
  36. <!-- 农事执行弹窗 -->
  37. <agri-execute-popup v-model:show="showAgriExecutePopup" :popupData="agriExecuteData"
  38. @executed="handleAgriExecuted" />
  39. </template>
  40. <script setup>
  41. import customHeader from "@/components/customHeader.vue";
  42. import { ref, computed, onActivated, onDeactivated, onMounted } from "vue";
  43. import { useStore } from "vuex";
  44. import { Badge, List } from "vant";
  45. import weatherInfo from "@/components/weatherInfo.vue";
  46. import { useRouter, useRoute } from "vue-router";
  47. import farmInfoPopup from "../home/components/farmInfoPopup.vue";
  48. import tipPopup from "@/components/popup/tipPopup.vue";
  49. import { ElMessage, ElMessageBox } from "element-plus";
  50. import ArchivesFarmTimeLine from "@/components/pageComponents/ArchivesFarmTimeLine.vue";
  51. import agriExecutePopup from "@/components/popup/agriExecutePopup.vue";
  52. const showAgriExecutePopup = ref(false); // 农事执行弹窗
  53. const agriExecuteData = ref({});
  54. const handleAgriExecuted = () => {
  55. showAgriExecutePopup.value = false;
  56. // router.push("/interaction_list?expertMiniUserId=81881&oldUser=true");
  57. router.push("/interaction_list?expertMiniUserId=81881");
  58. };
  59. const checkHasUnrepliedTriggeredInteraction = async () => {
  60. const { data } = await VE_API.home.hasUnrepliedTriggeredInteraction({ farmId: localStorage.getItem("selectedFarmId") });
  61. if (data && data.id != null) {
  62. agriExecuteData.value = {
  63. title: data.interactionTypeName,
  64. abnormalText: data.reason,
  65. exampleImg: JSON.parse(data.exampleImagesJson)[0],
  66. executedButtonText: '开始采集',
  67. };
  68. showAgriExecutePopup.value = true;
  69. }
  70. };
  71. const showFarmPopup = ref(false); // 农场领取成功弹窗
  72. const date = ref(new Date());
  73. const defaultGardenId = ref(null);
  74. const isHeaderShow = ref(false);
  75. const isDefaultFarm = ref(false);
  76. const weatherInfoRef = ref(null);
  77. onActivated(() => {
  78. // 用来接收我的农场跳转过来的农场详情逻辑
  79. if (route.query.isHeaderShow) {
  80. isHeaderShow.value = true;
  81. defaultGardenId.value = route.query.farmId;
  82. // 统一转换为布尔值
  83. isDefaultFarm.value = route.query.defaultFarm === "true" || route.query.defaultFarm === true;
  84. }
  85. checkHasUnrepliedTriggeredInteraction();
  86. });
  87. const receiveFarm = (json) => {
  88. VE_API.monitor
  89. .receiveFarm({
  90. agriculturalStoreId: json.agriculturalStoreId,
  91. farmId: json.farmId,
  92. })
  93. .then((res) => {
  94. if (res.code === 0) {
  95. showFarmPopup.value = true;
  96. defaultGardenId.value = json.farmId;
  97. } else {
  98. ElMessage.warning(res.msg);
  99. }
  100. // 清空路由参数
  101. router.replace({ path: route.path });
  102. });
  103. };
  104. const store = useStore();
  105. const tabBarHeight = computed(() => store.state.home.tabBarHeight);
  106. const router = useRouter();
  107. const route = useRoute();
  108. const farmInfoRef = ref(null);
  109. function toFarmInfo() {
  110. farmInfoRef.value.handleShow();
  111. }
  112. // 功能卡片数据
  113. const functionCards = ref([
  114. {
  115. title: "农事规划",
  116. route: "/plan",
  117. },
  118. {
  119. title: "农场报告",
  120. status: "最新",
  121. route: "/farm_report",
  122. className: "blue",
  123. },
  124. {
  125. title: "农事方案",
  126. route: "/agricultural_plan",
  127. },
  128. {
  129. title: "复核成效",
  130. status: "最新",
  131. route: "/review-results",
  132. className: "yellow",
  133. },
  134. ]);
  135. const getStayCount = () => {
  136. VE_API.monitor
  137. .getCountByStatusAndFarmId({
  138. farmId: gardenId.value,
  139. startStatus: 1,
  140. endStatus: 3,
  141. })
  142. .then((res) => {
  143. functionCards.value[0].status = null;
  144. if (res.data && res.data != 0) {
  145. functionCards.value[0].status = res.data + " 待完成";
  146. }
  147. });
  148. };
  149. // 实时播报数据
  150. const broadcastList = ref([]);
  151. const loading = ref(false);
  152. const finished = ref(false);
  153. const currentPage = ref(1);
  154. const pageSize = ref(10);
  155. const getBroadcastList = async (page = 1, isLoadMore = false) => {
  156. if (!gardenId.value) {
  157. loading.value = false;
  158. return;
  159. }
  160. // 如果正在加载,直接返回(避免重复请求)
  161. if (loading.value) {
  162. return;
  163. }
  164. loading.value = true;
  165. try {
  166. const res = await VE_API.monitor.broadcastPage({
  167. farmId: gardenId.value,
  168. limit: pageSize.value,
  169. page: page,
  170. });
  171. const newData = res.data || [];
  172. if (isLoadMore) {
  173. broadcastList.value = [...broadcastList.value, ...newData];
  174. } else {
  175. broadcastList.value = newData;
  176. }
  177. // 判断是否还有更多数据
  178. if (newData.length < pageSize.value) {
  179. finished.value = true;
  180. } else {
  181. finished.value = false;
  182. // 如果未完成,页码+1,为下次加载做准备
  183. currentPage.value = page + 1;
  184. }
  185. } catch (error) {
  186. console.error("获取播报列表失败:", error);
  187. finished.value = true;
  188. } finally {
  189. // 确保 loading 状态被正确设置为 false
  190. loading.value = false;
  191. }
  192. };
  193. // 滚动加载更多
  194. const onLoad = async () => {
  195. if (finished.value || loading.value) return;
  196. // 判断是否是首次加载(页码为1)
  197. const isLoadMore = currentPage.value > 1;
  198. const pageToLoad = currentPage.value;
  199. // 加载数据(页码会在 getBroadcastList 成功后自动更新)
  200. await getBroadcastList(pageToLoad, isLoadMore);
  201. };
  202. // 卡片点击事件
  203. const handleCardClick = (card) => {
  204. const params = {
  205. farmId: gardenId.value,
  206. };
  207. router.push({
  208. path: card.route,
  209. query: { ...params, miniJson: JSON.stringify(params) },
  210. });
  211. };
  212. // 播报相关事件
  213. const isSpeaking = ref(false);
  214. const speechSynthesis = window.speechSynthesis;
  215. const handleBroadcast = () => {
  216. if (isSpeaking.value) {
  217. // 如果正在播放,则停止
  218. speechSynthesis.cancel();
  219. isSpeaking.value = false;
  220. return;
  221. }
  222. // 构建播报文本
  223. let broadcastText = "实时播报:";
  224. if (broadcastList.value.length === 0) {
  225. broadcastText += "暂无更多播报";
  226. } else {
  227. broadcastList.value.forEach((item, index) => {
  228. broadcastText += `${index + 1}、${item.title}。${item.content}。`;
  229. });
  230. }
  231. // 创建语音合成对象
  232. const utterance = new SpeechSynthesisUtterance(broadcastText);
  233. // 设置语音参数
  234. utterance.lang = "zh-CN";
  235. utterance.rate = 0.8; // 语速
  236. utterance.pitch = 1; // 音调
  237. utterance.volume = 1; // 音量
  238. // 播放开始事件
  239. utterance.onstart = () => {
  240. isSpeaking.value = true;
  241. };
  242. // 播放结束事件
  243. utterance.onend = () => {
  244. isSpeaking.value = false;
  245. };
  246. // 播放错误事件
  247. utterance.onerror = (event) => {
  248. isSpeaking.value = false;
  249. console.error("播报错误:", event.error);
  250. };
  251. // 开始播报
  252. speechSynthesis.speak(utterance);
  253. };
  254. // 组件卸载时停止语音播放
  255. onDeactivated(() => {
  256. showFarmPopup.value = false;
  257. isDefaultFarm.value = false;
  258. if (isSpeaking.value) {
  259. speechSynthesis.cancel();
  260. isSpeaking.value = false;
  261. }
  262. });
  263. const isExpanded = ref(false);
  264. const weatherExpanded = (isExpandedValue) => {
  265. isExpanded.value = isExpandedValue;
  266. };
  267. // 点击遮罩时收起天气
  268. const handleMaskClick = () => {
  269. if (weatherInfoRef.value && weatherInfoRef.value.toggleExpand) {
  270. weatherInfoRef.value.toggleExpand();
  271. }
  272. };
  273. const gardenId = ref(store.state.home.gardenId);
  274. // 初始化加载数据
  275. onMounted(() => {
  276. if (gardenId.value) {
  277. currentPage.value = 1;
  278. finished.value = false;
  279. broadcastList.value = [];
  280. getStayCount();
  281. }
  282. });
  283. const changeGarden = ({ id }) => {
  284. gardenId.value = id;
  285. // 更新 store 中的状态
  286. store.commit("home/SET_GARDEN_ID", id);
  287. // 重置分页状态
  288. currentPage.value = 1;
  289. finished.value = false;
  290. broadcastList.value = [];
  291. getStayCount();
  292. getBroadcastList(1, false);
  293. checkHasUnrepliedTriggeredInteraction();
  294. };
  295. function handlePage(url) {
  296. const query = {
  297. farmId: gardenId.value,
  298. };
  299. if (url === "/message_list") {
  300. query.from = "monitor";
  301. }
  302. router.push({
  303. path: url,
  304. query: query,
  305. });
  306. }
  307. </script>
  308. <style scoped lang="scss">
  309. .monitor-index {
  310. width: 100%;
  311. height: 100%;
  312. padding: 13px 10px;
  313. box-sizing: border-box;
  314. background: linear-gradient(180deg, #f9f9f9 0%, #f0f8ff 31.47%, #f9f9f9 46.81%, #f9f9f9 69.38%, #f9f9f9 100%);
  315. .weather-mask {
  316. position: fixed;
  317. top: 0;
  318. left: 0;
  319. width: 100%;
  320. height: 100%;
  321. background-color: rgba(0, 0, 0, 0.52);
  322. z-index: 2;
  323. }
  324. .weather-info {
  325. width: calc(100% - 20px);
  326. position: absolute;
  327. z-index: 3;
  328. }
  329. .archives-time-line {
  330. position: relative;
  331. margin-top: 96px;
  332. height: calc(100% - 90px);
  333. .archives-time-line-header {
  334. display: flex;
  335. align-items: center;
  336. justify-content: space-between;
  337. .line-title {
  338. position: relative;
  339. padding-left: 14px;
  340. font-size: 16px;
  341. &::before {
  342. content: "";
  343. position: absolute;
  344. left: 5px;
  345. top: 50%;
  346. transform: translateY(-50%);
  347. width: 4px;
  348. height: 15px;
  349. background: #2199f8;
  350. border-radius: 20px;
  351. }
  352. }
  353. }
  354. .archives-time-line-content {
  355. margin-top: 10px;
  356. height: calc(100% - 35px);
  357. background: #fff;
  358. border-radius: 8px;
  359. padding: 10px;
  360. box-sizing: border-box;
  361. .report-box {
  362. background: linear-gradient(120deg, #eef8ff, #bbe3ff);
  363. border-radius: 4px;
  364. padding: 6px 0 0 16px;
  365. display: flex;
  366. align-items: center;
  367. justify-content: space-between;
  368. margin-bottom: 12px;
  369. .box-content {
  370. .box-title {
  371. font-size: 16px;
  372. color: #2199f8;
  373. font-weight: 500;
  374. margin-bottom: 4px;
  375. display: flex;
  376. align-items: center;
  377. }
  378. .box-text {
  379. color: #4e5969;
  380. }
  381. }
  382. .report-icon {
  383. width: 120px;
  384. height: 85px;
  385. }
  386. }
  387. .time-line {
  388. height: calc(100% - 100px);
  389. }
  390. }
  391. }
  392. }
  393. </style>