import { request } from '../utils/request'
|
|
function normalizeMessage(item) {
|
const prototypeCopy = {
|
ORDER_ASSIGNED: {
|
title: '接单提醒',
|
content: '您有一个新的讲解订单,请查看'
|
},
|
ORDER_CANCELLED: {
|
title: '取消提醒',
|
content: '您有一个讲解订单被取消,请查看'
|
},
|
AUDIT_APPROVED: {
|
title: '系统提醒',
|
content: '您的资料审核已通过'
|
},
|
AUDIT_REJECTED: {
|
title: '系统提醒',
|
content: '您的资料审核未通过'
|
},
|
ORDER_REVIEWED: {
|
title: '评价提醒',
|
content: '您有一条新的订单评价,请查看'
|
}
|
}[item.type]
|
|
return {
|
...item,
|
...(prototypeCopy || {}),
|
time: item.time ? item.time.replace('T', ' ') : '',
|
url: 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
|
const context = new AudioContext()
|
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 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() {
|
if (typeof uni !== 'undefined' && uni.vibrateShort) {
|
uni.vibrateShort({ type: 'medium' })
|
}
|
try {
|
playNativeTone() || playWebTone()
|
} catch (error) {
|
return false
|
}
|
return true
|
}
|