工作日历前端数据交互完成

This commit is contained in:
XaoLi717 2024-12-04 16:10:35 +08:00
parent b258bd8d16
commit 7f4f5361d2

View File

@ -0,0 +1,210 @@
<template>
<ContentWrap>
<el-button-group style="display: flex; justify-content: space-between;">
<el-button size="small" @click="selectDate(-1)">上年</el-button>
<el-button size="small" @click="toYear()">今年</el-button>
<el-button size="small" @click="selectDate(1)">下年</el-button>
<el-button size="small" @click="getListDate()">刷新</el-button>
<!-- <el-button size="small" @click="createDate()">创建</el-button>-->
</el-button-group>
<el-row v-if="loading" :gutter="10">
<el-col v-for="(month, index) in months" :key="index" :span="6" >
<h5 class="month-title">{{ month }}</h5>
<el-calendar
v-loading="!loading"
ref="calendar"
class="custom-calendar"
:model-value="new Date(currentYear, index)"
>
<template #header="{ date }">
<el-row>
<el-col :span="24" class="header-date">
<span class="cl-title"> {{ date }}</span>
</el-col>
</el-row>
</template>
<template #date-cell="{ data }">
<div @click="handleSelect(data)" :class="{ 'holiday': holidayDate[data.day]?.isWorkday }">
<span style="font-size: 14px">
{{ formatDate2(data.date) === current? data.date.getDate()+'(今天)':data.date.getDate() }}
</span>
</div>
</template>
</el-calendar>
</el-col>
</el-row>
</ContentWrap>
</template>
<script setup lang="ts">
import {CalendarInstance} from "element-plus";
import {CalendarApi, CalendarVO} from "@/api/home/calendar";
defineOptions({ name: 'CalendarAgendaIndex' })
const message = useMessage() //
const calendar = ref<CalendarInstance>()
const currentYear = ref(new Date().getFullYear()) //
const months = reactive(['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'])
const currentDay = ref(new Date().getDate().toString().padStart(2, '0'));
const currentMonth2 = ref(new Date().getMonth()+1)
const current = currentYear.value+"-"+currentMonth2.value+"-"+currentDay.value
const holidayDate = ref()
const loading = ref(false) //
const list = ref<CalendarVO[]>([]) //
const total = ref(0) //
const queryParamsDate = reactive({
pageNo: 1,
pageSize: 1,
id: undefined,
date: [currentYear.value+"-01-01",currentYear.value+"-12-31"],//
isWorkday: undefined,
createTime: [],
})
const formDataCaln = ref({
id: undefined,
date: undefined,
isWorkday: undefined,
})
/** 创建数据 */
// const createDate = async () => {
// const dda = formDataCaln.value as unknown as CalendarVO
// dda.id = 1;
// dda.date = "2024-01-12"
// dda.isWorkday = 0
// const data = await CalendarApi.createCalendar(dda)
// console.log(data)
// }
/** 查询列表 */
const getListDate = async () => {
loading.value = false
try {
const data = await CalendarApi.getCalendarPage(queryParamsDate)
list.value = data.list
await mapDate()
total.value = data.total
} finally {
loading.value = true
}
}
//
const mapDate = async ()=> {
holidayDate.value = list.value.reduce((map, item) => {
map[item.date] = {
isWorkday: item.isWorkday,
date: item.date,
id: item.id
}
return map;
}, {});
}
//
const handleSelect = async (date:any) => {
const SelectDate = date.date
const year = SelectDate.getFullYear().toString()
const toYear = new Date().getFullYear().toString()
if (year !== toYear){
message.error("只能更改今年日期")
}
//,
const da = `${SelectDate.getFullYear()}-${(SelectDate.getMonth() + 1).toString().padStart(2, '0')}-${SelectDate.getDate().toString().padStart(2, '0')}`;
const dataClan = formDataCaln.value as unknown as CalendarVO
//
dataClan.id = holidayDate.value[da].id;
dataClan.date = holidayDate.value[da].date
dataClan.isWorkday = holidayDate.value[da].isWorkday === 1?0:1;
await CalendarApi.updateCalendar(dataClan)
//
holidayDate.value[da].isWorkday = holidayDate.value[da].isWorkday === 1?0:1;
};
//
function formatDate2(dat: number|Date) {
const date = new Date(dat)
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // 01
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
//
const selectDate = (val:number) => {
if (!val){
return
}
currentYear.value += val
}
//toYear Button change data go to year
const toYear = () => {
currentYear.value = new Date().getFullYear()
}
//
onMounted( async ()=> {
await getListDate()
})
</script>
<!-- 样式很重要 -->
<style lang="scss">
.custom-calendar {
border: 1px solid #1f1f1f !important; /* 使用 !important 确保样式生效 */
border-radius: 10px; /* 可选:添加圆角 */
}
.custom-calendar .el-calendar-day {
width: 100%;
height: 100%;
padding: 0 !important;
}
.custom-calendar .is-today {
width: 100%;
height: 100%;
background-color: #f56c6c;
font-size: 16px;
}
.holiday {
width: 100%;
height: 100%;
background-color: #15bc83;
font-size: 16px;
}
.schedule {
width: 100%;
height: 100%;
background-color: #CCFFCC;
color: #3b3e55;
font-size: 12px;
}
.month-title {
text-align: center
}
.cl-title {
display: inline-block;
font-size: 12px
}
.header-date {
text-align: center;
font-size: 10px;
}
</style>