Browse Source

ajax请求错误处理

WANGJIALIANG 3 years ago
parent
commit
9d66f3b6b9
1 changed files with 52 additions and 42 deletions
  1. 52 42
      src/main/resources/static/js/common/socket-com.js

+ 52 - 42
src/main/resources/static/js/common/socket-com.js

@@ -13,51 +13,61 @@ function openSocket(type) {
 //判断当前浏览器是否支持WebSocket
     try {
         if ('WebSocket' in window) {
-            request({
-                method: 'GET',
-                url: '/getWebSocketHost'
-            }).then((res) => {
-                prescriptionPrintWebsocket = new WebSocket(res.data + "prescriptionPrintSocket/" + type);
-                //连接发生错误的回调方法
-                prescriptionPrintWebsocket.onerror = function () {
-                    console.log(type + "连接prescriptionPrintSocket发生错误")
-                };
-                //连接成功建立的回调方法
-                prescriptionPrintWebsocket.onopen = function (event) {
-                    number = 0;
-                    console.log(type + "连接prescriptionPrintSocket连接成功");
-                    var chk_value = '';
-                    $('input[name="aotoPrint"]:checked').each(function () {
-                        chk_value = $(this).val();
-                    });
-                    if (type == 'PY' && chk_value != '') {//配药处方自动打印
-                        printPrescriptions(chk_value);
-                    }
-                };
-                //页面接收到消息的回调方法
-                prescriptionPrintWebsocket.onmessage = function (event) {
-                    var data = JSON.parse(event.data);
-                    if (type == 'PY' && data.groupNo == groupNo) {
-                        dispensingMessage(data);
-                    } else if (type == 'FY') {
-                        sendMedicineMessage(data);
-                    } else if (type == 'JH') {
-                        cellNumberMessage(data);
-                    }
-                };
-                //连接关闭的回调方法
-                prescriptionPrintWebsocket.onclose = function (event) {
-                    console.log(type + "连接prescriptionPrintSocket连接关闭 code:" + event.code + ",原因:" + event.reason + ",是否正常关闭:" + event.wasClean);
-                    if (event.code == '1001' || event.code == '1006') {//1001:终端离开, 可能因为服务端错误 1006:没有发送关闭帧
+            $.ajax({
+                type: "GET",
+                url: '/thmz/getWebSocketHost',
+                contentType: "application/json;charset=UTF-8",
+                dataType: "json",
+                headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
+                success: function (res) {
+                    if (res.code === 0) {
+                        prescriptionPrintWebsocket = new WebSocket(res.data + "prescriptionPrintSocket/" + type);
+                        //连接发生错误的回调方法
+                        prescriptionPrintWebsocket.onerror = function () {
+                            console.log(type + "连接prescriptionPrintSocket发生错误")
+                        };
+                        //连接成功建立的回调方法
+                        prescriptionPrintWebsocket.onopen = function (event) {
+                            console.log(type + "连接prescriptionPrintSocket连接成功");
+                            var chk_value = '';
+                            $('input[name="aotoPrint"]:checked').each(function () {
+                                chk_value = $(this).val();
+                            });
+                            if (type == 'PY' && chk_value != '') {//配药处方自动打印
+                                printPrescriptions(chk_value);
+                            }
+                        };
+                        //页面接收到消息的回调方法
+                        prescriptionPrintWebsocket.onmessage = function (event) {
+                            var data = JSON.parse(event.data);
+                            if (type == 'PY' && data.groupNo == groupNo) {
+                                dispensingMessage(data);
+                            } else if (type == 'FY') {
+                                sendMedicineMessage(data);
+                            } else if (type == 'JH') {
+                                cellNumberMessage(data);
+                            }
+                        };
+                        //连接关闭的回调方法
+                        prescriptionPrintWebsocket.onclose = function (event) {
+                            console.log(type + "连接prescriptionPrintSocket连接关闭 code:" + event.code + ",原因:" + event.reason + ",是否正常关闭:" + event.wasClean);
+                            if (event.code == '1001' || event.code == '1006') {//1001:终端离开, 可能因为服务端错误 1006:没有发送关闭帧
+                                reconnect(type);
+                            }
+                        };
+                        //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
+                        window.onbeforeunload = function () {
+                            prescriptionPrintWebsocket.close();
+                        }
+                    } else if (res.code === -1) {
+                        errorMesageSimaple('自动打印连接socket失败,正尝试重连...');
                         reconnect(type);
                     }
-                };
-                //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
-                window.onbeforeunload = function () {
-                    prescriptionPrintWebsocket.close();
+                },
+                error: function () {
+                    errorMesageSimaple('自动打印连接socket失败,正尝试重连...');
+                    reconnect(type);
                 }
-            }).catch(() => {
-                reconnect(type);
             });
         } else {
             alert('当前浏览器不支持WebSocket,请更换浏览器');