fix:修复语音推送限制接收多条信息时,导致叮叮响,限制了一个时间内只播放一条语音
This commit is contained in:
parent
079d305a36
commit
0632e36723
@ -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); // 这里的connectSocket应该会创建名为globalConnName的连接
|
||||||
|
}
|
||||||
|
} 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);
|
||||||
|
|||||||
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 导出单例实例
|
// 导出单例实例
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user