index.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <view class="course-page">
  3. <!-- 进行中的课程(上课状态) -->
  4. <view v-if="activeCourse" class="active-card">
  5. <view class="active-badge">
  6. <text class="active-badge-text">⏰ 课程进行中</text>
  7. </view>
  8. <text class="active-name">{{ activeCourse.level }} {{ activeCourse.name }}</text>
  9. <text class="active-desc">{{ activeCourse.description || '当前正在进行的课程' }}</text>
  10. <view class="active-time">
  11. <text class="active-time-text">开始 {{ fmtTime(activeCourse.startTime) }}</text>
  12. <text class="active-time-text" v-if="activeCourse.endTime">结束 {{ fmtTime(activeCourse.endTime) }}</text>
  13. </view>
  14. <button class="active-btn" :class="{ 'btn-disabled': isReviewing(activeCourse) }"
  15. :disabled="isReviewing(activeCourse)" @click="handleAction(activeCourse)">
  16. {{ actionText(activeCourse) }}
  17. </button>
  18. </view>
  19. <!-- 加载骨架 -->
  20. <LoadingSkeleton v-if="loading" :rows="3" />
  21. <!-- 错误态 -->
  22. <ErrorState v-else-if="error" message="课程列表加载失败" @retry="loadCourses" />
  23. <!-- 空态 -->
  24. <EmptyState
  25. v-else-if="courses.length === 0"
  26. icon="📚"
  27. title="暂无课程"
  28. desc="新课程发布后会出现在这里"
  29. action-text="重新加载"
  30. @action="loadCourses"
  31. />
  32. <!-- 正常态 -->
  33. <view v-else class="course-list">
  34. <view class="section-title">全部课程</view>
  35. <view class="course-card" v-for="(c, idx) in courses" :key="idx">
  36. <view class="course-head">
  37. <text class="course-level">{{ c.level }}</text>
  38. <text class="course-name">{{ c.name }}</text>
  39. </view>
  40. <view class="course-desc" v-if="c.description">{{ c.description }}</view>
  41. <view class="course-tags">
  42. <text class="course-tag" :class="'st-' + c.effectiveStatus">{{ statusLabel(c.effectiveStatus) }}</text>
  43. <text class="course-tag mine" :class="'my-' + c.myStatus">{{ myLabel(c.myStatus) }}</text>
  44. </view>
  45. <button class="course-btn" :class="{ 'btn-disabled': isReviewing(c) }"
  46. :disabled="isReviewing(c)" @click="handleAction(c)">
  47. {{ actionText(c) }}
  48. </button>
  49. </view>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. import { getCourseList, enterCourse } from '@/utils/api.js'
  55. import { formatDateTime } from '@/utils/format.js'
  56. import { requireLogin } from '@/utils/guard.js'
  57. import LoadingSkeleton from '@/components/loading-skeleton.vue'
  58. import ErrorState from '@/components/error-state.vue'
  59. import EmptyState from '@/components/empty-state.vue'
  60. export default {
  61. components: { LoadingSkeleton, ErrorState, EmptyState },
  62. data() {
  63. return {
  64. courses: [],
  65. loading: false,
  66. error: false
  67. }
  68. },
  69. computed: {
  70. isLoggedIn() {
  71. return this.$store.getters.isLoggedIn
  72. },
  73. activeCourse() {
  74. var list = this.courses || []
  75. for (var i = 0; i < list.length; i++) {
  76. if (list[i].effectiveStatus === 'active') {
  77. return list[i]
  78. }
  79. }
  80. return null
  81. }
  82. },
  83. onShow() {
  84. this.loadCourses()
  85. },
  86. methods: {
  87. loadCourses() {
  88. // 未登录也可浏览课程信息(后端 /api/course/list 已放行,我的状态为 none)
  89. var self = this
  90. self.loading = true
  91. self.error = false
  92. getCourseList().then(function(resp) {
  93. self.courses = resp.data || []
  94. }).catch(function() {
  95. self.error = true
  96. }).finally(function() {
  97. self.loading = false
  98. })
  99. },
  100. statusLabel(st) {
  101. var map = { upcoming: '未开始', active: '进行中', finished: '已结束', draft: '未发布' }
  102. return map[st] || st || '-'
  103. },
  104. myLabel(st) {
  105. var map = { none: '未报名', pending: '待支付', paid: '已支付', reviewing: '审核中', confirmed: '已进班', rejected: '未通过' }
  106. return map[st] || st || '-'
  107. },
  108. isReviewing(c) {
  109. return c && c.myStatus === 'reviewing'
  110. },
  111. actionText(c) {
  112. if (!c) return ''
  113. var s = c.myStatus
  114. if (s === 'confirmed') return '进入课程'
  115. if (s === 'reviewing') return '等待审核'
  116. if (s === 'pending') return '去支付'
  117. if (s === 'paid' || s === 'rejected') return '申请进班'
  118. return '报名进班'
  119. },
  120. handleAction(c) {
  121. if (!c) return
  122. // 报名/支付/申请进班均为实际操作,未登录需先登录(课程浏览已放行)
  123. if (!requireLogin()) return
  124. var s = c.myStatus
  125. if (s === 'confirmed') {
  126. uni.navigateTo({ url: '/pages/course/detail?courseId=' + c.id })
  127. } else if (s === 'pending') {
  128. uni.navigateTo({ url: '/pages/enroll/list' })
  129. } else if (s === 'paid' || s === 'rejected') {
  130. this.applyEnter(c)
  131. } else {
  132. // 未报名:先走报名流程
  133. uni.navigateTo({ url: '/pages/enroll/list' })
  134. }
  135. },
  136. applyEnter(c) {
  137. var self = this
  138. uni.showModal({
  139. title: '申请进班',
  140. content: '确认申请进入「' + c.name + '」?班主任/管理员审核通过后即可进入课程。',
  141. success: function(res) {
  142. if (!res.confirm) return
  143. enterCourse(c.id).then(function(resp) {
  144. var d = resp.data || {}
  145. uni.showToast({ title: d.message || '申请已提交', icon: 'none' })
  146. self.loadCourses()
  147. }).catch(function(err) {
  148. var msg = (err && err.message) || '申请失败'
  149. uni.showToast({ title: msg, icon: 'none' })
  150. // 未完成报名/支付等场景引导去报名
  151. if (msg.indexOf('报名') !== -1 || msg.indexOf('支付') !== -1) {
  152. setTimeout(function() {
  153. uni.navigateTo({ url: '/pages/enroll/list' })
  154. }, 1200)
  155. }
  156. })
  157. }
  158. })
  159. },
  160. fmtTime(v) {
  161. if (!v) return ''
  162. return formatDateTime(v)
  163. }
  164. }
  165. }
  166. </script>
  167. <style scoped>
  168. .course-page { min-height: 100vh; background: #F5FAFE; padding: 24rpx 32rpx; }
  169. .section-title { font-size: 30rpx; font-weight: 600; color: #1E3A5F; margin-bottom: 16rpx; border-left: 8rpx solid #2E6FA8; padding-left: 16rpx; }
  170. .active-card { background: #2E6FA8; border-radius: 20rpx; padding: 32rpx; margin-bottom: 32rpx; }
  171. .active-badge { display: inline-block; background: rgba(255,255,255,0.2); border-radius: 100rpx; padding: 4rpx 20rpx; margin-bottom: 16rpx; }
  172. .active-badge-text { font-size: 22rpx; color: #FFF; font-weight: 600; }
  173. .active-name { display: block; font-size: 36rpx; font-weight: 700; color: #FFF; margin-bottom: 8rpx; }
  174. .active-desc { display: block; font-size: 26rpx; color: rgba(255,255,255,0.9); margin-bottom: 16rpx; }
  175. .active-time { display: flex; flex-direction: column; margin-bottom: 24rpx; }
  176. .active-time-text { font-size: 22rpx; color: rgba(255,255,255,0.8); margin-bottom: 4rpx; }
  177. .active-btn { background: #FFF; color: #1F4E79; font-size: 30rpx; font-weight: 600; border-radius: 48rpx; height: 80rpx; line-height: 80rpx; border: none; margin: 0; }
  178. .active-btn:active { opacity: 0.9; }
  179. .course-list { display: flex; flex-direction: column; }
  180. .course-card { background: #FFF; border-radius: 16rpx; padding: 28rpx; margin-bottom: 20rpx; }
  181. .course-head { display: flex; align-items: center; margin-bottom: 8rpx; }
  182. .course-level { font-size: 24rpx; font-weight: 600; color: #2E6FA8; background: #E8F3FC; border-radius: 8rpx; padding: 4rpx 14rpx; margin-right: 16rpx; }
  183. .course-name { font-size: 30rpx; font-weight: 600; color: #1E3A5F; flex: 1; }
  184. .course-desc { font-size: 24rpx; color: #3B5A78; margin-bottom: 16rpx; line-height: 1.6; }
  185. .course-tags { display: flex; align-items: center; margin-bottom: 20rpx; }
  186. .course-tag { font-size: 20rpx; color: #3B5A78; background: #E3ECF4; border-radius: 6rpx; padding: 4rpx 14rpx; margin-right: 12rpx; }
  187. .course-tag.st-upcoming { color: #2E6FA8; background: #E8F3FC; }
  188. .course-tag.st-active { color: #0E8A4F; background: #E6F6EE; }
  189. .course-tag.st-finished { color: #6B7B8C; background: #E3ECF4; }
  190. .course-tag.mine { color: #2E6FA8; background: #E8F3FC; }
  191. .course-tag.my-confirmed { color: #0E8A4F; background: #E6F6EE; }
  192. .course-tag.my-reviewing { color: #2E6FA8; background: #E8F3FC; }
  193. .course-tag.my-rejected { color: #C0392B; background: #FDE8E8; }
  194. .course-btn { background: #2E6FA8; color: #FFF; font-size: 28rpx; font-weight: 600; border-radius: 44rpx; height: 76rpx; line-height: 76rpx; border: none; margin: 0; }
  195. .course-btn:active { opacity: 0.9; }
  196. .btn-disabled { background: #E3ECF4; color: #6B7B8C; }
  197. </style>