attendanceRecord.vue 10.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
<template>
  <view class="attendance-page">
    <!-- 顶部统计卡片 -->
    <view class="stat-card">
      <view class="stat-item">
        <view class="stat-label">本月打卡</view>
        <view class="stat-value">{{ statData.checkInDays }}</view>
      </view>
      <view class="stat-item">
        <view class="stat-label">正常天数</view>
        <view class="stat-value">{{ statData.normalDays }}</view>
      </view>
      <view class="stat-item">
        <view class="stat-label">异常天数</view>
        <view class="stat-value">{{ statData.errorDays }}</view>
      </view>
    </view>
    <!-- 手写内联日历,无弹窗 -->
    <view class="calendar-wrap">
      <view class="calendar-header">
        <!-- 左右横向排列:上一月 | 年月标题 | 下一月 -->
        <view class="calendar-btn" @click="prevMonth">上一月</view>
        <view class="calendar-title">{{ currentYear }}年{{ currentMonth }}月</view>
        <!-- 当前是最新月份时,隐藏下个月按钮 -->
        <view v-if="canNext" class="calendar-btn" @click="nextMonth">下一月</view>
        <view v-else class="calendar-btn empty-btn"></view>
      </view>
      <!-- 星期头部 -->
      <view class="week-row">
        <view class="week-item" v-for="w in weekList" :key="w">{{ w }}</view>
      </view>
      <!-- 日期网格 -->
      <view class="day-row" v-for="(row, rIdx) in dayRows" :key="rIdx">
        <view class="day-item" v-for="(day, dIdx) in row" :key="dIdx" :class="{
          'day-item--other': day.otherMonth,
          'day-item--active': day.dateStr === selectDate,
        }" @click="onDayClick(day)">
          <view class="day-text">{{ day.day }}</view>
          <view v-if="day.dot" class="day-dot" :style="{ backgroundColor: day.dotColor }"></view>
        </view>
      </view>
    </view>
    <!-- 打卡详情区域 -->
    <view class="detail-card">
      <view v-if="!currentAttendance">
        <view class="empty-text">暂无考勤记录</view>
      </view>
      <view v-else>
        <view class="detail-row">
          <view class="detail-title">上班打卡时间:</view>
          <view class="detail-text">{{ currentAttendance.onDutyTime || '缺少打卡记录' }}</view>
        </view>
        <view class="detail-row">
          <view class="detail-title">上班打卡地点:</view>
          <view class="detail-text">{{ currentAttendance.onDutyAddress || '-' }}</view>
        </view>
        <view class="divider"></view>
        <view class="detail-row">
          <view class="detail-title">下班打卡时间:</view>
          <view class="detail-text">{{ currentAttendance.offDutyTime || '缺少打卡记录' }}</view>
        </view>
        <view class="detail-row">
          <view class="detail-title">下班打卡地点:</view>
          <view class="detail-text">{{ currentAttendance.offDutyAddress || '-' }}</view>
        </view>
        <!-- 可选:展示异常原因 -->
        <view v-if="currentAttendance.abnormalReason" class="detail-row">
          <view class="detail-title">异常原因:</view>
          <view class="detail-text" style="color:#f56c6c">{{ currentAttendance.abnormalReason }}</view>
        </view>
      </view>
    </view>
    </view>
</template>
<script setup lang="ts">
import { ref, onMounted, computed, watch } from 'vue';
import { attendanceDaily, dayDetail } from '@/api/user'
const weekList = ['日', '一', '二', '三', '四', '五', '六'];
const today = new Date();
const todayTs = today.getTime();
// 当前显示月份
const currentYear = ref(today.getFullYear());
const currentMonth = ref(today.getMonth() + 1);
const selectDate = ref('');
const dayRows = ref<any[]>([]);
// 是否允许点击下一月:判断当前展示月份是否小于本月
const canNext = computed(() => {
  const showMonthDate = new Date(currentYear.value, currentMonth.value - 1, 1);
  const nowMonthDate = new Date(today.getFullYear(), today.getMonth(), 1);
  return showMonthDate < nowMonthDate;
});

