| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- <template>
- <view class="badge-container">
- <view v-if="hasFamily">
- <!-- 统计概览 -->
- <view class="stats-card">
- <view class="stat-item">
- <text class="stat-num">{{ stats.total || 0 }}</text>
- <text class="stat-label">获得勋章</text>
- </view>
- <view class="stat-item">
- <text class="stat-num">{{ stats.active || 0 }}</text>
- <text class="stat-label">有效</text>
- </view>
- <view class="stat-item">
- <text class="stat-num">{{ totalBadges || 0 }}</text>
- <text class="stat-label">总勋章</text>
- </view>
- </view>
- <!-- 家庭勋章排行 -->
- <view class="ranking-section" v-if="familyRanking.length > 0">
- <view class="ranking-title">家庭勋章排行</view>
- <view class="ranking-list">
- <view class="ranking-item" v-for="(item, index) in familyRanking" :key="index">
- <text class="ranking-index">{{ index + 1 }}</text>
- <image class="ranking-avatar" v-if="item.avatar" :src="item.avatar" mode="aspectFill" />
- <text class="ranking-avatar-placeholder" v-else>{{ item.memberName ? item.memberName.substring(0,1) : '?' }}</text>
- <view class="ranking-info">
- <text class="ranking-name">{{ item.memberName || '未知' }}</text>
- <text class="ranking-role">{{ item.role === 'child' ? '孩子' : '家长' }}</text>
- </view>
- <view class="ranking-count">
- <text class="ranking-num">{{ item.badgeCount || 0 }}</text>
- <text class="ranking-label">枚勋章</text>
- </view>
- </view>
- </view>
- </view>
- <!-- 分类筛选 -->
- <view class="filter-bar">
- <view
- v-for="cat in categories"
- :key="cat.code"
- :class="['filter-chip', currentCategory === cat.code ? 'active' : '']"
- @click="onCategoryChange(cat.code)"
- >
- {{ cat.label }}
- </view>
- </view>
- <!-- 勋章网格 -->
- <view class="badge-grid">
- <view
- v-for="item in badgeList"
- :key="item._key"
- class="badge-item"
- @click="showBadgeDetail(item)"
- >
- <view :class="['badge-icon', item._earned ? 'earned' : 'locked']">
- <image v-if="item._icon" :src="item._icon" mode="aspectFit" />
- <text v-else class="icon-placeholder">{{ getBadgeSymbol(item._badge) }}</text>
- </view>
- <text class="badge-name">{{ item._name }}</text>
- <text v-if="item._earned" class="badge-status">已获得</text>
- </view>
- </view>
- <view v-if="badgeList.length === 0" class="empty-state">
- <text class="empty-text">暂无可用的勋章</text>
- </view>
- <!-- 详情弹窗 -->
- <view class="modal-mask" v-if="showDetail" @click="showDetail = false">
- <view class="modal" @click.stop>
- <view class="modal-badge-icon">
- <image v-if="detailIcon" :src="detailIcon" mode="aspectFit" />
- <text v-else class="big-icon">{{ getBadgeSymbol(detailBadge) }}</text>
- </view>
- <text class="modal-badge-name">{{ detailBadge && detailBadge.name }}</text>
- <text class="modal-badge-desc">{{ (detailBadge && detailBadge.description) || '暂无描述' }}</text>
- <view class="modal-meta">
- <text class="meta-tag">{{ detailLevel }}</text>
- <text class="meta-tag">{{ detailRarity }}</text>
- <text class="meta-tag">{{ detailCategory }}</text>
- </view>
- <view class="modal-earned" v-if="detailEarned">
- <text>获得时间:{{ detailEarnedDate }}</text>
- </view>
- <view class="modal-btns">
- <button v-if="detailEarned" :class="['btn-fav', detailIsFav ? 'active' : '']" @click="onToggleFavorite">
- {{ detailIsFav ? '已收藏' : '收藏' }}
- </button>
- <button class="btn-close" @click="showDetail = false">关闭</button>
- </view>
- </view>
- </view>
- </view>
- <view v-else>
- <FamilyEmptyState type="card" />
- </view>
- </view>
- </template>
- <script>
- import { getActiveBadges, getChildBadges, getChildBadgeStats, toggleBadgeFavorite, getFamilyBadgeRanking } from '../../utils/api.js'
- import { parseDate } from '../../utils/format.js'
- export default {
- data() {
- return {
- badges: [],
- childBadges: [],
- badgeList: [],
- earnedMap: {},
- stats: {},
- totalBadges: 0,
- currentCategory: '',
- categories: [
- { code: '', label: '全部' },
- { code: '学习', label: '学习' },
- { code: '运动', label: '运动' },
- { code: '家务', label: '家务' },
- { code: '阅读', label: '阅读' },
- { code: '打卡', label: '打卡' },
- { code: '综合', label: '综合' }
- ],
- levelMap: { 'bronze': '铜牌', 'silver': '银牌', 'gold': '金牌', 'diamond': '钻石' },
- rarityMap: { 'common': '普通', 'rare': '稀有', 'epic': '史诗', 'legendary': '传说' },
- categoryMap: { '学习': '📚', '运动': '⚽', '家务': '🧹', '阅读': '📖', '打卡': '✅', '综合': '⭐' },
- familyRanking: [],
- showDetail: false,
- detailBadge: null,
- detailEarned: false,
- detailIsFav: false,
- detailEarnedDate: ''
- }
- },
- computed: {
- hasFamily: function() {
- return this.$store.getters.hasFamily
- },
- detailIcon() {
- return this.detailBadge ? this.detailBadge.icon : ''
- },
- detailLevel() {
- if (!this.detailBadge) return ''
- return this.levelMap[this.detailBadge.level] || this.detailBadge.level || ''
- },
- detailRarity() {
- if (!this.detailBadge) return ''
- return this.rarityMap[this.detailBadge.rarity] || this.detailBadge.rarity || ''
- },
- detailCategory() {
- if (!this.detailBadge) return ''
- return this.categoryMap[this.detailBadge.category] || this.detailBadge.category || ''
- }
- },
- onShow() {
- this.loadData()
- },
- methods: {
- async loadData() {
- try {
- const [badgesRes, childBadgesRes, statsRes] = await Promise.all([
- getActiveBadges(),
- getChildBadges(this.getChildId()),
- getChildBadgeStats(this.getChildId())
- ])
- this.badges = badgesRes.data || []
- this.childBadges = childBadgesRes.data || []
- this.stats = statsRes.data || {}
- this.totalBadges = this.badges.length
- this.buildEarnedMap()
- this.applyFilter()
- this.loadFamilyRanking()
- } catch (e) {
- console.error('加载勋章数据失败', e)
- }
- },
- getChildId() {
- return uni.getStorageSync('currentChildId') || ''
- },
- buildEarnedMap() {
- const map = {}
- for (const item of this.childBadges) {
- const badge = item.badge || {}
- const badgeId = badge.id || item.badgeId
- if (badgeId) map[badgeId] = true
- }
- this.earnedMap = map
- },
- onCategoryChange(code) {
- if (this.currentCategory === code) return
- this.currentCategory = code
- this.applyFilter()
- },
- applyFilter() {
- let filtered = this.badges.slice()
- if (this.currentCategory) {
- filtered = filtered.filter(function(b) { return b.category === this.currentCategory }.bind(this))
- }
- this.badgeList = filtered.map(function(b) {
- const badgeId = b.id
- const earned = this.earnedMap[badgeId] || false
- return {
- _key: badgeId,
- _badge: b,
- _name: b.name || '',
- _icon: b.icon || '',
- _earned: earned,
- _badgeId: badgeId
- }
- }.bind(this))
- },
- showBadgeDetail(item) {
- const badge = item._badge || item
- this.detailBadge = badge
- const found = this.childBadges.find(function(cb) {
- const cbBadge = cb.badge || {}
- return (cbBadge.id || cb.badgeId) === badge.id
- })
- this.detailEarned = !!found
- this.detailIsFav = found ? (found.isFavorite || (found.childBadge && found.childBadge.isFavorite)) : false
- this.detailEarnedDate = found ? this.formatDate(found.earnedAt || (found.childBadge && found.childBadge.earnedAt)) : ''
- this.showDetail = true
- },
- async onToggleFavorite() {
- const memberId = this.getChildId()
- const badgeId = this.detailBadge.id
- try {
- await toggleBadgeFavorite(memberId, badgeId)
- uni.showToast({ title: this.detailIsFav ? '已取消收藏' : '已收藏', icon: 'success' })
- await this.loadData()
- this.showDetail = false
- } catch (e) {
- console.error('操作失败', e)
- }
- },
- async loadFamilyRanking() {
- var ui = uni.getStorageSync('userInfo') || {}
- if (!ui.familyId) return
- try {
- var res = await getFamilyBadgeRanking()
- this.familyRanking = res.data || []
- } catch (e) {
- console.error('加载家庭排名失败', e)
- }
- },
- getBadgeSymbol(badge) {
- if (!badge) return '🏅'
- const symbols = {
- '学习': '📚', '运动': '⚽', '家务': '🧹', '阅读': '📖', '打卡': '✅', '综合': '⭐'
- }
- return symbols[badge.category] || '🏅'
- },
- formatDate(dateStr) {
- if (!dateStr) return ''
- var d = parseDate(dateStr)
- if (!d) return ''
- return d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
- }
- }
- }
- </script>
- <style scoped>
- .badge-container {
- min-height: 100vh;
- background: #f5f7fa;
- padding-bottom: 40rpx;
- }
- .stats-card {
- display: flex;
- background: linear-gradient(135deg, #F97316, #fb923c);
- padding: 30rpx;
- margin: 20rpx;
- border-radius: 16rpx;
- justify-content: space-around;
- }
- .stat-item {
- text-align: center;
- }
- .stat-num {
- display: block;
- font-size: 40rpx;
- font-weight: bold;
- color: #fff;
- }
- .stat-label {
- display: block;
- font-size: 22rpx;
- color: rgba(255,255,255,0.85);
- margin-top: 6rpx;
- }
- .filter-bar {
- display: flex;
- padding: 16rpx 20rpx;
- gap: 12rpx;
- flex-wrap: wrap;
- }
- .filter-chip {
- padding: 8rpx 24rpx;
- border-radius: 30rpx;
- font-size: 24rpx;
- color: #666;
- background: #fff;
- }
- .filter-chip.active {
- color: #fff;
- background: #F97316;
- }
- .badge-grid {
- display: flex;
- flex-wrap: wrap;
- padding: 10rpx 20rpx;
- gap: 16rpx;
- }
- .badge-item {
- width: calc(25% - 12rpx);
- background: #fff;
- border-radius: 16rpx;
- padding: 20rpx 10rpx;
- text-align: center;
- box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
- }
- .badge-icon {
- width: 80rpx;
- height: 80rpx;
- margin: 0 auto 10rpx;
- border-radius: 50%;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 48rpx;
- }
- .badge-icon.earned {
- background: linear-gradient(135deg, #fef3c7, #fde68a);
- }
- .badge-icon.locked {
- background: #f0f0f0;
- filter: grayscale(1);
- opacity: 0.5;
- }
- .badge-icon image {
- width: 60rpx;
- height: 60rpx;
- }
- .badge-name {
- display: block;
- font-size: 22rpx;
- color: #333;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- .badge-status {
- display: block;
- font-size: 18rpx;
- color: #F97316;
- margin-top: 4rpx;
- }
- .ranking-section {
- background: #fff;
- margin: 20rpx;
- border-radius: 16rpx;
- padding: 24rpx;
- box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
- }
- .ranking-title {
- font-size: 28rpx;
- font-weight: bold;
- color: #333;
- margin-bottom: 16rpx;
- padding-left: 8rpx;
- border-left: 6rpx solid #F97316;
- }
- .ranking-list {
- display: flex;
- flex-direction: column;
- gap: 12rpx;
- }
- .ranking-item {
- display: flex;
- align-items: center;
- padding: 12rpx 8rpx;
- border-radius: 12rpx;
- background: #fafafa;
- }
- .ranking-index {
- width: 40rpx;
- font-size: 28rpx;
- font-weight: bold;
- color: #999;
- text-align: center;
- }
- .ranking-item:first-child .ranking-index { color: #F97316; }
- .ranking-item:nth-child(2) .ranking-index { color: #666; }
- .ranking-item:nth-child(3) .ranking-index { color: #8B4513; }
- .ranking-avatar {
- width: 60rpx;
- height: 60rpx;
- border-radius: 50%;
- margin: 0 16rpx;
- }
- .ranking-avatar-placeholder {
- width: 60rpx;
- height: 60rpx;
- border-radius: 50%;
- margin: 0 16rpx;
- background: #F97316;
- color: #fff;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 28rpx;
- text-align: center;
- line-height: 60rpx;
- }
- .ranking-info {
- flex: 1;
- }
- .ranking-name {
- display: block;
- font-size: 26rpx;
- color: #333;
- font-weight: 500;
- }
- .ranking-role {
- display: block;
- font-size: 20rpx;
- color: #999;
- margin-top: 4rpx;
- }
- .ranking-count {
- text-align: center;
- }
- .ranking-num {
- display: block;
- font-size: 32rpx;
- font-weight: bold;
- color: #F97316;
- }
- .ranking-label {
- display: block;
- font-size: 20rpx;
- color: #999;
- }
- .empty-state {
- text-align: center;
- padding: 120rpx 30rpx;
- }
- .empty-text {
- font-size: 28rpx;
- color: #999;
- }
- .modal-mask {
- position: fixed;
- top: 0; left: 0; right: 0; bottom: 0;
- background: rgba(0,0,0,0.5);
- display: flex;
- align-items: center;
- justify-content: center;
- z-index: 100;
- }
- .modal {
- background: #fff;
- border-radius: 20rpx;
- padding: 40rpx;
- width: 75%;
- text-align: center;
- }
- .modal-badge-icon {
- width: 120rpx;
- height: 120rpx;
- margin: 0 auto 20rpx;
- border-radius: 50%;
- background: linear-gradient(135deg, #fef3c7, #fde68a);
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .big-icon {
- font-size: 72rpx;
- }
- .modal-badge-name {
- display: block;
- font-size: 34rpx;
- font-weight: bold;
- color: #333;
- margin-bottom: 12rpx;
- }
- .modal-badge-desc {
- display: block;
- font-size: 26rpx;
- color: #666;
- margin-bottom: 20rpx;
- }
- .modal-meta {
- display: flex;
- justify-content: center;
- gap: 12rpx;
- margin-bottom: 20rpx;
- }
- .meta-tag {
- padding: 6rpx 20rpx;
- border-radius: 20rpx;
- font-size: 22rpx;
- background: #f5f5f5;
- color: #666;
- }
- .modal-earned {
- font-size: 24rpx;
- color: #999;
- margin-bottom: 20rpx;
- }
- .modal-btns {
- display: flex;
- gap: 20rpx;
- }
- .modal-btns button {
- flex: 1;
- font-size: 28rpx;
- padding: 16rpx;
- border-radius: 40rpx;
- border: none;
- }
- .btn-fav {
- background: #F97316;
- color: #fff;
- }
- .btn-fav.active {
- background: #ddd;
- color: #999;
- }
- .btn-close {
- background: #f5f5f5;
- color: #666;
- }
- </style>
|