Kaynağa Gözat

提示语优化

shish 6 ay önce
ebeveyn
işleme
f3c96e3553

+ 112 - 31
ghsApp/src/utils/util.js

@@ -322,43 +322,124 @@ export async function callUp(mobile){
 	//#endif
 }
 
-//没有库存时的语音播放 shish
-export const noStockRemind = () =>{
-	try{
-		if(uni.getSystemInfoSync().platform != 'windows'){
-			const innerAudioContext = uni.createInnerAudioContext()
-			innerAudioContext.autoplay = true
-			innerAudioContext.src = "/static/noStock.mp3"
-			innerAudioContext.volume = 1
-			innerAudioContext.onPlay()
-			innerAudioContext.onError()
-			innerAudioContext.onPause(function() {
-				innerAudioContext.destroy()
-			})
+// 库存不足提示音 - 缓存对象,避免重复创建
+let noStockAudioContext = null
+let lastNoStockPlayTime = 0
+const NO_STOCK_MIN_INTERVAL = 1000
+
+const initNoStockAudio = () => {
+	if (noStockAudioContext) return
+	try {
+		if (uni.getSystemInfoSync().platform === 'windows') return
+		
+		noStockAudioContext = uni.createInnerAudioContext()
+		noStockAudioContext.autoplay = false
+		noStockAudioContext.src = "/static/noStock.mp3"
+		noStockAudioContext.volume = 1
+	} catch (e) {
+		console.error('[noStockRemind] 初始化失败:', e)
+		noStockAudioContext = null
+	}
+}
+
+export const noStockRemind = () => {
+	try {
+		// 防抖:1秒内只播放一次
+		const now = Date.now()
+		if (now - lastNoStockPlayTime < NO_STOCK_MIN_INTERVAL) {
+			return
 		}
+		
+		// 初始化音频
+		if (!noStockAudioContext) {
+			initNoStockAudio()
+		}
+		
+		if (!noStockAudioContext) {
+			return
+		}
+		
+		// 更新时间戳
+		lastNoStockPlayTime = now
+		
+		// 停止之前的播放
+		try {
+			noStockAudioContext.stop()
+		} catch (e) {
+			// 忽略停止失败
+		}
+		
+		// 设置音量并播放
+		noStockAudioContext.volume = 1
+		try {
+			noStockAudioContext.play()
+		} catch (playError) {
+			console.error('[noStockRemind] 播放异常:', playError)
+			noStockAudioContext = null
+		}
+	} catch (e) {
+		console.error('[noStockRemind] 执行出错:', e)
+	}
+}
+
+// 点击声效 - 缓存对象,避免重复创建
+let hitAudioContext = null
+let lastHitPlayTime = 0
+const HIT_MIN_INTERVAL = 100
+
+const initHitAudio = () => {
+	if (hitAudioContext) return
+	try {
+		if (uni.getSystemInfoSync().platform === 'windows') return
+		
+		hitAudioContext = uni.createInnerAudioContext()
+		hitAudioContext.autoplay = false
+		hitAudioContext.src = '/static/hit.mp3'
+		hitAudioContext.volume = 1
 	} catch (e) {
-      // 开发环境兼容处理
-      console.warn('开发环境音频播放异常(可忽略):', e);
-    }
+		console.error('[hitRemind] 初始化失败:', e)
+		hitAudioContext = null
+	}
 }
 
-//点击声效 shish
-export const hitRemind = () =>{
-	try{
-		if(uni.getSystemInfoSync().platform != 'windows'){
-			const innerAudioContext = uni.createInnerAudioContext()
-			innerAudioContext.autoplay = true
-			innerAudioContext.src = '/static/hit.mp3'
-			innerAudioContext.onPlay()
-			innerAudioContext.onError()
-			innerAudioContext.onPause(function() {
-				innerAudioContext.destroy()
-			})
+export const hitRemind = () => {
+	try {
+		// 防抖:100ms 内只播放一次
+		const now = Date.now()
+		if (now - lastHitPlayTime < HIT_MIN_INTERVAL) {
+			return
+		}
+		
+		// 初始化音频
+		if (!hitAudioContext) {
+			initHitAudio()
+		}
+		
+		if (!hitAudioContext) {
+			return
+		}
+		
+		// 更新时间戳
+		lastHitPlayTime = now
+		
+		// 停止之前的播放
+		try {
+			hitAudioContext.stop()
+		} catch (e) {
+			// 忽略停止失败
+		}
+		
+		// 设置音量并播放
+		hitAudioContext.volume = 1
+		try {
+			hitAudioContext.play()
+		} catch (playError) {
+			console.error('[hitRemind] 播放异常:', playError)
+			hitAudioContext = null
 		}
 	} catch (e) {
-      // 开发环境兼容处理
-      console.warn('开发环境音频播放异常(可忽略):', e);
-    }
+		console.error('[hitRemind] 执行出错:', e)
+	}
 }
 
 //去重 姜枫 2021.05.05

+ 1 - 1
ghsPad/src/mixins/product.js

@@ -227,7 +227,7 @@ export default {
 				if(item.unitType == 0){
 					if(Number(item.currentNum)>Number(item.stock)){
 						this.$util.noStockRemind()
-						this.$msg("库存不足")
+						this.$msg("库存不足")
 						return
 					}
 				}else{

+ 117 - 23
ghsPad/src/utils/util.js

@@ -295,31 +295,125 @@ export const callUp = (mobile) =>{
 	//#endif
 }
 
-//没有库存时的语音播放 shish
-export const noStockRemind = () =>{
-	const audioContext = uni.createInnerAudioContext()
-	audioContext.autoplay = true
-	audioContext.volume = 1
-	audioContext.src = "/static/noStock.mp3"
-	audioContext.onError(function() {
-		audioContext.destroy()
-	})
-	audioContext.onPause(function() {
-		audioContext.destroy()
-	})
-	audioContext.onStop(function() {
-		audioContext.destroy()
-	})
-	audioContext.onEnded(function() {
-		audioContext.destroy()
-	})
+// 库存不足提示音 - 独立音频对象,避免与 hitVoice 冲突
+let noStockAudioContext = null
+let lastNoStockPlayTime = 0
+const NO_STOCK_MIN_INTERVAL = 1000 // 库存提示间隔1秒
+
+const initNoStockAudio = () => {
+	if (noStockAudioContext) return
+	try {
+		noStockAudioContext = uni.createInnerAudioContext()
+		noStockAudioContext.autoplay = false
+		noStockAudioContext.volume = 1
+		noStockAudioContext.src = "/static/noStock.mp3"
+		//关键:不添加任何事件监听,避免与其他音效冲突
+	} catch (e) {
+		console.error('[noStockRemind] 初始化失败:', e)
+		noStockAudioContext = null
+	}
+}
+
+export const noStockRemind = () => {
+	try {
+		// 防抖:1秒内只播放一次
+		const now = Date.now()
+		if (now - lastNoStockPlayTime < NO_STOCK_MIN_INTERVAL) {
+			return
+		}
+		
+		// 初始化音频
+		if (!noStockAudioContext) {
+			initNoStockAudio()
+		}
+		
+		if (!noStockAudioContext) {
+			return
+		}
+		
+		// 更新时间戳
+		lastNoStockPlayTime = now
+		
+		// 停止之前的播放(确保不会被中断)
+		try {
+			noStockAudioContext.stop()
+		} catch (e) {
+			// 忽略停止失败
+		}
+		
+		// 设置音量确保正常播放
+		noStockAudioContext.volume = 1
+		
+		// 立即播放
+		try {
+			noStockAudioContext.play()
+		} catch (playError) {
+			console.error('[noStockRemind] 播放异常:', playError)
+			noStockAudioContext = null
+		}
+	} catch (e) {
+		console.error('[noStockRemind] 执行出错:', e)
+	}
 }
 
-const innerAudioContext = uni.createInnerAudioContext()
-innerAudioContext.autoplay = false
-innerAudioContext.src = "/static/hit.mp3"
-export const hitVoice = () =>{
-	innerAudioContext.play()
+// 音效播放优化:缓存、防抖、资源控制
+let innerAudioContext = null
+let lastPlayTime = 0
+const MIN_PLAY_INTERVAL = 100 // 最小播放间隔,防止重复触发
+
+const initAudio = () => {
+	if (innerAudioContext) return
+	try {
+		innerAudioContext = uni.createInnerAudioContext()
+		innerAudioContext.autoplay = false
+		innerAudioContext.src = "/static/hit.mp3"
+		innerAudioContext.volume = 1
+		//关键:不添加事件监听,让播放更简洁可靠
+		// 播放时间短,不需要监听完成事件
+	} catch (e) {
+		console.error('[hitVoice] 初始化音频失败:', e)
+	}
+}
+
+export const hitVoice = () => {
+	try {
+		// 防抖:检查距离上次播放是否太近
+		const now = Date.now()
+		if (now - lastPlayTime < MIN_PLAY_INTERVAL) {
+			return
+		}
+		
+		// 初始化
+		if (!innerAudioContext) {
+			initAudio()
+		}
+		
+		if (!innerAudioContext) {
+			return
+		}
+		
+		// 更新时间
+		lastPlayTime = now
+		
+		// 停止之前的播放,确保新的播放能完整进行
+		try {
+			innerAudioContext.stop()
+		} catch (e) {
+			// 忽略停止失败
+		}
+		
+		// 设置音量
+		innerAudioContext.volume = 1
+		
+		// 立即播放(不使用异步,避免被其他音效打断)
+		try {
+			innerAudioContext.play()
+		} catch (e) {
+			console.error('[hitVoice] 播放失败:', e)
+		}
+	} catch (e) {
+		console.error('[hitVoice] 错误:', e)
+	}
 }
 
 //去重 姜枫 2021.05.05

+ 111 - 30
hdApp/src/utils/util.js

@@ -314,23 +314,64 @@ export const unique = (arr) => {
 	return arr;
 };
 
-//点击声效 shish
-export const hitRemind = () =>{
+// 点击声效 - 缓存对象,避免重复创建
+let hitAudioContext = null
+let lastHitPlayTime = 0
+const HIT_MIN_INTERVAL = 100
+
+const initHitAudio = () => {
+	if (hitAudioContext) return
 	try {
-		if(uni.getSystemInfoSync().platform != 'windows'){
-			const innerAudioContext = uni.createInnerAudioContext()
-			innerAudioContext.autoplay = true
-			innerAudioContext.src = '/static/hit.mp3'
-			innerAudioContext.onPlay()
-			innerAudioContext.onError()
-			innerAudioContext.onPause(function() {
-				innerAudioContext.destroy()
-			})
+		if (uni.getSystemInfoSync().platform === 'windows') return
+		
+		hitAudioContext = uni.createInnerAudioContext()
+		hitAudioContext.autoplay = false
+		hitAudioContext.src = '/static/hit.mp3'
+		hitAudioContext.volume = 1
+	} catch (e) {
+		console.error('[hitRemind] 初始化失败:', e)
+		hitAudioContext = null
+	}
+}
+
+export const hitRemind = () => {
+	try {
+		// 防抖:100ms 内只播放一次
+		const now = Date.now()
+		if (now - lastHitPlayTime < HIT_MIN_INTERVAL) {
+			return
+		}
+		
+		// 初始化音频
+		if (!hitAudioContext) {
+			initHitAudio()
+		}
+		
+		if (!hitAudioContext) {
+			return
+		}
+		
+		// 更新时间戳
+		lastHitPlayTime = now
+		
+		// 停止之前的播放
+		try {
+			hitAudioContext.stop()
+		} catch (e) {
+			// 忽略停止失败
+		}
+		
+		// 设置音量并播放
+		hitAudioContext.volume = 1
+		try {
+			hitAudioContext.play()
+		} catch (playError) {
+			console.error('[hitRemind] 播放异常:', playError)
+			hitAudioContext = null
 		}
 	} catch (e) {
-      // 开发环境兼容处理
-      console.warn('开发环境音频播放异常(可忽略):', e);
-    }
+		console.error('[hitRemind] 执行出错:', e)
+	}
 }
 
 
@@ -431,24 +472,64 @@ export const isScanEnv = () =>{
 	return isScanEnv
 }
 
-//没有库存时的语音播放 shish
-export const noStockRemind = () =>{
-	try{
-		if(uni.getSystemInfoSync().platform != 'windows'){
-			const innerAudioContext = uni.createInnerAudioContext()
-			innerAudioContext.autoplay = true
-			innerAudioContext.src = "/static/noStock.mp3"
-			innerAudioContext.volume = 1
-			innerAudioContext.onPlay()
-			innerAudioContext.onError()
-			innerAudioContext.onPause(function() {
-				innerAudioContext.destroy()
-			})
+// 库存不足提示音 - 缓存对象,避免重复创建
+let noStockAudioContext = null
+let lastNoStockPlayTime = 0
+const NO_STOCK_MIN_INTERVAL = 1000
+
+const initNoStockAudio = () => {
+	if (noStockAudioContext) return
+	try {
+		if (uni.getSystemInfoSync().platform === 'windows') return
+		
+		noStockAudioContext = uni.createInnerAudioContext()
+		noStockAudioContext.autoplay = false
+		noStockAudioContext.src = "/static/noStock.mp3"
+		noStockAudioContext.volume = 1
+	} catch (e) {
+		console.error('[noStockRemind] 初始化失败:', e)
+		noStockAudioContext = null
+	}
+}
+
+export const noStockRemind = () => {
+	try {
+		// 防抖:1秒内只播放一次
+		const now = Date.now()
+		if (now - lastNoStockPlayTime < NO_STOCK_MIN_INTERVAL) {
+			return
+		}
+		
+		// 初始化音频
+		if (!noStockAudioContext) {
+			initNoStockAudio()
+		}
+		
+		if (!noStockAudioContext) {
+			return
+		}
+		
+		// 更新时间戳
+		lastNoStockPlayTime = now
+		
+		// 停止之前的播放
+		try {
+			noStockAudioContext.stop()
+		} catch (e) {
+			// 忽略停止失败
+		}
+		
+		// 设置音量并播放
+		noStockAudioContext.volume = 1
+		try {
+			noStockAudioContext.play()
+		} catch (playError) {
+			console.error('[noStockRemind] 播放异常:', playError)
+			noStockAudioContext = null
 		}
 	} catch (e) {
-      // 开发环境兼容处理
-      console.warn('开发环境音频播放异常(可忽略):', e);
-    }
+		console.error('[noStockRemind] 执行出错:', e)
+	}
 }
 
 export async function callUp(mobile){

+ 116 - 23
hdPad/src/utils/util.js

@@ -295,31 +295,124 @@ export const callUp = (mobile) =>{
 	//#endif
 }
 
-//没有库存时的语音播放 shish
-export const noStockRemind = () =>{
-	const audioContext = uni.createInnerAudioContext()
-	audioContext.autoplay = true
-	audioContext.volume = 1
-	audioContext.src = "/static/noStock.mp3"
-	audioContext.onError(function() {
-		audioContext.destroy()
-	})
-	audioContext.onPause(function() {
-		audioContext.destroy()
-	})
-	audioContext.onStop(function() {
-		audioContext.destroy()
-	})
-	audioContext.onEnded(function() {
-		audioContext.destroy()
-	})
+// 库存不足提示音 - 独立音频对象,避免与 hitVoice 冲突
+let noStockAudioContext = null
+let lastNoStockPlayTime = 0
+const NO_STOCK_MIN_INTERVAL = 1000 // 库存提示间隔1秒
+
+const initNoStockAudio = () => {
+	if (noStockAudioContext) return
+	try {
+		noStockAudioContext = uni.createInnerAudioContext()
+		noStockAudioContext.autoplay = false
+		noStockAudioContext.volume = 1
+		noStockAudioContext.src = "/static/noStock.mp3"
+		//关键:不添加任何事件监听,避免与其他音效冲突
+	} catch (e) {
+		console.error('[noStockRemind] 初始化失败:', e)
+		noStockAudioContext = null
+	}
+}
+
+export const noStockRemind = () => {
+	try {
+		// 防抖:1秒内只播放一次
+		const now = Date.now()
+		if (now - lastNoStockPlayTime < NO_STOCK_MIN_INTERVAL) {
+			return
+		}
+		
+		// 初始化音频
+		if (!noStockAudioContext) {
+			initNoStockAudio()
+		}
+		
+		if (!noStockAudioContext) {
+			return
+		}
+		
+		// 更新时间戳
+		lastNoStockPlayTime = now
+		
+		// 停止之前的播放(确保不会被中断)
+		try {
+			noStockAudioContext.stop()
+		} catch (e) {
+			// 忽略停止失败
+		}
+		
+		// 设置音量确保正常播放
+		noStockAudioContext.volume = 1
+		
+		// 立即播放
+		try {
+			noStockAudioContext.play()
+		} catch (playError) {
+			console.error('[noStockRemind] 播放异常:', playError)
+			noStockAudioContext = null
+		}
+	} catch (e) {
+		console.error('[noStockRemind] 执行出错:', e)
+	}
 }
 
-const innerAudioContext = uni.createInnerAudioContext()
-innerAudioContext.autoplay = false
-innerAudioContext.src = "/static/hit.mp3"
-export const hitVoice = () =>{
-	innerAudioContext.play()
+// 音效播放优化:缓存、防抖、资源控制
+let innerAudioContext = null
+let lastPlayTime = 0
+const MIN_PLAY_INTERVAL = 100 // 最小播放间隔,防止重复触发
+
+const initAudio = () => {
+	if (innerAudioContext) return
+	try {
+		innerAudioContext = uni.createInnerAudioContext()
+		innerAudioContext.autoplay = false
+		innerAudioContext.src = "/static/hit.mp3"
+		innerAudioContext.volume = 1
+		//关键:不添加事件监听,让播放更简洁可靠
+	} catch (e) {
+		console.error('[hitVoice] 初始化音频失败:', e)
+	}
+}
+
+export const hitVoice = () => {
+	try {
+		// 防抖:检查距离上次播放是否太近
+		const now = Date.now()
+		if (now - lastPlayTime < MIN_PLAY_INTERVAL) {
+			return
+		}
+		
+		// 初始化
+		if (!innerAudioContext) {
+			initAudio()
+		}
+		
+		if (!innerAudioContext) {
+			return
+		}
+		
+		// 更新时间
+		lastPlayTime = now
+		
+		// 停止之前的播放,确保新的播放能完整进行
+		try {
+			innerAudioContext.stop()
+		} catch (e) {
+			// 忽略停止失败
+		}
+		
+		// 设置音量
+		innerAudioContext.volume = 1
+		
+		// 立即播放(不使用异步,避免被其他音效打断)
+		try {
+			innerAudioContext.play()
+		} catch (e) {
+			console.error('[hitVoice] 播放失败:', e)
+		}
+	} catch (e) {
+		console.error('[hitVoice] 错误:', e)
+	}
 }
 
 //去重 姜枫 2021.05.05