// 统计数据
const statData = ref({
  checkInDays: 0,
  normalDays: 0,
  errorDays: 0
});
// 当月考勤打点字典 {dateStr: {dot:string, abnormalReason:string}}
const monthDayMap = ref<Record<string, { dot: string | null, abnormalReason: string | null }>>({});

// 选中日期详情
const currentAttendance = ref<any>(null);

// 拉取月度考勤汇总(stat + 当月日期打点)
const getMonthAttendance = async () => {
  try {
    const month = `${currentYear.value}-${String(currentMonth.value).padStart(2, '0')}`;
    const res = await attendanceDaily({ month });
    // 赋值顶部统计
    statData.value.checkInDays = res.stat.clockDays;
    statData.value.normalDays = res.stat.normalDays;
    statData.value.errorDays = res.stat.abnormalDays;
    // 构建日期映射字典
    const map: Record<string, { dot: string | null, abnormalReason: string | null }> = {};
    res.days.forEach((item: any) => {
      map[item.date] = {
        dot: item.dot,
        abnormalReason: item.abnormalReason
      };
    })
    monthDayMap.value = map;
    // 重新渲染日历
    renderMonth();
  } catch (err) {
    console.error('获取月度考勤失败', err);
    uni.showToast({ title: '加载月度考勤失败', icon: 'none' });
  }
};

// 生成月份日期网格
const renderMonth = () => {
  const year = currentYear.value;
  const month = currentMonth.value;
  // 当月第一天
  const firstDay = new Date(year, month - 1, 1);
  // 当月最后一天
  const lastDay = new Date(year, month, 0);
  // 上个月最后一天
  const prevLastDay = new Date(year, month - 1, 0).getDate();
  // 当月第一天星期 0周日~6周六
  const firstWeek = firstDay.getDay();
  const rows: any[] = [];
  let row: any[] = [];
  // 填充上月尾部
  for (let i = 0; i < firstWeek; i++) {
    const dayNum = prevLastDay - firstWeek + i + 1;
    row.push({
      day: dayNum,
      otherMonth: true,
      dateStr: `${year}-${String(month - 1).padStart(2, '0')}-${String(dayNum).padStart(2, '0')}`
    })
  }
  // 填充当月日期
  for (let d = 1; d <= lastDay.getDate(); d++) {
    const dateStr = `${year}-${String(month).padStart(2, '0')}-${String(d).padStart(2, '0')}`;
    const mark = monthDayMap.value[dateStr];
    const item: any = {
      day: d,
      otherMonth: false,
      dateStr,
      dot: false,
      dotColor: ''
    };
    if (mark?.dot) {
      item.dot = true;
      if (mark.dot === 'abnormal') {
        item.dotColor = '#f56c6c';
      } else if (mark.dot === 'partial') {
        item.dotColor = '#cccccc';
      }
    }
    row.push(item);
    if (row.length === 7) {
      rows.push(row);
      row = [];
    }
  }
  // 填充下月开头
  let nextDay = 1;
  while (row.length < 7) {
    row.push({
      day: nextDay,
      otherMonth: true,
      dateStr: `${year}-${String(month + 1).padStart(2, '0')}-${String(nextDay).padStart(2, '0')}`
    })
    nextDay++;
  }
  rows.push(row);
  dayRows.value = rows;
};

// 上一月
const prevMonth = () => {
  if (currentMonth.value === 1) {
    currentYear.value--;
    currentMonth.value = 12;
  } else {
    currentMonth.value--;
  }
  getMonthAttendance();
};
// 下一月
const nextMonth = () => {
  let nextY = currentYear.value;
  let nextM = currentMonth.value + 1;
  if (nextM > 12) {
    nextY++;
    nextM = 1;
  }
  currentYear.value = nextY;
  currentMonth.value = nextM;
  getMonthAttendance();
};

