index.vue 13 KB

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