seimin 3 年 前
コミット
e380516acd

+ 50 - 9
package-lock.json

@@ -784,7 +784,7 @@
784 784
     },
785 785
     "@types/jquery": {
786 786
       "version": "3.5.6",
787
-      "resolved": "https://registry.nlark.com/@types/jquery/download/@types/jquery-3.5.6.tgz?cache=0&sync_timestamp=1629708196495&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40types%2Fjquery%2Fdownload%2F%40types%2Fjquery-3.5.6.tgz",
787
+      "resolved": "https://registry.nlark.com/@types/jquery/download/@types/jquery-3.5.6.tgz",
788 788
       "integrity": "sha1-l6yONtzNitjtPz87SJM2FNn9jPA=",
789 789
       "dev": true,
790 790
       "requires": {
@@ -822,7 +822,7 @@
822 822
     },
823 823
     "@types/sizzle": {
824 824
       "version": "2.3.3",
825
-      "resolved": "https://registry.nlark.com/@types/sizzle/download/@types/sizzle-2.3.3.tgz?cache=0&sync_timestamp=1629709217852&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40types%2Fsizzle%2Fdownload%2F%40types%2Fsizzle-2.3.3.tgz",
825
+      "resolved": "https://registry.nlark.com/@types/sizzle/download/@types/sizzle-2.3.3.tgz?cache=0&sync_timestamp=1621243834244&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40types%2Fsizzle%2Fdownload%2F%40types%2Fsizzle-2.3.3.tgz",
826 826
       "integrity": "sha1-/14vGQKWnTBSJaBHyKD9XJFc6+8=",
827 827
       "dev": true
828 828
     },
@@ -1396,6 +1396,41 @@
1396 1396
       "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=",
1397 1397
       "dev": true
1398 1398
     },
1399
+    "atmosphere.js": {
1400
+      "version": "2.3.6",
1401
+      "resolved": "https://registry.npmmirror.com/atmosphere.js/download/atmosphere.js-2.3.6.tgz",
1402
+      "integrity": "sha1-177Y28qZHNOhQ8XzAjDJCU3/Zfs=",
1403
+      "requires": {
1404
+        "eventsource": "^0.1.6",
1405
+        "npm": "^6.2.0",
1406
+        "ws": "^1.0.1",
1407
+        "xmlhttprequest": "^1.8.0"
1408
+      },
1409
+      "dependencies": {
1410
+        "eventsource": {
1411
+          "version": "0.1.6",
1412
+          "resolved": "https://registry.npm.taobao.org/eventsource/download/eventsource-0.1.6.tgz?cache=0&sync_timestamp=1616041716084&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Feventsource%2Fdownload%2Feventsource-0.1.6.tgz",
1413
+          "integrity": "sha1-Cs7ehJ7X3RzMMsgRuxG5RNTykjI=",
1414
+          "requires": {
1415
+            "original": ">=0.0.5"
1416
+          }
1417
+        },
1418
+        "ultron": {
1419
+          "version": "1.0.2",
1420
+          "resolved": "https://registry.npm.taobao.org/ultron/download/ultron-1.0.2.tgz",
1421
+          "integrity": "sha1-rOEWq1V80Zc4ak6I9GhTeMiy5Po="
1422
+        },
1423
+        "ws": {
1424
+          "version": "1.1.5",
1425
+          "resolved": "https://registry.npmmirror.com/ws/download/ws-1.1.5.tgz?cache=0&sync_timestamp=1633200058938&other_urls=https%3A%2F%2Fregistry.npmmirror.com%2Fws%2Fdownload%2Fws-1.1.5.tgz",
1426
+          "integrity": "sha1-y9nm514J/F0skAFfIfDECHXg3VE=",
1427
+          "requires": {
1428
+            "options": ">=0.0.5",
1429
+            "ultron": "1.0.x"
1430
+          }
1431
+        }
1432
+      }
1433
+    },
1399 1434
     "atob": {
1400 1435
       "version": "2.1.2",
1401 1436
       "resolved": "https://registry.npm.taobao.org/atob/download/atob-2.1.2.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fatob%2Fdownload%2Fatob-2.1.2.tgz",
@@ -5244,7 +5279,7 @@
5244 5279
     },
5245 5280
     "jquery": {
5246 5281
       "version": "3.6.0",
5247
-      "resolved": "https://registry.npm.taobao.org/jquery/download/jquery-3.6.0.tgz",
5282
+      "resolved": "https://registry.npm.taobao.org/jquery/download/jquery-3.6.0.tgz?cache=0&sync_timestamp=1614705838607&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fjquery%2Fdownload%2Fjquery-3.6.0.tgz",
5248 5283
       "integrity": "sha1-xyoJ8Vwb3OFC9J2/EXC9+K2sJHA="
5249 5284
     },
5250 5285
     "js-tokens": {
@@ -9862,11 +9897,15 @@
9862 9897
         }
9863 9898
       }
9864 9899
     },
