Feat:如果有新消息,则做红点数字提示

This commit is contained in:
LinCyJang
2026-01-31 16:16:56 +08:00
parent 5983c4d17e
commit 56c00e61aa
+64 -6
View File
@@ -30,7 +30,6 @@
@click="gotoPage(item.url, item.type, item.index)" @click="gotoPage(item.url, item.type, item.index)"
> >
<view class="img-wrap"> <view class="img-wrap">
<!-- 预加载两张图片,可以在点击切换的时候不闪屏 -->
<image <image
:src="item.onIcon" :src="item.onIcon"
class="img on" class="img on"
@@ -41,6 +40,12 @@
/> />
</view> </view>
<view class="name">{{ item.name }}</view> <view class="name">{{ item.name }}</view>
<view
v-if="item.name === '消息' && showUnreadBadge"
class="badge"
>
{{ unreadCountText }}
</view>
</view> </view>
</view> </view>
</view> </view>
@@ -48,6 +53,8 @@
</template> </template>
<script> <script>
import store from '@/store'
import { getUnreadMessageCount } from '@/api/rooms'
export default { export default {
name: 'XddTabbar', name: 'XddTabbar',
props: { props: {
@@ -93,12 +100,39 @@ export default {
type: 1, type: 1,
index: 3 index: 3
} }
]] ]],
unreadCount: 0
}
},
computed: {
isLogin() {
return store.getters.isLogin
},
showUnreadBadge() {
return this.isLogin && this.unreadCount > 0
},
unreadCountText() {
if (this.unreadCount > 99) {
return '99+'
}
return this.unreadCount
}
},
watch: {
isLogin(val) {
if (val) {
this.fetchUnreadCount()
} else {
this.unreadCount = 0
}
} }
}, },
mounted() { mounted() {
this.getElementData('#quweiIcon') this.getElementData('#quweiIcon')
this.getIconData('#icon1') this.getIconData('#icon1')
if (this.isLogin) {
this.fetchUnreadCount()
}
}, },
methods: { methods: {
getElementData(el) { getElementData(el) {
@@ -117,10 +151,18 @@ export default {
} }
}) })
}, },
/** fetchUnreadCount() {
* 跳转至目标页面 getUnreadMessageCount().then(res => {
* type: 1-tabbar页面,2-非tabbar页面 const { success, data } = res
*/ if (success) {
this.unreadCount = data || 0
} else {
this.unreadCount = 0
}
}).catch(() => {
this.unreadCount = 0
})
},
gotoPage(url, type = 1, index) { gotoPage(url, type = 1, index) {
if (this.currIndex === index) return if (this.currIndex === index) return
if (type === 1) { if (type === 1) {
@@ -199,6 +241,7 @@ export default {
} }
.item { .item {
width: 140rpx; width: 140rpx;
position: relative;
&.curr { &.curr {
.name { .name {
color: #C52733; color: #C52733;
@@ -211,6 +254,21 @@ export default {
} }
} }
} }
.badge {
position: absolute;
top: -4rpx;
right: 32rpx;
min-width: 28rpx;
padding: 0 6rpx;
height: 28rpx;
line-height: 28rpx;
border-radius: 14rpx;
background-color: #FF564A;
color: #ffffff;
font-size: 20rpx;
text-align: center;
box-sizing: border-box;
}
} }
} }
} }