badge.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <template>
  2. <view class="badge-container">
  3. <!-- 统计概览 -->
  4. <view class="stats-card">
  5. <view class="stat-item">
  6. <text class="stat-num">{{ stats.total || 0 }}</text>
  7. <text class="stat-label">获得勋章</text>
  8. </view>
  9. <view class="stat-item">
  10. <text class="stat-num">{{ stats.active || 0 }}</text>
  11. <text class="stat-label">有效</text>
  12. </view>
  13. <view class="stat-item">
  14. <text class="stat-num">{{ totalBadges || 0 }}</text>
  15. <text class="stat-label">总勋章</text>
  16. </view>
  17. </view>
  18. <!-- 分类筛选 -->
  19. <view class="filter-bar">
  20. <view
  21. v-for="cat in categories"
  22. :key="cat.code"
  23. :class="['filter-chip', currentCategory === cat.code ? 'active' : '']"
  24. @click="onCategoryChange(cat.code)"
  25. >
  26. {{ cat.label }}
  27. </view>
  28. </view>
  29. <!-- 勋章网格 -->
  30. <view class="badge-grid">
  31. <view
  32. v-for="item in badgeList"
  33. :key="item._key"
  34. class="badge-item"
  35. @click="showBadgeDetail(item)"
  36. >
  37. <view :class="['badge-icon', item._earned ? 'earned' : 'locked']">
  38. <image v-if="item._icon" :src="item._icon" mode="aspectFit" />
  39. <text v-else class="icon-placeholder">{{ getBadgeSymbol(item._badge) }}</text>
  40. </view>
  41. <text class="badge-name">{{ item._name }}</text>
  42. <text v-if="item._earned" class="badge-status">已获得</text>
  43. </view>
  44. </view>
  45. <view v-if="badgeList.length === 0" class="empty-state">
  46. <text class="empty-text">暂无可用的勋章</text>
  47. </view>
  48. <!-- 详情弹窗 -->
  49. <view class="modal-mask" v-if="showDetail" @click="showDetail = false">
  50. <view class="modal" @click.stop>
  51. <view class="modal-badge-icon">
  52. <image v-if="detailIcon" :src="detailIcon" mode="aspectFit" />
  53. <text v-else class="big-icon">{{ getBadgeSymbol(detailBadge) }}</text>
  54. </view>
  55. <text class="modal-badge-name">{{ detailBadge && detailBadge.name }}</text>
  56. <text class="modal-badge-desc">{{ (detailBadge && detailBadge.description) || '暂无描述' }}</text>
  57. <view class="modal-meta">
  58. <text class="meta-tag">{{ detailLevel }}</text>
  59. <text class="meta-tag">{{ detailRarity }}</text>
  60. <text class="meta-tag">{{ detailCategory }}</text>
  61. </view>
  62. <view class="modal-earned" v-if="detailEarned">
  63. <text>获得时间:{{ detailEarnedDate }}</text>
  64. </view>
  65. <view class="modal-btns">
  66. <button v-if="detailEarned" :class="['btn-fav', detailIsFav ? 'active' : '']" @click="onToggleFavorite">
  67. {{ detailIsFav ? '已收藏' : '收藏' }}
  68. </button>
  69. <button class="btn-close" @click="showDetail = false">关闭</button>
  70. </view>
  71. </view>
  72. </view>
  73. </view>
  74. </template>
  75. <script>
  76. import { getActiveBadges, getChildBadges, getChildBadgeStats, toggleBadgeFavorite } from '../../utils/api.js'
  77. export default {
  78. data() {
  79. return {
  80. badges: [],
  81. childBadges: [],
  82. badgeList: [],
  83. earnedMap: {},
  84. stats: {},
  85. totalBadges: 0,
  86. currentCategory: '',
  87. categories: [
  88. { code: '', label: '全部' },
  89. { code: '学习', label: '学习' },
  90. { code: '运动', label: '运动' },
  91. { code: '家务', label: '家务' },
  92. { code: '阅读', label: '阅读' },
  93. { code: '打卡', label: '打卡' },
  94. { code: '综合', label: '综合' }
  95. ],
  96. levelMap: { 'bronze': '铜牌', 'silver': '银牌', 'gold': '金牌', 'diamond': '钻石' },
  97. rarityMap: { 'common': '普通', 'rare': '稀有', 'epic': '史诗', 'legendary': '传说' },
  98. categoryMap: { '学习': '📚', '运动': '⚽', '家务': '🧹', '阅读': '📖', '打卡': '✅', '综合': '⭐' },
  99. showDetail: false,
  100. detailBadge: null,
  101. detailEarned: false,
  102. detailIsFav: false,
  103. detailEarnedDate: ''
  104. }
  105. },
  106. computed: {
  107. detailIcon() {
  108. return this.detailBadge ? this.detailBadge.icon : ''
  109. },
  110. detailLevel() {
  111. if (!this.detailBadge) return ''
  112. return this.levelMap[this.detailBadge.level] || this.detailBadge.level || ''
  113. },
  114. detailRarity() {
  115. if (!this.detailBadge) return ''
  116. return this.rarityMap[this.detailBadge.rarity] || this.detailBadge.rarity || ''
  117. },
  118. detailCategory() {
  119. if (!this.detailBadge) return ''
  120. return this.categoryMap[this.detailBadge.category] || this.detailBadge.category || ''
  121. }
  122. },
  123. onShow() {
  124. this.loadData()
  125. },
  126. methods: {
  127. async loadData() {
  128. try {
  129. const [badgesRes, childBadgesRes, statsRes] = await Promise.all([
  130. getActiveBadges(),
  131. getChildBadges(this.getChildId()),
  132. getChildBadgeStats(this.getChildId())
  133. ])
  134. this.badges = badgesRes.data || []
  135. this.childBadges = childBadgesRes.data || []
  136. this.stats = statsRes.data || {}
  137. this.totalBadges = this.badges.length
  138. this.buildEarnedMap()
  139. this.applyFilter()
  140. } catch (e) {
  141. console.error('加载勋章数据失败', e)
  142. }
  143. },
  144. getChildId() {
  145. return uni.getStorageSync('currentChildId') || ''
  146. },
  147. buildEarnedMap() {
  148. const map = {}
  149. for (const item of this.childBadges) {
  150. const badge = item.badge || {}
  151. const badgeId = badge.id || item.badgeId
  152. if (badgeId) map[badgeId] = true
  153. }
  154. this.earnedMap = map
  155. },
  156. onCategoryChange(code) {
  157. if (this.currentCategory === code) return
  158. this.currentCategory = code
  159. this.applyFilter()
  160. },
  161. applyFilter() {
  162. let filtered = this.badges.slice()
  163. if (this.currentCategory) {
  164. filtered = filtered.filter(function(b) { return b.category === this.currentCategory }.bind(this))
  165. }
  166. this.badgeList = filtered.map(function(b) {
  167. const badgeId = b.id
  168. const earned = this.earnedMap[badgeId] || false
  169. return {
  170. _key: badgeId,
  171. _badge: b,
  172. _name: b.name || '',
  173. _icon: b.icon || '',
  174. _earned: earned,
  175. _badgeId: badgeId
  176. }
  177. }.bind(this))
  178. },
  179. showBadgeDetail(item) {
  180. const badge = item._badge || item
  181. this.detailBadge = badge
  182. const found = this.childBadges.find(function(cb) {
  183. const cbBadge = cb.badge || {}
  184. return (cbBadge.id || cb.badgeId) === badge.id
  185. })
  186. this.detailEarned = !!found
  187. this.detailIsFav = found ? (found.isFavorite || (found.childBadge && found.childBadge.isFavorite)) : false
  188. this.detailEarnedDate = found ? this.formatDate(found.earnedAt || (found.childBadge && found.childBadge.earnedAt)) : ''
  189. this.showDetail = true
  190. },
  191. async onToggleFavorite() {
  192. const childId = this.getChildId()
  193. const badgeId = this.detailBadge.id
  194. try {
  195. await toggleBadgeFavorite(childId, badgeId)
  196. uni.showToast({ title: this.detailIsFav ? '已取消收藏' : '已收藏', icon: 'success' })
  197. await this.loadData()
  198. this.showDetail = false
  199. } catch (e) {
  200. console.error('操作失败', e)
  201. }
  202. },
  203. getBadgeSymbol(badge) {
  204. if (!badge) return '🏅'
  205. const symbols = {
  206. '学习': '📚', '运动': '⚽', '家务': '🧹', '阅读': '📖', '打卡': '✅', '综合': '⭐'
  207. }
  208. return symbols[badge.category] || '🏅'
  209. },
  210. formatDate(dateStr) {
  211. if (!dateStr) return ''
  212. var d = new Date(dateStr)
  213. return d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
  214. }
  215. }
  216. }
  217. </script>
  218. <style scoped>
  219. .badge-container {
  220. min-height: 100vh;
  221. background: #f5f7fa;
  222. padding-bottom: 40rpx;
  223. }
  224. .stats-card {
  225. display: flex;
  226. background: linear-gradient(135deg, #F97316, #fb923c);
  227. padding: 30rpx;
  228. margin: 20rpx;
  229. border-radius: 16rpx;
  230. justify-content: space-around;
  231. }
  232. .stat-item {
  233. text-align: center;
  234. }
  235. .stat-num {
  236. display: block;
  237. font-size: 40rpx;
  238. font-weight: bold;
  239. color: #fff;
  240. }
  241. .stat-label {
  242. display: block;
  243. font-size: 22rpx;
  244. color: rgba(255,255,255,0.85);
  245. margin-top: 6rpx;
  246. }
  247. .filter-bar {
  248. display: flex;
  249. padding: 16rpx 20rpx;
  250. gap: 12rpx;
  251. flex-wrap: wrap;
  252. }
  253. .filter-chip {
  254. padding: 8rpx 24rpx;
  255. border-radius: 30rpx;
  256. font-size: 24rpx;
  257. color: #666;
  258. background: #fff;
  259. }
  260. .filter-chip.active {
  261. color: #fff;
  262. background: #F97316;
  263. }
  264. .badge-grid {
  265. display: flex;
  266. flex-wrap: wrap;
  267. padding: 10rpx 20rpx;
  268. gap: 16rpx;
  269. }
  270. .badge-item {
  271. width: calc(25% - 12rpx);
  272. background: #fff;
  273. border-radius: 16rpx;
  274. padding: 20rpx 10rpx;
  275. text-align: center;
  276. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  277. }
  278. .badge-icon {
  279. width: 80rpx;
  280. height: 80rpx;
  281. margin: 0 auto 10rpx;
  282. border-radius: 50%;
  283. display: flex;
  284. align-items: center;
  285. justify-content: center;
  286. font-size: 48rpx;
  287. }
  288. .badge-icon.earned {
  289. background: linear-gradient(135deg, #fef3c7, #fde68a);
  290. }
  291. .badge-icon.locked {
  292. background: #f0f0f0;
  293. filter: grayscale(1);
  294. opacity: 0.5;
  295. }
  296. .badge-icon image {
  297. width: 60rpx;
  298. height: 60rpx;
  299. }
  300. .badge-name {
  301. display: block;
  302. font-size: 22rpx;
  303. color: #333;
  304. overflow: hidden;
  305. text-overflow: ellipsis;
  306. white-space: nowrap;
  307. }
  308. .badge-status {
  309. display: block;
  310. font-size: 18rpx;
  311. color: #F97316;
  312. margin-top: 4rpx;
  313. }
  314. .empty-state {
  315. text-align: center;
  316. padding: 120rpx 30rpx;
  317. }
  318. .empty-text {
  319. font-size: 28rpx;
  320. color: #999;
  321. }
  322. .modal-mask {
  323. position: fixed;
  324. top: 0; left: 0; right: 0; bottom: 0;
  325. background: rgba(0,0,0,0.5);
  326. display: flex;
  327. align-items: center;
  328. justify-content: center;
  329. z-index: 100;
  330. }
  331. .modal {
  332. background: #fff;
  333. border-radius: 20rpx;
  334. padding: 40rpx;
  335. width: 75%;
  336. text-align: center;
  337. }
  338. .modal-badge-icon {
  339. width: 120rpx;
  340. height: 120rpx;
  341. margin: 0 auto 20rpx;
  342. border-radius: 50%;
  343. background: linear-gradient(135deg, #fef3c7, #fde68a);
  344. display: flex;
  345. align-items: center;
  346. justify-content: center;
  347. }
  348. .big-icon {
  349. font-size: 72rpx;
  350. }
  351. .modal-badge-name {
  352. display: block;
  353. font-size: 34rpx;
  354. font-weight: bold;
  355. color: #333;
  356. margin-bottom: 12rpx;
  357. }
  358. .modal-badge-desc {
  359. display: block;
  360. font-size: 26rpx;
  361. color: #666;
  362. margin-bottom: 20rpx;
  363. }
  364. .modal-meta {
  365. display: flex;
  366. justify-content: center;
  367. gap: 12rpx;
  368. margin-bottom: 20rpx;
  369. }
  370. .meta-tag {
  371. padding: 6rpx 20rpx;
  372. border-radius: 20rpx;
  373. font-size: 22rpx;
  374. background: #f5f5f5;
  375. color: #666;
  376. }
  377. .modal-earned {
  378. font-size: 24rpx;
  379. color: #999;
  380. margin-bottom: 20rpx;
  381. }
  382. .modal-btns {
  383. display: flex;
  384. gap: 20rpx;
  385. }
  386. .modal-btns button {
  387. flex: 1;
  388. font-size: 28rpx;
  389. padding: 16rpx;
  390. border-radius: 40rpx;
  391. border: none;
  392. }
  393. .btn-fav {
  394. background: #F97316;
  395. color: #fff;
  396. }
  397. .btn-fav.active {
  398. background: #ddd;
  399. color: #999;
  400. }
  401. .btn-close {
  402. background: #f5f5f5;
  403. color: #666;
  404. }
  405. </style>