guoshilong
2023-03-22 e0f9517f4b60d253b46abb20feb15d23ebaf32bc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<template>
  <div class="base-container">
    <el-row>
      <el-col :span="4">
        <div class="equipment-select">
          <!--          <el-select v-model="equipmentSelected" @change="selectChange" placeholder="请选择">-->
          <!--            <el-option-->
          <!--                v-for="item in equipmentList"-->
          <!--                :key="item.id"-->
          <!--                :label="item.name"-->
          <!--                :value="item.id">-->
          <!--            </el-option>-->
          <!--          </el-select>-->
          <el-input v-model="equipmentCode" placeholder="请输入要连接的模块id"></el-input>
          <el-button type="primary" @click="getAllMultiMedia">连接</el-button>
        </div>
 
        <el-menu
            :default-active="defaultActive"
            class="el-menu-vertical-demo"
            @select="handleSelect"
            background-color="#545c64"
            text-color="#fff"
            ref="elMenu"
            active-text-color="#ffd04b">
          <el-menu-item v-for="data in dataList" :index="data.id" :key="data.id">
            <span slot="title">{{ data.name }}</span>
          </el-menu-item>
        </el-menu>
      </el-col>
      <el-col :span="20">
        <iframe class="view-iframe" :src="path"/>
      </el-col>
    </el-row>
  </div>
</template>
 
<script>
import {getAllMM} from "@/api/multimedia/multimedia";
import {getAllEq} from "@/api/equipment/equipment";
 
export default {
  name: "viewPage",
  data() {
    return {
      dataList: [],
      webSocket: null,
      path: "",
      equipmentCode: "",
      equipmentList: [],
      defaultActive: ""
    }
  },
  mounted() {
  },
  methods: {
    //获取所有多媒体按钮
    getAllMultiMedia() {
      getAllMM(this.equipmentCode, 1).then(res => {
        if (res.data.code == "200") {
          let data = res.data.data
          this.dataList = data
          if (data.length > 0) {
            this.initWebSocket(this.equipmentCode)
          } else {
            this.$notify.error({
              title: '未查找到该设备',
            });
          }
        }
      })
    },
 
    //获取所有设备列表
    getAllEquipment() {
      getAllEq().then(res => {
        if (res.data.code == "200") {
          let data = res.data.data
          this.equipmentList = data
        }
      })
    },
 
    //初始化webSocket
    initWebSocket(code, type = 2) {
      const that = this
      let url = "ws://192.168.0.200:88/websocket"
      // let codeArray
      // codeArray = JSON.parse(sessionStorage.getItem('codeArray'));
      // if (codeArray){
      //   if (codeArray.some(e=>{return e == code})){
      //     this.$notify.warning({
      //       title: '该设备已连接',
      //     });
      //     return
      //   }
      // }else {
      //   codeArray = []
      // }
 
      if ("WebSocket" in window) {
        this.webSocket = new WebSocket(url)
      }
 
      this.webSocket.onopen = function () {
        // codeArray.push(code)
        // sessionStorage.setItem("codeArray",JSON.stringify(codeArray))
        that.$notify.success({
          title: '连接成功',
        });
      }
 
      //连接发生错误的回调方法
      this.webSocket.onerror = function () {
        that.$notify.error({
          title: '连接失败',
        });
      }
 
      this.webSocket.onmessage = function (event) {
        let msg = event.data
        that.handleSelect(msg)
        that.defaultActive = msg
      };
 
      this.webSocket.onclose = function (event) {
        that.webSocket.close()
      }
    },
    //文件预览
    viewFile(data) {
      let url
      if (data.content) {
        url = data.content
        this.path = 'http://192.168.0.200:8012/onlinePreview?url=' + encodeURIComponent(Base64.encode(url))
      } else if (data.webUrl) {
        url = data.webUrl
        this.path = url
      } else {
        url = ""
      }
    },
 
    //点击菜单时的事件
    handleSelect(index) {
      let selectData = this.dataList.filter(e => {
        return e.id == index
      })
      this.viewFile(selectData[0])
    },
 
    //选择框改变事件
    // selectChange(data){
    //   this.path = ""
    //   this.getAllMultiMedia(data)
    //   this.initWebSocket(data)
    // }
  }
}
</script>
 
<style scoped>
.base-container {
  height: 100%;
}
 
.equipment-select {
  display: flex;
  margin: 10px;
}
 
.el-select {
  width: 100%
}
 
.el-row {
  height: 100%;
}
 
.el-col {
  height: 100%;
}
 
.view-iframe {
  width: 100%;
  height: 100%;
}
 
 
</style>