index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <view class="class-page">
  3. <LoadingSkeleton v-if="infoLoading" :rows="2" />
  4. <ErrorState v-else-if="infoError" message="班级信息加载失败" @retry="loadClassInfo" />
  5. <template v-else>
  6. <view v-if="classInfo" class="class-card">
  7. <text class="card-title">当前班次</text>
  8. <view class="info-row">
  9. <text class="info-label">班次名称</text>
  10. <text class="info-value">{{ classInfo.name }}</text>
  11. </view>
  12. <view class="info-row">
  13. <text class="info-label">班级码</text>
  14. <text class="info-value">{{ classInfo.code }}</text>
  15. </view>
  16. <view class="info-row" v-if="classInfo.time">
  17. <text class="info-label">时间</text>
  18. <text class="info-value">{{ classInfo.time }}</text>
  19. </view>
  20. <view class="info-row" v-if="classInfo.place">
  21. <text class="info-label">地点</text>
  22. <text class="info-value">{{ classInfo.place }}</text>
  23. </view>
  24. <button class="go-btn" @click="goHome">进入首页</button>
  25. </view>
  26. <view v-else class="join-card">
  27. <text class="card-title">进班</text>
  28. <text class="card-desc">请输入班级码加入您的训练班级</text>
  29. <input class="code-input" v-model="classCode" placeholder="请输入班级码" />
  30. <text class="form-error" v-if="errorMsg">{{ errorMsg }}</text>
  31. <button class="join-btn" @click="handleJoin" :loading="loading" :disabled="loading">进班</button>
  32. </view>
  33. </template>
  34. </view>
  35. </template>
  36. <script>
  37. import { joinClass, getClassInfo } from '@/utils/api.js'
  38. import { guardPage } from '@/utils/guard.js'
  39. import LoadingSkeleton from '@/components/loading-skeleton.vue'
  40. import ErrorState from '@/components/error-state.vue'
  41. export default {
  42. components: { LoadingSkeleton, ErrorState },
  43. data() {
  44. return {
  45. classInfo: null,
  46. classCode: '',
  47. errorMsg: '',
  48. loading: false,
  49. infoLoading: false,
  50. infoError: false
  51. }
  52. },
  53. onShow() {
  54. if (!guardPage()) return
  55. this.loadClassInfo()
  56. },
  57. methods: {
  58. loadClassInfo() {
  59. var self = this
  60. self.infoLoading = true
  61. self.infoError = false
  62. getClassInfo().then(function(resp) {
  63. if (resp.data) {
  64. self.classInfo = resp.data
  65. self.$store.commit('setClassId', resp.data.id)
  66. }
  67. }).catch(function() {
  68. self.classInfo = null
  69. self.infoError = true
  70. }).finally(function() {
  71. self.infoLoading = false
  72. })
  73. },
  74. handleJoin() {
  75. if (!this.classCode.trim()) {
  76. this.errorMsg = '请输入班级码'
  77. return
  78. }
  79. this.errorMsg = ''
  80. if (this.loading) return
  81. this.loading = true
  82. var self = this
  83. joinClass(this.classCode).then(function(resp) {
  84. self.classInfo = resp.data
  85. self.$store.commit('setClassId', resp.data.classId)
  86. uni.showToast({ title: '进班成功', icon: 'success' })
  87. }).catch(function(err) {
  88. self.errorMsg = (err && err.message) || '进班失败'
  89. }).finally(function() {
  90. self.loading = false
  91. })
  92. },
  93. goHome() {
  94. uni.switchTab({ url: '/pages/index/index' })
  95. }
  96. }
  97. }
  98. </script>
  99. <style scoped>
  100. .class-page {
  101. min-height: 100vh;
  102. background: #F5FAFE;
  103. padding: 32rpx;
  104. }
  105. .class-card, .join-card {
  106. background: #FFFFFF;
  107. border-radius: 16rpx;
  108. padding: 40rpx 32rpx;
  109. }
  110. .card-title {
  111. display: block;
  112. font-size: 36rpx;
  113. font-weight: 700;
  114. color: #1E3A5F;
  115. margin-bottom: 32rpx;
  116. }
  117. .card-desc {
  118. display: block;
  119. font-size: 26rpx;
  120. color: #3B5A78;
  121. margin-bottom: 32rpx;
  122. }
  123. .info-row {
  124. display: flex;
  125. justify-content: space-between;
  126. padding: 20rpx 0;
  127. border-bottom: 1rpx solid #E3ECF4;
  128. }
  129. .info-label {
  130. font-size: 28rpx;
  131. color: #3B5A78;
  132. }
  133. .info-value {
  134. font-size: 28rpx;
  135. color: #1E3A5F;
  136. font-weight: 500;
  137. }
  138. .code-input {
  139. width: 100%;
  140. height: 88rpx;
  141. border: 2rpx solid #E3ECF4;
  142. border-radius: 12rpx;
  143. padding: 0 24rpx;
  144. font-size: 32rpx;
  145. color: #1E3A5F;
  146. background: #F5FAFE;
  147. margin-bottom: 16rpx;
  148. }
  149. .form-error {
  150. display: block;
  151. font-size: 24rpx;
  152. color: #C0392B;
  153. margin-bottom: 16rpx;
  154. }
  155. .go-btn, .join-btn {
  156. width: 100%;
  157. height: 88rpx;
  158. line-height: 88rpx;
  159. background: #2E6FA8;
  160. color: #FFFFFF;
  161. font-size: 30rpx;
  162. font-weight: 600;
  163. border-radius: 44rpx;
  164. border: none;
  165. margin-top: 32rpx;
  166. }
  167. .go-btn:active, .join-btn:active {
  168. opacity: 0.85;
  169. }
  170. </style>