2 Commits e051889aec ... c1ffe40131

Autore SHA1 Messaggio Data
  Liuteng c1ffe40131 1 1 mese fa
  Liuteng cf0803e188 1 1 mese fa

+ 2 - 13
package-lock.json

@@ -1,16 +1,15 @@
 {
   "name": "newsmzc",
-  "version": "0.1.0",
+  "version": "2.3.0",
   "lockfileVersion": 2,
   "requires": true,
   "packages": {
     "": {
       "name": "newsmzc",
-      "version": "0.1.0",
+      "version": "2.3.0",
       "dependencies": {
         "axios": "^1.3.5",
         "core-js": "^3.8.3",
-        "js-audio-recorder": "^0.5.7",
         "qrcodejs2": "^0.0.2",
         "vue": "^2.6.14",
         "vue-router": "^3.5.1"
@@ -6974,11 +6973,6 @@
         "@sideway/pinpoint": "^2.0.0"
       }
     },
-    "node_modules/js-audio-recorder": {
-      "version": "0.5.7",
-      "resolved": "https://registry.npmjs.org/js-audio-recorder/-/js-audio-recorder-0.5.7.tgz",
-      "integrity": "sha512-DIlv30N86AYHr7zGHN0O7V/3Rd8Q6SIJ/MBzVJaT9STWTdhF4E/8fxCX6ZMgRSv8xmx6fEqcFFNPoofmxJD4+A=="
-    },
     "node_modules/js-message": {
       "version": "1.0.7",
       "resolved": "https://registry.npmjs.org/js-message/-/js-message-1.0.7.tgz",
@@ -16839,11 +16833,6 @@
         "@sideway/pinpoint": "^2.0.0"
       }
     },
-    "js-audio-recorder": {
-      "version": "0.5.7",
-      "resolved": "https://registry.npmjs.org/js-audio-recorder/-/js-audio-recorder-0.5.7.tgz",
-      "integrity": "sha512-DIlv30N86AYHr7zGHN0O7V/3Rd8Q6SIJ/MBzVJaT9STWTdhF4E/8fxCX6ZMgRSv8xmx6fEqcFFNPoofmxJD4+A=="
-    },
     "js-message": {
       "version": "1.0.7",
       "resolved": "https://registry.npmjs.org/js-message/-/js-message-1.0.7.tgz",

+ 0 - 1
package.json

@@ -10,7 +10,6 @@
   "dependencies": {
     "axios": "^1.3.5",
     "core-js": "^3.8.3",
-    "js-audio-recorder": "^0.5.7",
     "qrcodejs2": "^0.0.2",
     "vue": "^2.6.14",
     "vue-router": "^3.5.1"

+ 114 - 85
src/views/HVversion/VocationalWork/TaxAdministration/znzx/component/znzx.vue

@@ -258,7 +258,9 @@
               @focus="focus()"
             ></textarea>
             <div class="chat-button-box1">
-              <div class="clear" v-if="msg.msg.length > 0" @click="resentMsg">清空</div>
+              <div class="clear" v-if="msg.msg.length > 0" @click="resentMsg">
+                清空
+              </div>
             </div>
             <div class="chat-button-box">
               <div class="chat-ly" @click="audioFun">
@@ -446,7 +448,6 @@ import {
   getAnswer,
   getFlyj,
 } from "@/api/TaxAdministration";
-import Recorder from "js-audio-recorder";
 import { debug } from "@/api/zxbg";
 let recorder = null;
 let socket = null;
@@ -476,9 +477,7 @@ export default {
       audioing: false,
       audioTime: null,
       sbjg: "",
-      // ws
-      clientid: null,
-      ws: "wss://mail12366.izrar.cn",
+      
       zntjList: [],
       cjwtList: [],
       ztzyList: [],
@@ -504,7 +503,7 @@ export default {
   watch: {
     message(e) {
       this.chatBoxScroll();
-    }
+    },
   },
 
   methods: {
@@ -724,103 +723,133 @@ export default {
         type: "from",
         msg: "",
       };
-      this.sbjg = ""
+      this.sbjg = "";
     },
     audioFun() {
       if (this.audioing === false) {
-        this.initSocket();
-        recorder = new Recorder({
-          compiling: true,
-          sampleBits: 16,
-          sampleRate: 11025,
-          numChannels: 1,
-        });
         this.audioing = true;
-        this.speech(true);
-        this.audioedFun();
+        this.speech(this.audioing);
       } else if (this.audioing === true) {
         this.audioing = false;
-        this.speech(false);
-        this.wsDestroy();
+        this.speech(this.audioing);
       }
     },
-    audioedFun() {
-      clearTimeout(this.audioTime)
+    stopAudio() {
+      clearTimeout(this.audioTime);
       this.audioTime = setTimeout(() => {
         this.audioing = false;
-        this.audioTime = null;
-        this.speech(false);
-        this.wsDestroy();
+        this.speech(this.audioing);
       }, 5 * 1000);
     },
-    speech(e) {
+    async speech(e) {
       if (e === true) {
-        recorder.start().then(
-          () => {
-            recorder.onprogress = function (params) {
-              var data = params.data;
-              socket.send(data[data.length - 1]);
-            };
-          },
-          (error) => {
-            this.$tip(error.message, "error");
-            this.audioing = false;
-            this.speech(false);
-            this.wsDestroy();
-            clearTimeout(this.audioTime);
-            this.audioTime = null;
-          }
-        );
-      } else if (e === false) {
-        if (recorder) {
-          recorder.destroy().then(function () {
-            recorder = null;
+        try {
+          this.initSocket();
+          this.stopAudio();
+          // 获取媒体设备
+          const stream = await navigator.mediaDevices.getUserMedia({
+            audio: true,
           });
+          // 创建AudioContext实例
+          recorder = new (window.AudioContext || window.webkitAudioContext)();
+          // 创建MediaStreamAudioSourceNode实例
+          const source = recorder.createMediaStreamSource(stream);
+          // 创建ScriptProcessorNode实例
+          const processor = recorder.createScriptProcessor(4096, 1, 1);
+          // 设置音频处理函数
+          processor.onaudioprocess = (e) => {
+            let i = e.inputBuffer.getChannelData(0),
+              s = 16,
+              a = 0;
+            try {
+              a = recorder.sampleRate;
+            } catch (e) {
+              a = 48e3;
+            }
+            let n = 16e3,
+              c = new Float32Array(i),
+              o = Math.round(a / n),
+              r = c.length / o,
+              l = new Float32Array(r),
+              d = 0,
+              h = 0;
+            while (d < r) {
+              (l[d] = c[h]), (h += o), d++;
+            }
+            let u = l.length * (s / 8),
+              p = new ArrayBuffer(u),
+              g = new DataView(p),
+              v = 0;
+            if (8 === s) {
+              for (let t = 0; t < l.length; t++) {
+                let e = Math.max(-1, Math.min(1, l[t])),
+                  i = e < 0 ? 128 * e : 127 * e;
+                (i = parseInt(i + 128)), g.setInt8(v, i, !0);
+              }
+            } else {
+              for (let t = 0; t < l.length; t++, v += 2) {
+                let e = Math.max(-1, Math.min(1, l[t]));
+                g.setInt16(v, e < 0 ? 32768 * e : 32767 * e, !0);
+              }
+            }
+            // 将二进制数据通过WebSocket发送到服务器
+            if (socket && socket.readyState === WebSocket.OPEN) {
+              socket.send(g);
+            }
+          };
+          // 连接音频节点
+          source.connect(processor);
+          processor.connect(recorder.destination);
+        } catch (error) {
+          recorder = null;
+          console.error("Error starting recording:", error);
         }
-      }
-    },
-    initSocket() {
-      this.clientid = Number(
-        Math.random().toString().substr(3, length) + Date.now()
-      ).toString(36);
-      socket = new WebSocket(this.ws + "/speech/websocket/" + this.clientid);
-      socket.addEventListener("open", this.wsOpen);
-      socket.addEventListener("message", this.wsMessage);
-      socket.addEventListener("error", this.wsError);
-      socket.addEventListener("close", this.wsClose);
-    },
-    wsOpen() {
-      socket.send("zh");
-    },
-    wsMessage(e) {
-      if (e.data) {
-        clearTimeout(this.audioTime);
-        this.audioTime = null;
-        var data = JSON.parse(e.data);
-        if (0 === data.status) {
-          this.msg.msg = this.sbjg + data.str;
+      } else if (e === false) {
+        if (recorder) {
+          // 停止录音
+          recorder.close();
+          recorder = null;
         }
-        if (1 === data.status) {
-          this.sbjg = this.sbjg + data.str;
-          this.msg.msg = this.sbjg;
+        // 关闭WebSocket连接
+        if (socket) {
+          socket.send("end");
+          socket.close();
+          socket = null; // 确保socket被重置为null
         }
-        this.audioedFun()
       }
     },
-    wsError() {
-      this.wsDestroy();
-    },
-    wsClose() {
-      this.wsDestroy();
-    },
-    wsDestroy() {
-      if (socket != null) {
-        socket.removeEventListener("open", this.wsOpenHanler);
-        socket.removeEventListener("message", this.wsMessageHanler);
-        socket.removeEventListener("error", this.wsErrorHanler);
-        socket.removeEventListener("close", this.wsCloseHanler);
-        socket.close();
-        socket = null;
+    initSocket() {
+      if (!socket) {
+        const url = "wss://mail12366.izrar.cn/speech/websocket/";
+        const clientid = Number(
+          Math.random().toString().substr(3, length) + Date.now()
+        ).toString(36);
+        // 创建WebSocket实例
+        socket = new WebSocket(url + clientid);
+        // 设置WebSocket事件监听器
+        socket.onopen = () => {
+          socket.send("zh");
+          console.log("WebSocket连接已打开");
+        };
+        socket.onmessage = (e) => {
+          if (e.data) {
+            this.stopAudio();
+            var data = JSON.parse(e.data);
+            if (0 === data.status) {
+              this.msg.msg = this.sbjg + data.str;
+            }
+            if (1 === data.status) {
+              this.sbjg = this.sbjg + data.str;
+              this.msg.msg = this.sbjg;
+            }
+          }
+        };
+        socket.onerror = (error) => {
+          console.error("WebSocket发生错误:", error);
+        };
+        socket.onclose = () => {
+          console.log("WebSocket连接已关闭");
+        };
       }
     },
     chatBoxScroll() {
@@ -1787,4 +1816,4 @@ export default {
     }
   }
 }
-</style>
+</style>