App.vue 3.81 KB
Newer Older
Pan's avatar
Pan committed
1 2
<template>
  <div id="app">
yanzhongrong's avatar
yanzhongrong committed
3
    <router-view />
yanzhongrong's avatar
yanzhongrong committed
4 5 6 7 8 9
    <audio
      ref="audio"
      src="./assets/audio/urgent.wav"
      muted="true"
      loop="false"
    />
neogcg's avatar
neogcg committed
10
    <transition name="tipalerm">
yanzhongrong's avatar
yanzhongrong committed
11
      <div v-if="tflag" class="tips">
neogcg's avatar
neogcg committed
12
        <p>提示</p>
yanzhongrong's avatar
yanzhongrong committed
13 14
        <span
          >系统新增{{ msg.count }}
yanzhongrong's avatar
yanzhongrong committed
15
          <span v-text="msg.type == 1 ? '设备连接' : '漏缆监测'" />
yanzhongrong's avatar
yanzhongrong committed
16 17
          <span id="alevel" :style="alevel">{{ msg.level }}</span
          >告警消息,请及时查看!
yanzhongrong's avatar
yanzhongrong committed
18
        </span>
neogcg's avatar
neogcg committed
19 20
      </div>
    </transition>
Pan's avatar
Pan committed
21 22 23 24
  </div>
</template>

