03f8caf3e9
後端 - 新增 6 個 Notification class(預約建立/確認/拒絕/取消/完成、收到評價),database + mail 雙 channel - 新增 NotificationController(list / unread-count / markRead / markAllRead / destroy) - 整合通知觸發至 MemberBookingController、ProviderBookingController、CompleteFinishedBookings、ReviewController - 新增 notifications / jobs / failed_jobs migration - Docker Compose 加入 queue-worker、mailpit service - DivingOffer 補上 provider() 關聯 前端 - 新增 notificationStore(Polling 30s/60s 自適應 + Page Visibility API) - 新增 NotificationBell(未讀 Badge)、NotificationDrawer(側邊通知中心) - main.js:auth store init 前置於 router.use(),修正 beforeEach guard 時序問題 - notificationAxios:依路徑動態選擇 member/coach token - NotificationDrawer:改用 new URL().pathname 提取 action_url 路徑 OpenSpec - 歸檔 notification-system change - 同步 notification-core / notification-email / notification-triggers specs 至主規格 - 更新 booking-lifecycle / review-lifecycle spec(補充通知觸發 requirement) Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
116 lines
3.2 KiB
JavaScript
116 lines
3.2 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { ref } from 'vue'
|
|
import api from '../api/notificationAxios'
|
|
|
|
export const useNotificationStore = defineStore('notifications', () => {
|
|
const unreadCount = ref(0)
|
|
const notifications = ref([])
|
|
const isOpen = ref(false)
|
|
|
|
let intervalId = null
|
|
let currentInterval = null
|
|
let visibilityHandler = null
|
|
|
|
async function fetchUnreadCount() {
|
|
try {
|
|
const res = await api.get('/notifications/unread-count')
|
|
const newCount = res.data?.data?.count ?? 0
|
|
if (newCount !== unreadCount.value) {
|
|
const wasZero = unreadCount.value === 0
|
|
unreadCount.value = newCount
|
|
if ((wasZero && newCount > 0) || (!wasZero && newCount === 0)) {
|
|
restartInterval()
|
|
}
|
|
}
|
|
} catch (e) {
|
|
console.error('[NotificationStore] fetchUnreadCount failed:', e?.response?.status, e?.message)
|
|
}
|
|
}
|
|
|
|
async function fetchNotifications() {
|
|
try {
|
|
const res = await api.get('/notifications')
|
|
notifications.value = res.data.data
|
|
unreadCount.value = res.data.unread_count
|
|
} catch (e) {
|
|
console.error('[NotificationStore] fetchNotifications failed:', e?.response?.status, e?.message)
|
|
}
|
|
}
|
|
|
|
function getInterval() {
|
|
return unreadCount.value > 0 ? 30000 : 60000
|
|
}
|
|
|
|
function restartInterval() {
|
|
if (intervalId) clearInterval(intervalId)
|
|
const ms = getInterval()
|
|
currentInterval = ms
|
|
intervalId = setInterval(fetchUnreadCount, ms)
|
|
}
|
|
|
|
function startPolling() {
|
|
fetchUnreadCount()
|
|
restartInterval()
|
|
|
|
visibilityHandler = () => {
|
|
if (document.visibilityState === 'hidden') {
|
|
if (intervalId) clearInterval(intervalId)
|
|
intervalId = null
|
|
} else {
|
|
fetchUnreadCount()
|
|
restartInterval()
|
|
}
|
|
}
|
|
document.addEventListener('visibilitychange', visibilityHandler)
|
|
}
|
|
|
|
function stopPolling() {
|
|
if (intervalId) clearInterval(intervalId)
|
|
intervalId = null
|
|
if (visibilityHandler) {
|
|
document.removeEventListener('visibilitychange', visibilityHandler)
|
|
visibilityHandler = null
|
|
}
|
|
unreadCount.value = 0
|
|
notifications.value = []
|
|
isOpen.value = false
|
|
}
|
|
|
|
async function markRead(id) {
|
|
const n = notifications.value.find(n => n.id === id)
|
|
if (n && !n.read_at) {
|
|
n.read_at = new Date().toISOString()
|
|
unreadCount.value = Math.max(0, unreadCount.value - 1)
|
|
}
|
|
try {
|
|
await api.patch(`/notifications/${id}/read`)
|
|
} catch {}
|
|
}
|
|
|
|
async function markAllRead() {
|
|
notifications.value.forEach(n => {
|
|
if (!n.read_at) n.read_at = new Date().toISOString()
|
|
})
|
|
unreadCount.value = 0
|
|
try {
|
|
await api.patch('/notifications/read-all')
|
|
} catch {}
|
|
}
|
|
|
|
async function remove(id) {
|
|
const n = notifications.value.find(n => n.id === id)
|
|
if (n && !n.read_at) unreadCount.value = Math.max(0, unreadCount.value - 1)
|
|
notifications.value = notifications.value.filter(n => n.id !== id)
|
|
try {
|
|
await api.delete(`/notifications/${id}`)
|
|
} catch {}
|
|
}
|
|
|
|
return {
|
|
unreadCount, notifications, isOpen,
|
|
fetchNotifications, fetchUnreadCount,
|
|
startPolling, stopPolling,
|
|
markRead, markAllRead, remove,
|
|
}
|
|
})
|