plan copy.vue 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. <template>
  2. <div class="plan-page">
  3. <custom-header name="农事规划"></custom-header>
  4. <div class="plan-content">
  5. <div class="filter-wrap">
  6. <div class="season-tabs">
  7. <div
  8. v-for="s in seasons"
  9. :key="s.value"
  10. class="season-tab"
  11. :class="{ active: s.value === activeSeason }"
  12. @click="activeSeason = s.value"
  13. >
  14. {{ s.label }}
  15. </div>
  16. </div>
  17. <div class="status-filter">
  18. <div v-for="status in statusList" :key="status.value" class="status-item" :class="status.color">
  19. <div class="status-dot"></div>
  20. <span class="status-text">{{ status.label }}</span>
  21. </div>
  22. </div>
  23. </div>
  24. <!-- 三行循环时间线 -->
  25. <div class="cycle-timeline-container">
  26. <div class="cycle-timeline">
  27. <div
  28. v-for="(row, rowIndex) in timelineRows"
  29. :key="rowIndex"
  30. class="cycle-row"
  31. :class="{ 'odd-index': rowIndex % 2 === 1 }"
  32. >
  33. <div
  34. v-for="(item, itemIndex) in row.items"
  35. :key="itemIndex"
  36. class="cycle-item"
  37. @click="handleRowClick(item)"
  38. :class="[item.type + '-item']"
  39. >
  40. <!-- 节气节点 -->
  41. <template v-if="item.type === 'term'">
  42. <!-- <div class="cycle-term-dot"></div> -->
  43. <div class="cycle-term-label">{{ item.name || item.id }}</div>
  44. </template>
  45. </div>
  46. <!-- 生育期名称(根据时间范围显示在对应行) -->
  47. <div class="cycle-phenology-wrap" v-if="getPhenologyBarsForRow(rowIndex).length > 0">
  48. <div
  49. v-for="p in getPhenologyBarsForRow(rowIndex)"
  50. :key="p.id"
  51. class="cycle-label"
  52. :class="p.color"
  53. :style="
  54. isOddVisualRow(rowIndex)
  55. ? { right: p.left, width: p.width }
  56. : { left: p.left, width: p.width }
  57. "
  58. >
  59. {{ p.name }}
  60. <div v-if="p.arranges && p.arranges.length" class="arranges">
  61. <div v-for="a in p.arranges" :key="a.id" :class="['cycle-task-box', a.status]">
  62. <div class="cycle-task-text">{{ a.farmWorkName || a.name }}</div>
  63. <!-- 任务连接器 -->
  64. <div class="cycle-task-connector"></div>
  65. <div
  66. v-if="a.status === 'complete' || a.status === 'warning'"
  67. class="status-icon"
  68. :class="a.status"
  69. >
  70. <el-icon v-if="a.status === 'complete'" size="16" color="#1CA900"
  71. ><SuccessFilled
  72. /></el-icon>
  73. <el-icon v-else size="18" color="#FF953D"><WarnTriangleFilled /></el-icon>
  74. </div>
  75. </div>
  76. </div>
  77. </div>
  78. </div>
  79. <!-- 行连接器 -->
  80. <div
  81. v-if="rowIndex < timelineRows.length - 1"
  82. class="cycle-connector"
  83. :class="[
  84. rowIndex % 2 === 1 ? 'middle-connector' : 'top-connector',
  85. getConnectorColorClass(rowIndex),
  86. ]"
  87. >
  88. <img v-if="isConnectorGray(rowIndex)" src="@/assets/img/monitor/defalut-arrow.png" alt="" />
  89. <img v-else src="@/assets/img/monitor/arrow.png" alt="" />
  90. </div>
  91. </div>
  92. </div>
  93. </div>
  94. <div class="control-section">
  95. <div class="toggle-group">
  96. <el-switch v-model="isDefaultEnabled" />
  97. <span class="toggle-label">{{ isDefaultEnabled ? "默认" : "" }}发起农情需求</span>
  98. </div>
  99. <div class="add-button-group">
  100. <div class="add-button button" @click="addNewTask">新增农事</div>
  101. <div class="button" @click="manageTask">农事管理</div>
  102. </div>
  103. </div>
  104. </div>
  105. </div>
  106. <!-- 农事信息弹窗 -->
  107. <detail-dialog ref="detailDialogRef"></detail-dialog>
  108. <!-- 新增:激活上传弹窗 -->
  109. <active-upload-popup></active-upload-popup>
  110. </template>
  111. <script setup>
  112. import { reactive, ref, onMounted, nextTick, onBeforeUnmount } from "vue";
  113. import customHeader from "@/components/customHeader.vue";
  114. import { useRouter, useRoute } from "vue-router";
  115. import detailDialog from "@/components/detailDialog.vue";
  116. import activeUploadPopup from "@/components/popup/activeUploadPopup.vue";
  117. const router = useRouter();
  118. const route = useRoute();
  119. // 状态列表数据
  120. const seasons = reactive([
  121. { value: "spring", label: "春季" },
  122. { value: "summer", label: "夏季" },
  123. { value: "autumn", label: "秋季" },
  124. { value: "winter", label: "冬季" },
  125. ]);
  126. const activeSeason = ref("spring");
  127. const statusList = reactive([
  128. { value: "pending", label: "待触发", color: "gray" },
  129. { value: "executing", label: "待完成", color: "blue" },
  130. { value: "completed", label: "已完成", color: "green" },
  131. { value: "expired", label: "已过期", color: "orange" },
  132. ]);
  133. // 切换开关状态
  134. const isDefaultEnabled = ref(true);
  135. // 时间线行数据(由接口节气生成)
  136. const timelineRows = reactive([]);
  137. // 目标定位日期(当前生育期参考点)
  138. const targetDate = new Date("2025-04-04T00:00:00");
  139. // 每行“当前生育期”标记的位置样式(按行索引)
  140. const phenologyPositions = ref({});
  141. // 生育期条(按行分组)
  142. const phenologyBarsByRow = ref([]);
  143. // 每一行可视区域的实际像素宽度(用于将最小像素宽度换算为百分比)
  144. const rowWidths = ref([]);
  145. // 节气 id 到对象的索引,便于通过 id 查找节气日期
  146. let solarTermIdToTerm = {};
  147. // 接口返回的生育期数据
  148. const phenologyList = ref([]);
  149. // 安全日期解析(兼容 'YYYY-MM-DD HH:mm:ss' / 'YYYY/MM/DD HH:mm:ss')
  150. const parseDate = (val) => {
  151. if (!val) return null;
  152. if (val instanceof Date) return isNaN(val.getTime()) ? null : val;
  153. if (typeof val === "number") return new Date(val);
  154. if (typeof val === "string") {
  155. // 统一到可被 Safari 解析的格式
  156. const s = val.replace(/-/g, "/").replace("T", " ");
  157. const d = new Date(s);
  158. return isNaN(d.getTime()) ? null : d;
  159. }
  160. return null;
  161. };
  162. onMounted(() => {
  163. getFarmWorkPlan();
  164. window.addEventListener("resize", handleResize, { passive: true });
  165. });
  166. onBeforeUnmount(() => {
  167. window.removeEventListener("resize", handleResize);
  168. });
  169. const handleResize = () => {
  170. // 重新测量并基于最新宽度重算条目
  171. nextTick(() => {
  172. measureRowWidths();
  173. // 需要基于最新数据重算
  174. if (phenologyList.value && phenologyList.value.length && cachedValidSolarTerms.value) {
  175. groupPhenologyBarsByRow(phenologyList.value, cachedValidSolarTerms.value);
  176. }
  177. });
  178. };
  179. // 缓存已过滤/排序后的节气用于重复计算
  180. const cachedValidSolarTerms = ref(null);
  181. const getFarmWorkPlan = () => {
  182. const paramFarmId = Number(route.query.farmId) || undefined;
  183. VE_API.monitor
  184. .farmWorkPlan({ farmId: paramFarmId ?? 92844 }) // 优先使用路由传入的 farmId
  185. .then(({ data, code }) => {
  186. if (code === 0) {
  187. const solarTermsList = data.solarTermsList;
  188. // 仅保留 type === 1 的节气,按需要的顺序(示例:反转)
  189. // 取 type===1 的节气,并按日期降序排序(晚到早)
  190. const validSolarTerms = Array.isArray(solarTermsList)
  191. ? solarTermsList
  192. .filter((t) => t && t.type === 1 && t.createDate)
  193. .sort((a, b) => {
  194. const da = parseDate(a.createDate)?.getTime() ?? 0;
  195. const db = parseDate(b.createDate)?.getTime() ?? 0;
  196. return db - da;
  197. })
  198. : [];
  199. cachedValidSolarTerms.value = validSolarTerms;
  200. generateTimelineData(validSolarTerms);
  201. computeCurrentPhenologyPositions(validSolarTerms, targetDate);
  202. // 保存生育期数据并生成各行生育期条
  203. phenologyList.value = Array.isArray(data.phenologyList) ? data.phenologyList : [];
  204. // 生成 id->term 的索引
  205. solarTermIdToTerm = {};
  206. validSolarTerms.forEach((t) => {
  207. if (t && (t.id || t.solarTermsId)) solarTermIdToTerm[t.id ?? t.solarTermsId] = t;
  208. });
  209. // 先等待 DOM 渲染完成后测量每行宽度,再据此计算最小可显示宽度
  210. nextTick(() => {
  211. measureRowWidths();
  212. groupPhenologyBarsByRow(phenologyList.value, validSolarTerms);
  213. });
  214. }
  215. })
  216. .catch((error) => {
  217. console.error("获取农事规划数据失败:", error);
  218. });
  219. };
  220. // 测量每一行生育期容器的实际宽度
  221. const measureRowWidths = () => {
  222. const rows = document.querySelectorAll(".cycle-timeline .cycle-row");
  223. const widths = [];
  224. rows.forEach((rowEl, idx) => {
  225. const wrap = rowEl.querySelector(".cycle-phenology-wrap");
  226. widths[idx] = wrap ? wrap.offsetWidth : 0;
  227. });
  228. rowWidths.value = widths;
  229. };
  230. // 生成时间轴数据
  231. const generateTimelineData = (solarTerms) => {
  232. // 清空
  233. timelineRows.splice(0, timelineRows.length);
  234. // 无数据则给一行示例
  235. if (!solarTerms || solarTerms.length === 0) {
  236. timelineRows.push({
  237. items: [
  238. { type: "task", status: "default", taskName: "梢期", taskDesc: "杀虫" },
  239. { type: "term", name: "节气" },
  240. { type: "task", status: "default", taskName: "梢期", taskDesc: "杀虫" },
  241. { type: "term", name: "节气" },
  242. { type: "task", status: "default", taskName: "梢期", taskDesc: "杀虫" },
  243. { type: "term", name: "节气" },
  244. ],
  245. });
  246. return;
  247. }
  248. const itemsPerRow = 6; // 任务/节气交替
  249. const termsPerRow = 3; // 每行3个节气
  250. const totalRows = Math.ceil(solarTerms.length / termsPerRow);
  251. for (let rowIndex = 0; rowIndex < totalRows; rowIndex++) {
  252. const rowItems = [];
  253. const startTermIndex = rowIndex * termsPerRow;
  254. for (let i = 0; i < itemsPerRow; i++) {
  255. if (i % 2 === 0) {
  256. // 任务位
  257. const taskData = getTaskDataForIndex(Math.floor(i / 2));
  258. rowItems.push({
  259. type: "task",
  260. status: taskData.status,
  261. taskName: taskData.taskName,
  262. taskDesc: taskData.taskDesc,
  263. icon: taskData.icon,
  264. });
  265. } else {
  266. // 节气位
  267. const termIndex = startTermIndex + Math.floor(i / 2);
  268. if (termIndex < solarTerms.length) {
  269. const term = solarTerms[termIndex] || {};
  270. rowItems.push({
  271. type: "term",
  272. status: "default",
  273. name: term.name || term.solarTermsName || term.termName || "节气",
  274. id: term.id,
  275. createDate: term.createDate,
  276. });
  277. } else {
  278. // 不足时补任务
  279. rowItems.push({
  280. type: "task",
  281. status: "default",
  282. taskName: "梢期",
  283. taskDesc: "杀虫",
  284. });
  285. }
  286. }
  287. }
  288. timelineRows.push({ items: rowItems });
  289. }
  290. };
  291. // 任务占位数据(可按需接后端)
  292. const getTaskDataForIndex = (index) => {
  293. const defaultTasks = [
  294. { status: "default", taskName: "梢期", taskDesc: "杀虫" },
  295. { status: "active", taskName: "梢期", taskDesc: "营养" },
  296. { status: "complete", taskName: "梢期", taskDesc: "修剪", icon: { type: "complete" } },
  297. { status: "warning", taskName: "梢期", taskDesc: "施肥", icon: { type: "warning" } },
  298. { status: "normal", taskName: "梢期", taskDesc: "灌溉", icon: { type: "normal" } },
  299. ];
  300. return defaultTasks[index % defaultTasks.length];
  301. };
  302. // 计算“当前生育期”在各行的定位(只在包含目标日期的那一行显示)
  303. const computeCurrentPhenologyPositions = (solarTerms, date) => {
  304. phenologyPositions.value = {};
  305. if (!Array.isArray(solarTerms) || solarTerms.length === 0 || !(date instanceof Date)) return;
  306. const termsPerRow = 3;
  307. const totalRows = Math.ceil(solarTerms.length / termsPerRow);
  308. // 1) 找到最接近目标日期的节气(按时间升序)
  309. const termsAsc = solarTerms
  310. .filter((t) => t && t.createDate)
  311. .slice()
  312. .sort((a, b) => (parseDate(a.createDate)?.getTime() ?? 0) - (parseDate(b.createDate)?.getTime() ?? 0));
  313. if (termsAsc.length === 0) return;
  314. const targetMs = date.getTime();
  315. let nearest = termsAsc[0];
  316. let bestDiff = Math.abs((parseDate(nearest.createDate)?.getTime() ?? 0) - targetMs);
  317. for (let i = 1; i < termsAsc.length; i++) {
  318. const ms = parseDate(termsAsc[i].createDate)?.getTime() ?? 0;
  319. const diff = Math.abs(ms - targetMs);
  320. if (diff < bestDiff) {
  321. bestDiff = diff;
  322. nearest = termsAsc[i];
  323. }
  324. }
  325. // 2) 将该节气映射回当前(降序)数组中的索引与行
  326. const nearestIdxDesc = solarTerms.findIndex((t) => t && nearest && t.id === nearest.id);
  327. const rowIndex = Math.max(0, Math.floor(nearestIdxDesc / termsPerRow));
  328. const startIdx = rowIndex * termsPerRow;
  329. const endIdx = Math.min(startIdx + termsPerRow - 1, solarTerms.length - 1);
  330. if (startIdx > endIdx) return;
  331. const rowTerms = solarTerms.slice(startIdx, endIdx + 1);
  332. // 视觉顺序用于方向(偶数行正向,奇数行反向),但时间范围应取该行真实最早/最晚
  333. const rowDates = rowTerms
  334. .map((t) => parseDate(t?.createDate))
  335. .filter((d) => d && !isNaN(d.getTime()))
  336. .map((d) => d.getTime());
  337. if (rowDates.length === 0) return;
  338. const minMs = Math.min(...rowDates);
  339. const maxMs = Math.max(...rowDates);
  340. const rowStart = new Date(minMs);
  341. const rowEnd = new Date(maxMs);
  342. // 3) 若目标日期不在该行范围内,则就近夹到边界(避免跨行导致丢失)
  343. let anchorMs = targetMs;
  344. if (anchorMs < minMs) anchorMs = minMs;
  345. if (anchorMs > maxMs) anchorMs = maxMs;
  346. // 4) 计算在该行范围内的比例
  347. const total = Math.max(1, maxMs - minMs);
  348. const ratio = Math.max(0, Math.min(1, (anchorMs - minMs) / total));
  349. const percent = `${(ratio * 100).toFixed(2)}%`;
  350. // 5) 偶数行用 left,奇数行用 right,与 Z 字方向一致
  351. if (rowIndex % 2 === 1) {
  352. phenologyPositions.value[rowIndex] = { right: percent };
  353. } else {
  354. phenologyPositions.value[rowIndex] = { left: percent };
  355. }
  356. };
  357. // moved above with other refs
  358. // 将生育期条按行计算定位与宽度
  359. const groupPhenologyBarsByRow = (phenologyList, solarTerms) => {
  360. phenologyBarsByRow.value = [];
  361. if (
  362. !Array.isArray(phenologyList) ||
  363. phenologyList.length === 0 ||
  364. !Array.isArray(solarTerms) ||
  365. solarTerms.length === 0
  366. ) {
  367. return;
  368. }
  369. const termsPerRow = 3;
  370. const totalRows = Math.ceil(solarTerms.length / termsPerRow);
  371. // 行范围:使用该行包含的节气最早/最晚时间(按真实时间线性映射)
  372. const rowRanges = [];
  373. for (let rowIndex = 0; rowIndex < totalRows; rowIndex++) {
  374. const startIdx = rowIndex * termsPerRow;
  375. const endIdx = Math.min(startIdx + termsPerRow - 1, solarTerms.length - 1);
  376. const rowTerms = solarTerms.slice(startIdx, endIdx + 1);
  377. const rowDates = rowTerms
  378. .map((t) => parseDate(t?.createDate))
  379. .filter((d) => d && !isNaN(d.getTime()))
  380. .map((d) => d.getTime());
  381. if (rowDates.length === 0) continue;
  382. const minMs = Math.min(...rowDates);
  383. const maxMs = Math.max(...rowDates);
  384. const rowStart = new Date(minMs);
  385. const rowEnd = new Date(maxMs);
  386. const totalMs = Math.max(1, rowEnd.getTime() - rowStart.getTime());
  387. rowRanges.push({ rowIndex, rowStart, rowEnd, totalMs });
  388. phenologyBarsByRow.value[rowIndex] = [];
  389. }
  390. // 中点归属法:每条生育期归属到中点所在的行;在行内按 Z 字方向计算 left/right 与 width
  391. phenologyList.forEach((p, pIndex) => {
  392. const list = Array.isArray(p?.reproductiveList) ? p.reproductiveList : [];
  393. const baseColorClass = pIndex % 2 === 0 ? "blue" : "orange";
  394. list.forEach((r) => {
  395. // 优先使用节气 id 对应的节气日期
  396. let sTermDate = null;
  397. let eTermDate = null;
  398. if (r?.startSolarTermId && solarTermIdToTerm[r.startSolarTermId]?.createDate) {
  399. sTermDate = parseDate(solarTermIdToTerm[r.startSolarTermId].createDate);
  400. }
  401. if (r?.endSolarTermId && solarTermIdToTerm[r.endSolarTermId]?.createDate) {
  402. eTermDate = parseDate(solarTermIdToTerm[r.endSolarTermId].createDate);
  403. }
  404. const s = sTermDate || parseDate(r?.startDate);
  405. const e = eTermDate || parseDate(r?.endDate);
  406. if (!s || !e) return;
  407. const start = new Date(Math.min(s.getTime(), e.getTime()));
  408. const end = new Date(Math.max(s.getTime(), e.getTime()));
  409. if (end < start) return;
  410. const mid = new Date(start.getTime() + (end.getTime() - start.getTime()) / 2);
  411. // 找到中点所在行;若不在任何行,则归最近行
  412. let target = rowRanges.find(({ rowStart, rowEnd }) => mid >= rowStart && mid <= rowEnd);
  413. if (!target) {
  414. target = rowRanges.reduce((best, curr) => {
  415. const dist =
  416. mid < curr.rowStart
  417. ? curr.rowStart.getTime() - mid.getTime()
  418. : mid.getTime() - curr.rowEnd.getTime();
  419. if (!best || dist < best.dist) return { dist, curr };
  420. return best;
  421. }, null)?.curr;
  422. }
  423. if (!target) return;
  424. // 位置:基于真实的 startDate(不截断),确保相邻条的间距 = (后一个startDate - 前一个endDate) 的时间差映射
  425. const startRatio = (start.getTime() - target.rowStart.getTime()) / target.totalMs;
  426. // 宽度:基于真实的 endDate - startDate 的时间差
  427. const actualDuration = end.getTime() - start.getTime();
  428. const widthRatio = actualDuration / target.totalMs;
  429. // 限制到行范围内
  430. const leftRatio = Math.max(0, Math.min(1, startRatio));
  431. const rightRatio = Math.max(0, Math.min(1, (end.getTime() - target.rowStart.getTime()) / target.totalMs));
  432. let clampedWidthRatio = Math.max(0.001, Math.min(widthRatio, rightRatio - leftRatio));
  433. // 强制最小显示宽度:若换算到像素后小于 CSS 中的 min-width:22px,则使用最小可见宽度
  434. const MIN_LABEL_PX = 22; // 与样式 .cycle-label 的最小宽度保持一致
  435. const rowWidthPx = rowWidths.value?.[target.rowIndex] || 0;
  436. let leftPercent = leftRatio * 100;
  437. let widthPercent = clampedWidthRatio * 100;
  438. if (rowWidthPx > 0) {
  439. const minPercent = (MIN_LABEL_PX / rowWidthPx) * 100;
  440. if (widthPercent < minPercent) {
  441. widthPercent = minPercent;
  442. }
  443. // 若越界则左移以保证完全可见
  444. if (leftPercent + widthPercent > 100) {
  445. leftPercent = Math.max(0, 100 - widthPercent);
  446. }
  447. // 回填为比例供后续使用
  448. clampedWidthRatio = widthPercent / 100;
  449. } else {
  450. // 无法测量时,保底给一个不至于 0 的最小显示比例(以 360px 近似,22px/360≈6.1%)
  451. if (widthPercent < 6.2) {
  452. widthPercent = 6.2;
  453. if (leftPercent + widthPercent > 100) leftPercent = Math.max(0, 100 - widthPercent);
  454. clampedWidthRatio = widthPercent / 100;
  455. }
  456. }
  457. const isFuture = start.getTime() > Date.now();
  458. const colorToUse = isFuture ? "" : baseColorClass;
  459. // 组装农事安排:按 reproductiveId 归属到当前生育期
  460. const arrangeList = Array.isArray(r.farmWorkArrangeList)
  461. ? r.farmWorkArrangeList.filter((fw) => !fw.reproductiveId || fw.reproductiveId === r.id)
  462. : [];
  463. const arrangeItems = arrangeList.map((fw) => {
  464. let status = "default";
  465. const t = fw.farmWorkType;
  466. if (t == null || t === 0) {
  467. status = "default";
  468. } else if (t >= 1 && t <= 4) {
  469. status = "normal";
  470. } else if (t === 5) {
  471. status = "complete";
  472. } else if (t === 6) {
  473. status = "warning";
  474. }
  475. return {
  476. id: fw.id,
  477. name: fw.farmWorkName,
  478. status,
  479. };
  480. });
  481. // // 附加两条测试数据:已完成、已过期
  482. // arrangeItems.push(
  483. // { id: `${r.id}-test-complete`, name: "测试完成", status: "complete" },
  484. // { id: `${r.id}-test-warning`, name: "测试过期", status: "warning" }
  485. // );
  486. phenologyBarsByRow.value[target.rowIndex].push({
  487. id: r.id || `${p.id || "p"}-${start.getTime()}-${end.getTime()}`,
  488. name: r.name && r.name.trim() ? r.name.trim() : r.phenologyName || "生育期",
  489. left: `${leftPercent.toFixed(4)}%`,
  490. width: `${(clampedWidthRatio * 100).toFixed(4)}%`,
  491. startTime: start.getTime(), // 用于排序,确保相邻条的顺序正确
  492. color: colorToUse,
  493. arranges: arrangeItems,
  494. });
  495. });
  496. });
  497. // 每行内部按 startTime 排序,确保相邻条的间距正确反映时间差
  498. phenologyBarsByRow.value.forEach((rowBars) => {
  499. rowBars.sort((a, b) => (a.startTime || 0) - (b.startTime || 0));
  500. });
  501. };
  502. // 获取指定行的生育期条
  503. const getPhenologyBarsForRow = (rowIndex) => {
  504. return phenologyBarsByRow.value[rowIndex] || [];
  505. };
  506. // 视觉奇偶:自下而上计算奇偶(与 UI Z 字一致)
  507. const isOddVisualRow = (rowIndex) => {
  508. const total = timelineRows.length;
  509. if (total <= 0) return rowIndex % 2 === 1;
  510. const visualIndex = total - 1 - rowIndex;
  511. return visualIndex % 2 === 1;
  512. };
  513. // 新增农事
  514. const addNewTask = () => {
  515. router.push({
  516. path: "/modify_work",
  517. query: { data: JSON.stringify(["生长异常"]), gardenId: 766, isAdd: true },
  518. });
  519. };
  520. const manageTask = () => {
  521. router.push({
  522. path: "/agri_services_manage",
  523. query: {
  524. type: "manage",
  525. },
  526. });
  527. };
  528. const detailDialogRef = ref(null);
  529. const handleRowClick = (item) => {
  530. if (item.status === "complete") {
  531. router.push({
  532. path: "/review_work",
  533. query: {
  534. id: item.id,
  535. },
  536. });
  537. } else if (item.type !== "term" && item.status === "default") {
  538. detailDialogRef.value.showDialog();
  539. } else if (item.status === "warning" || item.status === "normal") {
  540. router.push({
  541. path: "/completed_work",
  542. query: {
  543. id: item.id,
  544. status: item.status,
  545. },
  546. });
  547. // router.push({
  548. // path: "/services_agri",
  549. // query: {
  550. // id: item.id,
  551. // status: item.status,
  552. // },
  553. // });
  554. }
  555. };
  556. // 行连接器颜色:若后续生育期未开始则灰色,否则保持其颜色(蓝/橙)
  557. const getConnectorColorClass = (rowIndex) => {
  558. const nextIndex = rowIndex + 1;
  559. const bars = getPhenologyBarsForRow(nextIndex);
  560. if (!bars || bars.length === 0) return "";
  561. const nextIsOddIndex = nextIndex % 2 === 1; // 奇数行为左侧连接器
  562. const parsePercent = (val) => {
  563. if (typeof val !== "string") return 0;
  564. const n = parseFloat(val.replace("%", ""));
  565. return isNaN(n) ? 0 : n;
  566. };
  567. let target = bars[0];
  568. if (nextIsOddIndex) {
  569. // 左侧:选最靠左的条
  570. target = bars.reduce((best, cur) => (parsePercent(cur.left) < parsePercent(best.left) ? cur : best), bars[0]);
  571. } else {
  572. // 右侧:选最靠右的条(left + width 最大)
  573. const score = (b) => parsePercent(b.left) + parsePercent(b.width);
  574. target = bars.reduce((best, cur) => (score(cur) > score(best) ? cur : best), bars[0]);
  575. }
  576. // 未来(未开始)时,color 为空串;过去/当前一律显示蓝色
  577. const hasStarted = !!target?.color;
  578. return hasStarted ? "" : "connector-gray";
  579. };
  580. // 行连接器是否为灰色(用于切换箭头图片)
  581. const isConnectorGray = (rowIndex) => getConnectorColorClass(rowIndex) === "connector-gray";
  582. </script>
  583. <style scoped lang="scss">
  584. .plan-page {
  585. width: 100%;
  586. height: 100vh;
  587. background: #fff;
  588. .plan-content {
  589. .filter-wrap {
  590. background: #fff;
  591. padding: 13px 12px;
  592. box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  593. border-radius: 0 0 20px 20px;
  594. .status-filter {
  595. background: #fff;
  596. padding: 3px 17px;
  597. display: flex;
  598. align-items: center;
  599. gap: 16px;
  600. font-size: 12px;
  601. .status-item {
  602. display: flex;
  603. align-items: center;
  604. justify-content: center;
  605. gap: 6px;
  606. flex: 1;
  607. &.gray {
  608. color: #c4c6c9;
  609. .status-dot {
  610. background-color: #c4c6c9;
  611. }
  612. }
  613. &.blue {
  614. color: #2199f8;
  615. .status-dot {
  616. background-color: #2199f8;
  617. }
  618. }
  619. &.green {
  620. color: #1ca900;
  621. .status-dot {
  622. background-color: #1ca900;
  623. }
  624. }
  625. &.orange {
  626. color: #ff953d;
  627. .status-dot {
  628. background-color: #ff953d;
  629. }
  630. }
  631. .status-dot {
  632. width: 6px;
  633. height: 6px;
  634. border-radius: 50%;
  635. }
  636. }
  637. }
  638. .season-tabs {
  639. display: flex;
  640. gap: 8px;
  641. margin-bottom: 12px;
  642. .season-tab {
  643. flex: 1;
  644. padding: 7px;
  645. text-align: center;
  646. background: #f3f3f3;
  647. color: #898a8a;
  648. border-radius: 3px;
  649. border: 1px solid transparent;
  650. font-size: 12px;
  651. }
  652. .season-tab.active {
  653. background: #ffffff;
  654. color: #2199f8;
  655. border-color: #2199f8;
  656. }
  657. }
  658. }
  659. // 循环时间线样式
  660. .cycle-timeline-container {
  661. padding: 35px 15px 25px;
  662. height: calc(100vh - 135px - 69px - 60px);
  663. overflow-y: auto;
  664. overflow-x: hidden;
  665. .cycle-timeline {
  666. position: relative;
  667. .cycle-row {
  668. position: relative;
  669. display: flex;
  670. justify-content: space-between;
  671. align-items: center;
  672. margin-bottom: 60px;
  673. padding-right: 30px;
  674. &.odd-index {
  675. padding: 0;
  676. padding-left: 30px;
  677. flex-direction: row-reverse;
  678. .cycle-phenology-wrap {
  679. left: 6px;
  680. width: calc(100% - 13px);
  681. }
  682. }
  683. &:last-child {
  684. margin-bottom: 0;
  685. .cycle-phenology-wrap {
  686. left: 20px;
  687. width: calc(100% - 10px);
  688. }
  689. }
  690. // 水平时间线
  691. &::before {
  692. content: "";
  693. position: absolute;
  694. top: 0;
  695. left: 6px;
  696. right: 6px;
  697. height: 5px;
  698. border-left: 2px solid #fff;
  699. border-right: 2px solid #fff;
  700. background: #e8e8e8;
  701. transform: translateY(-50%);
  702. z-index: 1;
  703. }
  704. .cycle-item {
  705. position: relative;
  706. z-index: 2;
  707. top: 12px;
  708. &.term-item {
  709. display: flex;
  710. flex-direction: column;
  711. align-items: center;
  712. top: -11px;
  713. .cycle-term-dot {
  714. width: 6px;
  715. height: 6px;
  716. background: #c7c7c7;
  717. border-radius: 50%;
  718. margin-bottom: 4px;
  719. }
  720. .cycle-term-label {
  721. font-size: 11px;
  722. color: #c7c7c7;
  723. margin-top: 16px;
  724. }
  725. &.active {
  726. .cycle-term-dot {
  727. background: #858383;
  728. }
  729. .cycle-term-label {
  730. color: #858383;
  731. }
  732. }
  733. }
  734. }
  735. .cycle-phenology-wrap {
  736. position: absolute;
  737. top: -23px;
  738. left: 6px;
  739. width: calc(100% - 10px);
  740. z-index: 3;
  741. height: 100px;
  742. overflow: hidden;
  743. .cycle-label {
  744. position: absolute;
  745. color: #4e4e4e;
  746. font-size: 12px;
  747. min-width: 24px;
  748. height: 20px;
  749. line-height: 20px;
  750. text-align: center;
  751. background: rgba(180, 182, 183, 0.1);
  752. border-bottom: 6px solid #e8e8e8;
  753. }
  754. .cycle-label + .cycle-label {
  755. border-right: 1px solid #fff;
  756. }
  757. .cycle-label.blue {
  758. color: #2199f8;
  759. background: rgba(33, 153, 248, 0.1);
  760. border-bottom-color: #2199f8;
  761. }
  762. .cycle-label.orange {
  763. color: #ff953d;
  764. background: #fff2e7;
  765. border-bottom-color: #ff953d;
  766. }
  767. .arranges {
  768. display: flex;
  769. gap: 8px;
  770. padding-top: 26px;
  771. justify-content: center;
  772. flex-wrap: nowrap;
  773. // 使用与任务框一致的视觉风格
  774. .cycle-task-box {
  775. border: 1px solid rgba(199, 199, 199, 0.5);
  776. border-radius: 2px;
  777. width: 36px;
  778. height: 36px;
  779. min-width: 36px;
  780. line-height: 15px;
  781. font-size: 12px;
  782. box-sizing: border-box;
  783. padding: 2px 0;
  784. text-align: center;
  785. position: relative;
  786. color: #c7c7c7;
  787. .status-icon {
  788. position: absolute;
  789. bottom: -10px;
  790. right: -10px;
  791. }
  792. }
  793. .cycle-task-connector {
  794. position: absolute;
  795. top: -4px;
  796. left: 50%;
  797. transform: translateX(-50%);
  798. width: 0;
  799. height: 0;
  800. border-left: 4px solid transparent;
  801. border-right: 4px solid transparent;
  802. border-bottom: 4px solid #dde1e7;
  803. }
  804. .cycle-task-box.warning {
  805. border-color: #ff953d;
  806. }
  807. .cycle-task-box.warning .cycle-task-text {
  808. color: #ff953d;
  809. }
  810. .cycle-task-box.warning + .cycle-task-connector,
  811. .cycle-task-box.warning .cycle-task-connector {
  812. border-bottom-color: #ff953d;
  813. }
  814. .cycle-task-box.complete {
  815. border-color: #1ca900;
  816. }
  817. .cycle-task-box.complete .cycle-task-text {
  818. color: #1ca900;
  819. }
  820. .cycle-task-box.complete + .cycle-task-connector,
  821. .cycle-task-box.complete .cycle-task-connector {
  822. border-bottom-color: #1ca900;
  823. }
  824. .cycle-task-box.normal {
  825. border-color: #2199f8;
  826. }
  827. .cycle-task-box.normal .cycle-task-text {
  828. color: #2199f8;
  829. }
  830. .cycle-task-box.normal + .cycle-task-connector,
  831. .cycle-task-box.normal .cycle-task-connector {
  832. border-bottom-color: #2199f8;
  833. }
  834. }
  835. }
  836. .cycle-connector {
  837. position: absolute;
  838. right: 0;
  839. top: 45.5px;
  840. transform: translateY(-50%);
  841. width: 2px;
  842. height: 87px;
  843. border: 5px solid #9dcaf7;
  844. border-left: none;
  845. background: transparent;
  846. img{
  847. width: 13px;
  848. height: 13px;
  849. position: absolute;
  850. top: 50%;
  851. transform: translateY(-50%);
  852. left: -8px;
  853. z-index: 1;
  854. }
  855. &.top-connector {
  856. border-top-right-radius: 5px;
  857. border-bottom-right-radius: 5px;
  858. img{
  859. left: -2px;
  860. }
  861. }
  862. &.middle-connector {
  863. border-top-left-radius: 5px;
  864. border-bottom-left-radius: 5px;
  865. left: 0;
  866. border-right: none;
  867. border-left: 5px solid #9dcaf7;
  868. }
  869. // 动态颜色
  870. &.connector-gray {
  871. border-color: #c4c6c9;
  872. }
  873. &.connector-gray.middle-connector {
  874. border-left-color: #c4c6c9;
  875. }
  876. }
  877. }
  878. }
  879. }
  880. // 控制区域样式
  881. .control-section {
  882. position: fixed;
  883. width: 100%;
  884. left: 0;
  885. box-sizing: border-box;
  886. bottom: 0px;
  887. background: #fff;
  888. padding: 16px 12px;
  889. display: flex;
  890. justify-content: space-between;
  891. align-items: center;
  892. border-top: 1px solid #f0f0f0;
  893. .toggle-group {
  894. display: flex;
  895. align-items: center;
  896. gap: 8px;
  897. .toggle-label {
  898. font-size: 13px;
  899. color: #141414;
  900. }
  901. }
  902. .add-button-group {
  903. display: flex;
  904. align-items: center;
  905. gap: 8px;
  906. .button {
  907. color: #2199f8;
  908. border-radius: 25px;
  909. padding: 9px 15px;
  910. border: 1px solid #2199f8;
  911. }
  912. .add-button {
  913. background: linear-gradient(120deg, #76c3ff 0%, #2199f8 100%);
  914. color: white;
  915. border: 1px solid transparent;
  916. }
  917. }
  918. }
  919. }
  920. }
  921. </style>