ArchivesFarmTimeLine.vue 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. <template>
  2. <div class="timeline-container" ref="timelineContainerRef">
  3. <div class="timeline-list" ref="timelineListRef">
  4. <div class="timeline-middle-line"></div>
  5. <div
  6. v-for="(t, tIdx) in solarTerms"
  7. :key="`term-${uniqueTimestamp}-${tIdx}`"
  8. class="timeline-term"
  9. :style="getTermStyle(t, tIdx)"
  10. >
  11. <span class="term-name">{{ t.displayName }}</span>
  12. <span class="term-date">01-05</span>
  13. </div>
  14. <div v-for="(p, idx) in phenologyList" :key="`phenology-${uniqueTimestamp}-${idx}`" class="phenology-bar">
  15. <div
  16. class="phenology-title"
  17. :class="{ 'phenology-red': !shouldShowBlue(p), 'phenology-blue': shouldShowBlue(p) }"
  18. v-if="p.reproductiveList[0]?.phenologyName === getNextPhenologyName(idx, 0)"
  19. >
  20. {{ p.reproductiveList[0]?.phenologyName }}
  21. </div>
  22. <div
  23. v-for="(r, rIdx) in Array.isArray(p.reproductiveList) ? p.reproductiveList : []"
  24. :key="`reproductive-${uniqueTimestamp}-${idx}-${rIdx}`"
  25. class="reproductive-item"
  26. >
  27. <div class="arranges">
  28. <div
  29. v-for="(fw, aIdx) in Array.isArray(r.farmWorkArrangeList) ? r.farmWorkArrangeList : []"
  30. :key="`arrange-${uniqueTimestamp}-${idx}-${rIdx}-${aIdx}`"
  31. class="arrange-card"
  32. :class="[
  33. getArrangeStatusClass(fw),
  34. { 'first-card': aIdx === 0 && rIdx !== 0 },
  35. { 'last-card': aIdx === r.farmWorkArrangeList.length - 1 && rIdx !== r.farmWorkArrangeList.length - 1 },
  36. // 右侧农事卡片跟随物候期颜色:未来节气对应的农事卡片置灰
  37. { 'future-card': !shouldShowBlue(p) }
  38. ]"
  39. @click="handleRowClick(fw)"
  40. >
  41. <div class="card-content">
  42. <div class="card-left">
  43. <div class="left-date">{{ formatDate(fw.createTime) }}</div>
  44. <span>张扬 上传了作物照片</span>
  45. </div>
  46. <div class="card-right">
  47. <img src="@/assets/img/home/farm.png" alt="">
  48. <div class="num">2</div>
  49. </div>
  50. </div>
  51. </div>
  52. </div>
  53. <template v-if="r.name === r.phenologyName">
  54. <div
  55. class="phenology-name single"
  56. :class="{ 'phenology-red': !shouldShowBlue(p), 'phenology-blue': shouldShowBlue(p) }"
  57. :style="r.phenologyName === getNextPhenologyName(idx, rIdx) ? 'padding: 6px 0;' : ''"
  58. >
  59. {{ r.name }}
  60. </div>
  61. </template>
  62. <template v-else>
  63. <template v-if="r.phenologyName === getNextPhenologyName(idx, rIdx)">
  64. <div
  65. class="phenology-name"
  66. :class="{ 'text-red': !shouldShowBlue(p), 'text-blue': shouldShowBlue(p) }"
  67. >
  68. {{ r.name }}
  69. </div>
  70. </template>
  71. <template v-else>
  72. <div
  73. class="phenology-name"
  74. :class="{ 'text-red': !shouldShowBlue(p), 'text-blue': shouldShowBlue(p) }"
  75. >
  76. {{ r.name }}
  77. </div>
  78. <div
  79. class="phenology-name mr"
  80. :class="{ 'phenology-red': !shouldShowBlue(p), 'phenology-blue': shouldShowBlue(p) }"
  81. >
  82. {{ r.phenologyName }}
  83. </div>
  84. </template>
  85. </template>
  86. </div>
  87. </div>
  88. </div>
  89. </div>
  90. <!-- 互动设置弹窗 -->
  91. <interact-popup ref="interactPopupRef" @handleSaveSuccess="updateFarmWorkPlan"></interact-popup>
  92. </template>
  93. <script setup>
  94. import { ref, nextTick, watch, onMounted, onUnmounted } from "vue";
  95. import interactPopup from "@/components/popup/interactPopup.vue";
  96. import { ElMessage } from "element-plus";
  97. import { WarningFilled } from "@element-plus/icons-vue";
  98. const props = defineProps({
  99. // 农场 ID,用于请求农事规划数据
  100. farmId: {
  101. type: [String, Number],
  102. default: null,
  103. },
  104. // 页面类型:种植方案 / 农事规划,用来控制高度样式
  105. pageType: {
  106. type: String,
  107. default: "",
  108. },
  109. // 是否禁用所有点击事件(用于只读展示)
  110. disableClick: {
  111. type: Boolean,
  112. default: false,
  113. },
  114. containerId: {
  115. type: [Number, String],
  116. default: null,
  117. },
  118. // 是否是标准农事
  119. isStandard: {
  120. type: Boolean,
  121. default: false,
  122. },
  123. // 方案ID
  124. schemeId: {
  125. type: [Number, String],
  126. default: null,
  127. },
  128. });
  129. const farmWorkType = {
  130. 0: "预警农事",
  131. 1: "标准农事",
  132. 2: "建议农事",
  133. 3: "自建农事",
  134. };
  135. const emits = defineEmits(["row-click"]);
  136. const solarTerms = ref([]);
  137. const phenologyList = ref([]);
  138. const timelineContainerRef = ref(null);
  139. const timelineListRef = ref(null);
  140. // 标记是否为首次加载
  141. const isInitialLoad = ref(true);
  142. // 存储timeline-list的实际渲染高度
  143. const timelineListHeight = ref(0);
  144. // 生成唯一的时间戳,用于确保key的唯一性
  145. const uniqueTimestamp = ref(Date.now());
  146. // ResizeObserver 实例,用于监听高度变化
  147. let resizeObserver = null;
  148. // 获取当前季节
  149. const getCurrentSeason = () => {
  150. const month = new Date().getMonth() + 1; // 1-12
  151. if (month >= 3 && month <= 5) {
  152. return "spring"; // 春季:3-5月
  153. } else if (month >= 6 && month <= 8) {
  154. return "summer"; // 夏季:6-8月
  155. } else if (month >= 9 && month <= 10) {
  156. return "autumn"; // 秋季:9-10月
  157. } else {
  158. return "winter"; // 冬季:11-2月
  159. }
  160. };
  161. // 安全解析时间到时间戳(ms)
  162. const safeParseDate = (val) => {
  163. if (!val) return NaN;
  164. if (val instanceof Date) return val.getTime();
  165. if (typeof val === "number") return val;
  166. if (typeof val === "string") {
  167. // 兼容 "YYYY-MM-DD HH:mm:ss" -> Safari
  168. const s = val.replace(/-/g, "/").replace("T", " ");
  169. const d = new Date(s);
  170. return isNaN(d.getTime()) ? NaN : d.getTime();
  171. }
  172. return NaN;
  173. };
  174. const batchValidateData = ref({});
  175. const allTrue = ref(false);
  176. const invalidIds = ref([]);
  177. const invalidArr = ref([]);
  178. // 验证农事卡片药肥报价信息是否完整
  179. const batchValidatePesticideFertilizerQuotes = (ids, items) => {
  180. if (props.isStandard) {
  181. return;
  182. }
  183. VE_API.monitor
  184. .batchValidatePesticideFertilizerQuotes({ ids, schemeId: props.schemeId })
  185. .then(({ data, code }) => {
  186. if (code === 0) {
  187. batchValidateData.value = data || {};
  188. allTrue.value = Object.values(data).every((value) => value === true);
  189. invalidIds.value = Object.keys(data).filter((key) => data[key] !== true);
  190. // 清空之前的arrangeIds
  191. invalidArr.value = [];
  192. // 遍历items,判断farmWorkId是否在invalidIds中,如果对应上了就把item.id push进去
  193. items.forEach((item) => {
  194. // 判断item.farmWorkId是否在invalidIds数组中(需要转换为字符串进行比较)
  195. const farmWorkIdStr = String(item.farmWorkId);
  196. if (invalidIds.value.includes(farmWorkIdStr)) {
  197. invalidArr.value.push({
  198. arrangeId: item.id,
  199. farmWorkId: item.farmWorkId,
  200. });
  201. }
  202. });
  203. }
  204. })
  205. .catch(() => {});
  206. };
  207. // 获取下一个reproductive-item的phenologyName
  208. const getNextPhenologyName = (currentPhenologyIdx, currentReproductiveIdx) => {
  209. const currentPhenology = phenologyList.value[currentPhenologyIdx];
  210. if (!currentPhenology || !Array.isArray(currentPhenology.reproductiveList)) {
  211. return null;
  212. }
  213. // 如果当前reproductive-item不是最后一个,获取同一个物候期的下一个
  214. if (currentReproductiveIdx < currentPhenology.reproductiveList.length - 1) {
  215. const nextReproductive = currentPhenology.reproductiveList[currentReproductiveIdx + 1];
  216. return nextReproductive?.phenologyName || null;
  217. }
  218. // 如果当前reproductive-item是最后一个,获取下一个物候期的第一个reproductive-item
  219. if (currentPhenologyIdx < phenologyList.value.length - 1) {
  220. const nextPhenology = phenologyList.value[currentPhenologyIdx + 1];
  221. if (nextPhenology && Array.isArray(nextPhenology.reproductiveList) && nextPhenology.reproductiveList.length > 0) {
  222. const firstReproductive = nextPhenology.reproductiveList[0];
  223. return firstReproductive?.phenologyName || null;
  224. }
  225. }
  226. return null;
  227. };
  228. // 计算物候期需要的实际高度(基于农事数量)
  229. const getPhenologyRequiredHeight = (item) => {
  230. // 统计该物候期内的农事数量
  231. let farmWorkCount = 0;
  232. if (Array.isArray(item.reproductiveList)) {
  233. item.reproductiveList.forEach((reproductive) => {
  234. if (Array.isArray(reproductive.farmWorkArrangeList)) {
  235. farmWorkCount += reproductive.farmWorkArrangeList.length;
  236. }
  237. });
  238. }
  239. // 如果没有农事,给一个最小高度
  240. if (farmWorkCount === 0) {
  241. return 50; // 最小50px
  242. }
  243. // 每个农事卡片的高度(根据实际内容,卡片高度可能因内容而异)
  244. // 卡片包含:padding(8px*2) + header(约25px) + content margin(4px+2px) + content(约25-30px) = 约72-77px
  245. // 考虑到内容可能换行,实际高度可能更高,设置为120px更安全,避免卡片重叠
  246. const farmWorkCardHeight = 120; // 卡片高度估算,确保能容纳内容且不重叠
  247. // 卡片之间的间距(与CSS中的gap保持一致)
  248. const cardGap = 12;
  249. // 计算总高度:卡片数量 * 卡片高度 + (卡片数量 - 1) * 间距
  250. // 如果有多个卡片,需要加上它们之间的间距
  251. const totalHeight = farmWorkCount * farmWorkCardHeight + (farmWorkCount > 1 ? (farmWorkCount - 1) * cardGap : 0);
  252. // 返回精确的总高度,只保留最小高度限制,不添加额外余量
  253. return Math.max(totalHeight, 50); // 最小50px,精确匹配农事卡片高度
  254. };
  255. // 计算所有物候期的累积位置和总高度
  256. const calculatePhenologyPositions = () => {
  257. let currentTop = 10; // 起始位置,留出顶部间距
  258. const positions = new Map();
  259. // 按progress排序物候期,确保按时间顺序排列
  260. const sortedPhenologyList = [...phenologyList.value].sort((a, b) => {
  261. const aProgress = Math.min(Number(a?.progress) || 0, Number(a?.progress2) || 0);
  262. const bProgress = Math.min(Number(b?.progress) || 0, Number(b?.progress2) || 0);
  263. return aProgress - bProgress;
  264. });
  265. sortedPhenologyList.forEach((phenology) => {
  266. const height = getPhenologyRequiredHeight(phenology);
  267. // 使用与数据生成时相同的ID生成逻辑
  268. const itemId =
  269. phenology.id ?? phenology.phenologyId ?? phenology.name ?? `${phenology.progress}-${phenology.progress2}`;
  270. positions.set(itemId, {
  271. top: currentTop,
  272. height: height,
  273. });
  274. currentTop += height; // 紧挨着下一个物候期,不留间距
  275. });
  276. return {
  277. positions,
  278. totalHeight: currentTop, // 总高度 = 最后一个物候期的底部位置,不添加额外间距
  279. };
  280. };
  281. // 计算所有农事的总高度(基于物候期紧挨排列)
  282. const calculateTotalHeightByFarmWorks = () => {
  283. const { totalHeight } = calculatePhenologyPositions();
  284. // 如果有物候期数据,直接使用计算出的总高度
  285. // totalHeight 已经包含了从 10 开始的起始位置和所有物候期的高度
  286. if (totalHeight > 10) {
  287. // 确保总高度至少能容纳所有节气(每个节气至少50px)
  288. const baseHeight = (solarTerms.value?.length || 0) * 50;
  289. // 返回物候期总高度和基础高度的较大值,确保节气能正常显示
  290. return Math.max(totalHeight, baseHeight);
  291. }
  292. // 如果没有物候期数据,返回基础高度
  293. const baseHeight = (solarTerms.value?.length || 0) * 50;
  294. return baseHeight || 100; // 至少返回100px,避免为0
  295. };
  296. const getTermStyle = (t, index) => {
  297. // 优先使用实际测量的timeline-list高度,如果没有测量到则使用计算值作为后备
  298. const totalHeight = timelineListHeight.value > 0 ? timelineListHeight.value : calculateTotalHeightByFarmWorks();
  299. // 获取节气总数
  300. const termCount = solarTerms.value?.length || 1;
  301. // 等分高度:总高度 / 节气数量
  302. const termHeight = totalHeight / termCount;
  303. // 计算top位置:索引 * 每个节气的高度
  304. const top = index * termHeight;
  305. return {
  306. position: "absolute",
  307. top: `${top}px`,
  308. left: 0,
  309. width: "32px",
  310. height: `${termHeight}px`, // 高度等分,使用实际测量的高度
  311. display: "flex",
  312. alignItems: "center",
  313. };
  314. };
  315. // 点击季节 → 滚动到对应节气(立春/立夏/立秋/立冬)
  316. const handleSeasonClick = (seasonValue) => {
  317. const mapping = {
  318. spring: "立春",
  319. summer: "立夏",
  320. autumn: "立秋",
  321. winter: "立冬",
  322. };
  323. const targetName = mapping[seasonValue];
  324. if (!targetName) return;
  325. // 查找对应的节气
  326. const targetIndex = solarTerms.value.findIndex((t) => (t?.displayName || "") === targetName);
  327. if (targetIndex === -1) return;
  328. // 计算目标节气的top位置
  329. const totalHeight = timelineListHeight.value > 0 ? timelineListHeight.value : calculateTotalHeightByFarmWorks();
  330. const termCount = solarTerms.value?.length || 1;
  331. const termHeight = totalHeight / termCount;
  332. const targetTop = targetIndex * termHeight;
  333. // 滚动到目标位置
  334. const wrap = timelineContainerRef.value;
  335. if (!wrap) return;
  336. const viewH = wrap.clientHeight || 0;
  337. const maxScroll = Math.max(0, wrap.scrollHeight - viewH);
  338. // 将目标位置稍微靠上(使用 0.1 视口高度做偏移)
  339. let scrollTop = Math.max(0, targetTop - viewH * 0.1);
  340. if (scrollTop > maxScroll) scrollTop = maxScroll;
  341. wrap.scrollTo({ top: scrollTop, behavior: "smooth" });
  342. };
  343. // 农事状态样式映射(0:取消关注,1:关注,2:托管农事,)
  344. const getArrangeStatusClass = (fw) => {
  345. const t = fw?.isFollow;
  346. // const warningStatus = shouldShowIncompleteStatus(fw.farmWorkId);
  347. if (t == 0) return "normal-style";
  348. // if (warningStatus) return "status-warning";
  349. // if (t >= 0 && t <= 4) return "status-normal";
  350. // if (t === 5) return "status-complete";
  351. return "status-normal";
  352. };
  353. const handleRowClick = (item) => {
  354. // 记录当前页面滚动位置
  355. if (timelineContainerRef.value) {
  356. const scrollTop = timelineContainerRef.value.scrollTop || 0;
  357. sessionStorage.setItem('timelineScrollTop', scrollTop.toString());
  358. }
  359. // item.isEdit = shouldShowIncompleteStatus(item.farmWorkId);
  360. item.invalidIds = invalidIds.value;
  361. item.invalidArr = invalidArr.value;
  362. emits("row-click", item);
  363. };
  364. const interactPopupRef = ref(null);
  365. const handleEdit = (item) => {
  366. if (props.disableClick) return;
  367. if (interactPopupRef.value) {
  368. interactPopupRef.value.showPopup(item);
  369. }
  370. };
  371. // 获取农事规划数据
  372. const getFarmWorkPlan = () => {
  373. if (!props.farmId) return;
  374. // 更新时间戳,确保key变化,触发DOM重新渲染
  375. uniqueTimestamp.value = Date.now();
  376. // 重置测量高度,等待重新测量
  377. timelineListHeight.value = 0;
  378. let savedScrollTop = 0;
  379. if (!isInitialLoad.value && timelineContainerRef.value) {
  380. savedScrollTop = timelineContainerRef.value.scrollTop || 0;
  381. }
  382. VE_API.monitor
  383. .getArchivesList({ farmId: props.farmId })
  384. .then(({ data, code }) => {
  385. if (code === 0) {
  386. const list = Array.isArray(data?.solarTermsList) ? data.solarTermsList : [];
  387. const filtered = list
  388. .filter((t) => t && t.type === 1)
  389. .map((t) => ({
  390. id:
  391. t.id ??
  392. t.solarTermsId ??
  393. t.termId ??
  394. `${t.name || t.solarTermsName || t.termName || "term"}-${t.createDate || ""}`,
  395. displayName: t.name || t.solarTermsName || t.termName || "节气",
  396. createDate: t.createDate || null,
  397. progress: Number(t.progress) || 0,
  398. }));
  399. solarTerms.value = filtered;
  400. // 物候期数据
  401. phenologyList.value = Array.isArray(data?.phenologyList)
  402. ? data.phenologyList.map((it) => {
  403. const reproductiveList = Array.isArray(it.reproductiveList)
  404. ? it.reproductiveList.map((r) => {
  405. const farmWorkArrangeList = Array.isArray(r.broadcastList)
  406. ? r.broadcastList.map((fw) => ({
  407. ...fw,
  408. containerSpaceTimeId: it.containerSpaceTimeId,
  409. }))
  410. : [];
  411. return {
  412. ...r,
  413. farmWorkArrangeList,
  414. };
  415. })
  416. : [];
  417. return {
  418. id: it.id ?? it.phenologyId ?? it.name ?? `${it.progress}-${it.progress2}`,
  419. progress: Number(it.progress) || 0, // 起点 %
  420. progress2: Number(it.progress2) || 0, // 终点 %
  421. startDate: it.startDate,
  422. startTimeMs: safeParseDate(
  423. it.startDate || it.beginDate || it.startTime || it.start || it.start_at
  424. ),
  425. reproductiveList,
  426. };
  427. })
  428. : [];
  429. // 使用多次 nextTick 和 requestAnimationFrame 确保DOM完全渲染
  430. nextTick(() => {
  431. requestAnimationFrame(() => {
  432. nextTick(() => {
  433. requestAnimationFrame(() => {
  434. // 测量timeline-list的实际渲染高度
  435. if (timelineListRef.value) {
  436. const height =
  437. timelineListRef.value.offsetHeight || timelineListRef.value.clientHeight;
  438. if (height > 0) {
  439. timelineListHeight.value = height;
  440. // 如果是首次加载,滚动到当前季节对应的节气
  441. if (isInitialLoad.value) {
  442. const currentSeason = getCurrentSeason();
  443. handleSeasonClick(currentSeason);
  444. isInitialLoad.value = false;
  445. }
  446. }
  447. }
  448. if (isInitialLoad.value) {
  449. // 如果测量失败,延迟一下再尝试滚动
  450. setTimeout(() => {
  451. if (timelineListRef.value) {
  452. const height =
  453. timelineListRef.value.offsetHeight ||
  454. timelineListRef.value.clientHeight;
  455. if (height > 0) {
  456. timelineListHeight.value = height;
  457. }
  458. }
  459. const currentSeason = getCurrentSeason();
  460. handleSeasonClick(currentSeason);
  461. isInitialLoad.value = false;
  462. }, 200);
  463. } else {
  464. // 尝试恢复之前保存的滚动位置
  465. const savedScrollTopFromStorage = sessionStorage.getItem('timelineScrollTop');
  466. if (savedScrollTopFromStorage) {
  467. // 等待 DOM 完全渲染后再恢复滚动位置
  468. nextTick(() => {
  469. requestAnimationFrame(() => {
  470. if (timelineContainerRef.value) {
  471. const scrollTop = Number(savedScrollTopFromStorage);
  472. timelineContainerRef.value.scrollTop = scrollTop;
  473. // 恢复后清除保存的位置,避免下次误恢复
  474. sessionStorage.removeItem('timelineScrollTop');
  475. }
  476. });
  477. });
  478. } else if (timelineContainerRef.value && savedScrollTop > 0) {
  479. timelineContainerRef.value.scrollTop = savedScrollTop;
  480. }
  481. }
  482. });
  483. });
  484. });
  485. });
  486. // 收集所有farmWorkId
  487. const farmWorkIds = [];
  488. const farmWorks = [];
  489. phenologyList.value.forEach((phenology) => {
  490. if (Array.isArray(phenology.reproductiveList)) {
  491. phenology.reproductiveList.forEach((reproductive) => {
  492. if (Array.isArray(reproductive.farmWorkArrangeList)) {
  493. reproductive.farmWorkArrangeList.forEach((farmWork) => {
  494. if (farmWork.farmWorkId && farmWork.isFollow !== 0) {
  495. farmWorkIds.push(farmWork.farmWorkId);
  496. farmWorks.push(farmWork);
  497. }
  498. });
  499. }
  500. });
  501. }
  502. });
  503. // 调用验证方法,传入所有ids
  504. if (farmWorkIds.length > 0) {
  505. batchValidatePesticideFertilizerQuotes(farmWorkIds, farmWorks);
  506. }
  507. }
  508. })
  509. .catch((error) => {
  510. console.error("获取农事规划数据失败:", error);
  511. ElMessage.error("获取农事规划数据失败");
  512. });
  513. };
  514. const updateFarmWorkPlan = () => {
  515. solarTerms.value = [];
  516. phenologyList.value = [];
  517. getFarmWorkPlan();
  518. };
  519. watch(
  520. () => props.farmId || props.containerId,
  521. (val) => {
  522. if (val) {
  523. isInitialLoad.value = true;
  524. updateFarmWorkPlan();
  525. }
  526. },
  527. { immediate: true }
  528. );
  529. watch(
  530. () => props.schemeId,
  531. (val) => {
  532. // if (val) {
  533. updateFarmWorkPlan();
  534. // }
  535. }
  536. );
  537. // 格式化日期为 MM-DD 格式
  538. const formatDate = (dateStr) => {
  539. if (!dateStr) return "--";
  540. const date = new Date(dateStr);
  541. if (Number.isNaN(date.getTime())) return dateStr;
  542. const m = `${date.getMonth() + 1}`.padStart(2, "0");
  543. const d = `${date.getDate()}`.padStart(2, "0");
  544. return `${m}-${d}`;
  545. };
  546. // 获取下一个即将到来的节气(当前节气)的 progress
  547. const getNextTermProgress = () => {
  548. if (!solarTerms.value || solarTerms.value.length === 0) return Infinity;
  549. const now = new Date();
  550. now.setHours(0, 0, 0, 0);
  551. let nextTermProgress = Infinity;
  552. // 找到当前日期之后的下一个节气(当前节气)
  553. solarTerms.value.forEach((term) => {
  554. const termDate = safeParseDate(term.createDate);
  555. if (!isNaN(termDate)) {
  556. const termDateObj = new Date(termDate);
  557. termDateObj.setHours(0, 0, 0, 0);
  558. // 找到大于等于当前日期的第一个节气
  559. if (termDateObj >= now) {
  560. const termProgress = Number(term.progress) || 0;
  561. if (termProgress < nextTermProgress) {
  562. nextTermProgress = termProgress;
  563. }
  564. }
  565. }
  566. });
  567. // 如果没有找到未来的节气,说明所有节气都已过,返回 Infinity(所有物候期都显示蓝色)
  568. return nextTermProgress === Infinity ? Infinity : nextTermProgress;
  569. };
  570. // 根据物候期的 progress 判断它所属节气的 progress
  571. const getPhenologyTermProgress = (phenologyProgress) => {
  572. if (!solarTerms.value || solarTerms.value.length === 0) return -1;
  573. const progress = Number(phenologyProgress) || 0;
  574. // 找到物候期所属的节气(progress 最接近且小于等于的节气)
  575. let matchedTermProgress = -1;
  576. solarTerms.value.forEach((term) => {
  577. const termProgress = Number(term.progress) || 0;
  578. if (progress >= termProgress && termProgress > matchedTermProgress) {
  579. matchedTermProgress = termProgress;
  580. }
  581. });
  582. // 如果物候期的 progress 小于所有节气,返回第一个节气的 progress
  583. if (matchedTermProgress === -1 && solarTerms.value.length > 0) {
  584. const firstTermProgress = Number(solarTerms.value[0].progress) || 0;
  585. return firstTermProgress;
  586. }
  587. return matchedTermProgress;
  588. };
  589. // 判断物候期是否应该显示蓝色(已过或当前节气的物候期)
  590. const shouldShowBlue = (phenology) => {
  591. // 获取下一个即将到来的节气(当前节气)的 progress
  592. const nextTermProgress = getNextTermProgress();
  593. // 如果所有节气都已过(nextTermProgress === Infinity),所有物候期都显示蓝色
  594. if (nextTermProgress === Infinity) {
  595. return true;
  596. }
  597. // 根据物候期的 progress 判断它属于哪个节气
  598. const phenologyProgress = Math.min(Number(phenology?.progress) || 0, Number(phenology?.progress2) || 0);
  599. const phenologyTermProgress = getPhenologyTermProgress(phenologyProgress);
  600. // 找到下一个节气的完整信息,用于判断物候期是否属于当前节气
  601. let nextTerm = null;
  602. solarTerms.value.forEach((term) => {
  603. const termProgress = Number(term.progress) || 0;
  604. if (termProgress === nextTermProgress) {
  605. nextTerm = term;
  606. }
  607. });
  608. // 如果物候期所属的节气的 progress < 下一个节气的 progress,显示蓝色
  609. // 如果物候期所属的节气的 progress === 下一个节气的 progress,也显示蓝色(当前节气)
  610. // 也就是说,只有属于当前节气或之前节气的物候期才显示蓝色
  611. if (phenologyTermProgress === -1) {
  612. return false;
  613. }
  614. // 如果物候期正好属于下一个节气,需要判断它的 progress 是否在下一个节气的范围内
  615. if (phenologyTermProgress === nextTermProgress && nextTerm) {
  616. // 如果物候期的 progress 小于等于下一个节气的 progress,说明它属于当前节气,显示蓝色
  617. return phenologyProgress <= nextTermProgress;
  618. }
  619. // 如果物候期所属的节气的 progress < 下一个节气的 progress,显示蓝色
  620. return phenologyTermProgress < nextTermProgress;
  621. };
  622. defineExpose({
  623. updateFarmWorkPlan,
  624. });
  625. // 使用 ResizeObserver 监听高度变化,确保在DOM完全渲染后获取准确高度
  626. const setupResizeObserver = () => {
  627. if (!timelineListRef.value || typeof ResizeObserver === "undefined") {
  628. return;
  629. }
  630. // 如果已经存在观察者,先断开
  631. if (resizeObserver) {
  632. resizeObserver.disconnect();
  633. }
  634. // 创建新的观察者
  635. resizeObserver = new ResizeObserver((entries) => {
  636. for (const entry of entries) {
  637. const height = entry.contentRect.height;
  638. if (height > 0 && height !== timelineListHeight.value) {
  639. timelineListHeight.value = height;
  640. }
  641. }
  642. });
  643. // 开始观察
  644. resizeObserver.observe(timelineListRef.value);
  645. };
  646. // 组件挂载后设置 ResizeObserver
  647. onMounted(() => {
  648. nextTick(() => {
  649. requestAnimationFrame(() => {
  650. setupResizeObserver();
  651. });
  652. });
  653. });
  654. // 组件卸载前清理 ResizeObserver
  655. onUnmounted(() => {
  656. if (resizeObserver) {
  657. resizeObserver.disconnect();
  658. resizeObserver = null;
  659. }
  660. });
  661. // 在数据更新后重新设置 ResizeObserver
  662. watch(
  663. () => phenologyList.value.length,
  664. () => {
  665. nextTick(() => {
  666. requestAnimationFrame(() => {
  667. setupResizeObserver();
  668. });
  669. });
  670. }
  671. );
  672. </script>
  673. <style scoped lang="scss">
  674. .timeline-container {
  675. height: 100%;
  676. overflow: auto;
  677. position: relative;
  678. box-sizing: border-box;
  679. .timeline-list {
  680. position: relative;
  681. }
  682. .timeline-middle-line {
  683. position: absolute;
  684. left: 15px; /* 位于节气文字列中间(列宽约30px) */
  685. top: 0;
  686. bottom: 0;
  687. width: 2px;
  688. background: #e8e8e8;
  689. z-index: 1;
  690. }
  691. .phenology-bar {
  692. align-items: stretch;
  693. justify-content: center;
  694. box-sizing: border-box;
  695. position: relative;
  696. .phenology-title{
  697. width: 18px;
  698. height: 98.5%;
  699. color: #fff;
  700. font-size: 12px;
  701. position: absolute;
  702. left: 32px;
  703. z-index: 10;
  704. text-align: center;
  705. display: flex;
  706. align-items: center;
  707. &.phenology-blue {
  708. background: #2199f8;
  709. }
  710. &.phenology-red {
  711. background: #F1F1F1;
  712. color: #808080;
  713. }
  714. }
  715. .reproductive-item {
  716. font-size: 12px;
  717. text-align: center;
  718. word-break: break-all;
  719. writing-mode: vertical-rl;
  720. text-orientation: upright;
  721. letter-spacing: 3px;
  722. width: 100%;
  723. line-height: 23px;
  724. color: inherit;
  725. position: relative;
  726. .phenology-name {
  727. width: 18px;
  728. line-height: 16px;
  729. height: 100%;
  730. color: #fff;
  731. padding: 4px 0;
  732. font-size: 12px;
  733. box-sizing: border-box;
  734. &.mr{
  735. margin-right: 3px;
  736. }
  737. &.single{
  738. width: 39px;
  739. line-height: 39px;
  740. }
  741. &.phenology-blue {
  742. background: #2199f8;
  743. }
  744. &.phenology-red {
  745. background: #F1F1F1;
  746. color: #808080;
  747. }
  748. &.text-blue{
  749. background: rgba(33, 153, 248, 0.15);
  750. color: #2199f8;
  751. border: 1px solid #2199f8;
  752. line-height: 16px;
  753. box-sizing: border-box;
  754. }
  755. &.text-red{
  756. background:rgba(128, 128, 128,.15);
  757. color: #808080;
  758. border: 1px solid rgba(128, 128, 128,.35);
  759. line-height: 16px;
  760. box-sizing: border-box;
  761. }
  762. }
  763. .arranges {
  764. display: flex;
  765. max-width: calc(100vw - 111px);
  766. min-width: calc(100vw - 111px);
  767. gap: 5px;
  768. letter-spacing: 0px;
  769. .arrange-card {
  770. width: 95%;
  771. border: 0.5px solid #2199f8;
  772. border-radius: 8px;
  773. background: #fff;
  774. box-sizing: border-box;
  775. position: relative;
  776. padding: 8px 15px 8px 10px;
  777. writing-mode: horizontal-tb;
  778. &.first-card{
  779. margin-top: 10px;
  780. }
  781. &.last-card{
  782. margin-bottom: 10px;
  783. }
  784. .card-content {
  785. color: #242424;
  786. display: flex;
  787. justify-content: space-between;
  788. align-items: center;
  789. font-size: 14px;
  790. .card-left {
  791. display: flex;
  792. align-items: center;
  793. gap: 8px;
  794. .left-date{
  795. color: #fff;
  796. background: #2199f8;
  797. padding: 1px 5px;
  798. border-radius: 2px;
  799. font-size: 12px;
  800. }
  801. }
  802. .card-right {
  803. display: flex;
  804. align-items: center;
  805. position: relative;
  806. img {
  807. width: 45px;
  808. height: 45px;
  809. }
  810. .num {
  811. position: absolute;
  812. width: 18px;
  813. height: 18px;
  814. box-sizing: border-box;
  815. top: -4px;
  816. right: -6px;
  817. background: #2199f8;
  818. color: #fff;
  819. font-size: 12px;
  820. border-radius: 50%;
  821. display: flex;
  822. align-items: center;
  823. justify-content: center;
  824. }
  825. }
  826. }
  827. &::before {
  828. content: "";
  829. position: absolute;
  830. left: -5px;
  831. top: 50%;
  832. transform: translateY(-50%);
  833. width: 0;
  834. height: 0;
  835. border-top: 5px solid transparent;
  836. border-bottom: 5px solid transparent;
  837. border-right: 5px solid #2199f8;
  838. }
  839. }
  840. .arrange-card.normal-style {
  841. opacity: 0.3;
  842. }
  843. .arrange-card.future-card .card-content {
  844. color: #808080;
  845. }
  846. .arrange-card.status-warning {
  847. border-color: #ff953d;
  848. &::before {
  849. border-right-color: #ff953d;
  850. }
  851. }
  852. .arrange-card.status-complete {
  853. border-color: #1ca900;
  854. &::before {
  855. border-right-color: #1ca900;
  856. }
  857. }
  858. .arrange-card.status-normal {
  859. border-color: #2199f8;
  860. &::before {
  861. border-right-color: #2199f8;
  862. }
  863. }
  864. // 未来节气对应的农事卡片:跟随左侧物候期的“未开始”灰色样式
  865. .arrange-card.future-card {
  866. border-color: #E4E4E4;
  867. .card-left{
  868. .left-date{
  869. background: #E4E4E4;
  870. color: #fff;
  871. }
  872. }
  873. &::before {
  874. border-right-color: #E4E4E4;
  875. }
  876. }
  877. }
  878. }
  879. }
  880. .reproductive-item + .reproductive-item {
  881. padding-top: 3px;
  882. }
  883. .phenology-bar + .phenology-bar {
  884. padding-top: 3px;
  885. }
  886. .timeline-term {
  887. position: absolute;
  888. width: 32px;
  889. display: flex;
  890. align-items: flex-start;
  891. flex-direction: column;
  892. z-index: 2; /* 置于中线之上 */
  893. color: rgba(174, 174, 174, 0.6);
  894. .term-name {
  895. display: inline-block;
  896. width: 100%;
  897. min-height: 35px;
  898. line-height: 30px;
  899. background: #fff;
  900. font-size: 12px;
  901. word-break: break-all;
  902. writing-mode: vertical-rl;
  903. text-orientation: upright;
  904. text-align: center;
  905. }
  906. .term-date {
  907. // position: absolute;
  908. bottom: 0;
  909. left: 0;
  910. font-size: 12px;
  911. color: #242424;
  912. }
  913. }
  914. }
  915. </style>