front/src/composables/useOperationScanFeedback.ts
zzh 3ce8955472
All checks were successful
Build and Deploy Front / deploy (push) Successful in 4m11s
feat: 经营地图放大下钻与中美口岸标注,并完善扫码/品牌/培训
地图支持全屏漫游、美国邮编港口与中国城市口岸图层;同步入出库扫码与培训手册等改动。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-20 13:55:47 +08:00

87 lines
3.0 KiB
TypeScript

import { ref } from 'vue';
import { extractOmsRequestError } from '@/api/oms/common';
import { useOperationVoiceBroadcast } from './useOperationVoiceBroadcast';
export type ScanFeedbackType = 'idle' | 'success' | 'error' | 'warning';
/** 高频业务失败:界面仍显示完整句,语音播短词,便于连扫 */
const SHORT_VOICE_RULES: Array<{ test: RegExp; voice: string }> = [
{ test: /订单已出库/, voice: '已出库' },
{ test: /未找到对应订单|未找到大包|未找到主单/, voice: '未找到' },
{ test: /未付款/, voice: '未付款' },
{ test: /入库处理中|请勿重复扫描/, voice: '处理中' },
{ test: /不允许出库|不允许入库|仅待发货|状态为/, voice: '状态不符' },
{ test: /实重与长宽高|件重尺|不能为空且须大于/, voice: '请补件重尺' },
{ test: /请扫描或输入单号|运单号不能为空|请输入大包号|请输入主单号/, voice: '请扫单号' },
{ test: /库存不足|SKU 库存/, voice: '库存不足' },
{ test: /已在本袋中|已在其它大包|已在本主单|已归属其它/, voice: '重复扫描' },
];
function resolveVoiceText(message: string): string {
const text = message.trim();
if (!text) return text;
for (const rule of SHORT_VOICE_RULES) {
if (rule.test.test(text)) return rule.voice;
}
return text;
}
/**
* 扫描页左侧反馈区 + 语音播报。
*/
export function useOperationScanFeedback(pageKey: string) {
const { voiceEnabled, speak, stopSpeak } = useOperationVoiceBroadcast(pageKey);
const feedbackType = ref<ScanFeedbackType>('idle');
const feedbackMessage = ref('');
function clearFeedback() {
feedbackType.value = 'idle';
feedbackMessage.value = '';
}
function showFeedback(type: ScanFeedbackType, message: string, withVoice = true) {
const text = String(message || '').trim();
feedbackType.value = type;
feedbackMessage.value = text;
if (withVoice && text && (type === 'error' || type === 'success' || type === 'warning')) {
speak(type === 'error' || type === 'warning' ? resolveVoiceText(text) : text);
}
}
function showError(message: string, withVoice = true) {
showFeedback('error', message, withVoice);
}
function showSuccess(message: string, withVoice = true) {
showFeedback('success', message, withVoice);
}
function showWarning(message: string, withVoice = true) {
showFeedback('warning', message, withVoice);
}
/** 从 axios / 业务响应提取可读错误,写入反馈区并短语音播报 */
function showRequestError(err: unknown, fallback = '请求失败', withVoice = true) {
const msg = extractOmsRequestError(err) || fallback;
const text =
/request failed with status code/i.test(msg) || /^Network Error$/i.test(msg)
? fallback
: msg;
showError(text || fallback, withVoice);
}
return {
voiceEnabled,
feedbackType,
feedbackMessage,
speak,
stopSpeak,
clearFeedback,
showFeedback,
showError,
showSuccess,
showWarning,
showRequestError,
};
}