vant 日历改造上一月 ,下一月 , 农历 ,节气, 节日 ,月份联动

直接上成品

在这里插入图片描述

代码

<template>
  <div class="be-on-duty">
    <!--nav-->
    <van-nav-bar title="值班" left-text="返回" class="nav" :fixed="true">
      <template #left>
        <i class="icon-home" @click="goHome()"></i>
      </template>
    </van-nav-bar>
    <!--calendar-->
    <div class="calendar mb-10">
      <van-calendar
        ref="calendars"
        :poppable="false"
        :show-confirm="false"
        :default-date="defaultDate"
        :style="{ height: '450px' }"
        :formatter="formatter"
        :min-date="minDate"
        :max-date="maxDate"
      >
      </van-calendar>
      <van-icon name="arrow-left" @click="arrowLeft" />
      <van-button type="danger" @click="goNow">回今天</van-button>
      <van-icon name="arrow" @click="arrowRight" />
    </div>
    <!--remind-->
    <ul class="remind">
      <li
        class="mt-10 pt-10 pb-10"
        v-for="(item, index) in dutyList"
        :key="index"
      >
        <div class="remind-left">
          <span class="remind-left ml-15 mr-15">{{ item.dutyDateShow }}</span>
          <span>{{ item.dutyWeek }}</span>
        </div>
        <div
          class="remind-right"
          :style="{ color: item.dutyPeriod == '白班' ? '#F8960B' : '#027DB4' }"
        >
          <span class="mr-15"></span
          ><span class="mr-20">{{ item.dutyPeriod }}</span>
        </div>
      </li>
    </ul>
  </div>
</template>
 
<script>
import { queryDutySchedule } from "@/api/system/beOnDuty";
import Calendar from "@/utils/calendar.js";
export default {
  name: "beOnDuty",
  data() {
    return {
      minDate: new Date(),
      maxDate: new Date(),
      defaultDate: new Date(),
      dutyList: [],
      cont: 0,
      year: new Date().getFullYear(),
      month: new Date().getMonth(),
      nowDay: new Date().getDate(),
    };
  },
  created() {
    this.getNowDuty();
    this.setMinMaxDay();
  },
  methods: {
    // 返回首页
    goHome() {},
    // 回到今天
    goNow() {
      this.defaultDate = new Date();
      this.minDate = new Date();
      this.maxDate = new Date();
      this.cont = 0;
      this.setMinMaxDay();
      this.getNowDuty();
    },
    // 设置显示月份可选择的天数区间
    setMinMaxDay() {
      var firstDay = new Date(this.defaultDate);
      firstDay.setDate(1);
      this.minDate = new Date(
        this.year,
        this.month + this.cont,
        firstDay.getDate()
      );
      var endDate = new Date(this.defaultDate);
      endDate.setMonth(this.defaultDate.getMonth() + 1);
      endDate.setDate(0);
      this.maxDate = new Date(
        this.year,
        this.month + this.cont,
        endDate.getDate()
      );
    },
    // 当前月上一个月
    arrowLeft() {
      this.cont--;
      this.defaultDate = new Date(
        this.year,
        this.month + this.cont,
        this.nowDay
      );
      this.setMinMaxDay();
      this.getNowDuty();
    },
    // 当前月下一个月
    arrowRight() {
      this.cont++;
      this.defaultDate = new Date(
        this.year,
        this.month + this.cont,
        this.nowDay
      );
      this.setMinMaxDay();
      this.getNowDuty();
    },
    getNowDuty() {
      var y = this.defaultDate.getFullYear();
      var m = this.defaultDate.getMonth() + 1;
      m = m < 10 ? "0" + m : m;
      const obj = {
        month: y + "-" + m,
      };
      queryDutySchedule(obj).then((res) => {
        this.dutyList = res.data;
      });
    },
    // Vant日历日期添加法定节假日以及24节气
    formatter(day) {
      const _time = new Date(day.date);
      const y = _time.getFullYear();
      const m = _time.getMonth() + 1;
      const d = _time.getDate();
      const w = _time.getDay();
      const info = Calendar.solar2lunar(y, m, d);
      //   改变周六周日显示颜色
      if (w === 0 || w === 6) {
        day.className = "weekendRed";
      }
      var mm = m;
      mm = m < 10 ? "0" + m : m;
      var dd = d;
      dd = d < 10 ? "0" + d : d;
      var nowDate = y + "-" + mm + "-" + dd;
      this.dutyList.forEach((item) => {
        if (item.dutyDate == nowDate) {
          if (item.dutyPeriod == "白班") {
            day.topInfo = "白";
          } else if (item.dutyPeriod == "夜班") {
            day.topInfo = "夜";
          }
        }
      });
      //   优先级:节日>节气>农历
      if (info.festival != null && info.festival != "") {
        day.bottomInfo = info.festival;
      } else if (info.Term != null && info.Term != "") {
        day.bottomInfo = info.Term;
      } else if (info.IDayCn != null && info.IDayCn != "") {
        day.bottomInfo = info.IDayCn;
      }
      return day;
    },
  },
};
</script>
 
<style scoped lang="scss">
@import "@/assets/style/variables.scss";
::v-deep.be-on-duty {
  background-color: $bgcolor;
  .icon-home {
    display: block;
    width: 1.0625rem;
    height: 1.125rem;
    background: url(../../../assets/image/icon_home.png) no-repeat center;
    background-size: 100%;
  }
  .calendar {
    .van-button--danger {
      height: 1.475rem;
      padding: 0.3125rem;
      position: absolute;
      top: 3.4rem;
      right: 4rem;
    }
    .van-icon-arrow {
      position: absolute;
      top: 3.6rem;
      right: 1.2rem;
      font-size: 1.3rem;
    }
    .van-icon-arrow-left {
      position: absolute;
      top: 3.6rem;
      left: 1.2rem;
      font-size: 1.3rem;
    }
    .van-calendar {
      .van-calendar__header {
        .van-calendar__header-subtitle {
          font-size: 1rem;
        }
        .van-calendar__weekdays {
          :nth-child(1),
          :nth-child(7) {
            color: $mainTone;
          }
        }
      }
    }
    .van-calendar__body {
      .van-calendar__month {
        .van-calendar__days {
          .van-calendar__day {
            font-size: 1.125rem;
            .van-calendar__top-info {
              padding-right: 0.625rem;
              text-align: right;
            }
          }
          .weekendRed {
            color: $mainTone !important;
          }
        }
      }
    }
  }
  .remind {
    li {
      background-color: $bgcolor0;
      display: flex;
      justify-content: space-between;
      .remind-left,
      .remind-right {
        text-align: left;
      }
    }
  }
}
</style>
  • 10
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 27
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李泽举

如对你有帮助,那我就没白写

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值