wode-jihua-paike.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 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
<template>
  <view class="paike-container">
    <!-- 1. 自定义顶部导航栏 (uview-plus) -->
    <u-navbar
      title="排课日历"
      :auto-back="true"
      placeholder
      bg-color="#121212"
      title-style="color: #FFFFFF; font-size: 34rpx; font-weight: 600;"
      left-icon-color="#FFFFFF"
    />

    <!-- 2. 星期吸顶头部 -->
    <view class="week-header-sticky">
      <view class="week-grid">
        <view v-for="item in WEEK_TEXTS" :key="item" class="week-cell">
          {{ item }}
        </view>
      </view>
    </view>

    <!-- 3. 加载中状态 -->
    <view v-if="loading" class="state-box">
      <u-loading-icon
        mode="semicircle"
        color="#E9EE50"
        text="正在加载排课数据..."
        text-color="#999999"
        vertical
      />
    </view>

    <!-- 4. 日历主体列表 -->
    <view v-else-if="monthList.length > 0" class="calendar-content">
      <view
        v-for="monthItem in monthList"
        :key="`${monthItem.year}-${monthItem.month}`"
        class="month-card"
      >
        <!-- 月份标题 -->
        <view class="month-header">
          <text class="month-title">{{ monthItem.year }}年 {{ monthItem.month }}月</text>
        </view>

        <!-- 日期 7 列网格 -->
        <view class="date-grid">
          <!-- 开头空白填充格 -->
          <view
            v-for="emptyIdx in monthItem.emptyCount"
            :key="`empty-${emptyIdx}`"
            class="date-cell cell-empty"
          />

          <!-- 有效日期格 -->
          <view
            v-for="day in monthItem.dayList"
            :key="`${day.year}-${day.month}-${day.day}`"
            class="date-cell"
            :class="{ 'has-schedule': day.trainName }"
            @tap="handleDateClick(day)"
          >
            <!-- 日期数 -->
            <text class="day-number">{{ day.day }}</text>

            <!-- 排课标签/未排课点位 -->
            <view class="schedule-box">
              <view v-if="day.trainName" class="schedule-label">
                {{ day.trainName }}
              </view>
              <view v-else class="schedule-placeholder">
                <view class="dot" />
              </view>
            </view>
          </view>
        </view>
      </view>

      <!-- 底部提示 -->
      <view class="bottom-tip">
        <text>最多仅可设置到未来 {{ MONTHS_AHEAD }} 个月哦~</text>
      </view>
    </view>

    <!-- 5. 空数据状态 -->
    <view v-else class="state-box">
      <u-empty mode="data" text="暂无排课计划数据" icon-size="160rpx" />
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import QueryPlanApi from '@/sheep/api/plan/queryplan'

// ==================== 常量定义 ====================
const WEEK_TEXTS = ['一', '二', '三', '四', '五', '六', '日']
const MONTHS_AHEAD = 2 // 最多展示未来月份数(当前月 + 2个月)

// ==================== 响应式状态 ====================
const planId = ref(0)
const loading = ref(true)
const monthList = ref([])

// ==================== 生命周期 ====================
onLoad((options) => {
  planId.value = Number(options?.planid) || 0
})

onMounted(() => {
  fetchPlanCalendar()
  uni.$on('calendarDataRefresh', fetchPlanCalendar)
})

onUnmounted(() => {
  uni.$off('calendarDataRefresh', fetchPlanCalendar)
})

// ==================== 工具函数(数据解耦) ====================

/**
 * 根据基础日期与月份偏移量,计算目标的年、月
 */
const getTargetYearMonth = (baseDate, offsetMonth) => {
  const d = new Date(baseDate.getFullYear(), baseDate.getMonth() + offsetMonth, 1)
  return {
    year: d.getFullYear(),
    month: d.getMonth() + 1,
  }
}

/**
 * 计算某月某日是周几,并转换为“以周一为每周第一天”的前置空白格数量
 */
const calcEmptySlots = (year, month, day = 1) => {
  const dayOfWeek = new Date(year, month - 1, day).getDay()
  const isoWeekDay = dayOfWeek === 0 ? 7 : dayOfWeek // 周日转为7
  return isoWeekDay - 1
}

/**
 * 将后端排课接口数组构建成映射 Map,Key: `${year}-${month}`, Value: { [day]: { trainName, DailyTemplateId } }
 */
const buildTrainMap = (rawList = []) => {
  const map = {}
  if (!Array.isArray(rawList)) return map

  rawList.forEach((item) => {
    if (!item?.trainingDate || !Array.isArray(item.trainingDate)) return
    const [y, m, d] = item.trainingDate
    const template = item.templates?.[0]
    const key = `${y}-${m}`

    if (!map[key]) map[key] = {}
    map[key][d] = {
      trainName: template?.templateName || '',
      DailyTemplateId: template?.id || null,
    }
  })
  return map
}

/**
 * 生成多月份日历完整渲染结构
 */
const generateCalendarList = (apiData) => {
  const now = new Date()
  const today = now.getDate()
  const trainMap = buildTrainMap(apiData)
  const result = []

  for (let i = 0; i <= MONTHS_AHEAD; i++) {
    const { year, month } = getTargetYearMonth(now, i)
    const isCurrentMonth = i === 0
    const startDay = isCurrentMonth ? today : 1
    const totalDaysInMonth = new Date(year, month, 0).getDate()
    const mapKey = `${year}-${month}`

    const dayList = []
    for (let d = startDay; d <= totalDaysInMonth; d++) {
      const scheduleInfo = trainMap[mapKey]?.[d] || {}
      dayList.push({
        year,
        month,
        day: d,
        trainName: scheduleInfo.trainName || '',
        DailyTemplateId: scheduleInfo.DailyTemplateId || null,
      })
    }

    result.push({
      year,
      month,
      emptyCount: calcEmptySlots(year, month, startDay),
      dayList,
    })
  }

  return result
}

