测试版
This commit is contained in:
78
src/stores/noticeStore.js
Normal file
78
src/stores/noticeStore.js
Normal file
@@ -0,0 +1,78 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import { getActiveNotices } from '@/api/notice'
|
||||
|
||||
export const useNoticeStore = defineStore('notice', () => {
|
||||
// 状态
|
||||
const notices = ref([])
|
||||
const isLoading = ref(false)
|
||||
const dismissedIds = ref([]) // 当前会话已关闭的公告 ID
|
||||
const lastFetchTime = ref(null)
|
||||
const useMock = ref(true) // 后台接口就绪后改为 false
|
||||
|
||||
// 过滤已关闭公告后的有效列表
|
||||
const activeNotices = computed(() =>
|
||||
notices.value.filter(n => !dismissedIds.value.includes(n.id))
|
||||
)
|
||||
|
||||
// 是否有可显示的公告
|
||||
const hasNotices = computed(() => activeNotices.value.length > 0)
|
||||
|
||||
/**
|
||||
* 从后台拉取公告
|
||||
*/
|
||||
const fetchNotices = async () => {
|
||||
if (isLoading.value) return
|
||||
if (useMock.value) {
|
||||
loadMockNotices()
|
||||
return
|
||||
}
|
||||
|
||||
isLoading.value = true
|
||||
try {
|
||||
const res = await getActiveNotices()
|
||||
if (res && res.data) {
|
||||
notices.value = res.data
|
||||
lastFetchTime.value = Date.now()
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[NoticeStore] 获取公告失败:', error)
|
||||
} finally {
|
||||
isLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭某条公告(仅当前会话生效)
|
||||
*/
|
||||
const dismissNotice = (id) => {
|
||||
if (!dismissedIds.value.includes(id)) {
|
||||
dismissedIds.value.push(id)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载 Mock 数据(后台接口未就绪时使用)
|
||||
*/
|
||||
const loadMockNotices = () => {
|
||||
notices.value = [
|
||||
{ id: 1, content: '欢迎使用 Yolo 系统,如有问题请联系管理员。', type: 'info' },
|
||||
{ id: 2, content: '系统将于本周六凌晨 2:00-4:00 进行维护升级,届时服务将暂停,请提前做好安排。', type: 'warning' },
|
||||
]
|
||||
lastFetchTime.value = Date.now()
|
||||
}
|
||||
|
||||
return {
|
||||
// 状态
|
||||
notices,
|
||||
activeNotices,
|
||||
hasNotices,
|
||||
isLoading,
|
||||
useMock,
|
||||
|
||||
// 方法
|
||||
fetchNotices,
|
||||
dismissNotice,
|
||||
loadMockNotices,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user