calendar.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. defineExpose({
  134. setSolarTerm,
  135. setCounts
  136. });
  137. const dateRange = computed(() => {
  138. let start = calendarDays.value[0].date;
  139. start = start.replace(/-/g, ".");
  140. let end = calendarDays.value[6].date;
  141. end = end.replace(/^\d{4}-(\d{2})-(\d{2})$/, "$1.$2");
  142. return { start, end };
  143. });
  144. function getAlignedStartDate(referenceDate) {
  145. const start = new Date(referenceDate);
  146. const dayOfWeek = start.getDay();
  147. start.setDate(start.getDate() - dayOfWeek + (dayOfWeek === 0 ? -13 : 1)); // 对齐至周一,确保21天周期合理
  148. return start;
  149. }
  150. function formatDate(date) {
  151. // String(currentMonth.value).padStart(2, "0")
  152. return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(
  153. 2,
  154. "0"
  155. )}`;
  156. }
  157. // 获取指定日期的节气
  158. function getSolarTerm(date) {
  159. try {
  160. const year = date.getFullYear();
  161. const month = date.getMonth() + 1; // getMonth() 返回 0-11,需要加1
  162. const day = date.getDate();
  163. const lunar = solarLunar.solar2lunar(year, month, day);
  164. // lunar.term 返回节气名称,如果没有节气则返回空字符串
  165. return lunar.term || null;
  166. } catch (error) {
  167. console.error("获取节气失败:", error);
  168. return null;
  169. }
  170. }
  171. function prevPeriod() {
  172. startDate.value.setDate(startDate.value.getDate() - 7);
  173. startDate.value = new Date(startDate.value);
  174. }
  175. function nextPeriod() {
  176. startDate.value.setDate(startDate.value.getDate() + 7);
  177. startDate.value = new Date(startDate.value);
  178. }
  179. const activeDay = ref(null);
  180. const selectDate = (date, day) => {
  181. activeDay.value = date;
  182. selectedDate.value = `${date} (${day.dayOfWeek})`;
  183. };
  184. // 初始化时选择今天
  185. onMounted(() => {
  186. selectDate(formatDate(today), {
  187. day: today.getDate(),
  188. dayOfWeek: weekdaysShort[today.getDay() === 0 ? 6 : today.getDay() - 1],
  189. });
  190. });
  191. function closeDialog() {
  192. activeDay.value = null;
  193. }
  194. const selectedDate = ref(null);
  195. </script>
  196. <style lang="scss" scoped>
  197. .calendar {
  198. width: 100%;
  199. text-align: center;
  200. box-sizing: border-box;
  201. .header-wrap {
  202. display: flex;
  203. justify-content: space-between;
  204. align-items: center;
  205. margin-bottom: 10px;
  206. .header-l {
  207. display: flex;
  208. align-items: center;
  209. justify-content: space-between;
  210. width: 100%;
  211. .top-c {
  212. flex: 1;
  213. text-align: center;
  214. }
  215. .header-text {
  216. color: #000;
  217. font-size: 16px;
  218. font-weight: bold;
  219. .center-line {
  220. position: relative;
  221. top: -3px;
  222. }
  223. }
  224. .icon {
  225. width: 20px;
  226. height: 20px;
  227. background: #F2F3F5;
  228. border-radius: 50%;
  229. text-align: center;
  230. line-height: 20px;
  231. &.icon-l {
  232. margin-right: 2px;
  233. }
  234. &.icon-r {
  235. margin-left: 2px;
  236. }
  237. }
  238. .top-tag {
  239. font-size: 12px;
  240. padding: 0 8px;
  241. height: 20px;
  242. line-height: 20px;
  243. border-radius: 20px;
  244. &.red {
  245. color: #FF0000;
  246. background: rgba(255, 0, 0, 0.1);
  247. }
  248. &.orange {
  249. color: #FF790B;
  250. background: rgba(255, 149, 61, 0.2);
  251. }
  252. }
  253. }
  254. .header-r {
  255. background: rgba(252, 138, 44, 0.12);
  256. padding: 6px 10px;
  257. border-radius: 28px;
  258. color: #fc8a2c;
  259. display: inline-flex;
  260. align-items: center;
  261. font-size: 10px;
  262. .line {
  263. width: 12px;
  264. height: 1px;
  265. margin-right: 5px;
  266. background: #fc8a2c;
  267. }
  268. }
  269. }
  270. }
  271. .weekdays {
  272. display: grid;
  273. grid-template-columns: repeat(7, 1fr);
  274. font-size: 12px;
  275. }
  276. .days {
  277. display: grid;
  278. grid-template-columns: repeat(7, 1fr);
  279. // gap: 5px;
  280. font-size: 12px;
  281. .days-item + .days-item {
  282. margin-left: 6px;
  283. }
  284. .days-item {
  285. cursor: pointer;
  286. position: relative;
  287. &.today {
  288. min-height: 70px;
  289. .day-box {
  290. color: #2199f8;
  291. }
  292. }
  293. &.activeDay {
  294. .day-box {
  295. color: #fff;
  296. background: linear-gradient(136deg, #9fd5ff, #2199f8);
  297. }
  298. }
  299. .day-box {
  300. background: #ffffff;
  301. color: #000;
  302. border-radius: 8px;
  303. padding: 7px 0;
  304. position: relative;
  305. .days-week {
  306. font-size: 12px;
  307. }
  308. }
  309. .solar-term {
  310. padding-top: 3px;
  311. color: #8D8D8D;
  312. font-size: 12px;
  313. }
  314. .type-num {
  315. position: absolute;
  316. top: -5px;
  317. right: -5px;
  318. color: #fff;
  319. font-size: 10px;
  320. background: #2199F8;
  321. width: 14px;
  322. height: 14px;
  323. line-height: 16px;
  324. border-radius: 50%;
  325. }
  326. .days-one {
  327. text-align: center;
  328. display: block;
  329. margin: 0 auto;
  330. font-size: 14px;
  331. line-height: 16px;
  332. font-weight: bold;
  333. padding-top: 2px;
  334. // width: 32px;
  335. // height: 32px;
  336. // line-height: 32px;
  337. // border-radius: 50%;
  338. }
  339. .type-name {
  340. font-size: 10px;
  341. position: relative;
  342. top: -4px;
  343. border-radius: 12px;
  344. position: relative;
  345. background: #fff;
  346. padding-top: 2px;
  347. .type-text {
  348. border-radius: 12px;
  349. padding: 2px;
  350. }
  351. }
  352. }
  353. }
  354. .today {
  355. position: relative;
  356. &::after {
  357. content: "";
  358. position: absolute;
  359. left: 0;
  360. right: 0;
  361. bottom: 0;
  362. margin: 0 auto;
  363. width: 10px;
  364. height: 10px;
  365. background: url("@/assets/img/home/today.png") no-repeat center center / 100% 100%;
  366. }
  367. &.no-type {
  368. &::after {
  369. bottom: 14px;
  370. }
  371. }
  372. }
  373. </style>