fix:修复语音推送限制接收多条信息时,导致叮叮响,限制了一个时间内只播放一条语音

This commit is contained in:
lihaoyuan 2025-11-03 15:12:01 +08:00
parent 079d305a36
commit 0632e36723
2 changed files with 72 additions and 11 deletions

View File

@ -8,7 +8,9 @@ import { GetShopBaseInfo } from "@/api/shop.js";
export default { export default {
data() { data() {
return { return {
version: "", version: "",
lastPlayTime: 0, //
playInterval: 20000 // 20
}; };
}, },
onLaunch: function () { onLaunch: function () {
@ -30,18 +32,26 @@ export default {
computed: { computed: {
...mapState("user", ["uid", "userInfo"]), ...mapState("user", ["uid", "userInfo"]),
}, },
onShow: function () { onShow: function () {
this.globalData.isAppActive = true; this.globalData.isAppActive = true;
const globalConnName = 'globalSocket';
if (this.userInfo && Object.keys(this.userInfo).length > 0) {
//
if (!webSocketManager.isConnected(globalConnName)) {
this.connectSocket(this.userInfo); // connectSocketglobalConnName
}
} else {
//
webSocketManager.closeAllGlobalConnections(); webSocketManager.closeAllGlobalConnections();
setTimeout(() => { }
if (this.userInfo && Object.keys(this.userInfo).length > 0) { //
this.connectSocket(this.userInfo); this.initPushListener();
} },
});
},
onHide: function () { onHide: function () {
this.globalData.isAppActive = false; this.globalData.isAppActive = false;
webSocketManager.closeAllGlobalConnections(); // webSocketManager.closeAllGlobalConnections();
}, },
globalData: { globalData: {
isAppActive: false, isAppActive: false,
@ -69,6 +79,39 @@ export default {
); );
}); });
}, },
//
initPushListener() {
const _self = this;
//
uni.offPushMessage();
uni.onPushMessage(async (res) => {
console.log("收到推送消息:", res);
//
if (!_self.userInfo || Object.keys(_self.userInfo).length === 0) {
console.log("用户未登录,忽略推送");
return;
}
if (res.data) {
if (!_self.globalData.isAppActive) {
if (res.type == "click") {
_self.handlePushClick(res.data.payload);
} else {
let shopRes = await GetShopBaseInfo();
if (shopRes.data.ringtone_is_enable === 1) {
_self.handlePushSound(res.data);
uni.setStorageSync("pendingPushData", res.data);
uni.createPushMessage(res.data);
}
}
} else {
console.log("应用已打开,直接处理消息");
_self.$store.dispatch("push/setPush", res.data);
}
}
});
},
async initAppOrderPush() { async initAppOrderPush() {
uni.getPushClientId({ uni.getPushClientId({
success: (res) => { success: (res) => {
@ -149,6 +192,12 @@ export default {
async handlePushSound(pushData) { async handlePushSound(pushData) {
console.log(pushData.payload); console.log(pushData.payload);
try { try {
const currentTime = Date.now();
// 10
if (currentTime - this.lastPlayTime < this.playInterval) {
console.log("10秒内已播放过语音本次不播放");
return; // 10
}
// category使 // category使
const category = pushData.payload.category; const category = pushData.payload.category;
var AUDIO = uni.createInnerAudioContext(); var AUDIO = uni.createInnerAudioContext();
@ -169,7 +218,7 @@ export default {
AUDIO.play(); AUDIO.play();
break; break;
} }
this.lastPlayTime = currentTime; //
// //
AUDIO.onError((res) => { AUDIO.onError((res) => {
console.error("播放声音失败:", res); console.error("播放声音失败:", res);

View File

@ -182,6 +182,18 @@ class WebSocketManager {
this.connections = {}; this.connections = {};
this.globalConnections = []; this.globalConnections = [];
} }
// 新增:判断指定名称的连接是否处于连接状态
isConnected(connectionName) {
// 获取指定名称的连接实例
const connection = this.connections[connectionName];
if (!connection) return false;
// 检查连接状态WebsocketUtil的is_open_socket为true且socketTask的readyState为1连接中
return connection.is_open_socket &&
connection.socketTask &&
connection.socketTask.readyState === 1;
}
} }
// 导出单例实例 // 导出单例实例