Forráskód Böngészése

fix(clockIn,checkIn):打卡,考勤页数据错误显示

HMY 1 éve
szülő
commit
b392ac9a81
3 módosított fájl, 172 hozzáadás és 100 törlés
  1. 14 0
      api/mine.js
  2. 141 93
      pages/mine/checkIn/checkIn.vue
  3. 17 7
      pages/mine/clockIn/clockIn.vue

+ 14 - 0
api/mine.js

@@ -52,6 +52,20 @@ export function getMyTotalCount({
 		}
 	})
 }
+//获取月考勤数据
+export function getMyTotalMonthCount({staffId,now_date}) {
+	return request({
+		url: preUrl,
+		method: 'post',
+		data: {
+			serviceId: 'miniapp_getMyMonthCount',
+			params: {
+				staffId,
+				now_date
+			}
+		}
+	})
+}
 
 //考勤打卡
 export function createAttendance(params) {

+ 141 - 93
pages/mine/checkIn/checkIn.vue

@@ -137,7 +137,7 @@
 
 <script setup lang="ts">
 	import {ref,onMounted,reactive} from 'vue';
-	import {checkAttendance,getMyTotalCount,getMyQDQtAttendance} from '@/api/mine.js'
+	import {checkAttendance,getMyTotalCount,getMyQDQtAttendance,getMyTotalMonthCount} from '@/api/mine.js'
 	import {useUserStore} from '@/store/user.js';
 	const userStore = useUserStore();
 
@@ -237,9 +237,19 @@
 					return
 				}
 				const attList = res.returnParams.list;
-				
-				const time1 = attList.find(item => item.att_type_id === '1');
-				const time2 = attList.filter(item => item.att_type_id === '2').pop();
+			
+				const filteredRecord1 = attList.filter(item => item.att_type_id === '1');
+				const time1 = filteredRecord1.reduce((latest, current) => {
+					const latestTime = new Date(latest.att_time);
+					const currentTime = new Date(current.att_time);
+					return (currentTime < latestTime) ? current : latest;
+				});
+				const filteredRecord2 = attList.filter(item => item.att_type_id === '2');
+				const time2 = filteredRecord2.reduce((latest, current) => {
+					const latestTime = new Date(latest.att_time);
+					const currentTime = new Date(current.att_time);
+					return (currentTime > latestTime) ? current : latest;
+				});
 				if (time1 !== undefined) {
 					todayData.startTime = time1.att_time.split(' ')[1];
 				}
@@ -428,14 +438,16 @@
 				break;
 			case '月':
 				chartsType.value = 'column'; // 设置图表类型为柱状图
-				fetchMonthAttData().then(message => {
-					console.log('message', message);
-					showWeekAndMonth();
-					updateChart(message);
-				})
-					.catch(error => {
-						console.error(error);
-					});
+				fetchMonthAttData();
+				showWeekAndMonth();
+				// fetchMonthAttData().then(message => {
+				// 	console.log('message', message);
+				// 	showWeekAndMonth();
+				// 	updateChart(message);
+				// })
+				// 	.catch(error => {
+				// 		console.error(error);
+				// 	});
 				break;
 		}
 	};
