index.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <view class="vote-page">
  3. <view class="round-toggle">
  4. <view :class="['round-tab', round === 1 ? 'active' : '']" @click="switchRound(1)">第1轮 · 我学会了</view>
  5. <view :class="['round-tab', round === 2 ? 'active' : '']" @click="switchRound(2)">第2轮 · 最想抄走</view>
  6. </view>
  7. <LoadingSkeleton v-if="loading" :rows="3" />
  8. <ErrorState v-else-if="error" message="投票数据加载失败" @retry="loadGroup" />
  9. <EmptyState v-else-if="groups.length === 0" icon="🗳️" title="暂无可投票小组" desc="进班后小组信息会在此展示" />
  10. <view v-else-if="!voted" class="vote-section">
  11. <text class="vote-hint">{{ round === 1 ? '每人2票,不可投本组' : '每人1票,可投本组' }}</text>
  12. <view class="group-list">
  13. <view class="group-item" v-for="(g, idx) in groups" :key="idx"
  14. :class="{ selected: selectedGroups.includes(g.id), disabled: round === 1 && g.id === myGroupId }"
  15. @click="toggleGroup(g.id)">
  16. <text class="group-name">{{ g.name }}</text>
  17. <text class="group-check" v-if="selectedGroups.includes(g.id)">✅</text>
  18. <text class="group-disabled" v-if="round === 1 && g.id === myGroupId">(本组)</text>
  19. </view>
  20. </view>
  21. <button class="submit-btn" @click="handleVote" :loading="voting"
  22. :disabled="voting || selectedGroups.length === 0">提交投票</button>
  23. </view>
  24. <view v-else class="result-section">
  25. <text class="result-title">投票结果</text>
  26. <view class="bar-list">
  27. <view class="bar-item" v-for="(b, idx) in bars" :key="idx">
  28. <view class="bar-label">{{ b.groupName }}</view>
  29. <view class="bar-track">
  30. <view class="bar-fill" :style="{ width: barWidth(b.count) }"></view>
  31. </view>
  32. <view class="bar-count">{{ b.count }}票</view>
  33. </view>
  34. </view>
  35. <view class="voted-badge">您已在第{{ round }}轮投票</view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. import { castVote, getVoteResult, getVotedStatus, getMyGroup, getClassGroups } from '@/utils/api.js'
  41. import { guardPage } from '@/utils/guard.js'
  42. import LoadingSkeleton from '@/components/loading-skeleton.vue'
  43. import ErrorState from '@/components/error-state.vue'
  44. import EmptyState from '@/components/empty-state.vue'
  45. export default {
  46. components: { LoadingSkeleton, ErrorState, EmptyState },
  47. data() {
  48. return {
  49. round: 1,
  50. classId: '',
  51. myGroupId: '',
  52. groups: [],
  53. selectedGroups: [],
  54. voted: false,
  55. bars: [],
  56. voting: false,
  57. loading: false,
  58. error: false
  59. }
  60. },
  61. onShow() {
  62. if (!guardPage()) return
  63. this.loadGroup()
  64. },
  65. methods: {
  66. loadGroup() {
  67. var self = this
  68. self.loading = true
  69. self.error = false
  70. getMyGroup().then(function(resp) {
  71. if (resp.data) {
  72. self.myGroupId = resp.data.id
  73. self.classId = resp.data.classId
  74. return getClassGroups()
  75. }
  76. }).then(function(resp) {
  77. if (resp && resp.data) {
  78. self.groups = resp.data
  79. }
  80. self.checkVoted()
  81. self.loadResult()
  82. }).catch(function() {
  83. self.error = true
  84. }).finally(function() {
  85. self.loading = false
  86. })
  87. },
  88. switchRound(r) {
  89. this.round = r
  90. this.selectedGroups = []
  91. this.checkVoted()
  92. this.loadResult()
  93. },
  94. checkVoted() {
  95. var self = this
  96. if (!self.classId) return
  97. getVotedStatus({ round: self.round, classId: self.classId }).then(function(resp) {
  98. self.voted = resp.data === true
  99. }).catch(function() {
  100. self.voted = false
  101. })
  102. },
  103. loadResult() {
  104. var self = this
  105. if (!self.classId) return
  106. getVoteResult({ round: self.round, classId: self.classId }).then(function(resp) {
  107. self.bars = (resp.data && resp.data.bars) || []
  108. }).catch(function() {})
  109. },
  110. toggleGroup(id) {
  111. if (this.round === 1 && id === this.myGroupId) return
  112. var idx = this.selectedGroups.indexOf(id)
  113. if (idx >= 0) {
  114. this.selectedGroups.splice(idx, 1)
  115. } else {
  116. var max = this.round === 1 ? 2 : 1
  117. if (this.selectedGroups.length >= max) {
  118. uni.showToast({ title: '第' + this.round + '轮最多' + max + '票', icon: 'none' })
  119. return
  120. }
  121. this.selectedGroups.push(id)
  122. }
  123. },
  124. handleVote() {
  125. if (this.selectedGroups.length === 0 || this.voting) return
  126. this.voting = true
  127. var self = this
  128. castVote({
  129. round: self.round,
  130. classId: self.classId,
  131. targetGroupIds: self.selectedGroups
  132. }).then(function() {
  133. uni.showToast({ title: '投票成功', icon: 'success' })
  134. self.voted = true
  135. self.loadResult()
  136. }).catch(function(err) {
  137. uni.showToast({ title: (err && err.message) || '投票失败', icon: 'none' })
  138. }).finally(function() {
  139. self.voting = false
  140. })
  141. },
  142. barWidth(count) {
  143. var max = 1
  144. this.bars.forEach(function(b) { if (b.count > max) max = b.count })
  145. return Math.round((count / max) * 100) + '%'
  146. }
  147. }
  148. }
  149. </script>
  150. <style scoped>
  151. .vote-page { min-height: 100vh; background: #F5FAFE; padding: 24rpx 32rpx; }
  152. .round-toggle { display: flex; background: #FFF; border-radius: 12rpx; overflow: hidden; margin-bottom: 24rpx; }
  153. .round-tab { flex: 1; height: 80rpx; line-height: 80rpx; text-align: center; font-size: 28rpx; color: #3B5A78; background: #FFF; }
  154. .round-tab.active { color: #FFF; background: #2E6FA8; font-weight: 600; }
  155. .vote-hint { display: block; font-size: 26rpx; color: #2E6FA8; margin-bottom: 20rpx; text-align: center; }
  156. .group-list { background: #FFF; border-radius: 12rpx; overflow: hidden; margin-bottom: 24rpx; }
  157. .group-item { display: flex; align-items: center; justify-content: space-between; padding: 24rpx 28rpx; border-bottom: 1rpx solid #E3ECF4; }
  158. .group-item.selected { background: #E8F3FC; }
  159. .group-item.disabled { opacity: 0.4; }
  160. .group-name { font-size: 28rpx; color: #1E3A5F; }
  161. .group-check { font-size: 28rpx; }
  162. .group-disabled { font-size: 24rpx; color: #6B7B8C; margin-left: 8rpx; }
  163. .submit-btn { width: 100%; height: 88rpx; line-height: 88rpx; background: #2E6FA8; color: #FFF; font-size: 30rpx; font-weight: 600; border-radius: 44rpx; border: none; }
  164. .submit-btn:active { opacity: 0.85; }
  165. .result-section { background: #FFF; border-radius: 16rpx; padding: 32rpx; }
  166. .result-title { display: block; font-size: 32rpx; font-weight: 700; color: #1E3A5F; margin-bottom: 24rpx; }
  167. .bar-list { margin-bottom: 24rpx; }
  168. .bar-item { display: flex; align-items: center; margin-bottom: 16rpx; }
  169. .bar-label { width: 160rpx; font-size: 26rpx; color: #1E3A5F; text-align: right; margin-right: 16rpx; }
  170. .bar-track { flex: 1; height: 32rpx; background: #E3ECF4; border-radius: 16rpx; overflow: hidden; }
  171. .bar-fill { height: 100%; background: #2E6FA8; border-radius: 16rpx; transition: width 0.5s; }
  172. .bar-count { width: 80rpx; font-size: 24rpx; color: #3B5A78; text-align: right; margin-left: 12rpx; }
  173. .voted-badge { text-align: center; font-size: 26rpx; color: #0E8A4F; background: #E6F6EE; padding: 16rpx; border-radius: 8rpx; }
  174. </style>