liujie
2026-07-30 280ef5100bece9b40ac51736aef037704a52a4ca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { request } from '../utils/request'
 
function normalizeMessage(item) {
  return {
    ...item,
    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
}