<script>
dupengyu's avatar
dupengyu committed
25 26 27 28
import { receiveMessage } from "@/utils/websocket";
import socket from "@/utils/websocket";
import { mapState } from "vuex";
import EventBus from "@/utils/bus";
Pan's avatar
Pan committed
29
export default {
dupengyu's avatar
dupengyu committed
30
  name: "App",
yanzhongrong's avatar
yanzhongrong committed
31
  data() {
neogcg's avatar
neogcg committed
32
    return {
yanzhongrong's avatar
yanzhongrong committed
33 34
      websocket: null,
      tflag: false,
dupengyu's avatar
dupengyu committed
35
      msg: "",
neogcg's avatar
neogcg committed
36
      alevel: {
dupengyu's avatar
dupengyu committed
37
        color: "",
neogcg's avatar
neogcg committed
38
      },
dupengyu's avatar
dupengyu committed
39
      audioUrl: "/assets/audio/urgent.wav",
yanzhongrong's avatar
yanzhongrong committed
40
      audioObj: {
dupengyu's avatar
dupengyu committed
41 42 43 44 45 46
        一般: "commonly.wav",
        重要: "importang.wav",
        紧急: "urgent.wav",
        连接异常: "urgent.wav",
      },
    };
yanzhongrong's avatar
yanzhongrong committed
47 48
  },
  computed: {
dupengyu's avatar
dupengyu committed
49 50
    ...mapState("user", ["userBaseInfo", "token"]),
    ...mapState("alarm", ["cableList", "deviceList"]),
neogcg's avatar
neogcg committed
51
    usertoken() {
dupengyu's avatar
dupengyu committed
52 53
      return this.token;
    },
yanzhongrong's avatar
yanzhongrong committed
54 55
  },
  watch: {
neogcg's avatar
neogcg committed
56
    $route(to, from) {
dupengyu's avatar
dupengyu committed
57
      if (to.path === "/login") {
yanzhongrong's avatar
yanzhongrong committed
58
        setTimeout(this.closeWebsocket(), 300);
neogcg's avatar
neogcg committed
59 60
      }
    },
neogcg's avatar
neogcg committed
61
    usertoken: {
yanzhongrong's avatar
yanzhongrong committed
62 63 64
      immediate: true,
      handler(newV) {
        if (newV) {
dupengyu's avatar
dupengyu committed
65
          if (newV !== "") {
yanzhongrong's avatar
yanzhongrong committed
66
            this.websocket = socket({ onmessage: this.receiveMessage });
yanzhongrong's avatar
yanzhongrong committed
67
          } else {
yanzhongrong's avatar
yanzhongrong committed
68
            this.closeWebsocket();
neogcg's avatar
neogcg committed
69
          }
yanzhongrong's avatar
yanzhongrong committed
70
        }
dupengyu's avatar
dupengyu committed
71
      },
yanzhongrong's avatar
yanzhongrong committed
72 73 74 75 76
    },
    cableList: {
      handler(newV) {
        if (newV.length || this.deviceList.length) {
          setTimeout(() => {
yanzhongrong's avatar
yanzhongrong committed
77 78
            this.$refs.audio.play();
            this.$refs.audio.loop = true;
dupengyu's avatar
dupengyu committed
79
          }, 500);
yanzhongrong's avatar
yanzhongrong committed
80
        } else {
yanzhongrong's avatar
yanzhongrong committed
81
          this.$refs.audio.loop = false;
yanzhongrong's avatar
yanzhongrong committed
82
        }
dupengyu's avatar
dupengyu committed
83 84
      },
    },
yanzhongrong's avatar
yanzhongrong committed
85 86
  },
  mounted() {
yanzhongrong's avatar
yanzhongrong committed
87
    document.body.addEventListener(
dupengyu's avatar
dupengyu committed
88
      "mousedown",
yanzhongrong's avatar
yanzhongrong committed
89
      () => {
yanzhongrong's avatar
yanzhongrong committed
90
        this.$refs.audio.muted = false;
dupengyu's avatar
dupengyu committed
91
        document.body.removeEventListener("mousedown", () => {});
yanzhongrong's avatar
yanzhongrong committed
92 93
      },
      false
dupengyu's avatar
dupengyu committed
94 95
    );
    EventBus.$on("dialogAlarm", (data) => {
yanzhongrong's avatar
yanzhongrong committed
96
      // 接收消息之后 声音弹窗显示
yanzhongrong's avatar
yanzhongrong committed
97 98 99
      this.msg = data;
      this.levelcolor();
      this.tflag = true;
yanzhongrong's avatar
yanzhongrong committed
100
      setTimeout(() => {
yanzhongrong's avatar
yanzhongrong committed
101
        this.tflag = false;
dupengyu's avatar
dupengyu committed
102 103 104 105
      }, 3000);
    });
    EventBus.$on("autioPlay", (data) => {
      this.audioUrl = "./assets/audio/" + this.audioObj[data.level];
yanzhongrong's avatar
yanzhongrong committed
106
      setTimeout(() => {
yanzhongrong's avatar
yanzhongrong committed
107
        this.$refs.audio.play();
dupengyu's avatar
dupengyu committed
108
      }, 500);
yanzhongrong's avatar
yanzhongrong committed
109
      if (this.cableList.length || this.deviceList.length) {
yanzhongrong's avatar
yanzhongrong committed
110
        this.$refs.audio.loop = true;
yanzhongrong's avatar
yanzhongrong committed
111
      }
dupengyu's avatar
dupengyu committed
112 113
    });
    EventBus.$on("cancelWS", () => {
yanzhongrong's avatar
yanzhongrong committed
114
      this.closeWebsocket();
dupengyu's avatar
dupengyu committed
115
    });
yanzhongrong's avatar
yanzhongrong committed
116
  },
yanzhongrong's avatar
yanzhongrong committed
117
  destroyed() {
dupengyu's avatar
dupengyu committed
118
    document.body.removeEventListener("mousedown", () => {});
yanzhongrong's avatar
yanzhongrong committed
119
  },
yanzhongrong's avatar
yanzhongrong committed
120 121 122 123
  methods: {
    receiveMessage,
    closeWebsocket() {
      if (this.websocket) {
dupengyu's avatar
dupengyu committed
124
        this.websocket.close();
yanzhongrong's avatar
yanzhongrong committed
125 126 127
      }
    },
    levelcolor() {
dupengyu's avatar
dupengyu committed
128 129 130 131 132 133 134 135
      if (this.msg.level === "正常" || this.msg.level === "连接正常") {
        this.alevel.color = "green";
      } else if (this.msg.level === "紧急" || this.msg.level === "连接异常") {
        this.alevel.color = "#f00";
      } else if (this.msg.level === "重要") {
        this.alevel.color = "#f89850";
      } else if (this.msg.level === "一般") {
        this.alevel.color = "#ead906";
yanzhongrong's avatar
yanzhongrong committed
136
      }
dupengyu's avatar
dupengyu committed
137 138 139
    },
  },
};
Pan's avatar
Pan committed
140
</script>
许倩倩's avatar
许倩倩 committed
141
<style lang="scss" scoped>
neogcg's avatar
neogcg committed
142 143 144 145 146 147
.tipalerm-enter-active {
  transition: 0.5s;
}
.tipalerm-enter {
  transform: translateX(200px);
}
neogcg's avatar
neogcg committed
148
.tips {
neogcg's avatar
neogcg committed
149
  width: 160px;
neogcg's avatar
neogcg committed
150 151 152
  height: 120px;
  font-size: 14px;
  letter-spacing: 1px;
yanzhongrong's avatar
yanzhongrong committed
153 154 155
  background: #fff;
  box-shadow: 0px 2px 14px #888888;
  border-radius: 5px;
neogcg's avatar
neogcg committed
156 157
  padding: 4px 16px 16px 16px;
  position: fixed;
yanzhongrong's avatar
yanzhongrong committed
158
  text-align: center;
neogcg's avatar
neogcg committed
159 160
  bottom: 0px;
  right: 0px;
yanzhongrong's avatar
yanzhongrong committed
161
}
许倩倩's avatar
许倩倩 committed
162
</style>