index.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <view class="calendar-page">
  3. <view class="calendar-header">
  4. <text class="calendar-title">课程日历</text>
  5. </view>
  6. <view class="calendar-content">
  7. <view class="calendar-row calendar-week">
  8. <text v-for="w in weeks" :key="w">{{ w }}</text>
  9. </view>
  10. <view v-for="(row, i) in calendarRows" :key="i" class="calendar-row">
  11. <text v-for="d in row" :key="d" :class="{'today': isToday(d)}">{{ d > 0 ? d : '' }}</text>
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. data() {
  19. return {
  20. weeks: ['日','一','二','三','四','五','六'],
  21. calendarRows: this.getCalendarRows()
  22. }
  23. },
  24. methods: {
  25. getCalendarRows() {
  26. const d = new Date()
  27. const year = d.getFullYear()
  28. const month = d.getMonth()
  29. const firstDay = new Date(year, month, 1).getDay()
  30. const days = new Date(year, month + 1, 0).getDate()
  31. const rows = []
  32. let row = new Array(7).fill(0)
  33. let day = 1
  34. for (let i = 0; i < 6; i++) {
  35. for (let j = 0; j < 7; j++) {
  36. if (i === 0 && j < firstDay) continue
  37. if (day > days) break
  38. row[j] = day++
  39. }
  40. rows.push([...row])
  41. row = new Array(7).fill(0)
  42. if (day > days) break
  43. }
  44. return rows
  45. },
  46. isToday(d) {
  47. const now = new Date()
  48. return d === now.getDate()
  49. }
  50. }
  51. }
  52. </script>
  53. <style scoped>
  54. .calendar-page { padding: 40rpx; }
  55. .calendar-header { text-align: center; margin-bottom: 32rpx; }
  56. .calendar-title { font-size: 36rpx; font-weight: bold; color: #3498db; }
  57. .calendar-content { background: #fff; border-radius: 16rpx; box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04); padding: 24rpx; }
  58. .calendar-row { display: flex; justify-content: space-between; margin-bottom: 8rpx; }
  59. .calendar-week text { color: #888; font-size: 28rpx; flex: 1; text-align: center; }
  60. .calendar-row text { flex: 1; text-align: center; font-size: 30rpx; color: #222; padding: 8rpx 0; border-radius: 8rpx; }
  61. .today { background: #3498db; color: #fff; }
  62. </style>