import { request } from '../utils/request' import notificationSound from '../static/notification.mp3' const ORDER_ALERT_TYPES = new Set(['ORDER_ASSIGNED', 'ORDER_CANCELLED']) const MINI_PROGRAM_SOUND_PATH = '/static/notification.mp3' let miniProgramAudio = null let webAudioContext = null export function isOrderAlertType(type) { return ORDER_ALERT_TYPES.has(String(type || '').toUpperCase()) } function normalizeMessage(item) { const type = String(item.type || item.notificationType || '').toUpperCase() const prototypeCopy = { ORDER_ASSIGNED: { title: '接单提醒', content: '您有一个新的讲解订单,请查看' }, ORDER_CANCELLED: { title: '取消提醒', content: '您有一个讲解订单被取消,请查看' }, AUDIT_APPROVED: { title: '系统提醒', content: '您的资料审核已通过' }, AUDIT_REJECTED: { title: '系统提醒', content: '您的资料审核未通过' }, ORDER_REVIEWED: { title: '评价提醒', content: '您有一条新的订单评价,请查看' } }[type] return { ...item, type, ...(prototypeCopy || {}), time: item.time ? item.time.replace('T', ' ') : '', url: type === 'ORDER_REVIEWED' ? '/pages/rating/index' : (item.targetUrl || (item.orderId ? `/pages/order-detail/index?id=${item.orderId}` : '')) } } function playWebTone() { if (typeof window === 'undefined') return false const AudioContext = window.AudioContext || window.webkitAudioContext if (!AudioContext) return false if (!webAudioContext) webAudioContext = new AudioContext() const context = webAudioContext if (context.state === 'suspended' && context.resume) context.resume() const oscillator = context.createOscillator() const gain = context.createGain() oscillator.type = 'sine' oscillator.frequency.value = 880 gain.gain.setValueAtTime(0.12, context.currentTime) gain.gain.exponentialRampToValueAtTime(0.001, context.currentTime + 0.22) oscillator.connect(gain) gain.connect(context.destination) oscillator.start() oscillator.stop(context.currentTime + 0.22) return true } function playMiniProgramTone() { if (typeof uni === 'undefined' || typeof uni.createInnerAudioContext !== 'function') return false if (!miniProgramAudio) { miniProgramAudio = uni.createInnerAudioContext() miniProgramAudio.obeyMuteSwitch = false miniProgramAudio.src = typeof wx !== 'undefined' ? MINI_PROGRAM_SOUND_PATH : notificationSound } miniProgramAudio.stop() const playback = miniProgramAudio.play() if (playback && typeof playback.catch === 'function') { playback.catch(() => playWebTone()) } return true } function playNativeTone() { if (typeof plus === 'undefined' || !plus.device || !plus.device.beep) return false plus.device.beep(1) return true } export function getMessageList() { return request({ url: '/guide/notifications' }).then((list) => list.map(normalizeMessage)) } export function getUnreadMessageCount() { return getMessageList().then((list) => list.filter((item) => !item.read).length) } export function markMessageRead(id) { return request({ url: `/guide/notifications/${id}/read`, method: 'PUT' }) } export function playNotificationAlert(type) { if (type && !isOrderAlertType(type)) return false if (typeof uni !== 'undefined' && uni.vibrateShort) { uni.vibrateShort({ type: 'medium' }) } try { return Boolean(playMiniProgramTone() || playNativeTone() || playWebTone()) } catch (error) { return false } }