badge.vue 14 KB

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