index.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <template>
  2. <view>
  3. <u-toast ref="uToast" />
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. return {
  10. text: "",
  11. // 确保websocket是打开状态
  12. is_open_socket: false,
  13. timer: 10000,
  14. alertList: [],
  15. num: 0,
  16. isClose: false,
  17. showMian: false,
  18. is_open_socket: false,
  19. objData: {},
  20. host: "",
  21. };
  22. },
  23. props: {
  24. isBack: {
  25. type: [Boolean, String],
  26. default: false,
  27. },
  28. },
  29. onLaunch: function () {
  30. console.warn(
  31. "当前组件仅支持 uni_modules 目录结构 ,请升级 HBuilderX 到 3.1.0 版本以上!"
  32. );
  33. console.log("App Launc11h");
  34. },
  35. onShow() {},
  36. onHide: function () {
  37. console.log("App Hide");
  38. let that = this;
  39. that.isClose = true;
  40. uni.getStorage({
  41. key: "isBackShow",
  42. success(res) {
  43. console.log("退出", res.data);
  44. if (res.data == true) {
  45. that.isClose = true;
  46. uni.closeSocket();
  47. that.socketTask = null;
  48. return clearInterval(that.timer);
  49. }
  50. },
  51. });
  52. },
  53. created() {
  54. console.log("创建");
  55. console.log("路径", this.$route.path);
  56. },
  57. mounted() {
  58. let _this = this;
  59. uni.getStorage({
  60. key: "hostappst",
  61. success(res) {
  62. console.log("获取成功", res.data);
  63. _this.host = res.data;
  64. uni.showToast({
  65. title: _this.host,
  66. icon: "none",
  67. });
  68. },
  69. });
  70. uni.getStorage({
  71. key: "num",
  72. success(res) {
  73. console.log("获取成功", res.data);
  74. if (res.data != "") {
  75. _this.num = res.data;
  76. _this.getStort();
  77. }
  78. },
  79. });
  80. console.log("渲染1");
  81. },
  82. beforeDestroy() {
  83. console.log("退出0");
  84. let that = this;
  85. that.isClose = true;
  86. uni.getStorage({
  87. key: "isBackShow",
  88. success(res) {
  89. console.log("退出", res.data);
  90. uni.closeSocket();
  91. that.socketTask = null;
  92. return (that.isClose = true);
  93. clearInterval(that.timer);
  94. },
  95. });
  96. //
  97. },
  98. methods: {
  99. getStort() {
  100. let _this = this;
  101. uni.getStorage({
  102. key: "num",
  103. success(res) {
  104. console.log("获取成功", res.data);
  105. _this.num = res.data;
  106. _this.connectSocket();
  107. },
  108. });
  109. },
  110. //连接websocket
  111. connectSocket() {
  112. let that = this;
  113. console.log("调用连接websocket", this.socketTas, "this.num", this.num);
  114. this.socketTask = uni.connectSocket({
  115. url: `ws://${that.host}/websocket/device/` + this.num,
  116. success(res) {
  117. console.log("res", res);
  118. console.log("websocket连接成功");
  119. },
  120. fail(err) {
  121. console.log("报错", err);
  122. },
  123. });
  124. this.socketTask.onOpen(function (res) {
  125. console.log("WebSocket连接已打开!");
  126. that.isSuccess = true;
  127. that.heart();
  128. });
  129. this.socketTask.onMessage(function (res) {
  130. that.objData = JSON.parse(JSON.stringify(JSON.parse(res.data)));
  131. console.log("收到服务器内容:", that.objData.type);
  132. console.log("未解析:收到服务器内容:", res.data);
  133. if (that.objData.type == "notify") {
  134. console.log("进", that.objData.content.content);
  135. if (that.objData.content.status == 1) {
  136. uni.showToast({
  137. title:
  138. that.objData.content.deviceName + that.objData.content.content,
  139. icon: "none",
  140. });
  141. } else {
  142. that.showMian = true;
  143. if (that.$store.state.alertList.length < 16) {
  144. console.log("长度");
  145. that.$store.state.alertList.unshift(that.objData.content);
  146. that.$store.commit("setalertList", that.$store.state.alertList);
  147. } else {
  148. // 使用filter和findIndex进行去重
  149. let uniqueDataList = that.$store.state.alertList.filter(
  150. (item, index, self) => {
  151. console.log("进到里面---------------");
  152. // 返回第一个匹配项的索引,用于判断当前项是否为第一个匹配项
  153. const firstIndex = self.findIndex(
  154. (obj) => obj.deviceId === item.deviceId
  155. );
  156. // 如果当前项是第一个匹配项,则保留,否则过滤掉
  157. return index === firstIndex;
  158. }
  159. );
  160. that.$store.state.alertList = "";
  161. console.log("uniqueDataList", uniqueDataList);
  162. that.$store.state.alertList = [...uniqueDataList];
  163. that.$store.commit("setalertList", that.$store.state.alertList);
  164. }
  165. console.log(
  166. "数组",
  167. that.$store.state.alertList,
  168. "$store.state.name"
  169. );
  170. }
  171. }
  172. });
  173. this.socketTask.onError(function (res) {
  174. console.log("WebSocket连接打开失败,请检查!");
  175. console.log(res);
  176. //进入重新连接
  177. setTimeout(() => {
  178. if (that.$route.path != "/") {
  179. that.reconnect();
  180. }
  181. }, 5000);
  182. });
  183. // // 监听连接关闭 -
  184. this.socketTask.onClose((e) => {
  185. console.log("WebSocket连接关闭!");
  186. clearInterval(that.timer);
  187. that.timer = "";
  188. console.log("that.isClose", this.isClose);
  189. setTimeout(() => {
  190. if (that.$route.path != "/") {
  191. that.reconnect();
  192. }
  193. }, 5000);
  194. });
  195. console.log(this.socketTask);
  196. },
  197. //进入重新连接
  198. reconnect() {
  199. console.log("进入断线重连");
  200. this.socketTask = null;
  201. setTimeout(() => {
  202. this.connectSocket();
  203. }, 5000);
  204. },
  205. //发送消息
  206. sendSocketMessage(msg) {
  207. console.log("发送信息", msg);
  208. console.log(msg);
  209. return new Promise((reslove, reject) => {
  210. this.socketTask.send({
  211. data: msg,
  212. success(res) {
  213. console.log("发送成功");
  214. reslove(res);
  215. },
  216. fail(res) {
  217. console.log("发送失败");
  218. console.log(res);
  219. reject(res);
  220. },
  221. });
  222. });
  223. },
  224. //心跳
  225. heart() {
  226. console.log("this.timer", this.timer);
  227. let that = this;
  228. clearInterval(this.timer);
  229. this.timer = "";
  230. let msg = {
  231. type: "heartbeat",
  232. };
  233. this.timer = setInterval(() => {
  234. that
  235. .sendSocketMessage(JSON.stringify(this.num))
  236. .then((res) => {
  237. console.log("心跳成功");
  238. })
  239. .catch((res) => {
  240. console.log("发送失败");
  241. console.log(res);
  242. });
  243. }, 10000);
  244. },
  245. closeSocket() {
  246. this.socketTask.close({
  247. success(res) {
  248. this.is_open_socket = false;
  249. console.log("关闭成功", res);
  250. },
  251. fail(err) {
  252. console.log("关闭失败", err);
  253. },
  254. });
  255. },
  256. },
  257. };
  258. </script>
  259. <style lang="scss"></style>