新增设置语音播报开关并完善收货列表语音播报逻辑

This commit is contained in:
zm2000 2026-04-24 17:57:40 +08:00
parent eaa0d03207
commit 7c7639a1e9
4 changed files with 211 additions and 18 deletions

View File

@ -12,6 +12,9 @@ const defaultSettings = {
'scan.sound': 'true',
'scan.vibrate': 'true',
'scan.auto_focus': 'true',
// 声音与通知
'voice.enabled': 'true',
// 界面相关
'ui.theme': 'light',
@ -242,6 +245,19 @@ const getters = {
auto_focus: state.settings['scan.auto_focus'] === 'true'
};
},
// 获取声音与通知设置
voiceSettings: state => {
if (Object.keys(state.settings).length === 0) {
return {
enabled: defaultSettings['voice.enabled'] === 'true'
};
}
return {
enabled: state.settings['voice.enabled'] === 'true'
};
},
// 获取界面设置
uiSettings: state => {

View File

@ -148,6 +148,22 @@ export default {
* @return {Promise<Boolean>}
*/
speakText(text = '', options = {}) {
try {
const saved = uni.getStorageSync('app_settings');
let settings = null;
if (typeof saved === 'string' && saved) {
settings = JSON.parse(saved);
} else if (saved && typeof saved === 'object') {
settings = saved;
}
if (settings && settings['voice.enabled'] === 'false') {
return Promise.resolve(false);
}
} catch (error) {
console.warn('[tools.speakText] 读取语音播报开关失败,使用默认开启:', error);
}
return voice.speak(text, options);
},

View File

@ -12,12 +12,22 @@
<view class="settings-list">
<u-cell-group :border="false">
<u-cell v-for="(item, index) in operationList" :key="index" :title="item.title" :icon="item.icon"
:icon-color="item.iconColor" @click="handleOperationClick(item)">
<!-- 角标 -->
<template #right-icon>
<u-badge v-if="item.badge && item.badge > 0" :value="item.badge" type="error"
size="mini"></u-badge>
<u-icon v-else name="arrow-right" size="20" color="#ccc"></u-icon>
:icon-color="item.iconColor" :isLink="true" @click="handleOperationClick(item)">
</u-cell>
</u-cell-group>
</view>
<!-- 声音与通知 -->
<view class="settings-list settings-list--notification">
<view class="settings-section-title">声音与通知</view>
<u-cell-group :border="false">
<u-cell title="语音播报" icon="volume" icon-color="#ff9900">
<template #value>
<u-switch
v-model="voiceBroadcastEnabled"
:active-color="'#4cd964'"
@change="handleVoiceBroadcastToggle"
></u-switch>
</template>
</u-cell>
</u-cell-group>
@ -94,12 +104,14 @@
// 登出相关
showLogoutModal: false,
logoutLoading: false
logoutLoading: false,
voiceBroadcastEnabled: true
}
},
onShow() {
// 页面显示时加载用户信息
this.loadUserInfo();
this.loadVoiceSettings();
},
methods: {
// 加载用户信息
@ -115,6 +127,36 @@
}
},
async loadVoiceSettings() {
try {
const enabled = await this.$store.dispatch('setting/getValue', 'voice.enabled');
this.voiceBroadcastEnabled = enabled !== 'false';
} catch (error) {
console.warn('加载语音播报设置失败:', error);
this.voiceBroadcastEnabled = true;
}
},
async handleVoiceBroadcastToggle(value) {
const enabled = value !== false;
this.voiceBroadcastEnabled = enabled;
try {
await this.$store.dispatch('setting/setValue', {
key: 'voice.enabled',
value: enabled ? 'true' : 'false'
});
uni.showToast({
title: enabled ? '已开启语音播报' : '已关闭语音播报',
icon: 'none'
});
} catch (error) {
console.error('保存语音播报设置失败:', error);
this.voiceBroadcastEnabled = !enabled;
uni.showToast({
title: '保存失败,请重试',
icon: 'none'
});
}
},
// 处理设置项点击
handleOperationClick(item) {
console.log('点击了:', item.title);
@ -269,6 +311,16 @@
}
}
.settings-list--notification {
padding-top: 6rpx;
}
.settings-section-title {
font-size: 26rpx;
color: #909399;
padding: 10rpx 24rpx 6rpx;
}
// 登出按钮区域
.logout-section {
padding: 40rpx 30rpx;

View File

@ -259,15 +259,19 @@
onShow() {
this.$store.dispatch('broadcast/resume');
this.loadReceivingList();
this.loadReceivingList({
silentVoice: true
});
},
onHide() {
this.$store.dispatch('broadcast/stop');
this.stopReceivingSpeak();
},
onUnload() {
this.$store.dispatch('broadcast/stop');
this.stopReceivingSpeak();
},
onPullDownRefresh() {
@ -277,11 +281,88 @@
watch: {
currentPage(newval) {
console.log('页码变化:', newval);
this.loadReceivingList();
this.loadReceivingList({
silentVoice: false
});
}
},
methods: {
normalizeSpeechText(message) {
const rawText = String(message || '').trim();
if (!rawText) {
return '';
}
const digitMap = {
'0': '零',
'1': '一',
'2': '二',
'3': '三',
'4': '四',
'5': '五',
'6': '六',
'7': '七',
'8': '八',
'9': '九'
};
const normalizeCodeSegment = (segment) => {
return segment
.split('-')
.map(part => String(part || '')
.split('')
.map(char => (digitMap[char] || char))
.join(''))
.join('的');
};
return rawText.replace(/([A-Za-z0-9]+(?:-[A-Za-z0-9]+)+)/g, normalizeCodeSegment);
},
speakReceivingMessage(message, options = {}) {
const text = this.normalizeSpeechText(message);
if (!text) {
return;
}
const tools = this.$tools;
if (!tools || typeof tools.speakText !== 'function') {
return;
}
try {
const task = tools.speakText(text, options);
if (task && typeof task.catch === 'function') {
task.catch(error => {
console.warn('[ReceivingList] 语音播报失败:', error);
});
}
} catch (error) {
console.warn('[ReceivingList] 语音播报异常:', error);
}
},
stopReceivingSpeak() {
const tools = this.$tools;
if (!tools || typeof tools.stopSpeak !== 'function') {
return;
}
try {
tools.stopSpeak();
} catch (error) {
console.warn('[ReceivingList] 停止语音播报异常:', error);
}
},
getReceivingListVoiceText() {
const modeText = this.activeStatus === 'all'
? '全部状态'
: this.getStatusText(this.activeStatus);
const count = Number(this.totalItems || 0);
if (count > 0) {
return `${modeText},查询到${count}条收货任务`;
}
return `${modeText},未查询到收货任务`;
},
// 日期格式化函数
formatDate(date) {
const d = new Date(date);
@ -448,7 +529,8 @@
},
// 加载收货列表
async loadReceivingList() {
async loadReceivingList(options = {}) {
const silentVoice = !!(options && options.silentVoice);
if (this.loading) return;
this.loading = true;
@ -514,6 +596,10 @@
if (summary) {
this.updateStatusCount(summary);
}
if (!silentVoice) {
this.speakReceivingMessage(this.getReceivingListVoiceText());
}
}
} catch (error) {
console.error('加载收货列表失败:', error);
@ -521,6 +607,9 @@
title: '加载失败',
icon: 'error'
});
if (!silentVoice) {
this.speakReceivingMessage('加载收货列表失败,请重试');
}
} finally {
this.loading = false;
this.refreshing = false;
@ -561,22 +650,37 @@
},
// 搜索处理
handleSearch() {
this.currentPage = 1;
this.loadReceivingList();
if (this.currentPage !== 1) {
this.currentPage = 1;
return;
}
this.loadReceivingList({
silentVoice: false
});
},
// 状态切换
changeStatus(status) {
this.activeStatus = status;
this.currentPage = 1;
this.loadReceivingList();
if (this.currentPage !== 1) {
this.currentPage = 1;
return;
}
this.loadReceivingList({
silentVoice: false
});
},
// 下拉刷新
onRefresh() {
this.refreshing = true;
this.currentPage = 1;
this.loadReceivingList();
if (this.currentPage !== 1) {
this.currentPage = 1;
return;
}
this.loadReceivingList({
silentVoice: false
});
},
// 显示筛选弹窗
@ -600,8 +704,13 @@
// 应用筛选
applyFilter() {
this.filterVisible = false;
this.currentPage = 1;
this.loadReceivingList();
if (this.currentPage !== 1) {
this.currentPage = 1;
return;
}
this.loadReceivingList({
silentVoice: false
});
},
// 新建收货