无人机管理后台前端(已迁走)
shuishen
2025-05-09 e6b32fe9b386cd1b267ff7fdf47baf2c9419fe2e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<template>
  <div class="calenBox">
    <el-calendar ref="calendar" v-model="leftValue" @change="handleMonthChange(date)">
      <template #date-cell="{ data }">
        <div>
          <div class="date-number">{{ data.day.slice(8, 10) }}</div>
          <div class="events">
            <div
              v-for="(event, index) in getEvents(data.day)"
              :key="index"
              class="event-item"
              :class="event.type"
            >
              <span></span>
              {{ event.content }}
            </div>
          </div>
        </div>
      </template>
    </el-calendar>
  </div>
</template>
 
<script setup>
import dayjs from 'dayjs';
import { jobEventBar, getCalen } from '@/api/home/index';
// 日历事件数据
// 格式: 'YYYY-MM-DD': [...events]
const events = ref({
  '2025-05-01': [
    { type: 'work-order', content: '工单26' },
    { type: 'task', content: '任务26' },
  ],
  '2025-05-04': [
    { type: 'task', content: '任务26' },
    { type: 'work-order', content: '工单26' },
  ],
});
const params = ref({
  end_date: undefined,
  start_date: undefined,
});
 
function getCurrentMonthRange() {
  return {
    start_date: dayjs().startOf('month').format('YYYY-MM-DD HH:mm:ss'),
    end_date: dayjs().endOf('month').format('YYYY-MM-DD HH:mm:ss'),
  };
}
const leftValue = ref(new Date());
watch(
  () => leftValue.value,
  (newV, oldV) => {
    if (newV && dayjs(newV).isSame(dayjs(), 'day')) {
      console.log('点击了今天');
    }
 
    if (newV && oldV) {
      const newDate = dayjs(newV);
      const oldDate = dayjs(oldV);
 
      if (newDate.isBefore(oldDate, 'month')) {
        console.log('点击了上个月');
      } else if (newDate.isAfter(oldDate, 'month')) {
        console.log('点击了下个月');
      }
    }
  },
  { deep: true }
);
const handleMonthChange = date => {
  console.log('date', date);
 
  // 获取当前视图月份的第一天
  const firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
 
  // 获取当前视图月份的最后一天
  const lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
 
  // 格式化为YYYY-MM-DD格式(可选)
  const formatDate = d => d.toISOString().slice(0, 10);
 
  console.log('首日:', formatDate(firstDay));
  console.log('末日:', formatDate(lastDay));
};
// 获取日期数字
const getDate = date => {
  return date.getDate();
};
// 获取对应日期的事件
const getEvents = dateString => {
  // console.log('000',dateString);
 
  return events.value[dateString] || [];
};
const getJobEventBar = () => {
  const monthRange = getCurrentMonthRange();
  params.value = monthRange;
  console.log(monthRange);
  getCalen(params.value).then(res => {
    if (res.data.code !== 0) return;
    // events.value = res.data.data
    console.log('日历', res.data.data);
  });
};
onMounted(() => {
  getJobEventBar();
});
</script>
 
<style lang="scss" scoped>
.calenBox {
  margin-top: 10px;
  height: 546px;
  overflow: hidden;
  .event-item {
    font-size: 12px;
    padding: 2px;
    margin: 2px 0;
    border-radius: 3px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
 
    &.work-order {
      // background: #ecf5ff;
      color: #409eff;
      span {
        display: inline-block;
        width: 7px;
        height: 7px;
        background: #1c5cff;
        border-radius: 50%;
        margin-right: 2px;
      }
    }
 
    &.task {
      // background: #f0f9eb;
      color: #67c23a;
      span {
        display: inline-block;
        width: 7px;
        height: 7px;
        background: #029d36;
        border-radius: 50%;
        margin-right: 2px;
      }
    }
  }
}
</style>