2025-05-15 14:17:51 +08:00
|
|
|
|
// store/useReadMsg.js
|
|
|
|
|
|
import {
|
|
|
|
|
|
defineStore
|
|
|
|
|
|
} from 'pinia'
|
|
|
|
|
|
import {
|
|
|
|
|
|
ref,
|
|
|
|
|
|
computed,
|
|
|
|
|
|
watch
|
|
|
|
|
|
} from 'vue'
|
|
|
|
|
|
import {
|
|
|
|
|
|
msg,
|
|
|
|
|
|
$api,
|
|
|
|
|
|
} from '../common/globalFunction';
|
2025-05-16 09:58:21 +08:00
|
|
|
|
|
2025-11-30 16:47:06 +08:00
|
|
|
|
// 常量定义:消息在 TabBar 的索引位置
|
|
|
|
|
|
const TABBAR_INDEX = 3;
|
|
|
|
|
|
|
2025-05-15 14:17:51 +08:00
|
|
|
|
export const useReadMsg = defineStore('readMsg', () => {
|
|
|
|
|
|
const msgList = ref([])
|
2025-11-30 16:47:06 +08:00
|
|
|
|
// 用于自定义 Tabbar 组件的渲染
|
2025-07-21 14:49:45 +08:00
|
|
|
|
const badges = ref([{
|
2025-11-30 16:47:06 +08:00
|
|
|
|
count: 0
|
|
|
|
|
|
}, {
|
|
|
|
|
|
count: 0
|
|
|
|
|
|
}, {
|
|
|
|
|
|
count: 0
|
|
|
|
|
|
}, {
|
|
|
|
|
|
count: 0
|
|
|
|
|
|
}, {
|
|
|
|
|
|
count: 0
|
|
|
|
|
|
}])
|
2025-05-15 14:17:51 +08:00
|
|
|
|
|
2025-11-30 16:47:06 +08:00
|
|
|
|
// 计算总未读数量
|
2025-05-15 14:17:51 +08:00
|
|
|
|
const unreadCount = computed(() =>
|
|
|
|
|
|
msgList.value.reduce((sum, msg) => sum + (msg.notReadCount || 0), 0)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 未读消息列表
|
|
|
|
|
|
const unreadMsgList = computed(() =>
|
|
|
|
|
|
msgList.value.filter(msg => msg.notReadCount > 0)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-11-30 16:47:06 +08:00
|
|
|
|
function updateBadgeEffect() {
|
2025-05-15 14:17:51 +08:00
|
|
|
|
const count = unreadCount.value
|
2025-11-30 16:47:06 +08:00
|
|
|
|
// 处理显示文本:超过99显示99+
|
|
|
|
|
|
const countStr = count > 99 ? '99+' : String(count)
|
|
|
|
|
|
|
|
|
|
|
|
// 1. 更新内部状态 (用于自定义 UI)
|
|
|
|
|
|
if (badges.value[TABBAR_INDEX]) {
|
|
|
|
|
|
badges.value[TABBAR_INDEX].count = count === 0 ? 0 : countStr
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 更新系统原生 TabBar
|
|
|
|
|
|
// 加 try-catch 防止在非 Tabbar 页面或加栽未完成时报错
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
|
uni.setTabBarBadge({
|
|
|
|
|
|
index: TABBAR_INDEX,
|
|
|
|
|
|
text: countStr
|
|
|
|
|
|
})
|
|
|
|
|
|
} else {
|
|
|
|
|
|
uni.removeTabBarBadge({
|
|
|
|
|
|
index: TABBAR_INDEX
|
|
|
|
|
|
})
|
2025-07-21 14:49:45 +08:00
|
|
|
|
}
|
2025-11-30 16:47:06 +08:00
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.warn('TabBar Badge 更新失败(可能当前非TabBar页面):', e)
|
2025-05-15 14:17:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-30 16:47:06 +08:00
|
|
|
|
watch(unreadCount, () => {
|
|
|
|
|
|
updateBadgeEffect()
|
|
|
|
|
|
console.log('value', unreadCount.value)
|
|
|
|
|
|
}, {
|
|
|
|
|
|
immediate: true
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-05-15 14:17:51 +08:00
|
|
|
|
|
|
|
|
|
|
// 拉取消息列表
|
|
|
|
|
|
async function fetchMessages() {
|
|
|
|
|
|
try {
|
2025-11-30 16:47:06 +08:00
|
|
|
|
const res = await $api.createRequest('/app/notice/info', {
|
2025-05-15 14:17:51 +08:00
|
|
|
|
isRead: 1
|
2025-11-30 16:47:06 +08:00
|
|
|
|
}, "GET")
|
|
|
|
|
|
msgList.value = res.data || []
|
2025-05-15 14:17:51 +08:00
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error('获取消息失败:', err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置为已读
|
|
|
|
|
|
async function markAsRead(item, index) {
|
2025-11-30 16:47:06 +08:00
|
|
|
|
const targetMsg = msgList.value[index]
|
|
|
|
|
|
if (!targetMsg) return
|
|
|
|
|
|
|
|
|
|
|
|
// 如果已经是已读,直接返回,避免无效请求
|
|
|
|
|
|
// 假设服务端逻辑是:isRead=1 表示已读 (注意检查你的字段定义)
|
|
|
|
|
|
// 你的原代码判断是 if (msg.isRead === 1) return,如果是这样,下面请求成功应该设为 1
|
|
|
|
|
|
// 但通常未读是0,已读是1。这里维持你原有的逻辑,假设服务端把 notReadCount 清零
|
2025-05-15 14:17:51 +08:00
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
let params = {
|
2025-11-30 16:47:06 +08:00
|
|
|
|
id: targetMsg.noticeId
|
2025-05-15 14:17:51 +08:00
|
|
|
|
}
|
2025-11-30 16:47:06 +08:00
|
|
|
|
await $api.createRequest('/app/notice/read?id=' + targetMsg.noticeId, params, "POST")
|
|
|
|
|
|
|
|
|
|
|
|
// 更新本地数据
|
|
|
|
|
|
msgList.value[index].notReadCount = 0
|
|
|
|
|
|
msgList.value[index].isRead = 1 // 标记已读状态
|
2025-05-15 14:17:51 +08:00
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error('设置消息已读失败:', err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
2025-07-21 14:49:45 +08:00
|
|
|
|
badges,
|
2025-05-15 14:17:51 +08:00
|
|
|
|
msgList,
|
|
|
|
|
|
unreadMsgList,
|
|
|
|
|
|
unreadCount,
|
|
|
|
|
|
fetchMessages,
|
|
|
|
|
|
markAsRead,
|
2025-11-30 16:47:06 +08:00
|
|
|
|
updateTabBarBadge: updateBadgeEffect
|
2025-05-15 14:17:51 +08:00
|
|
|
|
}
|
2025-11-28 19:47:42 +08:00
|
|
|
|
}, {
|
2025-11-30 16:47:06 +08:00
|
|
|
|
unistorage: true, // 开启持久化
|
2025-05-15 14:17:51 +08:00
|
|
|
|
})
|