Work-calendar
介紹 (底部附鏈接)
基于uni-calendar做的定制化開發,主要功能為工作日歷展示和提報組件
? 1.支持周計劃日報狀態展示且可配置
? 2.支持農歷展示配置,回到當日,月份切換
? 3.日歷,周報,日報等點擊事件封裝
? 4.支持休假展示
使用說明
1.下載插件到工程中,并在業務代碼中引入
import Calendar from '@/components/calendar-zhikuany/calendar.vue'
2.布局中引入插件,并配置所需要的展示信息
<template><view><calendar ref="calendar" :date="date" @monthSwitch="monthSwitch" @change="handleSelect"@weekPlanClick="weekPlanClick" @datePlanClick="datePlanClick" lunar :showDay="prop.showDay":showPlan="prop.showPlan" :showMonth="prop.showMonth" :needState="prop.needState" /></view>
</template>/*** Calendar 日歷* @property {String} date 自定義當前時間,默認為今天* @property {String} text.size 文字大小* @property {String} text.color 文字顏色* @property {Boolean} lunar 顯示農歷* @property {Boolean} showPlan 展示周計劃* @property {Boolean} showDay 展示日報* @property {String} startDate 日期選擇范圍-開始日期* @property {String} endDate 日期選擇范圍-結束日期* @property {Boolean} range 范圍選擇* @property {Boolean} showMonth 是否選擇月份為背景* @property {Boolean} needState 周計劃日報是否需要審核* @event {Function} change 日期改變,* @event {Function} monthSwitch 切換月份時觸發*/
3.設置本月周報日報假期數據
/*** 設置數據*/const setDate = (year : number, month : number) => {// 獲取當月第一天和最后一天// const firstDay = new Date(year, month, 1);// const lastDay = new Date(year, month + 1, 0); // 下個月第0天 = 本月最后一天uni.showLoading({title: ''})//仿照調用接口 設置工作日歷數據setTimeout(() => {uni.hideLoading()//測試周報和日報數據 為 9月12號date.value = '2025-09-12'calendar.value.setPlanList(planList)}, 1000)}
4.核心數據設置邏輯
/*** 獲取每周數據* @param {Object} dateData*/_getWeek(dateData) {const {year,month} = this.getDate(dateData)let firstDay = new Date(year, month - 1, 1).getDay()// 調整為周一作為第一天(0=周一,6=周日)let adjustedFirstDay = firstDay === 0 ? 6 : firstDay - 1;let currentDay = new Date(year, month, 0).getDate()let dates = {lastMonthDays: this._getLastMonthDays(adjustedFirstDay, this.getDate(dateData)), // 上個月末尾幾天currentMonthDys: this._currentMonthDys(currentDay, this.getDate(dateData)), // 本月天數nextMonthDays: [], // 下個月開始幾天weeks: []}let canlender = []const surplus = 42 - (dates.lastMonthDays.length + dates.currentMonthDys.length)dates.nextMonthDays = this._getNextMonthDays(surplus, this.getDate(dateData))canlender = canlender.concat(dates.lastMonthDays, dates.currentMonthDys, dates.nextMonthDays)this.canlender = canlenderlet weekTemp = {}let weekplan = {} //每周的周計劃// 拼接數組 上個月開始幾天 + 本月天數+ 下個月開始幾天for (let i = 0; i < canlender.length; i++) {//獲取本周的周計劃數據if (i % 7 === 0) {//本周開始日期const weekBeginDate = canlender[i].fullDate// console.log('開始',weekBeginDate)//獲取計劃狀態weekplan = {}for (let i = 0; i < this.planList.length; i++) {if (this.planList[i]) {const item = this.planList[i]// console.log(item.monday)if (item.beginDate == weekBeginDate) {weekplan = item// console.log('weekplan', weekplan)break}}}weekTemp[parseInt(i / 7)] = {weeks: new Array(7),planCode: weekplan.planCode,planId: weekplan.planId,attachIds: weekplan.attachIds,// fileUrl: weekplan.fileUrl,weekWorkPlan: weekplan.weekWorkPlan,workSummary: weekplan.workSummary,coordinateHelp: weekplan.coordinateHelp,dataState: this.isEmpty(weekplan.dataState) ? weekplan.dataState : 1,// dataState: weekplan.dataState //周計劃 狀態}}//每天的計劃狀態var dayPlan = {}switch (i % 7) {case 0: //周一// dayPlan.dayStatus = weekplan.mondayStatusdayPlan.dayStatus = this.isEmptyWithZero(weekplan.mondayStatus) ? weekplan.mondayStatus : 2dayPlan.isHoliday = weekplan.mondayIsHolidaybreak;case 1:// dayPlan.dayStatus = weekplan.tuesdayStatusdayPlan.dayStatus = this.isEmptyWithZero(weekplan.tuesdayStatus) ? weekplan.tuesdayStatus : 2dayPlan.isHoliday = weekplan.tuesdayIsHolidaybreak;case 2:// dayPlan.dayStatus = weekplan.wednesdayStatusdayPlan.dayStatus = this.isEmptyWithZero(weekplan.wednesdayStatus) ? weekplan.wednesdayStatus : 2dayPlan.isHoliday = weekplan.wednesdayIsHolidaybreak;case 3:// dayPlan.dayStatus = weekplan.thursdayStatusdayPlan.dayStatus = this.isEmptyWithZero(weekplan.thursdayStatus) ? weekplan.thursdayStatus : 2dayPlan.isHoliday = weekplan.thursdayIsHolidaybreak;case 4:// dayPlan.dayStatus = weekplan.fridayStatusdayPlan.dayStatus = this.isEmptyWithZero(weekplan.fridayStatus) ? weekplan.fridayStatus : 2dayPlan.isHoliday = weekplan.fridayIsHolidaybreak;case 5:// dayPlan.dayStatus = weekplan.saturdayStatusdayPlan.dayStatus = this.isEmptyWithZero(weekplan.saturdayStatus) ? weekplan.saturdayStatus : 2dayPlan.isHoliday = weekplan.saturdayIsHolidaybreak;case 6:// dayPlan.dayStatus = weekplan.sundayStatusdayPlan.dayStatus = this.isEmptyWithZero(weekplan.sundayStatus) ? weekplan.sundayStatus : 2dayPlan.isHoliday = weekplan.sundayIsHolidaybreak;}// console.log('swich',dayPlan)weekTemp[parseInt(i / 7)].weeks[i % 7] = {...canlender[i],...dayPlan}}this.weeks = weekTempconsole.log(this.weeks)}
完整示例
<template><view><calendar ref="calendar" :date="date" @monthSwitch="monthSwitch" @change="handleSelect"@weekPlanClick="weekPlanClick" @datePlanClick="datePlanClick" lunar :showDay="prop.showDay":showPlan="prop.showPlan" :showMonth="prop.showMonth" :needState="prop.needState" /></view>
</template><script setup lang="ts">import { ref, onMounted } from 'vue'import { onLoad, onShow } from '@dcloudio/uni-app'import { planList } from './data.js'import Calendar from '@/components/calendar-zhikuany/calendar.vue'/*** 本組件是基于uni-calendar(V1.5.5)之上增加了一些個性化功能點,如打卡日歷,日歷日程,日歷排班,打卡排班等功能* 小點的位置 point.position [1, 2, 3, 4, 5, 6] 紅點位置,[左上角, 右上角, 右下角, 左下角, 上中間, 下中間]* 小點的顏色 point.color 如 #000 #fff* 日期數字下方的自定義文本 info* text.size 自定義文本字體大小 如 12 14 16* text.color 自定義文本顏色 如 #000 #fff*/const calendar = ref({})const date = ref('')const query = defineProps<{ prop : string }>()const prop : any = ref({})//頁面加載onLoad(() => {//解析配置參數prop.value = JSON.parse(query.prop)})onShow(() => {initDate()})//獲取當月所有日期的數組 初始化數據const initDate = () => {const now = new Date();const year = now.getFullYear();const month = now.getMonth() + 1; // 月份從 0 開始(0=1月)setDate(year, month);}/*** 設置數據*/const setDate = (year : number, month : number) => {// 獲取當月第一天和最后一天// const firstDay = new Date(year, month, 1);// const lastDay = new Date(year, month + 1, 0); // 下個月第0天 = 本月最后一天uni.showLoading({title: ''})//仿照調用接口 設置工作日歷數據setTimeout(() => {uni.hideLoading()//測試周報和日報數據 為 9月12號date.value = '2025-09-12'calendar.value.setPlanList(planList)}, 1000)}/*** 選中*/const handleSelect = (e : any) => {console.log(e)if (e.extraInfo.date) {// do something}}//周計劃點擊const weekPlanClick = (item : any) => {console.log(item)if (item.dataState == 1 || item.dataState == 0) {//審核中或者審核通過uni.showToast({title: JSON.stringify(item),icon: 'none',duration: 2000})} else {if (!checkWeekRange(item.beginDate, item.endDate)) {uni.showToast({title: '只能提交當本周或下周的周計劃',icon: 'none'})return}uni.showToast({title: '跳轉周計劃填報頁面',icon: 'none',duration: 2000})}}/*** 判斷給定的日期范圍是否屬于本周或下周* @param {string} beginDate - 開始日期,格式為 "YYYY-MM-DD"* @param {string} endDate - 結束日期,格式為 "YYYY-MM-DD"* @returns {string} - 返回 "本周"、"下周" 或 "不在本周或下周"*/function checkWeekRange(beginDate : any, endDate : any) : Boolean {// 將字符串日期轉換為Date對象const begin = new Date(beginDate);const end = new Date(endDate);begin.setHours(0, 0, 0, 0)end.setHours(0, 0, 0, 0)console.log(begin + ' ' + end)// 獲取當前日期const today = new Date();today.setHours(0, 0, 0, 0); // 清除時間部分// 計算當前周的起始和結束日期(周一至周日)const currentWeekStart = new Date(today);const dayOfWeek = today.getDay(); // 0是周日,1是周一,...,6是周六const diffToMonday = dayOfWeek === 0 ? -6 : 1 - dayOfWeek; // 調整為周一作為一周的第一天currentWeekStart.setDate(today.getDate() + diffToMonday);currentWeekStart.setHours(0, 0, 0, 0);const currentWeekEnd = new Date(currentWeekStart);currentWeekEnd.setDate(currentWeekStart.getDate() + 6);// 計算下周的起始和結束日期const nextWeekStart = new Date(currentWeekStart);nextWeekStart.setDate(currentWeekStart.getDate() + 7);const nextWeekEnd = new Date(currentWeekEnd);nextWeekEnd.setDate(currentWeekEnd.getDate() + 7);console.log(currentWeekStart + ' ' + currentWeekEnd)// 檢查日期范圍是否完全在本周內const isCurrentWeek = begin >= currentWeekStart && end <= currentWeekEnd;console.log(nextWeekStart + ' ' + nextWeekEnd)// 檢查日期范圍是否完全在下周內const isNextWeek = begin >= nextWeekStart && end <= nextWeekEnd;// 檢查日期范圍是否跨越本周和下周(部分在本周,部分在下周)const isOverlapping = (begin <= currentWeekEnd && end >= nextWeekStart);if (isCurrentWeek) {console.log('本周')return true;} else if (isNextWeek || isOverlapping) {console.log('下周')return true;} else {console.log('非本周或下周')return false;}}//是否屬于當前周const isInRange = (bengin : any, end : any) => {const benginDate = new Date(bengin)const endDate = new Date(end)const currentDate = new Date()return currentDate >= benginDate && currentDate <= endDate}//日報點擊const datePlanClick = (item : any) => {console.log(item)//展示周計劃的時候需要校驗周計劃狀態if (prop.showPlan) {if (item.dataState == null || item.dataState == 'undefined') {uni.showToast({title: '請先提交周計劃',icon: 'none'})return}if (item.dataState == 0) {uni.showToast({title: '周計劃審核中...',icon: 'none'})return}}//審批中 已通過if (item.dayStatus == 1 || item.dayStatus == 2) {uni.showToast({title: '跳轉日報詳情頁面',icon: 'none',duration: 2000})} else {//未提交或者駁回if (!isDateInCurrentWeek(item.fullDate)) {uni.showToast({title: '只能提交本周內的日報',icon: 'none'})return}uni.showToast({title: '跳轉日報填寫頁面',icon: 'none',duration: 2000})}}/*** 判斷某個日期是否屬于本周* @param {Date|string} targetDate - 目標日期(Date 對象或 "YYYY-MM-DD" 字符串)* @returns {boolean} - 是否屬于本周*/function isDateInCurrentWeek(targetDate : any) : Boolean {// 如果傳入的是字符串,轉換為 Date 對象const date = typeof targetDate === 'string' ? new Date(targetDate) : new Date(targetDate);// 獲取當前日期const today = new Date();// 計算本周一的日期(以周一作為一周的第一天)const currentWeekMonday = new Date(today);currentWeekMonday.setDate(today.getDate() - today.getDay() + 1); // getDay() 周日是 0,周一是 1,...,周六是 6currentWeekMonday.setHours(0, 0, 0, 0); // 清除時間部分,確保比較的是日期// 計算本周日的日期const currentWeekSunday = new Date(currentWeekMonday);currentWeekSunday.setDate(currentWeekMonday.getDate() + 6);currentWeekSunday.setHours(23, 59, 59, 999); // 設置為周日最后一刻// 判斷目標日期是否在本周范圍內return date >= currentWeekMonday && date <= currentWeekSunday;}//月份切換const monthSwitch = (e : any) => {console.log(e)setDate(e.year, e.month)}//日期轉字符串格式const dateToStr = (date : any) => {var year = date.getFullYear() //年var month = date.getMonth() //月var day = date.getDate() //日var hours = date.getHours() //時var min = date.getMinutes() //分var second = date.getSeconds() //秒return year + "-" +((month + 1) > 9 ? (month + 1) : "0" + (month + 1)) + "-" +(day > 9 ? day : ("0" + day)) + " " +(hours > 9 ? hours : ("0" + hours)) + ":" +(min > 9 ? min : ("0" + min))}//字符串轉日期const strToDate = (str : string) => {var date = new Date(str)return date.getTime()}
</script><style></style>
周計劃日報數據示例
? 含用戶信息 ,周計劃信息和狀態,日報信息和狀態,是否休假
const planList = [{//計劃編碼和id"planId": 58,"planCode": "1015297758478274563",//本周開始 截止時間"beginDate": "2025-09-08","endDate": "2025-09-14","planPeriod": "2025-09-08~2025-09-14",//用戶信息"userCode": "1009761934881456147","username": "員工001","departCode": "951784785749401608","departName": "xxx總公司",//周一到周日狀態信息"monday": "2025-09-08","mondayStatus": 0,"tuesday": "2025-09-09","tuesdayStatus": 1,"wednesday": "2025-09-10","wednesdayStatus": 1,"thursday": "2025-09-11","thursdayStatus": 0,"friday": "2025-09-12","fridayStatus": 0,"saturday": "2025-09-13","saturdayStatus": 0,"sunday": "2025-09-14","sundayStatus": 0,//"weekWorkPlan": "工作計劃","workSummary": '跟最后工作總結',"coordinateHelp": "需要協調需幫助",//附件列表 字符串 逗號分隔"attachIds": "",//創建更新時間"gmtCreate": "2025-09-03 08:27:25","gmtModified": "2025-09-03 08:27:25",//周計劃狀態 "dataState": 0,//是否請假 周一到周五"mondayIsHoliday": null,"tuesdayIsHoliday": 1,"wednesdayIsHoliday": 0,"thursdayIsHoliday": null,"fridayIsHoliday": null,"saturdayIsHoliday": null,"sundayIsHoliday": null
}]
uniapp市場連接 工作日歷