// ==================== 接口交互 & 事件处理 ====================

/** 加载排课数据 */
const fetchPlanCalendar = async () => {
  if (!planId.value) {
    loading.value = false
    return
  }

  loading.value = true
  try {
    const res = await QueryPlanApi.getPlanArrangeCalendar(planId.value)
    // 根据后端统一响应结构兜底处理
    const data = res?.data || res
    monthList.value = generateCalendarList(Array.isArray(data) ? data : [])
  } catch (err) {
    console.error('[PlanCalendar] 获取排课日历失败:', err)
    uni.showToast({ title: '加载排课失败', icon: 'none' })
  } finally {
    loading.value = false
  }
}

/** 日期卡片点击事件 */
const handleDateClick = (dayItem) => {
  const { DailyTemplateId, year, month, day } = dayItem
  if (!DailyTemplateId) return

  const formattedMonth = String(month).padStart(2, '0')
  const formattedDay = String(day).padStart(2, '0')
  const dateStr = `${year}-${formattedMonth}-${formattedDay}`

  uni.navigateTo({
    url: `/pages4/pages/xunji/xunji-moban-xiangqing?id=${DailyTemplateId}&date=${dateStr}&isOffice=false&isMyPlan=true&isDailytemplateId=true`,
  })
}
</script>

<style scoped lang="scss">
// ==================== 主题色系与变量 ====================
$bg-main: #121212;
$bg-card: #1e1e1e;
$bg-cell: #262626;

$text-primary: #ffffff;
$text-secondary: #8c8c8c;

$theme-yellow: #e9ee50;
$theme-yellow-light: rgba(233, 238, 80, 0.15);
$theme-yellow-border: rgba(233, 238, 80, 0.35);

$cell-height: 116rpx;

// ==================== 容器根样式 ====================
.paike-container {
  min-height: 100vh;
  background-color: $bg-main;
  padding-bottom: calc(40rpx + env(safe-area-inset-bottom));
  box-sizing: border-box;
}

// ==================== 星期头吸顶 ====================
.week-header-sticky {
  position: sticky;
  /* #ifdef H5 */
  top: 44px;
  /* #endif */
  /* #ifndef H5 */
  top: 0;
  /* #endif */
  z-index: 10;
  background-color: $bg-main;
  padding: 20rpx 16rpx 12rpx;
  border-bottom: 1rpx solid rgba(255, 255, 255, 0.06);

  .week-grid {
    display: grid;
    grid-template-columns: repeat(7, 1fr);

    .week-cell {
      text-align: center;
      font-size: 24rpx;
      font-weight: 500;
      color: $text-secondary;
    }
  }
}

// ==================== 状态容器 (Loading/Empty) ====================
.state-box {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 500rpx;
}

// ==================== 日历主体 ====================
.calendar-content {
  padding: 20rpx 16rpx 0;
}

.month-card {
  margin-bottom: 32rpx;
  background: $bg-card;
  border-radius: 24rpx;
  padding: 20rpx 12rpx 16rpx;
  border: 1rpx solid rgba(255, 255, 255, 0.05);
  box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.3);

  .month-header {
    text-align: center;
    padding-bottom: 20rpx;

    .month-title {
      font-size: 30rpx;
      font-weight: 600;
      color: $text-primary;
      letter-spacing: 1rpx;
    }
  }

  .date-grid {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 8rpx;
  }

  .date-cell {
    height: $cell-height;
    background-color: $bg-cell;
    border-radius: 12rpx;
    padding: 8rpx 6rpx;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    transition: all 0.2s ease;

    /* 点击效果 */
    &:active {
      transform: scale(0.95);
      opacity: 0.8;
    }

    /* 空白填补格 */
    &.cell-empty {
      background-color: transparent !important;
      pointer-events: none;
    }

    /* 有排课状态 */
    &.has-schedule {
      background-color: $theme-yellow-light;
      border: 1rpx solid $theme-yellow-border;

      .day-number {
        color: $theme-yellow;
        font-weight: 700;
      }
    }

    .day-number {
      font-size: 26rpx;
      color: $text-primary;
      font-weight: 500;
      line-height: 1;
    }

    /* 排课内容展示区 */
    .schedule-box {
      width: 100%;
      height: 56rpx;
      display: flex;
      align-items: center;
      justify-content: center;

      .schedule-label {
        width: 100%;
        height: 100%;
        background-color: $theme-yellow;
        color: #000000;
        font-size: 20rpx;
        font-weight: 600;
        border-radius: 6rpx;
        padding: 4rpx;
        box-sizing: border-box;
        text-align: center;
        line-height: 1.25;

        /* 超出两行省略 */
        display: -webkit-box;
        -webkit-line-clamp: 2;
        line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        word-break: break-all;
      }

      /* 无排课时的精致小点,替代原本突兀的大灰块 */
      .schedule-placeholder {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 100%;

        .dot {
          width: 8rpx;
          height: 8rpx;
          border-radius: 50%;
          background-color: rgba(255, 255, 255, 0.15);
        }
      }
    }
  }
}

// ==================== 底部提示 ====================
.bottom-tip {
  text-align: center;
  padding: 24rpx 0 10rpx;

  text {
    font-size: 24rpx;
    color: $text-secondary;
  }
}
</style>