badge.vue 14 KB

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