WXL
3 天以前 96dd34f77d81db58f54e3d0ad4a8cc8082189a61
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<template>
  <div class="attendance-calendar">
    <!-- <el-button @click="refreshCalendar" size="small" type="primary"
      >刷新</el-button
    > -->
 
    <!-- Element UI 的日历组件 -->
    <el-calendar v-model="value">
      <template slot="dateCell" slot-scope="{ date, data }">
        <div class="calendar-date">
          <div class="date-number">
            {{
              data.day
                .split("-")
                .slice(1)
                .join("-")
            }}
          </div>
          <!-- <div style="font-size: 10px; color: #666;">{{ data.day }}</div> -->
          <div class="date-events">
            <!-- Element UI 中使用插槽作用域 -->
            <div
              v-for="event in getDateEvents(data.day)"
              :key="event.id"
              class="event-item"
              :class="event.type"
            >
              <span class="event-dot"></span>
              <span class="event-text">{{ event.text }}</span>
            </div>
          </div>
        </div>
      </template>
    </el-calendar>
  </div>
</template>
 
<script>
export default {
  name: "AttendanceCalendar",
  props: {
    attendanceData: Array,
    businessTripData: Array
  },
  data() {
    return {
      value: new Date(), // Element UI 使用 value
      localAttendanceData: [],
      localBusinessTripData: []
    };
  },
  watch: {
    attendanceData: {
      immediate: true,
      handler(val) {
        this.localAttendanceData = val || [];
        console.log("考勤数据:", this.localAttendanceData);
        this.$nextTick(() => {
          this.$forceUpdate(); // Vue 2 的强制更新
        });
      }
    },
    businessTripData: {
      immediate: true,
      handler(val) {
        this.localBusinessTripData = val || [];
        console.log("出差数据:", this.localBusinessTripData);
        this.$nextTick(() => {
          this.$forceUpdate();
        });
      }
    }
  },
  mounted() {
    console.log("Calendar mounted with Vue 2.6.12");
    this.testMethod();
  },
  methods: {
    refreshCalendar() {
      console.log("手动刷新日历");
      this.$forceUpdate();
    },
    testMethod() {
      console.log("测试方法是否可调用");
      console.log("getDateEvents 测试:", this.getDateEvents("2024-01-15"));
    },
    getDateEvents(date) {
      console.log("getDateEvents 被调用, 日期:", date);
      console.log("当前考勤数据:", this.localAttendanceData);
      console.log("当前出差数据:", this.localBusinessTripData);
 
      const events = [];
 
      // 考勤数据
      const attendance = (this.localAttendanceData || []).find(item => {
        return item && item.date === date;
      });
 
      if (attendance) {
        events.push({
          id: `attendance-${date}`,
          type: "attendance",
          text: `${attendance.checkIn || "--"}-${attendance.checkOut || "--"}`
        });
      }
 
      // 出差数据
      const businessTrip = (this.localBusinessTripData || []).find(item => {
        return item && date >= item.startDate && date <= item.endDate;
      });
 
      if (businessTrip) {
        events.push({
          id: `business-trip-${date}`,
          type: "business-trip",
          text: `出差: ${businessTrip.endCity || ""}`
        });
      }
 
      console.log("找到的事件:", events);
      return events;
    }
  }
};
</script>
 
<style scoped>
.attendance-calendar {
  padding: 20px;
}
 
.calendar-date {
  height: 100%;
  padding: 8px;
  display: flex;
  flex-direction: column;
}
 
.date-number {
  font-weight: bold;
  margin-bottom: 4px;
  font-size: 16px;
}
 
.date-events {
  flex: 1;
  overflow: hidden;
}
 
.event-item {
  display: flex;
  align-items: center;
  margin-bottom: 2px;
  font-size: 12px;
  padding: 2px 4px;
  border-radius: 2px;
  background-color: #f5f7fa;
}
 
.event-item.attendance {
  background-color: #f0f9eb;
  color: #67c23a;
}
 
.event-item.business-trip {
  background-color: #ecf5ff;
  color: #409eff;
}
 
.event-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  margin-right: 4px;
}
 
.event-item.attendance .event-dot {
  background-color: #67c23a;
}
 
.event-item.business-trip .event-dot {
  background-color: #409eff;
}
::v-deep .el-calendar-table .el-calendar-day {
  height: 115px;
}
.event-text {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}
</style>