tj
2025-06-05 bba272999cc546f65781bf3d20245a3f819af67f
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<template>
  <div class="container">
    <div class="main-calendar">
      <el-calendar v-model="calendarDate">
        <template #date-cell="{ data }">
          <div class="calendarClass" @click="handleCalendarClick(data.day)">
            <p>
              {{ data.day.split('-').slice(1).join('-') }}
              {{ data.isSelected ? '✔️' : '' }}
            </p>
            <p>{{ getCurHolidayByDay(data.day) }}</p>
          </div>
        </template>
      </el-calendar>
    </div>
    <div class="main-calendar-holiday">
      <div class="search-section">
        <el-form :inline="true" :model="queryForm" class="demo-form-inline">
          <el-form-item label="标题">
            <el-input v-model="queryForm.title" placeholder="标题" clearable />
          </el-form-item>
          <el-form-item label="日期" prop="daily_date" style="width: 400px">
            <!-- <el-date-picker
              v-model="queryForm.date_range"
              type="daterange"
              range-separator="到"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              value-format="YYYY-MM-DD"
              format="YYYY-MM-DD"
              clearable
            /> -->
            <el-date-picker v-model="queryForm.calDate" type="date" placeholder="选择时间" clearable />
          </el-form-item>
        </el-form>
      </div>
 
      <el-button @click="handleAdd()">新增</el-button>
      <el-table :data="calendarHolidayList" style="width: 100%">
        <el-table-column fixed prop="title" label="标题" width="180" />
        <el-table-column prop="start_date" label="时间" width="220">
          <template #default="scope"> {{ scope.row.start_date }} - {{ scope.row.end_date }} </template>
        </el-table-column>
        <!-- <el-table-column prop="start_date" label="开始时间" width="180" />
        <el-table-column prop="end_date" label="结束时间" width="180" /> -->
        <el-table-column prop="type" label="假期类型" width="100">
          <template #default="scope">
            {{ getSysDictName(scope.row.type, calendarTypeList) }}
          </template>
        </el-table-column>
        <el-table-column prop="update_time" label="更新时间" width="220" />
        <el-table-column fixed="right" label="操作" min-width="120">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="handleEditClick(scope.row)">编辑</el-button>
            <el-button link type="primary" size="small" @click="handleDelClick(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
 
    <el-drawer v-model="addDrawer" :before-close="handleItemClose">
      <calendar-holiday-add
        @editClose="handleCalendarHolidayClose"
        :editId="calendarHoliday.id"
        :key="uniKey"
        :showBack="false"
        :pointDate="pointDate"
      ></calendar-holiday-add>
    </el-drawer>
  </div>
</template>
 
<script>
import { ref, onMounted, watch, reactive } from 'vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import Calendar from '@/model/calendar'
import CalendarHoliday from '@/model/calendarHoliday'
import SysDictItemModel from '@/model/sysDictItem'
import calendarHolidayAdd from '@/view/calendar/calendar-holiday-add'
 