// 点击日期:获取当日详情
const onDayClick = async (day: any) => {
  // 禁止点击其他月份
  if (day.otherMonth) return;
  const selectDay = new Date(day.dateStr);
  if (selectDay > today) {
    currentAttendance.value = null;
    selectDate.value = day.dateStr;
    return;
  }
  selectDate.value = day.dateStr;
  try {
    const res = await dayDetail({ date: day.dateStr });
    // 取第一条记录,大多数场景单条记录
    const record = res.records?.[0] ?? null;
    if (record) {
      currentAttendance.value = {
        ...record,
        abnormalReason: res.abnormalReason
      };
    } else {
      currentAttendance.value = null;
    }
  } catch (err) {
    console.error('获取当日考勤详情失败', err);
    uni.showToast({ title: '加载当日考勤失败', icon: 'none' });
    currentAttendance.value = null;
  }
};

// 页面初始化
onMounted(async () => {
  const todayStr = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`;
  selectDate.value = todayStr;
  await getMonthAttendance();
  // 默认选中今天,加载当日详情
  await onDayClick({ dateStr: todayStr, otherMonth: false });
});
</script>
<style scoped lang="scss">
.attendance-page {
  padding: 24rpx;
  background-color: #F4F6FA;
  min-height: 100vh;
  box-sizing: border-box;
}

.stat-card {
  display: flex;
  background-color: #2563EB;
  border-radius: 16rpx;
  padding: 30rpx 0;
  margin-bottom: 24rpx;
  box-shadow: 0 4rpx 16rpx rgba(17, 24, 39, 0.06);

  .stat-item {
    flex: 1;
    text-align: center;
    color: #fff;

    .stat-label {
      font-size: 28rpx;
      opacity: 0.9;
    }

    .stat-value {
      font-size: 36rpx;
      margin-top: 10rpx;
      font-weight: bold;
    }
  }
}

.calendar-wrap {
  background-color: #ffffff;
  border-radius: 16rpx;
  padding: 24rpx;
  margin-bottom: 24rpx;
  box-shadow: 0 4rpx 16rpx rgba(17, 24, 39, 0.06);
}

.calendar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}

.calendar-title {
  font-size: 30rpx;
  font-weight: bold;
}

.calendar-btn {
  padding: 8rpx 16rpx;
  border: 1rpx solid #eee;
  border-radius: 8rpx;
  font-size: 28rpx;
}

.empty-btn {
  border-color: transparent;
  color: transparent;
}

.week-row {
  display: flex;

  .week-item {
    flex: 1;
    text-align: center;
    font-size: 28rpx;
    color: #606266;
    padding: 10rpx 0;
  }
}

.day-row {
  display: flex;

  .day-item {
    flex: 1;
    min-height: 70rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 8rpx 0;
    position: relative;

    .day-text {
      font-size: 28rpx;
    }

    .day-dot {
      position: absolute;
      bottom: 8rpx;
      width: 10rpx;
      height: 10rpx;
      border-radius: 50%;
    }
  }

  .day-item--other {
    color: #ccc;
  }

  .day-item--active {
    color: #f00;
  }
}

.detail-card {
  background-color: #ffffff;
  border-radius: 16rpx;
  padding: 28rpx;
  box-shadow: 0 4rpx 16rpx rgba(17, 24, 39, 0.06);

  .empty-text {
    text-align: center;
    color: #909399;
    font-size: 30rpx;
    padding: 40rpx 0;
  }

  .detail-row {
    margin-bottom: 20rpx;
    display: flex;

    .detail-title {
      font-size: 30rpx;
      color: #1a1a1a;
      width: 220rpx;
    }

    .detail-text {
      flex: 1;
      font-size: 30rpx;
      color: #606266;
    }
  }

  .divider {
    height: 1px;
    background: #eee;
    margin: 30rpx 0;
  }
}
</style>