新增打手端新订单语音播报开关

This commit is contained in:
yml2213
2026-08-22 09:12:26 +08:00
parent 700bda8250
commit 1f7ff016df
4 changed files with 131 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
const WORKER_SPEECH_KEY = 'order-site-worker-new-order-speech'
export function isWorkerSpeechEnabled() {
return window.localStorage.getItem(WORKER_SPEECH_KEY) === '1'
}
export function setWorkerSpeechEnabled(enabled: boolean) {
window.localStorage.setItem(WORKER_SPEECH_KEY, enabled ? '1' : '0')
}
/** 用浏览器语音合成播报一段中文;不支持或被浏览器拦截时静默失败。 */
export function speakWorkerText(text: string) {
if (!('speechSynthesis' in window)) return
try {
window.speechSynthesis.cancel()
const utterance = new SpeechSynthesisUtterance(text)
utterance.lang = 'zh-CN'
utterance.rate = 1
utterance.volume = 1
const zhVoice = window.speechSynthesis
.getVoices()
.find((voice) => voice.lang.toLowerCase().startsWith('zh'))
if (zhVoice) utterance.voice = zhVoice
window.speechSynthesis.speak(utterance)
} catch {
// 部分浏览器禁用语音合成,不影响页面功能。
}
}