export default {
  components: {
    calendarHolidayAdd,
  },
  setup() {
    const uniKey = ref(0)
    const calendarDate = ref(new Date())
    const addDrawer = ref(false)
    const pointDate = ref(null)
    const calendarHoliday = ref({
      id: '',
    })
    const loading = ref(false)
    const calendarTypeList = ref([])
    const queryForm = reactive({
      date_range: [],
      title: '',
      calDate: '',
      page: 1,
      count: 10,
    })
 
    onMounted(async () => {
      // 先获取字典
      await loadDictDitems()
      // 获取当前日历日期的列表情况
      await getCalendarList()
      // 列出所有项目信息
      getCalendarHolidayList()
    })
 
    // 监听日历日期的变化
    watch(
      () => calendarDate,
      (newDate, oldDate) => {
        getCalendarList()
      },
    )
    // 监听日历日期的变化
    watch(
      () => queryForm,
      (newData, oldData) => {
        //   console.log("查询条件")
        //  console.log(queryForm)
        getCalendarHolidayList()
      },
      { deep: true }, // 深度监听
    )
 
    // 根据字典类型查询字典
    const loadDictDitems = async () => {
      calendarTypeList.value = await SysDictItemModel.getSysDictItemListByType('calendar_type')
    }
 
    // 日历记事信息
    const calendarList = ref([])
    const getCalendarList = async () => {
      try {
        loading.value = true
 
        const params = {
          // calMonth:calendarDate.value.getMonth()+1,
          calMonthStart: calendarDate.value.getMonth(),
          calMonthEnd: calendarDate.value.getMonth() + 2,
        }
        const res = await Calendar.getCalendarList(params)
        calendarList.value = res
      } catch (error) {
        console.log(error)
        loading.value = false
        if (error.code === 10020) {
          calendarList.value = []
        }
      }
    }
 
    // 日历记事信息
    const calendarHolidayList = ref([])
    const getCalendarHolidayList = async () => {
      try {
        loading.value = true
        const res = await CalendarHoliday.getCalendarHolidayList(queryForm)
        calendarHolidayList.value = res
        // 同时更新日历的情况
        getCalendarList()
      } catch (error) {
        console.log(error)
        loading.value = false
        if (error.code === 10020) {
          calendarHolidayList.value = []
        }
      }
    }
 
    const handleAdd = () => {
      uniKey.value++
      pointDate.value = ''
      calendarHoliday.value = {}
      addDrawer.value = true
    }
    const handleEditClick = row => {
      uniKey.value++
      pointDate.value = ''
      calendarHoliday.value = row
      addDrawer.value = true
    }
    const handleDelClick = row => {
      // 删除操作
      ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        const res = await CalendarHoliday.deleteCalendarHoliday(row.id)
        if (res.code < window.MAX_SUCCESS_CODE) {
          getCalendarHolidayList()
 
          ElMessage.success(`${res.message}`)
        }
      }).catch(() => {
        // 用户点击取消按钮时执行的操作
        // 可以选择不做任何事情,也可以显示取消消息
        // ElMessage.info('操作已取消'); // 如果需要显示取消消息,可以取消注释
      });
    }
    const handleCalendarHolidayClose = () => {
      uniKey.value++
      addDrawer.value = true
      getCalendarHolidayList()
    }
 
    const handleItemClose = () => {
      uniKey.value++
      addDrawer.value = false
      getCalendarHolidayList()
    }
 
    const getSysDictName = (value, data) => {
      const tmp = data.find(tmp => tmp.value == value)
      if (tmp) {
        return tmp.label
      }
      return ''
    }
 
    const getCurHolidayByDay = currDate => {
      const data = calendarList.value.find(f => f.cal_date == `${currDate} 00:00:00`)
      if (data) {
        if (data.type != 0) {
          return getSysDictName(data.type, calendarTypeList.value)
        }
        return ''
      }
      return ''
    }
 
    // 日历点击操作
    const handleCalendarClick = async currDate => {
      // queryForm.date_range[0]=currDate
      // queryForm.date_range[1]=currDate
      queryForm.calDate = currDate
      // 根据当前日期判断是否有记录
      calendarHoliday.value = {}
      pointDate.value = ''
      const res = await CalendarHoliday.getCalendarHolidayList({ calDate: currDate })
      if (res && res.length > 0) {
        calendarHoliday.value = res[0]
      } else {
        calendarHoliday.value = {}
        pointDate.value = currDate
      }
      uniKey.value++
      addDrawer.value = true
    }
 
    return {
      loading,
      calendarDate,
      calendarHolidayList,
      getCalendarHolidayList,
 
      // 新增
      calendarHoliday,
      addDrawer,
      handleAdd,
      handleCalendarHolidayClose,
      handleItemClose,
      handleEditClick,
      handleDelClick,
      uniKey,
      calendarList,
      getCurHolidayByDay,
      calendarTypeList,
      getSysDictName,
      handleCalendarClick,
      pointDate,
      queryForm,
    }
  },
}
</script>
 
<style scoped lang="scss">
.container {
  display: flex;
  justify-content: center;
 
  .main-calendar {
    width: 50%;
    height: 100%;
    padding: 20px;
 
    .calendarClass {
      width: 100%;
      height: 100%;
      text-align: center;
    }
  }
 
  .main-calendar-holiday {
    width: 50%;
    height: 100%;
    padding: 20px;
  }
}
</style>