@@ -474,6 +486,7 @@
 			}
 			getMyTotalCount(params).then(res => {
 				const myWeekAttData = res.returnParams;
+				
 				const mockData = {
 					series: [{
 						data: [
@@ -490,34 +503,34 @@
 							{
 								"name": "早退",
 								"value": myWeekAttData.type5,
-								"labelText": "迟到:" + myWeekAttData.type5 + "次"
+								"labelText": "早退:" + myWeekAttData.type5 + "次"
 							},
 							{
 								"name": "出勤",
-								"value": myWeekAttData.type0,
-								"labelText": "出勤:" + myWeekAttData.type0 + "次"
+								"value": myWeekAttData.attDays,
+								"labelText": "出勤:" + myWeekAttData.attDays + "次"
 							},
 							{
 								"name": "外出",
-								"value": myWeekAttData.type1,
-								"labelText": "外出:" + myWeekAttData.type1 + "次"
+								"value": myWeekAttData.type2,
+								"labelText": "外出:" + myWeekAttData.type2 + "次"
 							},
 							{
 								"name": "公休",
-								"value": myWeekAttData.type2,
-								"labelText": "公休:" + myWeekAttData.type2 + "次"
+								"value": myWeekAttData.type1,
+								"labelText": "公休:" + myWeekAttData.type1 + "次"
 							},
-							{
-								"name": "未排班",
-								"value": myWeekAttData.type3,
-								"labelText": "未排班:" + myWeekAttData.type3 + "次"
-							}
+							// {
+							// 	"name": "未排班",
+							// 	"value": myWeekAttData.type3,
+							// 	"labelText": "未排班:" + myWeekAttData.type3 + "次"
+							// }
 						]
 					}],
 				};
 				// const weekData = mockData.series[0].data;
-				attendanceCount.value = myWeekAttData.type1;
-				goOutCount.value = myWeekAttData.type1;
+				attendanceCount.value = myWeekAttData.attDays;
+				goOutCount.value = myWeekAttData.type2;
 				lateCount.value = myWeekAttData.type4;
 				leaveEarlyCount.value = myWeekAttData.type5;
 				absenteeismCount.value = myWeekAttData.type6;
@@ -531,75 +544,110 @@
 
 	// 获取月考勤记录
 	function fetchMonthAttData() {
-		return new Promise((resolve, reject) => {
-			const weekNodeList = [];// 存储近月每周的周一,周天日期
-			weekNodeList.unshift(thisMondayDate.value, todayData.day);
-			while (weekNodeList.length !== 8) {
-				const weekNode = getOneDaysAgo(weekNodeList[0]);
-				weekNodeList.unshift(weekNode);
-				weekNodeList.unshift(getSixDaysAgo(weekNode));
-			}
-
-			const data1 = [];
-			const data2 = [];
-			const data3 = [];
-			const dataFetchPromises = []; // 存储所有数据请求的promise
-
-			for (let i = 0; i < weekNodeList.length - 1; i += 2) {
-				const params = {
-					staffId: userId,
-					start_date: weekNodeList[i],
-					end_date: weekNodeList[i + 1]
-				};
-				const fetchPromise = getMyTotalCount(params).then(res => {
-					// for (const type in res.returnParams) {
-					// 	console.log('type', type);
-					// }
-					console.log('res.returnParams', res.returnParams);
-
-					data1.push(res.returnParams.type6);
-					data2.push(res.returnParams.type4);
-					data3.push(res.returnParams.type5);
-				});
-				dataFetchPromises.push(fetchPromise);
-			}
-
-			// 等待所有数据请求完成
-			Promise.all(dataFetchPromises).then(() => {
-				const params = {
-					staffId: userId,
-					start_date: weekNodeList[0],
-					end_date: weekNodeList[weekNodeList.length - 1]
-				};
-				return getMyTotalCount(params);
-			}).then(res => {
-				const myMonthAttData = res.returnParams;
-				attendanceCount.value = myMonthAttData.type0;
-				goOutCount.value = myMonthAttData.type2;
-				lateCount.value = myMonthAttData.type4;
-				leaveEarlyCount.value = myMonthAttData.type5;
-				absenteeismCount.value = myMonthAttData.type6;
-
-				const mockData = {
-					categories: ["第1周", "第2周", "第3周", "第4周"],
-					series: [{
-						name: "缺勤",
-						data: data1
-					}, {
-						name: "迟到",
-						data: data2
-					}, {
-						name: "早退",
-						data: data3
-					}]
-				};
-				// console.log(data1); // 打印data1检查内容
-				resolve(mockData);
-			}).catch(error => {
-				reject(error);
+		//数据初始化
+		attendanceCount.value = 0;
+		goOutCount.value = 0;
+		lateCount.value = 0;
+		leaveEarlyCount.value = 0;
+		absenteeismCount.value = 0;
+		const params = {
+			staffId: userId,
+			now_date: todayData.day
+		}
+		getMyTotalMonthCount(params).then(res=>{
+			const myMonthAttData = res.returnParams;
+			let data1 = []; // 收集 type6 的值
+			let data2 = []; // 收集 type4 的值
+			let data3 = []; // 收集 type5 的值
+			console.log('attendanceCount: ',attendanceCount.value);
+			// 遍历 myMonthAttData 并收集指定类型的值
+			myMonthAttData.forEach(item => {
+			    data1.push(item.type6);
+			    data2.push(item.type4);
+			    data3.push(item.type5);
+				attendanceCount.value += item.attDays;
+				goOutCount.value += item.type2;
+				lateCount.value += item.type4;
+				leaveEarlyCount.value += item.type5;
+				absenteeismCount.value += item.type6;
+				// console.log('attendanceCount: ',attendanceCount.value,typeof myMonthAttData.attDays );
 			});
-		});
+			console.log('data1',data1);
+			
+			// 定义一个常量数组生成器
+			const generateWeekArray = n => Array.from({ length: n }, (_, i) => `第${i + 1}周`);
+			let categoriesArr=generateWeekArray(4)
+			console.log(categoriesArr);
+			const mockData = {
+				categories:  categoriesArr,
+				series: [{
+					name: "缺勤",
+					data: data1
+				}, {
+					name: "迟到",
+					data: data2
+				}, {
+					name: "早退",
+					data: data3
+				}]
+			};
+			updateChart(mockData)
+		})
+		
+		// return new Promise((resolve, reject) => {
+		// 	const weekNodeList = [];// 存储近月每周的周一,周天日期
+		// 	weekNodeList.unshift(thisMondayDate.value, todayData.day);
+		// 	while (weekNodeList.length !== 8) {
+		// 		const weekNode = getOneDaysAgo(weekNodeList[0]);
+		// 		weekNodeList.unshift(weekNode);
+		// 		weekNodeList.unshift(getSixDaysAgo(weekNode));
+		// 	}
+
+		// 	const data1 = [];
+		// 	const data2 = [];
+		// 	const data3 = [];
+		// 	const dataFetchPromises = []; // 存储所有数据请求的promise
+
+		// 	for (let i = 0; i < weekNodeList.length - 1; i += 2) {
+		// 		const params = {
+		// 			staffId: userId,
+		// 			start_date: weekNodeList[i],
+		// 			end_date: weekNodeList[i + 1]
+		// 		};
+		// 		const fetchPromise = getMyTotalCount(params).then(res => {
+		// 			// for (const type in res.returnParams) {
+		// 			// 	console.log('type', type);
+		// 			// }
+		// 			console.log('res.returnParams', res.returnParams);
+
+		// 			data1.push(res.returnParams.type6);
+		// 			data2.push(res.returnParams.type4);
+		// 			data3.push(res.returnParams.type5);
+		// 		});
+		// 		dataFetchPromises.push(fetchPromise);
+		// 	}
+
+		// 	// 等待所有数据请求完成
+		// 	Promise.all(dataFetchPromises).then(() => {
+		// 		const params = {
+		// 			staffId: userId,
+		// 			start_date: weekNodeList[0],
+		// 			end_date: weekNodeList[weekNodeList.length - 1]
+		// 		};
+		// 		return getMyTotalCount(params);
+		// 	}).then(res => {
+		// 		const myMonthAttData = res.returnParams;
+				
+
+				
+		// 		// console.log(data1); // 打印data1检查内容
+		// 		resolve(mockData);
+		// 	}).catch(error => {
+		// 		reject(error);
+		// 	});
+		// });
 	}
+	
 
 
 	//获取六天前的日期

+ 17 - 7
pages/mine/clockIn/clockIn.vue

@@ -122,10 +122,19 @@
 			if ("success" == res.returnMsg) {
 				// console.log("kaoqin",res);
 				const attList = res.returnParams.list;
-				const time1 = attList.find(item => item.att_type_id === '1');
-				//拿到所有签退数据后,用pop取最后一个
-				const time2 = attList.filter(item => item.att_type_id === '2').pop();
-				console.log('time1', time1);
+				const filteredRecord1 = attList.filter(item => item.att_type_id === '1');
+				const time1 = filteredRecord1.reduce((latest, current) => {
+					const latestTime = new Date(latest.att_time);
+					const currentTime = new Date(current.att_time);
+					return (currentTime < latestTime) ? current : latest;
+				});
+				//拿到所有签退数据后,取最大时间的那个
+				const filteredRecord2 = attList.filter(item => item.att_type_id === '2');
+				const time2 = filteredRecord2.reduce((latest, current) => {
+					const latestTime = new Date(latest.att_time);
+					const currentTime = new Date(current.att_time);
+					return (currentTime > latestTime) ? current : latest;
+				});
 				if (time1 !== undefined) {
 					// console.log('getTime1',time1);
 					signInTime.value = time1.att_time.split(' ')[1];
@@ -137,9 +146,9 @@
 						signInStatusName.value = '旷工';
 						isSignInStatusDisabled.value = false;
 					}
-				}else if (new Date().getHours()>=12) {
+				} else if (new Date().getHours() >= 12) {
 					signName.value = '下班签退'
-				}  else {
+				} else {
 					signName.value = '上班签到';
 				}
 				if (time2 !== undefined) {
@@ -162,6 +171,7 @@
 
 
 
+
 	//上班卡或下班卡
 	function signInOrOut() {
 		if ('上班签到' == signName.value) {
@@ -450,7 +460,7 @@
 					$tab.navigateTo('/pages/mine/checkIn/checkIn')
 				})
 		}
-		
+
 	}
 
 	//判断是否已经打卡