Ver Fonte

fix: unify chat sounds with the shared AudioContext pattern

unknown há 3 dias atrás
pai
commit
499025cbac
2 ficheiros alterados com 31 adições e 24 exclusões
  1. 2 23
      src/components/OrderChat.vue
  2. 29 1
      src/stores/auth.ts

+ 2 - 23
src/components/OrderChat.vue

@@ -129,28 +129,7 @@ function startCooldown() {
 }
 const authStore = useAuthStore();
 
-function playDing() {
-  try {
-    const AudioContext = window.AudioContext || (window as any).webkitAudioContext;
-    if (!AudioContext) return;
-    const ctx = new AudioContext();
-    const osc = ctx.createOscillator();
-    const gainNode = ctx.createGain();
-    
-    osc.type = "sine";
-    osc.frequency.setValueAtTime(880, ctx.currentTime);
-    osc.frequency.exponentialRampToValueAtTime(440, ctx.currentTime + 0.1);
-    
-    gainNode.gain.setValueAtTime(0.3, ctx.currentTime);
-    gainNode.gain.exponentialRampToValueAtTime(0.01, ctx.currentTime + 0.3);
-    
-    osc.connect(gainNode);
-    gainNode.connect(ctx.destination);
-    
-    osc.start();
-    osc.stop(ctx.currentTime + 0.3);
-  } catch(e) {}
-}
+
 
 let ws: WebSocket | null = null;
 let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
@@ -198,7 +177,7 @@ function connectWebSocket() {
           const myRole = authStore.user?.role === 'admin' ? 'admin' : 'user';
           const isMsgFromMe = (msg.is_from_admin && myRole === 'admin') || (!msg.is_from_admin && myRole === 'user');
           if (!isMsgFromMe) {
-            playDing();
+            authStore.playChatSound();
             ws?.send("read");
             setTimeout(() => authStore.refreshUnreadCount(), 300);
           }

+ 29 - 1
src/stores/auth.ts

@@ -109,6 +109,33 @@ export const useAuthStore = defineStore("auth", () => {
       console.warn("Audio notification sound error", e);
     }
   }
+
+  async function playChatSound() {
+    try {
+      const ctx = getAudioCtx();
+      if (!ctx) return;
+      if (ctx.state === 'suspended') await ctx.resume();
+
+      const osc = ctx.createOscillator();
+      const gainNode = ctx.createGain();
+      
+      osc.type = "sine";
+      osc.frequency.setValueAtTime(880, ctx.currentTime);
+      osc.frequency.exponentialRampToValueAtTime(440, ctx.currentTime + 0.1);
+      
+      gainNode.gain.setValueAtTime(0, ctx.currentTime);
+      gainNode.gain.linearRampToValueAtTime(0.3, ctx.currentTime + 0.05);
+      gainNode.gain.exponentialRampToValueAtTime(0.01, ctx.currentTime + 0.3);
+      
+      osc.connect(gainNode);
+      gainNode.connect(ctx.destination);
+      
+      osc.start();
+      osc.stop(ctx.currentTime + 0.3);
+    } catch (e) {
+      console.warn("Audio chat sound error", e);
+    }
+  }
   // ───────────────────────────────────────────────────────────────────────────
 
   function startPing() {
@@ -251,6 +278,7 @@ export const useAuthStore = defineStore("auth", () => {
     refreshUser,
     onProfileComplete,
     logout,
-    refreshUnreadCount
+    refreshUnreadCount,
+    playChatSound
   };
 });