calendar.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <template>
  2. <div class="calendar">
  3. <div class="header-wrap">
  4. <div class="header-l">
  5. <div class="top-l">
  6. <el-icon class="icon icon-l" color="#999999" size="11" @click="prevPeriod"><ArrowLeftBold /></el-icon>
  7. <!-- <span class="top-tag red" v-if="expiredCounts">{{ expiredCounts }}过期</span> -->
  8. </div>
  9. <div class="top-c">
  10. <span class="header-text">
  11. {{ dateRange.start }} <span class="center-line">-</span> {{ dateRange.end }}
  12. </span>
  13. </div>
  14. <div class="top-r">
  15. <span class="top-tag orange" v-if="completedCounts">{{ completedCounts }}待完成</span>
  16. <el-icon class="icon icon-r" color="#999999" size="11" @click="nextPeriod"><ArrowRightBold /></el-icon>
  17. </div>
  18. </div>
  19. <!-- <div class="header-r">
  20. <span class="line"></span>
  21. 高温预警
  22. </div> -->
  23. </div>
  24. <div class="days">
  25. <div
  26. class="days-item"
  27. v-for="(day, index) in calendarDays"
  28. :key="index"
  29. :class="[{ activeDay: activeDay === day.date, today: day.isToday && !day.solarTerm }]"
  30. @click="selectDate(day.date, day)"
  31. >
  32. <div class="day-box">
  33. <span class="days-week">{{ day.isToday ? "今天" : `周${day.dayOfWeek}` }}</span>
  34. <span class="days-one">{{ day.day }}</span>
  35. </div>
  36. <div v-if="day.solarTerm" class="solar-term">{{ day.solarTerm }}</div>
  37. <div v-if="day.typeName" class="type-num">{{ day.typeName.lengthVal }}</div>
  38. <!-- <div v-if="day.isHeatWarning" class="heat-warning"></div>
  39. <div v-if="day.typeName" class="type-name">
  40. <div class="type-text">{{ day.typeName.farmWorkName }}</div>
  41. </div> -->
  42. </div>
  43. </div>
  44. </div>
  45. </template>
  46. <script setup>
  47. import { ref, computed, onDeactivated, onMounted } from "vue";
  48. import solarLunar from "solarlunar";
  49. // const props = defineProps({
  50. // calendarWorkList: {
  51. // type: Array,
  52. // required: true,
  53. // },
  54. // });
  55. const today = new Date();
  56. // const startDate = ref(getAlignedStartDate(today));
  57. const startDate = ref(new Date(today));
  58. // 定义星期几的名称
  59. const weekdays = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"];
  60. const weekdaysShort = ["一", "二", "三", "四", "五", "六", "日"];
  61. // 存储任务列表数据
  62. const taskListData = ref([]);
  63. const days = computed(() => {
  64. return Array.from({ length: 7 }, (_, i) => {
  65. const date = new Date(startDate.value);
  66. date.setDate(startDate.value.getDate() + i);
  67. return date;
  68. });
  69. });
  70. const calendarDays = computed(() => {
  71. const daysList = [];
  72. for (let i = 0; i < days.value.length; i++) {
  73. const date = days.value[i];
  74. const dayOfWeek = date.getDay(); // 0是周日,1是周一,...,6是周六
  75. // 调整显示:周一显示为"一",周二显示为"二",...,周日显示为"日"
  76. const displayDayOfWeek = dayOfWeek === 0 ? "日" : weekdaysShort[dayOfWeek - 1];
  77. // 获取该日期的节气
  78. const solarTerm = getSolarTerm(date);
  79. // 获取该日期的农事数据
  80. const typeName = getTaskByDate(formatDate(date));
  81. daysList.push({
  82. day: date.getDate(),
  83. date: formatDate(date),
  84. isToday: formatDate(date) === formatDate(today),
  85. dayOfWeek: displayDayOfWeek,
  86. solarTerm: solarTerm, // 使用真实计算的节气数据
  87. isHeatWarning: i === 5,
  88. typeName: typeName, // 使用传入的任务数据
  89. });
  90. }
  91. return daysList;
  92. });
  93. // 根据日期获取农事数据
  94. function getTaskByDate(dateStr) {
  95. if (!taskListData.value || taskListData.value.length === 0) {
  96. return null;
  97. }
  98. // 查找匹配日期的农事
  99. const matchedTasks = taskListData.value.filter(task => {
  100. if (!task.executeDate) return false;
  101. // 格式化日期字符串进行比较(确保格式一致)
  102. const taskDate = formatDate(new Date(task.executeDate));
  103. return taskDate === dateStr;
  104. });
  105. if (matchedTasks.length === 0) {
  106. return null;
  107. }
  108. // const farmWorkNames = matchedTasks.map(task => task.farmWorkName || '').filter(Boolean);
  109. return {
  110. lengthVal: matchedTasks.length,
  111. // farmWorkName: farmWorkNames.length > 1 ? farmWorkNames.join('、') : farmWorkNames[0] || ''
  112. };
  113. }
  114. function setSolarTerm(taskList) {
  115. // 存储任务列表数据
  116. taskListData.value = taskList || [];
  117. // 为每个任务计算节气
  118. for (let item of taskListData.value) {
  119. if (item.executeDate) {
  120. item.solarTerm = getSolarTerm(new Date(item.executeDate));
  121. }
  122. }
  123. }
  124. const expiredCounts = ref(0);
  125. const completedCounts = ref(0);
  126. function setCounts(index, counts) {
  127. if (index === 2) {
  128. completedCounts.value = counts;
  129. } else if (index === 3) {
  130. expiredCounts.value = counts;
  131. }
  132. }
  133. // 清除选中状态
  134. const clearSelection = () => {
  135. activeDay.value = null;
  136. selectedDate.value = null;
  137. };
  138. defineExpose({
  139. setSolarTerm,
  140. setCounts,
  141. clearSelection
  142. });
  143. const dateRange = computed(() => {
  144. let start = calendarDays.value[0].date;
  145. start = start.replace(/-/g, ".");
  146. let end = calendarDays.value[6].date;
  147. end = end.replace(/^\d{4}-(\d{2})-(\d{2})$/, "$1.$2");
  148. return { start, end };
  149. });
  150. function getAlignedStartDate(referenceDate) {
  151. const start = new Date(referenceDate);
  152. const dayOfWeek = start.getDay();
  153. start.setDate(start.getDate() - dayOfWeek + (dayOfWeek === 0 ? -13 : 1)); // 对齐至周一,确保21天周期合理
  154. return start;
  155. }
  156. function formatDate(date) {
  157. // String(currentMonth.value).padStart(2, "0")
  158. return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(
  159. 2,
  160. "0"
  161. )}`;
  162. }
  163. // 获取指定日期的节气
  164. function getSolarTerm(date) {
  165. try {
  166. const year = date.getFullYear();
  167. const month = date.getMonth() + 1; // getMonth() 返回 0-11,需要加1
  168. const day = date.getDate();
  169. const lunar = solarLunar.solar2lunar(year, month, day);
  170. // lunar.term 返回节气名称,如果没有节气则返回空字符串
  171. return lunar.term || null;
  172. } catch (error) {
  173. console.error("获取节气失败:", error);
  174. return null;
  175. }
  176. }
  177. function prevPeriod() {
  178. startDate.value.setDate(startDate.value.getDate() - 7);
  179. startDate.value = new Date(startDate.value);
  180. }
  181. function nextPeriod() {
  182. startDate.value.setDate(startDate.value.getDate() + 7);
  183. startDate.value = new Date(startDate.value);
  184. }
  185. const activeDay = ref(null);
  186. const emit = defineEmits(['dateSelect']);
  187. const selectDate = (date, day) => {
  188. // 如果点击的是已选中的日期,取消选中
  189. if (activeDay.value === date) {
  190. activeDay.value = null;
  191. selectedDate.value = null;
  192. emit('dateSelect', null); // 传递 null 表示取消筛选
  193. } else {
  194. activeDay.value = date;
  195. selectedDate.value = `${date} (${day.dayOfWeek})`;
  196. emit('dateSelect', date); // 传递选中的日期
  197. }
  198. };
  199. // 初始化时不选择任何日期(默认不选中)
  200. // onMounted(() => {
  201. // selectDate(formatDate(today), {
  202. // day: today.getDate(),
  203. // dayOfWeek: weekdaysShort[today.getDay() === 0 ? 6 : today.getDay() - 1],
  204. // });
  205. // });
  206. function closeDialog() {
  207. activeDay.value = null;
  208. }
  209. const selectedDate = ref(null);
  210. </script>
  211. <style lang="scss" scoped>
  212. .calendar {
  213. width: 100%;
  214. text-align: center;
  215. box-sizing: border-box;
  216. .header-wrap {
  217. display: flex;
  218. justify-content: space-between;
  219. align-items: center;
  220. margin-bottom: 10px;
  221. .header-l {
  222. display: flex;
  223. align-items: center;
  224. justify-content: space-between;
  225. width: 100%;
  226. .top-c {
  227. flex: 1;
  228. text-align: center;
  229. }
  230. .header-text {
  231. color: #000;
  232. font-size: 16px;
  233. font-weight: bold;
  234. .center-line {
  235. position: relative;
  236. top: -3px;
  237. }
  238. }
  239. .icon {
  240. width: 20px;
  241. height: 20px;
  242. background: #F2F3F5;
  243. border-radius: 50%;
  244. text-align: center;
  245. line-height: 20px;
  246. &.icon-l {
  247. margin-right: 2px;
  248. }
  249. &.icon-r {
  250. margin-left: 2px;
  251. }
  252. }
  253. .top-tag {
  254. font-size: 12px;
  255. padding: 0 8px;
  256. height: 20px;
  257. line-height: 20px;
  258. border-radius: 20px;
  259. &.red {
  260. color: #FF0000;
  261. background: rgba(255, 0, 0, 0.1);
  262. }
  263. &.orange {
  264. color: #FF790B;
  265. background: rgba(255, 149, 61, 0.2);
  266. }
  267. }
  268. }
  269. .header-r {
  270. background: rgba(252, 138, 44, 0.12);
  271. padding: 6px 10px;
  272. border-radius: 28px;
  273. color: #fc8a2c;
  274. display: inline-flex;
  275. align-items: center;
  276. font-size: 10px;
  277. .line {
  278. width: 12px;
  279. height: 1px;
  280. margin-right: 5px;
  281. background: #fc8a2c;
  282. }
  283. }
  284. }
  285. }
  286. .weekdays {
  287. display: grid;
  288. grid-template-columns: repeat(7, 1fr);
  289. font-size: 12px;
  290. }
  291. .days {
  292. display: grid;
  293. grid-template-columns: repeat(7, 1fr);
  294. // gap: 5px;
  295. font-size: 12px;
  296. .days-item + .days-item {
  297. margin-left: 6px;
  298. }
  299. .days-item {
  300. cursor: pointer;
  301. position: relative;
  302. &.today {
  303. min-height: 70px;
  304. .day-box {
  305. color: #2199f8;
  306. }
  307. }
  308. &.activeDay {
  309. .day-box {
  310. color: #fff;
  311. background: linear-gradient(136deg, #9fd5ff, #2199f8);
  312. }
  313. }
  314. .day-box {
  315. background: #ffffff;
  316. color: #000;
  317. border-radius: 8px;
  318. padding: 7px 0;
  319. position: relative;
  320. .days-week {
  321. font-size: 12px;
  322. }
  323. }
  324. .solar-term {
  325. padding-top: 3px;
  326. color: #8D8D8D;
  327. font-size: 12px;
  328. }
  329. .type-num {
  330. position: absolute;
  331. top: -5px;
  332. right: -5px;
  333. color: #fff;
  334. font-size: 10px;
  335. background: #2199F8;
  336. width: 14px;
  337. height: 14px;
  338. line-height: 16px;
  339. border-radius: 50%;
  340. }
  341. .days-one {
  342. text-align: center;
  343. display: block;
  344. margin: 0 auto;
  345. font-size: 14px;
  346. line-height: 16px;
  347. font-weight: bold;
  348. padding-top: 2px;
  349. // width: 32px;
  350. // height: 32px;
  351. // line-height: 32px;
  352. // border-radius: 50%;
  353. }
  354. .type-name {
  355. font-size: 10px;
  356. position: relative;
  357. top: -4px;
  358. border-radius: 12px;
  359. position: relative;
  360. background: #fff;
  361. padding-top: 2px;
  362. .type-text {
  363. border-radius: 12px;
  364. padding: 2px;
  365. }
  366. }
  367. }
  368. }
  369. .today {
  370. position: relative;
  371. &::after {
  372. content: "";
  373. position: absolute;
  374. left: 0;
  375. right: 0;
  376. bottom: 0;
  377. margin: 0 auto;
  378. width: 10px;
  379. height: 10px;
  380. background: url("@/assets/img/home/today.png") no-repeat center center / 100% 100%;
  381. }
  382. &.no-type {
  383. &::after {
  384. bottom: 14px;
  385. }
  386. }
  387. }
  388. </style>