|
@@ -1,319 +0,0 @@
|
|
|
-<template>
|
|
|
- <div :class="ui" class="hc-date-calendar-box">
|
|
|
- <div class="hc-date-calendar-panel">
|
|
|
- <div class="hc-date-month-bar">
|
|
|
- <div class="hc-date-month" @click="prevYearClick">
|
|
|
- <i class="hcicon-direction-left"/>
|
|
|
- </div>
|
|
|
- <div class="hc-date-month" @click="prevMonthClick">
|
|
|
- <i class="hcicon-return"/>
|
|
|
- </div>
|
|
|
- <div class="hc-date-month-year">
|
|
|
- <!--n-popselect v-model:value="selectedDate.year" :options="yearOptions" size="large" trigger="click" scrollable @update:value="popselectYear">
|
|
|
- <div class="date-year">{{selectedDate.year}}年</div>
|
|
|
- </n-popselect>
|
|
|
- <n-popselect v-model:value="selectedDate.month" :options="monthOptions" size="large" trigger="click" scrollable @update:value="popselectMonth">
|
|
|
- <div class="date-month">{{selectedDate.month}}月</div>
|
|
|
- </n-popselect-->
|
|
|
- </div>
|
|
|
- <div class="hc-date-month" @click="nextMonthClick">
|
|
|
- <i class="hcicon-enter"/>
|
|
|
- </div>
|
|
|
- <div class="hc-date-month" @click="nextYearClick">
|
|
|
- <i class="hcicon-direction-right"/>
|
|
|
- </div>
|
|
|
- </div>
|
|
|
- <div class="hc-date-weekdays">
|
|
|
- <div class="hc-date-weekdays-day" v-for="item in weekdays" :key="item">{{item}}</div>
|
|
|
- </div>
|
|
|
- <div class="hc-date-dates">
|
|
|
- <template v-for="item in datesDay">
|
|
|
- <div class="hc-date-dates-day" :class="item.type" @click="datesDayClick(item)">{{item.key}}</div>
|
|
|
- </template>
|
|
|
- </div>
|
|
|
- <div class="hc-date-actions">
|
|
|
- <div class="choice-date">{{choiceDate.year}}-{{choiceDate.month}}-{{choiceDate.date}}</div>
|
|
|
- <!--n-button size="small" @click="backToDay">回到今天</n-button-->
|
|
|
- </div>
|
|
|
- </div>
|
|
|
- </div>
|
|
|
-</template>
|
|
|
-
|
|
|
-<script setup>
|
|
|
-import { ref } from "vue";
|
|
|
-import dayjs from "dayjs"
|
|
|
-const props = defineProps({
|
|
|
- ui: {
|
|
|
- type: String,
|
|
|
- default: ''
|
|
|
- }
|
|
|
-})
|
|
|
-//变量
|
|
|
-const weekdays = ['日', '一', '二', '三', '四', '五', '六'] //星期头
|
|
|
-const curDateData = ref([]) //设置的日期
|
|
|
-const datesDay = ref([]) //日期数据
|
|
|
-//选择年月的日期
|
|
|
-const selectedDate = ref({
|
|
|
- year: dayjs().get('year'),
|
|
|
- month: dayjs().get('month') +1
|
|
|
-})
|
|
|
-//选择的日期,年月日
|
|
|
-const choiceDate = ref({
|
|
|
- year: dayjs().get('year'),
|
|
|
- month: dayjs().get('month') +1,
|
|
|
- date: dayjs().get('date')
|
|
|
-})
|
|
|
-
|
|
|
-//处理设置的日期
|
|
|
-const setCurDateData = (dateData) => {
|
|
|
- let curData = [];
|
|
|
- for (let i = 0; i < dateData.length; i++) {
|
|
|
- let toDate = dayjs(dateData[i].date, "YYYY-MM-DD").format("YYYY-MM-DD");
|
|
|
- curData.push({
|
|
|
- year: dayjs(toDate).get('year'),
|
|
|
- month: dayjs(toDate).get('month')+1,
|
|
|
- date: dayjs(toDate).get('date')
|
|
|
- })
|
|
|
- }
|
|
|
- curDateData.value = curData;
|
|
|
-}
|
|
|
-
|
|
|
-//生成2010年 - 当前年
|
|
|
-const yearOptions = ref(getSelectedYear())
|
|
|
-function getSelectedYear() {
|
|
|
- const {year} = selectedDate.value
|
|
|
- let yearArr = [], yearNum = year - 2009;
|
|
|
- for (let i = 0; i < yearNum; i++) {
|
|
|
- yearArr.push({label: (2010 + i) + "年", value: 2010 + i})
|
|
|
- }
|
|
|
- yearArr.reverse()
|
|
|
- return yearArr
|
|
|
-}
|
|
|
-
|
|
|
-//生成1-12月
|
|
|
-const monthOptions = ref(getSelectedMonth())
|
|
|
-function getSelectedMonth() {
|
|
|
- let monthArr = [];
|
|
|
- for (let i = 0; i < 12; i++) {
|
|
|
- let m = i + 1;
|
|
|
- monthArr.push({label: m + "月", value: m})
|
|
|
- }
|
|
|
- return monthArr
|
|
|
-}
|
|
|
-
|
|
|
-let dateData = [
|
|
|
- {date: '2022-05-2'},
|
|
|
- {date: '2022-05-3'},
|
|
|
- {date: '2022-05-4'},
|
|
|
- {date: '2022-05-05'},
|
|
|
- {date: '2022-05-8'},
|
|
|
- {date: '2022-05-09'},
|
|
|
- {date: '2022-05-10'},
|
|
|
-]
|
|
|
-setCurDateData(dateData)
|
|
|
-
|
|
|
-//获取日期
|
|
|
-const getDatesDay = (year,month) => {
|
|
|
- if (year && month) {
|
|
|
- let toDate = dayjs(year + '-' + month, "YYYY-MM").format("YYYY-MM-DD"); //格式化一下先
|
|
|
- let days = momedayjsnt(toDate).daysInMonth(); //天数
|
|
|
- let monthOneDay = dayjs(toDate).startOf("month").format("YYYY-MM-DD"); //获取当月第一天
|
|
|
- let weekday = dayjs(monthOneDay, "YYYY-MM-DD").get('weekday'); //获取当月第一天是星期几
|
|
|
- setDatesDay(days,weekday)
|
|
|
- } else {
|
|
|
- let days = dayjs().daysInMonth(); //天数
|
|
|
- let monthOneDay = dayjs().startOf("month").format("YYYY-MM-DD"); //获取当月第一天
|
|
|
- let weekday = dayjs(monthOneDay, "YYYY-MM-DD").get('weekday'); //获取当月第一天是星期几
|
|
|
- setDatesDay(days,weekday)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-//设置日期
|
|
|
-const setDatesDay = (days,weekday) => {
|
|
|
- let datesDayData = [];
|
|
|
- const selected = selectedDate.value; //选择的日期
|
|
|
- const choiceData = choiceDate.value; //选择的日期
|
|
|
- const dayData = {
|
|
|
- year: dayjs().get('year'),
|
|
|
- month: dayjs().get('month') +1,
|
|
|
- date: dayjs().get('date')
|
|
|
- }
|
|
|
- //添加头部
|
|
|
- for (let i = 0; i < weekday; i++) {
|
|
|
- datesDayData.push({type: 'excluded', key: '-'})
|
|
|
- }
|
|
|
- //处理日期
|
|
|
- for (let i = 0; i < days; i++) {
|
|
|
- let day = i + 1, type = '';
|
|
|
- type = setCurDate(selected,day)
|
|
|
- if (selected.year === dayData.year && selected.month === dayData.month && dayData.date === day) {
|
|
|
- type += ' selected'
|
|
|
- }
|
|
|
- if (choiceData.year === selected.year && choiceData.month === selected.month && choiceData.date === day) {
|
|
|
- type += ' choice'
|
|
|
- }
|
|
|
- datesDayData.push({type: type, key: day})
|
|
|
- }
|
|
|
- //添加尾数
|
|
|
- let lastNum = 42 - (weekday + days);
|
|
|
- for (let i = 0; i < lastNum; i++) {
|
|
|
- datesDayData.push({type: 'excluded', key: '-'})
|
|
|
- }
|
|
|
- datesDay.value = datesDayData;
|
|
|
-}
|
|
|
-
|
|
|
-const setCurDate = (selected,day) => {
|
|
|
- const curDate = curDateData.value; //设置的日期
|
|
|
- for (let x = 0; x < curDate.length; x++) {
|
|
|
- if (selected.year === curDate[x].year && selected.month === curDate[x].month && curDate[x].date === day) {
|
|
|
- return 'cur'
|
|
|
- }
|
|
|
- }
|
|
|
- return ''
|
|
|
-}
|
|
|
-
|
|
|
-getDatesDay()
|
|
|
-
|
|
|
-//上一年
|
|
|
-const prevYearClick = () => {
|
|
|
- let {year,month} = selectedDate.value
|
|
|
- let years = year - 1;
|
|
|
- selectedDate.value.year = years;
|
|
|
- getDatesDay(years,month)
|
|
|
-}
|
|
|
-//上一月
|
|
|
-const prevMonthClick = () => {
|
|
|
- let {year,month} = selectedDate.value
|
|
|
- let years = year, months;
|
|
|
- if (month === 1) {
|
|
|
- years = year - 1;
|
|
|
- months = 12;
|
|
|
- } else {
|
|
|
- months = month - 1;
|
|
|
- }
|
|
|
- selectedDate.value = {year: years, month: months}
|
|
|
- getDatesDay(years,months)
|
|
|
-}
|
|
|
-//下一月
|
|
|
-const nextMonthClick = () => {
|
|
|
- let {year,month} = selectedDate.value
|
|
|
- let years = year, months;
|
|
|
- if (month === 12) {
|
|
|
- years = year + 1;
|
|
|
- months = 1;
|
|
|
- } else {
|
|
|
- months = month + 1;
|
|
|
- }
|
|
|
- selectedDate.value = {year: years, month: months}
|
|
|
- getDatesDay(years,months)
|
|
|
-}
|
|
|
-//下一年
|
|
|
-const nextYearClick = () => {
|
|
|
- let {year,month} = selectedDate.value
|
|
|
- let years = year + 1;
|
|
|
- selectedDate.value.year = years;
|
|
|
- getDatesDay(years,month)
|
|
|
-}
|
|
|
-//选择年份
|
|
|
-const popselectYear = () => {
|
|
|
- let {year,month} = selectedDate.value
|
|
|
- getDatesDay(year,month)
|
|
|
-}
|
|
|
-//选择月份
|
|
|
-const popselectMonth = () => {
|
|
|
- let {year,month} = selectedDate.value
|
|
|
- getDatesDay(year,month)
|
|
|
-}
|
|
|
-//天被点击
|
|
|
-const datesDayClick = (item) => {
|
|
|
- let {year,month} = selectedDate.value
|
|
|
- choiceDate.value = {year: year, month: month, date: item.key}
|
|
|
- getDatesDay(year,month)
|
|
|
- backChoiceDate()
|
|
|
-}
|
|
|
-
|
|
|
-const zpadStart = (val,leng,pad) => {
|
|
|
- val += ''
|
|
|
- while (val.length < leng) {
|
|
|
- val = pad + val
|
|
|
- }
|
|
|
- return val
|
|
|
-}
|
|
|
-
|
|
|
-//事件
|
|
|
-const emit = defineEmits(['choice-date'])
|
|
|
-
|
|
|
-//返回选择的日期
|
|
|
-const backChoiceDate = () => {
|
|
|
- //选择的日期处理
|
|
|
- const {year,month,date} = choiceDate.value
|
|
|
- let months = zpadStart(month, 2, '0')
|
|
|
- let dates = zpadStart(date, 2, '0')
|
|
|
- const choice = {year: year, month: months, date: dates}
|
|
|
- const choiceVal = year + months + dates
|
|
|
- //今天的日期处理
|
|
|
- const dayData = {
|
|
|
- year: dayjs().get('year'),
|
|
|
- month: zpadStart(dayjs().get('month') + 1, 2, '0'),
|
|
|
- date: zpadStart(dayjs().get('date'), 2, '0')
|
|
|
- }
|
|
|
- const today = dayData.year + dayData.month + dayData.date
|
|
|
- //判断处理
|
|
|
- if (choiceVal > today) {
|
|
|
- backToDay(false)
|
|
|
- } else {
|
|
|
- emit('choice-date', {date: choice, choice: choiceVal})
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-//回到今天
|
|
|
-const backToDay = (isEmit = true) => {
|
|
|
- const year = dayjs().get('year');
|
|
|
- const month = dayjs().get('month') + 1;
|
|
|
- const date = dayjs().get('date');
|
|
|
- selectedDate.value = {year: year, month: month}
|
|
|
- choiceDate.value = {year: year, month: month, date: date}
|
|
|
- getDatesDay(year,month)
|
|
|
- if (isEmit) {
|
|
|
- //今天的日期
|
|
|
- const today = {
|
|
|
- year: year,
|
|
|
- month: zpadStart(month, 2, '0'),
|
|
|
- date: zpadStart(date, 2, '0')
|
|
|
- }
|
|
|
- const choice = today.year + today.month + today.date
|
|
|
- emit('choice-date', {date: today, choice})
|
|
|
- }
|
|
|
-}
|
|
|
-</script>
|
|
|
-
|
|
|
-<style lang="scss" scoped>
|
|
|
-@import './style.scss';
|
|
|
-</style>
|
|
|
-
|
|
|
-<style lang="scss">
|
|
|
-.hc-date-month-year .n-date-picker {
|
|
|
- position: absolute;
|
|
|
- top: 0;
|
|
|
- width: 100%;
|
|
|
- height: 34.4px;
|
|
|
- .n-input {
|
|
|
- background: transparent;
|
|
|
- cursor: pointer;
|
|
|
- width: 100%;
|
|
|
- height: 34.4px;
|
|
|
- }
|
|
|
- .n-input .n-input-wrapper {
|
|
|
- padding: 0;
|
|
|
- display: block;
|
|
|
- }
|
|
|
- .n-input .n-input__input,
|
|
|
- .n-input .n-input__textarea,
|
|
|
- .n-input .n-input__suffix,
|
|
|
- .n-input .n-input__prefix,
|
|
|
- .n-input .n-input__border,
|
|
|
- .n-input .n-input__state-border{
|
|
|
- display: none;
|
|
|
- }
|
|
|
-}
|
|
|
-</style>
|