cuilei
5 天以前 ba77fdc6a30c9ad0941e40319f8c4f6fe6fdf9f1
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<!-- src/views/mes/modules/MesProductionWeekCalendar.vue -->
<template>
  <div class="week-calendar">
    <div class="calendar-header">
      <a-button icon="left" @click="prevWeek" size="small" />
      <span class="current-week-range">{{ weekRangeText }}</span>
      <a-button icon="right" @click="nextWeek" size="small" />
      <a-button @click="goToToday" size="small" style="margin-left: 8px">今天</a-button>
    </div>
 
    <div class="calendar-grid">
      <div class="week-header">
        <div
          v-for="(day, index) in weekDays"
          :key="index"
          class="header-cell"
        >
          <div class="day-name">{{ day.format('ddd') }}</div>
          <div
            class="day-number"
            :class="{ today: isToday(day), selected: isSelected(day) }"
            @click="selectDay(day)"
          >
            {{ day.date() }}
          </div>
        </div>
      </div>
 
      <div class="calendar-body">
        <div
          v-for="(day, index) in weekDays"
          :key="index"
          class="day-cell"
          :class="{ today: isToday(day), selected: isSelected(day) }"
          @click="selectDay(day)"
        >
          <div class="cell-content">
            <!-- 插槽用于渲染日期内容 -->
            <slot
              name="dateCell"
              :date="day"
              :isSelected="isSelected(day)"
              :isToday="isToday(day)"
            >
              <!-- 默认内容 -->
              <div class="default-content">
                <!-- 可以在这里添加默认的日期内容渲染逻辑 -->
              </div>
            </slot>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
 
<script>
import moment from 'moment'
 
export default {
  name: 'MesProductionWeekCalendar',
  props: {
    // 起始日期,默认为今天
    startDate: {
      type: [Object, String, Date],
      default: null
    }
  },
  data() {
    return {
      // 当前显示的起始日期
      currentStartDate: this.startDate ? moment(this.startDate) : moment(),
      // 选中的日期
      selectedDate: this.startDate ? moment(this.startDate) : moment()
    }
  },
  computed: {
    // 计算连续7天的日期
    weekDays() {
      const days = []
      for (let i = 0; i < 7; i++) {
        days.push(this.currentStartDate.clone().add(i, 'days'))
      }
      return days
    },
    // 当前显示的日期范围文本
    weekRangeText() {
      const start = this.currentStartDate
      const end = this.currentStartDate.clone().add(6, 'days')
 
      if (start.month() === end.month()) {
        return `${start.format('YYYY年M月D日')} - ${end.format('D日')}`
      } else {
        return `${start.format('YYYY年M月D日')} - ${end.format('M月D日')}`
      }
    }
  },
  watch: {
    startDate(newVal) {
      if (newVal) {
        this.currentStartDate = moment(newVal)
        this.selectedDate = moment(newVal)
      }
    }
  },
  methods: {
    // 切换到上7天
    prevWeek() {
      this.currentStartDate = this.currentStartDate.clone().subtract(7, 'days')
      this.$emit('change', this.currentStartDate)
    },
    // 切换到下7天
    nextWeek() {
      this.currentStartDate = this.currentStartDate.clone().add(7, 'days')
      this.$emit('change', this.currentStartDate)
    },
    // 跳转到今天
    goToToday() {
      this.currentStartDate = moment()
      this.selectedDate = moment()
      this.$emit('change', this.currentStartDate)
      this.$emit('select', this.selectedDate)
    },
    // 选择日期
    selectDay(day) {
      this.selectedDate = day.clone()
      this.$emit('select', day)
    },
    // 判断是否是今天
    isToday(day) {
      return day.isSame(moment(), 'day')
    },
    // 判断是否是选中的日期
    isSelected(day) {
      return day.isSame(this.selectedDate, 'day')
    }
  }
}
</script>
 
<style scoped>
.week-calendar {
  padding: 16px;
}
 
.calendar-header {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 16px;
}
 
.current-week-range {
  margin: 0 16px;
  font-weight: bold;
  font-size: 14px;
}
 
.calendar-grid {
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  overflow: hidden;
}
 
.week-header {
  display: flex;
  background-color: #fafafa;
  border-bottom: 1px solid #e8e8e8;
}
 
.header-cell {
  flex: 1;
  text-align: center;
  padding: 8px 0;
  border-right: 1px solid #e8e8e8;
}
 
.header-cell:last-child {
  border-right: none;
}
 
.day-name {
  font-size: 12px;
  color: #666;
  margin-bottom: 4px;
}
 
.day-number {
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  width: 30px;
  height: 30px;
  line-height: 30px;
  margin: 0 auto;
  border-radius: 50%;
}
 
.day-number.today {
  background-color: #1890ff;
  color: white;
}
 
.day-number.selected {
  background-color: #bae7ff;
  color: #1890ff;
}
 
.calendar-body {
  display: flex;
  height: 300px;
}
 
.day-cell {
  flex: 1;
  border-right: 1px solid #e8e8e8;
  cursor: pointer;
  padding: 4px;
}
 
.day-cell:last-child {
  border-right: none;
}
 
.day-cell.today {
  background-color: #e6f7ff;
}
 
.day-cell.selected {
  background-color: #bae7ff;
}
 
.cell-content {
  height: 100%;
  overflow: hidden;
}
 
.default-content {
  height: 100%;
}
</style>