badge.vue 14 KB

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