badge.vue 14 KB

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