9900
+    "options": {
9901
+      "version": "0.0.6",
9902
+      "resolved": "https://registry.npm.taobao.org/options/download/options-0.0.6.tgz",
9903
+      "integrity": "sha1-7CLTEoBrtT5zF3Pnza788cZDEo8="
9904
+    },
9865 9905
     "original": {
9866 9906
       "version": "1.0.2",
9867 9907
       "resolved": "https://registry.npm.taobao.org/original/download/original-1.0.2.tgz",
9868 9908
       "integrity": "sha1-5EKmHP/hxf0gpl8yYcJmY7MD8l8=",
9869
-      "dev": true,
9870 9909
       "requires": {
9871 9910
         "url-parse": "^1.4.3"
9872 9911
       }
@@ -10660,8 +10699,7 @@
10660 10699
     "querystringify": {
10661 10700
       "version": "2.2.0",
10662 10701
       "resolved": "https://registry.nlark.com/querystringify/download/querystringify-2.2.0.tgz",
10663
-      "integrity": "sha1-M0WUG0FTy50ILY7uTNogFqmu9/Y=",
10664
-      "dev": true
10702
+      "integrity": "sha1-M0WUG0FTy50ILY7uTNogFqmu9/Y="
10665 10703
     },
10666 10704
     "randombytes": {
10667 10705
       "version": "2.1.0",
@@ -10905,8 +10943,7 @@
10905 10943
     "requires-port": {
10906 10944
       "version": "1.0.0",
10907 10945
       "resolved": "https://registry.npm.taobao.org/requires-port/download/requires-port-1.0.0.tgz",
10908
-      "integrity": "sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8=",
10909
-      "dev": true
10946
+      "integrity": "sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8="
10910 10947
     },
10911 10948
     "resolve": {
10912 10949
       "version": "1.20.0",
@@ -12755,7 +12792,6 @@
12755 12792
       "version": "1.5.1",
12756 12793
       "resolved": "https://registry.npm.taobao.org/url-parse/download/url-parse-1.5.1.tgz",
12757 12794
       "integrity": "sha1-1fqYkK+KXh8nSiyYN2UQ9kJfbjs=",
12758
-      "dev": true,
12759 12795
       "requires": {
12760 12796
         "querystringify": "^2.1.1",
12761 12797
         "requires-port": "^1.0.0"
@@ -13660,6 +13696,11 @@
13660 13696
       "integrity": "sha1-vpuuHIoEbnazESdyY0fQrXACvrM=",
13661 13697
       "dev": true
13662 13698
     },
13699
+    "xmlhttprequest": {
13700
+      "version": "1.8.0",
13701
+      "resolved": "https://registry.npm.taobao.org/xmlhttprequest/download/xmlhttprequest-1.8.0.tgz",
13702
+      "integrity": "sha1-Z/4HXFwk/vOfnWX197f+dRcZaPw="
13703
+    },
13663 13704
     "xmlhttprequest-ssl": {
13664 13705
       "version": "1.5.5",
13665 13706
       "resolved": "https://registry.nlark.com/xmlhttprequest-ssl/download/xmlhttprequest-ssl-1.5.5.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fxmlhttprequest-ssl%2Fdownload%2Fxmlhttprequest-ssl-1.5.5.tgz",

+ 1 - 0
package.json

@@ -24,6 +24,7 @@
24 24
     "@angular/router": "~8.1.1",
25 25
     "@types/crypto-js": "^3.1.47",
26 26
     "@types/overlayscrollbars": "^1.12.1",
27
+    "atmosphere.js": "2.3.6",
27 28
     "core-js": "^3.18.2",
28 29
     "crypto-js": "^3.3.0",
29 30
     "datatables.net": "^1.11.3",

+ 106 - 0
src/app/services/websocket-index.service.ts

@@ -0,0 +1,106 @@
1
+import { Injectable } from "@angular/core";
2
+import { Subject, Observable, from } from "rxjs";
3
+import * as Atmosphere from "atmosphere.js";
4
+
5
+@Injectable({
6
+  providedIn: "root",
7
+})
8
+export class WebsocketIndexService {
9
+  constructor() {}
10
+  private lockReconnect = false; //避免ws重复连接
11
+  ws; //定义websocket
12
+  private url = ""; //ws连接地址
13
+  private urlParams; //ws连接传参
14
+  private isHandler = false; //是否手动
15
+  private subject;
16
+  // 连接websocket
17
+  connectWs(url, data): Observable<any> {
18
+    this.url = url;
19
+    this.urlParams = data;
20
+    this.subject = new Subject<any>();
21
+    let request: any = {
22
+      url,
23
+      contentType: "application/json",
24
+      shared: true, // 标签共享
25
+      trackMessageLength: true, //校验数据完整性
26
+      transport: "websocket",
27
+    };
28
+
29
+    request.onOpen = (response) => {
30
+      console.log("ws连接成功" + new Date().toLocaleString());
31
+      this.heartCheck.reset().start(this);
32
+      this.ws.push(JSON.stringify(data));
33
+    };
34
+
35
+    request.onMessage = (response) => {
36
+      this.heartCheck.reset().start(this);
37
+      let message = response.responseBody;
38
+      try {
39
+        // alert(message);
40
+        console.log("收到消息" + message);
41
+        if (message !== "pong") {
42
+          this.subject.next(JSON.parse(message));
43
+        }
44
+      } catch (e) {
45
+        console.log("This doesn't look like a valid JSON: ", message);
46
+        return;
47
+      }
48
+    };
49
+
50
+    request.onClose = (response) => {
51
+      console.log("ws连接关闭" + new Date().toLocaleString(), this.isHandler);
52
+      this.heartCheck.reset();
53
+      if (!this.isHandler) {
54
+        this.reconnect(this.url, this.urlParams);
55
+      }
56
+    };
57
+
58
+    request.onError = (response) => {
59
+      console.log("ws连接错误", this.isHandler);
60
+      this.heartCheck.reset();
61
+      if (!this.isHandler) {
62
+        this.reconnect(this.url, this.urlParams);
63
+      }
64
+    };
65
+
66
+    this.ws = Atmosphere.subscribe(request);
67
+    return this.subject.asObservable();
68
+  }
69
+  //断线重连
70
+  private reconnect(url, data) {
71
+    if (this.lockReconnect) return;
72
+    this.lockReconnect = true;
73
+    setTimeout(() => {
74
+      //没连接上会一直重连,设置延迟避免请求过多
75
+      this.connectWs(url, data);
76
+      this.lockReconnect = false;
77
+    }, 15000);
78
+  }
79
+  private heartCheck = {
80
+    timeout: 60000, //1分钟发一次心跳
81
+    timeoutObj: null,
82
+    serverTimeoutObj: null,
83
+    reset: function () {
84
+      clearTimeout(this.timeoutObj);
85
+      clearTimeout(this.serverTimeoutObj);
86
+      return this;
87
+    },
88
+    start: function (_this) {
89
+      this.timeoutObj = setTimeout(() => {
90
+        //这里发送一个心跳,后端收到后,返回一个心跳消息,
91
+        //onmessage拿到返回的心跳就说明连接正常
92
+        _this.ws.push("ping");
93
+        this.serverTimeoutObj = setTimeout(() => {
94
+          //如果超过一定时间还没重置,说明后端主动断开了
95
+          _this.closeWs(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
96
+        }, this.timeout);
97
+      }, this.timeout);
98
+    },
99
+  };
100
+
101
+  // 断开websocket
102
+  closeWs(flag: boolean = false) {
103
+    this.isHandler = flag;
104
+    this.ws.close();
105
+  }
106
+}

+ 106 - 0
src/app/services/websocket-main.service.ts

@@ -0,0 +1,106 @@
1
+import { Injectable } from "@angular/core";
2
+import { Subject, Observable, from } from "rxjs";
3
+import * as Atmosphere from "atmosphere.js";
4
+
5
+@Injectable({
6
+  providedIn: "root",
7
+})
8
+export class WebsocketMainService {
9
+  constructor() {}
10
+  private lockReconnect = false; //避免ws重复连接
11
+  ws; //定义websocket
12
+  private url = ""; //ws连接地址
13
+  private urlParams; //ws连接传参
14
+  private isHandler = false; //是否手动
15
+  private subject;
16
+  // 连接websocket
17
+  connectWs(url, data): Observable<any> {
18
+    this.url = url;
19
+    this.urlParams = data;
20
+    this.subject = new Subject<any>();
21
+    let request: any = {
22
+      url,
23
+      contentType: "application/json",
24
+      shared: true, // 标签共享
25
+      trackMessageLength: true, //校验数据完整性
26
+      transport: "websocket",
27
+    };
28
+
29
+    request.onOpen = (response) => {
30
+      console.log("ws连接成功" + new Date().toLocaleString());
31
+      this.heartCheck.reset().start(this);
32
+      this.ws.push(JSON.stringify(data));
33
+    };
34
+
35
+    request.onMessage = (response) => {
36
+      this.heartCheck.reset().start(this);
37
+      let message = response.responseBody;
38
+      try {
39
+        // alert(message);
40
+        console.log("收到消息" + message);
41
+        if (message !== "pong") {
42
+          this.subject.next(JSON.parse(message));
43
+        }
44
+      } catch (e) {
45
+        console.log("This doesn't look like a valid JSON: ", message);
46
+        return;
47
+      }
48
+    };
49
+
50
+    request.onClose = (response) => {
51
+      console.log("ws连接关闭" + new Date().toLocaleString(),this.isHandler);
52
+      this.heartCheck.reset();
53
+      if (!this.isHandler) {
54
+        this.reconnect(this.url, this.urlParams);
55
+      }
56
+    };
57
+
58
+    request.onError = (response) => {
59
+      console.log("ws连接错误",this.isHandler);
60
+      this.heartCheck.reset();
61
+      if (!this.isHandler) {
62
+        this.reconnect(this.url, this.urlParams);
63
+      }
64
+    };
65
+
66
+    this.ws = Atmosphere.subscribe(request);
67
+    return this.subject.asObservable();
68
+  }
69
+  //断线重连
70
+  private reconnect(url, data) {
71
+    if (this.lockReconnect) return;
72
+    this.lockReconnect = true;
73
+    setTimeout(() => {
74
+      //没连接上会一直重连,设置延迟避免请求过多
75
+      this.connectWs(url, data);
76
+      this.lockReconnect = false;
77
+    }, 15000);
78
+  }
79
+  private heartCheck = {
80
+    timeout: 60000, //1分钟发一次心跳
81
+    timeoutObj: null,
82
+    serverTimeoutObj: null,
83
+    reset: function () {
84
+      clearTimeout(this.timeoutObj);
85
+      clearTimeout(this.serverTimeoutObj);
86
+      return this;
87
+    },
88
+    start: function (_this) {
89
+      this.timeoutObj = setTimeout(() => {
90
+        //这里发送一个心跳,后端收到后,返回一个心跳消息,
91
+        //onmessage拿到返回的心跳就说明连接正常
92
+        _this.ws.push("ping");
93
+        this.serverTimeoutObj = setTimeout(() => {
94
+          //如果超过一定时间还没重置,说明后端主动断开了
95
+          _this.closeWs(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
96
+        }, this.timeout);
97
+      }, this.timeout);
98
+    },
99
+  };
100
+
101
+  // 断开websocket
102
+  closeWs(flag: boolean = false) {
103
+    this.isHandler = flag;
104
+    this.ws.close();
105
+  }
106
+}

+ 106 - 0
src/app/services/websocket-nurse.service.ts

@@ -0,0 +1,106 @@
1
+import { Injectable } from "@angular/core";
2
+import { Subject, Observable, from } from "rxjs";
3
+import * as Atmosphere from "atmosphere.js";
4
+
5
+@Injectable({
6
+  providedIn: "root",
7
+})
8
+export class WebsocketNurseService {
9
+  constructor() {}
10
+  private lockReconnect = false; //避免ws重复连接
11
+  ws; //定义websocket
12
+  private url = ""; //ws连接地址
13
+  private urlParams; //ws连接传参
14
+  private isHandler = false; //是否手动
15
+  private subject;
16
+  // 连接websocket
17
+  connectWs(url, data): Observable<any> {
18
+    this.url = url;
19
+    this.urlParams = data;
20
+    this.subject = new Subject<any>();
21
+    let request: any = {
22
+      url,
23
+      contentType: "application/json",
24
+      shared: true, // 标签共享
25
+      trackMessageLength: true, //校验数据完整性
26
+      transport: "websocket",
27
+    };
28
+
29
+    request.onOpen = (response) => {
30
+      console.log("ws连接成功" + new Date().toLocaleString());
31
+      this.heartCheck.reset().start(this);
32
+      this.ws.push(JSON.stringify(data));
33
+    };
34
+
35
+    request.onMessage = (response) => {
36
+      this.heartCheck.reset().start(this);
37
+      let message = response.responseBody;
38
+      try {
39
+        // alert(message);
40
+        console.log("收到消息" + message);
41
+        if (message !== "pong") {
42
+          this.subject.next(JSON.parse(message));
43
+        }
44
+      } catch (e) {
45
+        console.log("This doesn't look like a valid JSON: ", message);
46
+        return;
47
+      }
48
+    };
49
+
50
+    request.onClose = (response) => {
51
+      console.log("ws连接关闭" + new Date().toLocaleString(),this.isHandler);
52
+      this.heartCheck.reset();
53
+      if (!this.isHandler) {
54
+        this.reconnect(this.url, this.urlParams);
55
+      }
56
+    };
57
+
58
+    request.onError = (response) => {
59
+      console.log("ws连接错误",this.isHandler);
60
+      this.heartCheck.reset();
61
+      if (!this.isHandler) {
62
+        this.reconnect(this.url, this.urlParams);
63
+      }
64
+    };
65
+
66
+    this.ws = Atmosphere.subscribe(request);
67
+    return this.subject.asObservable();
68
+  }
69
+  //断线重连
70
+  private reconnect(url, data) {
71
+    if (this.lockReconnect) return;
72
+    this.lockReconnect = true;
73
+    setTimeout(() => {
74
+      //没连接上会一直重连,设置延迟避免请求过多
75
+      this.connectWs(url, data);
76
+      this.lockReconnect = false;
77
+    }, 15000);
78
+  }
79
+  private heartCheck = {
80
+    timeout: 60000, //1分钟发一次心跳
81
+    timeoutObj: null,
82
+    serverTimeoutObj: null,
83
+    reset: function () {
84
+      clearTimeout(this.timeoutObj);
85
+      clearTimeout(this.serverTimeoutObj);
86
+      return this;
87
+    },
88
+    start: function (_this) {
89
+      this.timeoutObj = setTimeout(() => {
90
+        //这里发送一个心跳,后端收到后,返回一个心跳消息,
91
+        //onmessage拿到返回的心跳就说明连接正常
92
+        _this.ws.push("ping");
93
+        this.serverTimeoutObj = setTimeout(() => {
94
+          //如果超过一定时间还没重置,说明后端主动断开了
95
+          _this.closeWs(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
96
+        }, this.timeout);
97
+      }, this.timeout);
98
+    },
99
+  };
100
+
101
+  // 断开websocket
102
+  closeWs(flag: boolean = false) {
103
+    this.isHandler = flag;
104
+    this.ws.close();
105
+  }
106
+}

+ 88 - 47
src/app/services/websocket-phone.service.ts

@@ -1,65 +1,106 @@
1
-import { Injectable } from '@angular/core';
2
-import { Subject, Observable, from } from 'rxjs';
1
+import { Injectable } from "@angular/core";
2
+import { Subject, Observable, from } from "rxjs";
3
+import * as Atmosphere from "atmosphere.js";
3 4
 
4 5
 @Injectable({
5
-  providedIn: 'root'
6
+  providedIn: "root",
6 7
 })
7 8
 export class WebsocketPhoneService {
8
-
9
-  constructor() { }
10
-
11
-
12
-  ws: WebSocket;//定义websocket
9
+  constructor() {}
10
+  private lockReconnect = false; //避免ws重复连接
11
+  ws; //定义websocket
12
+  private url = ""; //ws连接地址
13
+  private urlParams; //ws连接传参
14
+  private isHandler = false; //是否手动
13 15
   private subject;
14
-  flag = 1;//是否自动重连,1,正常,2,关闭,3,重连
15 16
   // 连接websocket
16 17
   connectWs(url, data): Observable<any> {
17
-    var postData = data;
18
-    if (this.ws != null) { this.ws.close() };
19
-    if (this.subject) { this.subject = null };
20
-    this.ws = new WebSocket(url);
18
+    this.url = url;
19
+    this.urlParams = data;
21 20
     this.subject = new Subject<any>();
22
-    let that = this;
23
-    this.ws.onopen = function (event) {
24
-      //socket 开启后执行,可以向后端传递信息
25
-      var sendData = JSON.stringify(postData)
26
-      that.ws.send(sendData);
27
-      console.log('open')
28
-    }
29
-    this.ws.onmessage = function (event) {
30
-      //socket 获取后端传递到前端的信息
31
-      console.log('onmessage')
32
-      if (event.data && event.data.length > 1) {
33
-        try {
34
-          that.subject.next(JSON.parse(event.data));
35
-        } catch (error) {
36
-          console.error('返回的值是'+event.data+',无法解析');
21
+    let request: any = {
22
+      url,
23
+      contentType: "application/json",
24
+      shared: true, // 标签共享
25
+      trackMessageLength: true, //校验数据完整性
26
+      transport: "websocket",
27
+    };
28
+
29
+    request.onOpen = (response) => {
30
+      console.log("ws连接成功" + new Date().toLocaleString());
31
+      this.heartCheck.reset().start(this);
32
+      this.ws.push(JSON.stringify(data));
33
+    };
34
+
35
+    request.onMessage = (response) => {
36
+      this.heartCheck.reset().start(this);
37
+      let message = response.responseBody;
38
+      try {
39
+        // alert(message);
40
+        console.log("收到消息" + message);
41
+        if (message !== "pong") {
42
+          this.subject.next(JSON.parse(message));
37 43
         }
44
+      } catch (e) {
45
+        console.log("This doesn't look like a valid JSON: ", message);
46
+        return;
38 47
       }
39
-    }
48
+    };
40 49
 
41
-    this.ws.onerror = function (event) {
42
-      //socket error信息
43
-      console.log('error')
50
+    request.onClose = (response) => {
51
+      console.log("ws连接关闭" + new Date().toLocaleString(),this.isHandler);
52
+      this.heartCheck.reset();
53
+      if (!this.isHandler) {
54
+        this.reconnect(this.url, this.urlParams);
55
+      }
56
+    };
44 57
 
45
-    }
46
-    this.ws.onclose = function (event) {
47
-      //socket 关闭后执行
48
-      console.log('close')
49
-      console.log('websocket 断开: ' + event.code + ' ' + event.reason + ' ' + event.wasClean)
50
-      if(event.wasClean){
51
-        that.flag = 2;
52
-      }else{
53
-        that.flag = 3;
58
+    request.onError = (response) => {
59
+      console.log("ws连接错误",this.isHandler);
60
+      this.heartCheck.reset();
61
+      if (!this.isHandler) {
62
+        this.reconnect(this.url, this.urlParams);
54 63
       }
55
-    }
64
+    };
56 65
 
57
-    return that.subject.asObservable();
66
+    this.ws = Atmosphere.subscribe(request);
67
+    return this.subject.asObservable();
58 68
   }
69
+  //断线重连
70
+  private reconnect(url, data) {
71
+    if (this.lockReconnect) return;
72
+    this.lockReconnect = true;
73
+    setTimeout(() => {
74
+      //没连接上会一直重连,设置延迟避免请求过多
75
+      this.connectWs(url, data);
76
+      this.lockReconnect = false;
77
+    }, 15000);
78
+  }
79
+  private heartCheck = {
80
+    timeout: 60000, //1分钟发一次心跳
81
+    timeoutObj: null,
82
+    serverTimeoutObj: null,
83
+    reset: function () {
84
+      clearTimeout(this.timeoutObj);
85
+      clearTimeout(this.serverTimeoutObj);
86
+      return this;
87
+    },
88
+    start: function (_this) {
89
+      this.timeoutObj = setTimeout(() => {
90
+        //这里发送一个心跳,后端收到后,返回一个心跳消息,
91
+        //onmessage拿到返回的心跳就说明连接正常
92
+        _this.ws.push("ping");
93
+        this.serverTimeoutObj = setTimeout(() => {
94
+          //如果超过一定时间还没重置,说明后端主动断开了
95
+          _this.closeWs(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
96
+        }, this.timeout);
97
+      }, this.timeout);
98
+    },
99
+  };
59 100
 
60
-  // 断开websocket,true关闭,false,断线重连
61
-  closeWs() {
62
-    this.ws.close()
101
+  // 断开websocket
102
+  closeWs(flag: boolean = false) {
103
+    this.isHandler = flag;
104
+    this.ws.close();
63 105
   }
64 106
 }
65
-

+ 106 - 0
src/app/services/websocket-ser.service.ts

@@ -0,0 +1,106 @@
1
+import { Injectable } from "@angular/core";
2
+import { Subject, Observable, from } from "rxjs";
3
+import * as Atmosphere from "atmosphere.js";
4
+
5
+@Injectable({
6
+  providedIn: "root",
7
+})
8
+export class WebsocketSerService {
9
+  constructor() {}
10
+  private lockReconnect = false; //避免ws重复连接
11
+  ws; //定义websocket
12
+  private url = ""; //ws连接地址
13
+  private urlParams; //ws连接传参
14
+  private isHandler = false; //是否手动
15
+  private subject;
16
+  // 连接websocket
17
+  connectWs(url, data): Observable<any> {
18
+    this.url = url;
19
+    this.urlParams = data;
20
+    this.subject = new Subject<any>();
21
+    let request: any = {
22
+      url,
23
+      contentType: "application/json",
24
+      shared: true, // 标签共享
25
+      trackMessageLength: true, //校验数据完整性
26
+      transport: "websocket",
27
+    };
28
+
29
+    request.onOpen = (response) => {
30
+      console.log("ws连接成功" + new Date().toLocaleString());
31
+      this.heartCheck.reset().start(this);
32
+      this.ws.push(JSON.stringify(data));
33
+    };
34
+
35
+    request.onMessage = (response) => {
36
+      this.heartCheck.reset().start(this);
37
+      let message = response.responseBody;
38
+      try {
39
+        // alert(message);
40
+        console.log("收到消息" + message);
41
+        if (message !== "pong") {
42
+          this.subject.next(JSON.parse(message));
43
+        }
44
+      } catch (e) {
45
+        console.log("This doesn't look like a valid JSON: ", message);
46
+        return;
47
+      }
48
+    };
49
+
50
+    request.onClose = (response) => {
51
+      console.log("ws连接关闭" + new Date().toLocaleString(),this.isHandler);
52
+      this.heartCheck.reset();
53
+      if (!this.isHandler) {
54
+        this.reconnect(this.url, this.urlParams);
55
+      }
56
+    };
57
+
58
+    request.onError = (response) => {
59
+      console.log("ws连接错误",this.isHandler);
60
+      this.heartCheck.reset();
61
+      if (!this.isHandler) {
62
+        this.reconnect(this.url, this.urlParams);
63
+      }
64
+    };
65
+
66
+    this.ws = Atmosphere.subscribe(request);
67
+    return this.subject.asObservable();
68
+  }
69
+  //断线重连
70
+  private reconnect(url, data) {
71
+    if (this.lockReconnect) return;
72
+    this.lockReconnect = true;
73
+    setTimeout(() => {
74
+      //没连接上会一直重连,设置延迟避免请求过多
75
+      this.connectWs(url, data);
76
+      this.lockReconnect = false;
77
+    }, 15000);
78
+  }
79
+  private heartCheck = {
80
+    timeout: 60000, //1分钟发一次心跳
81
+    timeoutObj: null,
82
+    serverTimeoutObj: null,
83
+    reset: function () {
84
+      clearTimeout(this.timeoutObj);
85
+      clearTimeout(this.serverTimeoutObj);
86
+      return this;
87
+    },
88
+    start: function (_this) {
89
+      this.timeoutObj = setTimeout(() => {
90
+        //这里发送一个心跳,后端收到后,返回一个心跳消息,
91
+        //onmessage拿到返回的心跳就说明连接正常
92
+        _this.ws.push("ping");
93
+        this.serverTimeoutObj = setTimeout(() => {
94
+          //如果超过一定时间还没重置,说明后端主动断开了
95
+          _this.closeWs(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
96
+        }, this.timeout);
97
+      }, this.timeout);
98
+    },
99
+  };
100
+
101
+  // 断开websocket
102
+  closeWs(flag: boolean = false) {
103
+    this.isHandler = flag;
104
+    this.ws.close();
105
+  }
106
+}

+ 0 - 56
src/app/services/websocket.service.ts

@@ -1,56 +0,0 @@
1
-import { Injectable } from '@angular/core';
2
-import { Subject, Observable } from 'rxjs';
3
-
4
-@Injectable({
5
-  providedIn: 'root'
6
-})
7
-export class WebsocketService {
8
-
9
-  constructor() { }
10
-  ws: WebSocket;//定义websocket
11
-  private subject;
12
-  // 连接websocket
13
-  connectWs(url, data): Observable<any> {
14
-    var postData = data;
15
-    if (this.ws != null) { this.ws.close() };
16
-    if (this.subject) { this.subject = null };
17
-    this.ws = new WebSocket(url);
18
-    this.subject = new Subject<any>();
19
-    let that = this;
20
-    this.ws.onopen = function (event) {
21
-      //socket 开启后执行,可以向后端传递信息
22
-      var sendData = JSON.stringify(postData)
23
-      that.ws.send(sendData);
24
-      console.log('open')
25
-    }
26
-    this.ws.onmessage = function (event) {
27
-      //socket 获取后端传递到前端的信息
28
-      console.log('onmessage')
29
-      if (event.data && event.data.length > 1) {
30
-        that.subject.next(JSON.parse(event.data));
31
-      }
32
-    }
33
-
34
-    this.ws.onerror = function (event) {
35
-      //socket error信息
36
-      console.log('error')
37
-
38
-    }
39
-    this.ws.onclose = function (event) {
40
-      //socket 关闭后执行
41
-      console.log('close')
42
-    }
43
-
44
-    return that.subject.asObservable();
45
-  }
46
-
47
-  // 断开websocket
48
-  closeWs() {
49
-    this.ws.close()
50
-  }
51
-
52
-
53
-
54
-
55
-
56
-}

+ 24 - 36
src/app/views/fuwutai/fuwutai.component.ts

@@ -9,8 +9,8 @@ import { FormBuilder, FormGroup, Validators } from "@angular/forms";
9 9
 import { Router } from "@angular/router";
10 10
 
11 11
 import { MainService } from "../../services/main.service";
12
-import { WebsocketService } from "../../services/websocket.service";
13 12
 import { WebsocketPhoneService } from "../../services/websocket-phone.service";
13
+import { WebsocketSerService } from "../../services/websocket-ser.service";
14 14
 import http from "../../../assets/js/http";
15 15
 
16 16
 import { NzMessageService } from "ng-zorro-antd/message";
@@ -104,8 +104,8 @@ export class FuwutaiComponent implements OnInit {
104 104
     private mainService: MainService,
105 105
     private msg: NzMessageService,
106 106
     private notification: NzNotificationService,
107
-    private webs: WebsocketService,
108
-    private phones: WebsocketPhoneService
107
+    private phones: WebsocketPhoneService,
108
+    private sers: WebsocketSerService
109 109
   ) {}
110 110
   validateFormZy!: FormGroup;
111 111
   validateFormQt!: FormGroup;
@@ -521,7 +521,7 @@ export class FuwutaiComponent implements OnInit {
521 521
       workOrderRemark: [null], //工单备注
522 522
     });
523 523
 
524
-    this.getWebsocket();
524
+    this.getWebsocketSer();
525 525
     this.initRole();
526 526
     this.initLogin();
527 527
     this.retract();
@@ -539,12 +539,12 @@ export class FuwutaiComponent implements OnInit {
539 539
     this.getUnassignedBuilding2();
540 540
   }
541 541
   ngOnDestroy() {
542
-    console.log("关闭ser", this.webs.ws);
543 542
     clearInterval(this.refreshTimerId);
544
-    clearInterval(this.timerWS);
545
-    this.webs.closeWs();
546 543
     if (this.phones.ws) {
547
-      this.phones.closeWs();
544
+      this.phones.closeWs(true);
545
+    }
546
+    if (this.sers.ws) {
547
+      this.sers.closeWs(true);
548 548
     }
549 549
   }
550 550
   unassignedSearchCon: string = ""; //未分派搜索框内容
@@ -608,39 +608,25 @@ export class FuwutaiComponent implements OnInit {
608 608
   }
609 609
 
610 610
   // 连接websocket
611
-  getWebsocket() {
612
-    let that = this;
613
-    this.webs
614
-      .connectWs(http.fwtWs, { userCount: that.user.user.account })
615
-      .subscribe((data) => {
616
-        console.log(data);
617
-        if (data && data.content) {
618
-          that.createBasicNotification(data);
611
+  getWebsocketSer() {
612
+    this.sers
613
+      .connectWs(http.fwtWs, { userCount: this.user.user.account })
614
+      .subscribe((result) => {
615
+        console.log(result);
616
+        if (result && result.content) {
617
+          this.createBasicNotification(result);
619 618
         }
620
-        that.windowResize();
619
+        this.windowResize();
621 620
       });
622 621
   }
623
-  // 监听ws
624
-  timerWS = null;
625
-  listenWs() {
626
-    clearInterval(this.timerWS);
627
-    this.timerWS = setInterval(() => {
628
-      console.log(this.phones.flag);
629
-      if (this.phones.flag == 3) {
630
-        this.getWebsocketPhone();
631
-      }
632
-    }, 15000);
633
-  }
634 622
   // 连接websocket---phone
635 623
   getWebsocketPhone() {
636
-    this.listenWs();
637 624
     let userCount =
638 625
       this.user.user.account + "|" + this.phoneNumListSelected.join("_");
639 626
     this.phones.connectWs(http.phoneWs, { userCount }).subscribe((data) => {
640
-      // data = { status: 201, phone: '013581394342' };//ceshi
627
+      // data = { status: 201, phone: '013581394341' };//ceshi
641 628
       console.log(data);
642 629
       if (data.status == 200 && data.phone) {
643
-        this.phones.flag = 1;
644 630
         localStorage.setItem("phones", data.phone);
645 631
         this.phoneNumListBind = data.phone.split("_");
646 632
         this.phoneNumListBind.forEach((item) => {
@@ -697,16 +683,18 @@ export class FuwutaiComponent implements OnInit {
697 683
   //确定绑定
698 684
   bindingOk() {
699 685
     if (this.phoneNumListSelected.length > 0) {
700
-      // setInterval(() => {
701
-      this.getWebsocketPhone(); //ceshi
702
-      // }, 5000)
686
+      if (this.phones.ws) {
687
+        this.phones.closeWs(true);
688
+      }
689
+      this.getWebsocketPhone();
703 690
     } else {
691
+      localStorage.removeItem("phones");
704 692
       this.binding = 3;
705 693
       this.phoneNumList.forEach((item) => {
706 694
         item.checked = false;
707 695
       });
708 696
       if (this.phones.ws) {
709
-        this.phones.closeWs();
697
+        this.phones.closeWs(true);
710 698
       }
711 699
     }
712 700
   }
@@ -2187,7 +2175,7 @@ export class FuwutaiComponent implements OnInit {
2187 2175
           that.showPromptModal("保存", true, "");
2188 2176
           // 关闭phone的websocket
2189 2177
           if (this.phones.ws) {
2190
-            this.phones.closeWs();
2178
+            this.phones.closeWs(true);
2191 2179
           }
2192 2180
           this.getPhoneNum(false);
2193 2181
           that.user.user.scope = data.data;

+ 212 - 225
src/app/views/home/home.component.ts

@@ -1,90 +1,77 @@
1
-import { Component, OnInit, ViewChild } from '@angular/core';
2
-import { ActivatedRoute, Router } from "@angular/router"
3
-import { FormBuilder, Validators, FormGroup } from '@angular/forms';
1
+import { Component, OnInit, ViewChild } from "@angular/core";
2
+import { Router } from "@angular/router";
3
+import { FormBuilder, Validators, FormGroup } from "@angular/forms";
4 4
 
5 5
 import { MainService } from "../../services/main.service";
6
-import { MyServiceService } from "../../services/my-service.service";
6
+// import { MyServiceService } from "../../services/my-service.service";
7
+import { WebsocketIndexService } from "../../services/websocket-index.service";
7 8
 import http from "../../../assets/js/http";
8
-import { OverlayScrollbarsComponent } from 'overlayscrollbars-ngx';
9
-import { ToolService } from '../../services/tool.service';
10
-
9
+import { OverlayScrollbarsComponent } from "overlayscrollbars-ngx";
10
+import { ToolService } from "../../services/tool.service";
11 11
 
12 12
 @Component({
13
-  selector: 'app-home',
14
-  templateUrl: './home.component.html',
15
-  styleUrls: ['./home.component.less']
13
+  selector: "app-home",
14
+  templateUrl: "./home.component.html",
15
+  styleUrls: ["./home.component.less"],
16 16
 })
17 17
 export class HomeComponent implements OnInit {
18
-  @ViewChild('osComponentRef1', { read: OverlayScrollbarsComponent, static: false })
18
+  @ViewChild("osComponentRef1", {
19
+    read: OverlayScrollbarsComponent,
20
+    static: false,
21
+  })
19 22
   osComponentRef1: OverlayScrollbarsComponent;
20
-  @ViewChild('osComponentRef2', { read: OverlayScrollbarsComponent, static: false })
23
+  @ViewChild("osComponentRef2", {
24
+    read: OverlayScrollbarsComponent,
25
+    static: false,
26
+  })
21 27
   osComponentRef2: OverlayScrollbarsComponent;
22
-  constructor(private fb: FormBuilder, private route: ActivatedRoute, private router: Router, private mainService: MainService, private myService: MyServiceService, private tool: ToolService) {
23
-  }
28
+  constructor(
29
+    private fb: FormBuilder,
30
+    private router: Router,
31
+    private mainService: MainService,
32
+    // private myService: MyServiceService,
33
+    private myService: WebsocketIndexService,
34
+    private tool: ToolService
35
+  ) {}
24 36
 
25 37
   ngOnInit() {
26 38
     let that = this;
27
-    that.initForm()
28
-    that.getAllHos()
29
-    that.connectWs()
39
+    that.initForm();
40
+    that.getAllHos();
41
+    // that.connectWs();
42
+    this.getWebsocketIndex();
30 43
   }
31 44
 
32
-  ws: WebSocket;//定义websocket
33
-  validateForm: FormGroup;//新增/编辑表单
34
-
35
-  promptContent: string;//操作提示框提示信息
36
-  ifSuccess: boolean;//操作成功/失败
37
-  promptInfo: string;//操作结果提示信息
38
-  promptModalShow: boolean;//操作提示框是否展示
39
-
40
-
41
-  //socket连接
42
-  wsData;//socket返回的数据
43
-  groupOrders = [];//当前各小组工单执行情况
44
-  userSituation: any = {};//用户信息
45
-  orderSituation: any = {};//工单信息
46
-  connectWs() {
47
-    if (this.ws != null) { this.ws.close() };
48
-    this.ws = new WebSocket(http.homeWs);
49
-    let that = this;
50
-    this.ws.onopen = function (event) {
51
-      //socket 开启后执行,可以向后端传递信息
52
-      var data = JSON.stringify({ hosIds: that.hosIds })
53
-      that.ws.send(data);
54
-    }
55
-    this.ws.onmessage = function (event) {
56
-      //socket 获取后端传递到前端的信息
57
-      if (event.data && event.data.length > 1) {
58
-        that.wsData = JSON.parse(event.data)
59
-        if (that.wsData.groupOrders && that.wsData.groupOrders.status == 200) {
60
-          that.groupOrders = that.wsData.groupOrders.data;
45
+  validateForm: FormGroup; //新增/编辑表单
46
+
47
+  promptContent: string; //操作提示框提示信息
48
+  ifSuccess: boolean; //操作成功/失败
49
+  promptInfo: string; //操作结果提示信息
50
+  promptModalShow: boolean; //操作提示框是否展示
51
+  //连接ws
52
+  groupOrders = []; //当前各小组工单执行情况
53
+  userSituation: any = {}; //用户信息
54
+  orderSituation: any = {}; //工单信息
55
+  getWebsocketIndex() {
56
+    this.myService
57
+      .connectWs(http.homeWs, { hosIds: this.hosIds })
58
+      .subscribe((result) => {
59
+        console.log(result);
60
+        if (result.groupOrders && result.groupOrders.status == 200) {
61
+          this.groupOrders = result.groupOrders.data;
61 62
         }
62
-        if (that.wsData.userSituation) {
63
-          that.userSituation = that.wsData.userSituation;
63
+        if (result.userSituation) {
64
+          this.userSituation = result.userSituation;
64 65
         }
65
-        if (that.wsData.orderSituation) {
66
-          that.orderSituation = that.wsData.orderSituation;
66
+        if (result.orderSituation) {
67
+          this.orderSituation = result.orderSituation;
67 68
         }
68
-      }
69
-    }
70
-
71
-    this.ws.onerror = function (event) {
72
-      //socket error信息
73
-      console.log('error')
74
-    }
75
-    this.ws.onclose = function () {
76
-      //socket 关闭后执行
77
-      setTimeout(() => {
78
-        that.connectWs();
79
-      }, 15000);
80
-    }
69
+      });
81 70
   }
82
-
83 71
   ngOnDestroy() {
84
-    clearInterval(this.moveId)
85
-    clearInterval(this.moveId1)
86
-    console.log('首页关闭')
87
-    this.ws.close()
72
+    clearInterval(this.moveId);
73
+    clearInterval(this.moveId1);
74
+    this.myService.closeWs(true);
88 75
   }
89 76
   // 小组好评
90 77
   groupInfo = [];
@@ -95,18 +82,20 @@ export class HomeComponent implements OnInit {
95 82
       hosIds: that.hosIds,
96 83
     };
97 84
     this.loading1 = true;
98
-    that.mainService.postCustom('index', 'groupEvaTop', postData).subscribe(data => {
99
-      this.loading1 = false;
100
-      that.groupInfo = data.data || []
101
-      if (data.data && data.data.length) {
102
-        data.data.forEach(e => {
103
-          e.value = e.percent
104
-        });
105
-        this.groupChart(data.data)
106
-      } else {
107
-        this.groupChart([])
108
-      }
109
-    })
85
+    that.mainService
86
+      .postCustom("index", "groupEvaTop", postData)
87
+      .subscribe((data) => {
88
+        this.loading1 = false;
89
+        that.groupInfo = data.data || [];
90
+        if (data.data && data.data.length) {
91
+          data.data.forEach((e) => {
92
+            e.value = e.percent;
93
+          });
94
+          this.groupChart(data.data);
95
+        } else {
96
+          this.groupChart([]);
97
+        }
98
+      });
110 99
   }
111 100
   // 好评Echarts
112 101
   groupOption;
@@ -114,12 +103,12 @@ export class HomeComponent implements OnInit {
114 103
     let labels = [];
115 104
     let nums = [];
116 105
     if (datas.length) {
117
-      datas.forEach(e => {
118
-        labels.push(e.name)
119
-        nums.push(e.percent - 0)
106
+      datas.forEach((e) => {
107
+        labels.push(e.name);
108
+        nums.push(e.percent - 0);
120 109
       });
121 110
     }
122
-    console.log(labels, nums)
111
+    console.log(labels, nums);
123 112
     var dataShadow = [];
124 113
 
125 114
     for (var i = 0; i < datas.length; i++) {
@@ -127,140 +116,137 @@ export class HomeComponent implements OnInit {
127 116
     }
128 117
     this.groupOption = {
129 118
       grid: {
130
-        left: '10',
131
-        right: '100',
132
-        top: '0',
133
-        bottom: '0',
134
-        containLabel: true
119
+        left: "10",
120
+        right: "100",
121
+        top: "0",
122
+        bottom: "0",
123
+        containLabel: true,
135 124
       },
136 125
       xAxis: {
137
-        show: ''
126
+        show: "",
138 127
       },
139 128
       yAxis: {
140
-        type: 'category',
129
+        type: "category",
141 130
         data: labels,
142 131
         axisLabel: {
143 132
           margin: 15,
144 133
           textStyle: {
145
-            color: '#666'
146
-          }
134
+            color: "#666",
135
+          },
147 136
         },
148 137
         axisTick: {
149 138
           lineStyle: {
150
-            color: '#fff'
151
-          }
139
+            color: "#fff",
140
+          },
152 141
         },
153 142
         axisLine: {
154 143
           lineStyle: {
155
-            color: '#fff'
156
-          }
157
-        }
144
+            color: "#fff",
145
+          },
146
+        },
158 147
       },
159 148
       series: [
160
-        { // For shadow
161
-          type: 'bar',
149
+        {
150
+          // For shadow
151
+          type: "bar",
162 152
           itemStyle: {
163
-            color: 'rgba(0,0,0,0.04)'
153
+            color: "rgba(0,0,0,0.04)",
164 154
           },
165
-          barGap: '-100%',
166
-          barCategoryGap: '26',
167
-          barWidth: '26',
155
+          barGap: "-100%",
156
+          barCategoryGap: "26",
157
+          barWidth: "26",
168 158
           data: dataShadow,
169
-          animation: false
159
+          animation: false,
170 160
         },
171 161
         {
172
-          name: '建单总数',
173
-          type: 'bar',
162
+          name: "建单总数",
163
+          type: "bar",
174 164
           data: nums,
175 165
           label: {
176 166
             show: true,
177
-            position: 'right',
178
-            formatter: '{c} % '
167
+            position: "right",
168
+            formatter: "{c} % ",
179 169
           },
180 170
           itemStyle: {
181 171
             normal: {
182 172
               //每根柱子颜色设置
183
-              color: '#49b856'
184
-            }
173
+              color: "#49b856",
174
+            },
185 175
           },
186
-          barCategoryGap: '26',
187
-          barWidth: '26'
188
-        }
189
-      ]
176
+          barCategoryGap: "26",
177
+          barWidth: "26",
178
+        },
179
+      ],
190 180
     };
191 181
   }
192 182
 
193
-
194 183
   // 近一周异常工单
195 184
   abOrders: Array<any> = [];
196 185
   loading2 = false;
197 186
   getAbnormityOrders() {
198 187
     let that = this;
199 188
     this.loading2 = true;
200
-    that.mainService.postCustom('index', 'abnormityOrders', { hosIds: that.hosIds }).subscribe(data => {
201
-      this.loading2 = false;
202
-      that.abOrders = data.data || [];
203
-    })
189
+    that.mainService
190
+      .postCustom("index", "abnormityOrders", { hosIds: that.hosIds })
191
+      .subscribe((data) => {
192
+        this.loading2 = false;
193
+        that.abOrders = data.data || [];
194
+      });
204 195
   }
205 196
 
206 197
   // 工单详情
207 198
   // 查看
208 199
   orderDetail(id) {
209
-    this.router.navigateByUrl('/main/home/orderDetail/' + id);
200
+    this.router.navigateByUrl("/main/home/orderDetail/" + id);
210 201
   }
211 202
 
212
-
213
-
214 203
   // swiper 轮播图
215
-  swiperArr = []
204
+  swiperArr = [];
216 205
   swiperTop: number = 0;
217 206
   moveId: any;
218 207
   index: number = 0;
219 208
   startSwiper(length) {
220 209
     let that = this;
221
-    clearInterval(that.moveId)
210
+    clearInterval(that.moveId);
222 211
     setInterval(() => {
223 212
       that.index++;
224
-      clearInterval(that.moveId)
213
+      clearInterval(that.moveId);
225 214
       that.moveId = setInterval(() => {
226
-        if (that.swiperTop > (-32 * that.index)) {
215
+        if (that.swiperTop > -32 * that.index) {
227 216
           that.swiperTop--;
228 217
         }
229
-        if (that.swiperTop <= length * (-32)) {
230
-          that.swiperTop = 0
231
-          that.index = 0
232
-          clearInterval(that.moveId)
218
+        if (that.swiperTop <= length * -32) {
219
+          that.swiperTop = 0;
220
+          that.index = 0;
221
+          clearInterval(that.moveId);
233 222
         }
234 223
       }, 20);
235 224
     }, 2000);
236
-
237 225
   }
238 226
 
239
-  swiperArr1 = [1, 2, 3]
227
+  swiperArr1 = [1, 2, 3];
240 228
   swiperTop1: number = 0;
241 229
   moveId1: any;
242 230
   index1: number = 0;
243 231
   startSwiper1(length) {
244 232
     let that = this;
245
-    clearInterval(that.moveId1)
233
+    clearInterval(that.moveId1);
246 234
     setInterval(() => {
247 235
       that.index1++;
248
-      clearInterval(that.moveId1)
236
+      clearInterval(that.moveId1);
249 237
       that.moveId1 = setInterval(() => {
250
-        if (that.swiperTop1 > (-32 * that.index1)) {
238
+        if (that.swiperTop1 > -32 * that.index1) {
251 239
           that.swiperTop1--;
252 240
         }
253
-        if (that.swiperTop1 <= length * (-32)) {
254
-          that.swiperTop1 = 0
255
-          that.index1 = 0
256
-          clearInterval(that.moveId1)
241
+        if (that.swiperTop1 <= length * -32) {
242
+          that.swiperTop1 = 0;
243
+          that.index1 = 0;
244
+          clearInterval(that.moveId1);
257 245
         }
258 246
       }, 20);
259 247
     }, 2000);
260
-
261 248
   }
262 249
 
263
-
264 250
   // 切换工单差评/最新意见
265 251
   thisTab = 1;
266 252
   checkTab(tab) {
@@ -272,7 +258,7 @@ export class HomeComponent implements OnInit {
272 258
 
273 259
   // 展开收起
274 260
   isOpen: boolean = false;
275
-  coopOpenId;//当前展开/收起id
261
+  coopOpenId; //当前展开/收起id
276 262
   more(id) {
277 263
     if (this.coopOpenId == id) {
278 264
       this.isOpen = !this.isOpen;
@@ -283,37 +269,36 @@ export class HomeComponent implements OnInit {
283 269
   }
284 270
 
285 271
   // 获取当前登录人所有院区
286
-  hosIds: string = '';//院区id
287
-  hosName: string = '';//当前院区名称
272
+  hosIds: string = ""; //院区id
273
+  hosName: string = ""; //当前院区名称
288 274
   getAllHos() {
289 275
     let hos: any = this.tool.getCurrentHospital();
290 276
     this.hosName = hos.hosName;
291
-    this.hosIds = hos.id + '';
292
-    this.refreshList()
293
-    this.getGroupCount()
294
-    this.getAbnormityOrders()
277
+    this.hosIds = hos.id + "";
278
+    this.refreshList();
279
+    this.getGroupCount();
280
+    this.getAbnormityOrders();
295 281
   }
296 282
 
297
-
298 283
   // 翻页获取当前列表
299 284
   getPageList() {
300 285
     if (this.thisTab == 1) {
301
-      this.getBadList()
286
+      this.getBadList();
302 287
     } else if (this.thisTab == 2) {
303
-      this.getAdviceList()
288
+      this.getAdviceList();
304 289
     }
305 290
   }
306 291
   // 刷新页面右侧列表
307 292
   refreshList() {
308
-    this.getAdviceList()
309
-    this.getBadList()
293
+    this.getAdviceList();
294
+    this.getBadList();
310 295
   }
311 296
   // 差评表格数据
312 297
   listIdx = 1;
313 298
   listLength = 0;
314
-  badNum = 0;//差评数量
315
-  badList = [];//差评列表
316
-  Loading: boolean = true;//差评loading
299
+  badNum = 0; //差评数量
300
+  badList = []; //差评列表
301
+  Loading: boolean = true; //差评loading
317 302
   getBadList() {
318 303
     var that = this;
319 304
     that.Loading = true;
@@ -322,40 +307,41 @@ export class HomeComponent implements OnInit {
322 307
       sum: 10,
323 308
       badEvaluationHandle: {
324 309
         hosIds: that.hosIds,
325
-        handleStatus: { id: "286" }//未调解
310
+        handleStatus: { id: "286" }, //未调解
326 311
       },
327
-    }
328
-    that.mainService.getFetchDataList('adviceCollection', 'badEvaluationHandle', data).subscribe(data => {
329
-      that.badList = data.list;
330
-      that.Loading = false;
331
-      that.listLength = that.badNum = data.totalNum;
332
-    })
333
-
312
+    };
313
+    that.mainService
314
+      .getFetchDataList("adviceCollection", "badEvaluationHandle", data)
315
+      .subscribe((data) => {
316
+        that.badList = data.list;
317
+        that.Loading = false;
318
+        that.listLength = that.badNum = data.totalNum;
319
+      });
334 320
   }
335 321
 
336
-
337 322
   // 意见列表
338 323
   adviceList = [];
339
-  adviceNum = 0;//意见数量
324
+  adviceNum = 0; //意见数量
340 325
   getAdviceList() {
341 326
     this.Loading = true;
342 327
     let data = {
343 328
       idx: this.listIdx - 1,
344 329
       sum: 10,
345 330
       advice: {
346
-        replaceFlag: 0,//未回复
347
-        hosIds: this.hosIds
331
+        replaceFlag: 0, //未回复
332
+        hosIds: this.hosIds,
348 333
       },
349
-    }
334
+    };
350 335
 
351
-    this.mainService.getFetchDataList('adviceCollection', 'advice', data).subscribe(data => {
352
-      this.adviceList = data.list;
353
-      this.Loading = false;
354
-      this.listLength = this.adviceNum = data.totalNum;
355
-    })
336
+    this.mainService
337
+      .getFetchDataList("adviceCollection", "advice", data)
338
+      .subscribe((data) => {
339
+        this.adviceList = data.list;
340
+        this.Loading = false;
341
+        this.listLength = this.adviceNum = data.totalNum;
342
+      });
356 343
   }
357 344
 
358
-
359 345
   // 初始化新增form表单
360 346
   initForm() {
361 347
     this.validateForm = this.fb.group({
@@ -375,31 +361,28 @@ export class HomeComponent implements OnInit {
375 361
       badEvaluationHandle: {
376 362
         id: that.coopId,
377 363
         mediationUser: { id: that.validateForm.value.user },
378
-      }
379
-    }
380
-    that.mainService.postCustom('adviceCollection', 'updData/badEvaluationHandle', postData).subscribe(data => {
381
-      that.hideDesModal()
382
-      that.btnLoading = false;
383
-      if (data.status == 200) {
384
-        that.showPromptModal('指派', true, '');
385
-        that.initForm()
386
-        that.listIdx = 1;
387
-
388
-        that.getBadList()
389
-      } else {
390
-        that.showPromptModal('指派', false, data.msg);
391
-      }
392
-    })
393
-
394
-
364
+      },
365
+    };
366
+    that.mainService
367
+      .postCustom("adviceCollection", "updData/badEvaluationHandle", postData)
368
+      .subscribe((data) => {
369
+        that.hideDesModal();
370
+        that.btnLoading = false;
371
+        if (data.status == 200) {
372
+          that.showPromptModal("指派", true, "");
373
+          that.initForm();
374
+          that.listIdx = 1;
375
+
376
+          that.getBadList();
377
+        } else {
378
+          that.showPromptModal("指派", false, data.msg);
379
+        }
380
+      });
395 381
   }
396 382
 
397
-
398
-
399
-
400 383
   // 回复弹框
401
-  modal: boolean = false;//回复模态框
402
-  replyCon: any;//意见内容
384
+  modal: boolean = false; //回复模态框
385
+  replyCon: any; //意见内容
403 386
   reply(data) {
404 387
     this.modal = true;
405 388
     this.replyCon = data;
@@ -408,14 +391,15 @@ export class HomeComponent implements OnInit {
408 391
     this.modal = false;
409 392
   }
410 393
 
411
-
412 394
   // 指派人员
413
-  allDesUser: any = [];//所有支助人员
395
+  allDesUser: any = []; //所有支助人员
414 396
   getAllDesUser(id) {
415 397
     let that = this;
416
-    that.mainService.getHosUser('getUsersByPermission', id).subscribe(data => {
417
-      that.allDesUser = data.data || []
418
-    })
398
+    that.mainService
399
+      .getHosUser("getUsersByPermission", id)
400
+      .subscribe((data) => {
401
+        that.allDesUser = data.data || [];
402
+      });
419 403
   }
420 404
 
421 405
   // 指派
@@ -424,7 +408,7 @@ export class HomeComponent implements OnInit {
424 408
   designate(id, hosId) {
425 409
     this.desModal = true;
426 410
     this.coopId = id;
427
-    this.getAllDesUser(hosId)
411
+    this.getAllDesUser(hosId);
428 412
   }
429 413
   hideDesModal() {
430 414
     this.desModal = false;
@@ -432,12 +416,13 @@ export class HomeComponent implements OnInit {
432 416
 
433 417
   // 调解
434 418
   detail(id) {
435
-    this.router.navigate(['/main/home/appraiseDetail/' + id], { fragment: 'tj' });
419
+    this.router.navigate(["/main/home/appraiseDetail/" + id], {
420
+      fragment: "tj",
421
+    });
436 422
   }
437 423
 
438
-
439 424
   // 回复
440
-  adviceSubContent: string;//回复内容
425
+  adviceSubContent: string; //回复内容
441 426
   btnLoading: boolean = false;
442 427
   submitReplyForm() {
443 428
     let that = this;
@@ -447,30 +432,32 @@ export class HomeComponent implements OnInit {
447 432
       advice: {
448 433
         id: that.replyCon.id,
449 434
         replyUser: { id: that.replyCon.createUser.id },
450
-        replyContent: that.adviceSubContent
451
-      }
452
-    }
453
-    that.mainService.postCustom('adviceCollection', 'updData/advice', postData).subscribe(data => {
454
-      that.btnLoading = false;
455
-      that.adviceSubContent = '';
456
-      if (data.status == 200) {
457
-        that.showPromptModal('回复', true, '');
458
-        that.hideModal()
459
-        that.listIdx = 1;
460
-        that.getAdviceList()
461
-      } else {
462
-        that.showPromptModal('回复', false, data.msg);
463
-      }
464
-    })
435
+        replyContent: that.adviceSubContent,
436
+      },
437
+    };
438
+    that.mainService
439
+      .postCustom("adviceCollection", "updData/advice", postData)
440
+      .subscribe((data) => {
441
+        that.btnLoading = false;
442
+        that.adviceSubContent = "";
443
+        if (data.status == 200) {
444
+          that.showPromptModal("回复", true, "");
445
+          that.hideModal();
446
+          that.listIdx = 1;
447
+          that.getAdviceList();
448
+        } else {
449
+          that.showPromptModal("回复", false, data.msg);
450
+        }
451
+      });
465 452
   }
466 453
 
467 454
   // 跳转到指定页面
468 455
   toMenu(type, title) {
469
-    if (type == 'orderManagement') {
470
-      this.router.navigate(['/main/' + type], { fragment: 'yc' })
456
+    if (type == "orderManagement") {
457
+      this.router.navigate(["/main/" + type], { fragment: "yc" });
471 458
       return;
472 459
     }
473
-    this.router.navigateByUrl('/main/' + type);
460
+    this.router.navigateByUrl("/main/" + type);
474 461
   }
475 462
 
476 463
   // 展示信息提示框(con:提示信息,success:操作是否成功,promptInfo:操作结果提示信息)

+ 3 - 4
src/app/views/hushijiandan/hushijiandan.component.ts

@@ -15,7 +15,7 @@ import { Router } from "@angular/router";
15 15
 
16 16
 import http from "../../../assets/js/http";
17 17
 import { MainService } from "../../services/main.service";
18
-import { WebsocketService } from "../../services/websocket.service";
18
+import { WebsocketNurseService } from "../../services/websocket-nurse.service";
19 19
 
20 20
 import { NzMessageService } from "ng-zorro-antd/message";
21 21
 import { NzNotificationService } from "ng-zorro-antd/notification";
@@ -100,7 +100,7 @@ export class HushijiandanComponent implements OnInit {
100 100
     private mainService: MainService,
101 101
     private msg: NzMessageService,
102 102
     private notification: NzNotificationService,
103
-    private webs: WebsocketService,
103
+    private webs: WebsocketNurseService,
104 104
     private tool: ToolService
105 105
   ) {
106 106
     this.othersListOptions = {
@@ -193,13 +193,12 @@ export class HushijiandanComponent implements OnInit {
193 193
     // 工单列表倒计时 end
194 194
   }
195 195
   ngOnDestroy() {
196
-    console.log("关闭护士端", this.webs.ws);
197 196
     clearInterval(this.moveId);
198 197
     clearInterval(this.moveId1);
199 198
     clearInterval(this.msgTimerId);
200 199
     clearInterval(this.logTimer);
201 200
     clearInterval(this.orderListTimer);
202
-    this.webs.closeWs();
201
+    this.webs.closeWs(true);
203 202
   }
204 203
   loginUser: any = localStorage.getItem("user")
205 204
     ? JSON.parse(localStorage.getItem("user")).user

+ 3 - 3
src/app/views/main/main.component.ts

@@ -3,7 +3,7 @@ import { NavigationEnd, Router } from "@angular/router";
3 3
 import { MainService } from "../../services/main.service";
4 4
 
5 5
 import { NzMessageService } from "ng-zorro-antd/message";
6
-import { WebsocketService } from "../../services/websocket.service";
6
+import { WebsocketMainService } from "../../services/websocket-main.service";
7 7
 import http from "../../../assets/js/http";
8 8
 
9 9
 import { NzNotificationService } from "ng-zorro-antd/notification";
@@ -38,7 +38,7 @@ export class MainComponent implements OnInit {
38 38
     private mainService: MainService,
39 39
     private msg: NzMessageService,
40 40
     private notification: NzNotificationService,
41
-    private webs: WebsocketService,
41
+    private webs: WebsocketMainService,
42 42
     private tool: ToolService
43 43
   ) {}
44 44
   ngOnInit() {
@@ -59,7 +59,7 @@ export class MainComponent implements OnInit {
59 59
     //取消路由监听
60 60
     this.routerEventsListener.unsubscribe();
61 61
     // 断掉连接
62
-    this.webs.closeWs();
62
+    this.webs.closeWs(true);
63 63
   }
64 64
   // 根据url高亮菜单baba
65 65
   highlightMenuByUrl() {