liujie
2026-09-16 a3a56121a476cd23d0f29bc391c07374dc5008f2
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
  }
}