liuxiaolong
2019-05-06 19e47fa0e48ac76a951bbfaa0a3e95211567f5a1
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<template>
  <div>
    <h4 class="font-weight-bold py-3 mb-0">
      <router-link to="/mapdevice">
        <span class="text-muted font-weight-light">设备地图管理 /</span>
      </router-link>
      设备地图{{!isEdit?'查看':'编辑'}}
    </h4>
    <b-card no-body class="pl20 pr20 pt20 pb20">
      <div class="row">
        <div :class="!isEdit?'col-12':'col-sm-6 col-md-8 mb-2'">
          <!-- 地图 start -->
          <b-card no-body style="height: 70vh;" >
            <svgDIYMapModelEdit
              :isIconEdit="isEdit"
              :mapInfo="mapInfo"
              :iconArr="deviceArr"
              @dragMapLayerFn="({x,y})=>{mapLayerX=x;mapLayerY=y}"
              @zoomMapLayerFn="(scale)=>{mapLayerScale=scale}"
              @iconIteamDragFn="iconIteamDrag"
              @iconIteamDelFn="iconIteamDel"
            />
          </b-card>
          <!-- 地图 start -->
        </div>
        <!-- 列表 start -->
        <div v-if="isEdit" class="col-sm-6 col-md-4 mb-2 border-right-0 pr">
          <div class="mr20 pr10 pl10 pt10 pb10 border" style="height: 70vh;">
            <b-tabs pills >
              <b-tab title="未描点设备" :active="listType==='未描点'" @click="listType='未描点';_initDeviceList()"/>
              <b-tab title="已描点设备" :active="listType==='已描点'" @click="listType='已描点';_initDeviceList()"/>
            </b-tabs>
            <hr class="mb-0 mt-2"/>
            <div class="pt10 pb10">
              <!-- 搜索开始 -->
              <b-input-group>
                <b-input-group-prepend>
                  <b-form-select v-model="form.deviceType" text-field="lable" required :options="newDeviceTypeDicts"/>
                </b-input-group-prepend>
                <b-input type="text" v-model="form.deviceName" @keyup.native.enter="_initDeviceList()" autocomplete="off"/>
                <b-input-group-append>
                  <b-btn variant="primary" @click="_initDeviceList()">搜索</b-btn>
                </b-input-group-append>
              </b-input-group>
              <!-- 搜索结束 -->
              <!-- 列表内容 start -->
              <div style="height: 58vh;" class="pt10">
                <perfect-scrollbar :options="{ suppressScrollX: true, wheelPropagation: true }" style="height: 100%;">
                  <div class="pr">
                    <el-collapse v-model="activeCollapse" class="pl5 pr5" style="border-top:0">
                      <el-collapse-item
                        :title="iteams.deviceTypeName"
                        :name="iteams.deviceType"
                        v-for="(iteams,index) in newDeviceList"
                        :key="index">
                          <div class="kanban-box pt-2" >
                            <dragger-box
                              :data="item"
                              toDOMId="drawMap"
                              :tags="{deviceTypeName:iteams.deviceTypeName,deviceType:iteams.deviceType}"
                              @_getContainerRect="addMapIconIteam"
                              v-for="item in iteams.dataList"
                              :key="item.id">
                              <div class="ui-bordered p-2 mb-2 dragger-li flex-box cursor-move">
                                <div class="img-icon mr10" :style="`background-image:url(${item.icon?'/static/img/map/'+item.icon+'.png':''})`"></div>
                                <div class="dragger-content">
                                  {{item.deviceName}}
                                </div>
                              </div>
                            </dragger-box>
                          </div>
                      </el-collapse-item>
                    </el-collapse>
                    <div v-show="!newDeviceList.length" class="text-center pt-4 pb-4 text-muted font-weight-light">
                      暂无数据
                    </div>
                  </div>
                  <!-- <div v-if="listType!=='未描点'">
                    <el-collapse v-model="activeCollapse" class="pl5 pr5" style="border-top:0">
                      <el-collapse-item :title="iteams.deviceTypeName" :name="iteams.deviceType" v-for="(iteams,index) in deviceList" :key="index">
                          <div
                            class="kanban-box pt-2"
                            v-for="item in iteams.dataList"
                            :key="item.id">
                              <div class="ui-bordered p-2 mb-2 dragger-li flex-box">
                                <div class="img-icon mr10">
                                  <img :src="'/static/img/map/'+item.icon+'.png'" width="30px" height="30px" :onerror="noneImg" :alt="item.icon" @mousedown.prevent>
                                </div>
                                <div class="dragger-content">
                                  {{item.deviceName}}
                                </div>
                              </div>
                          </div>
                      </el-collapse-item>
                    </el-collapse>
                    <div v-show="!deviceList.length" class="text-center pt-4 pb-4 text-muted font-weight-light">
                        暂无数据
                    </div>
                  </div> -->
                </perfect-scrollbar>
              </div>
              <!-- 列表内容 end -->
            </div>
          </div>
          <!-- 竖排页签 start -->
          <div class="verticalTabs">
             <perfect-scrollbar :options="{ suppressScrollX: true, wheelPropagation: true }" style="height: 100%;">
              <ul class="verticalTabs">
                <li
                  @click="form.authorizationSystem = iteam.value;_initDeviceList()"
                  :class="form.authorizationSystem === iteam.value?'active':''"
                  v-for="iteam in newSysDicts"
                  :key="iteam.id">
                  <a href="javascript:;">{{iteam.lable}}</a>
                </li>
              </ul>
            </perfect-scrollbar>
          </div>
          <!-- 竖排页签 end -->
        </div>
        <!-- 列表 end -->
      </div>
      <div class="text-right mt-1">
        <ladda-btn :loading="saveLoading" v-if="isEdit" @click.native="saveDevice" data-style="slide-down" class="btn btn-primary">保存</ladda-btn>
        &nbsp;
        <b-btn variant="default" @click="$router.push({path:'/mapdevice'})">返回</b-btn>
      </div>
    </b-card>
  </div>
</template>
 
<script>
import LaddaBtn from '@/vendor/libs/ladda/Ladda'
import { getGloDicts, getSysListWithHome } from '@/server/common.js'
import {
  getMapInfo,
  getNoDrawPointList,
  getDrawPointList,
  saveMapDevice,
  getMapDeviceInfo
} from '@/server/mapDeviceManagement.js'
import svgDIYMapModelEdit from '@/components/map/svgDIYMapModelEdit'
import { Collapse, CollapseItem } from 'element-ui'
import PerfectScrollbar from '@/vendor/libs/perfect-scrollbar/PerfectScrollbar'
import draggerBox from './components/draggerBox'
import { findInArrJson } from '@/components/common/util.js'
export default {
  name: 'mapDeviceAdd',
  metaInfo: {
    title: '设备地图添加'
  },
  data() {
    return {
      noneImg: 'this.src="' + require('@/assets/img/noneImg.png') + '"',
      userInfo: this.$store.getters.basicUserInfo,
      form: {
        deviceType: '',
        authorizationSystem: '',
        deviceName: ''
      },
      deviceTypeDicts: [],
      sysDicts: [],
 
      listType: '未描点', // '已描点'
      deviceList: [],
      activeCollapse: '',
 
      /* 地图组件使用 */
      mapInfo: null,
      deviceArr: [],
      mapLayerX: 0,
      mapLayerY: 0,
      mapLayerScale: 1,
 
      saveLoading: false,
      isEdit: false
    }
  },
  computed: {
    newSysDicts() {
      return [{ value: '', lable: '全部' }, ...this.sysDicts]
    },
    newDeviceTypeDicts() {
      return [{ value: '', lable: '全部' }, ...this.deviceTypeDicts]
    },
    newDeviceList() {
      if (!this.deviceArr.length) {
        return this.deviceList
      }
      let list = []
      for (let iteam of this.deviceList) {
        if (iteam.dataList.length) {
          let arr = []
          for (let val of iteam.dataList) {
            /* 是否在地图数组中 */
            if (val.id && !this.isMapHas(val.id)) {
              arr.push(val)
            }
          }
          if (arr.length) {
            let json = {}
            json.deviceType = iteam.deviceType
            json.deviceTypeName = iteam.deviceTypeName
            json.dataList = arr
            list.push(json)
          }
        }
      }
      return list
    }
  },
  methods: {
    /* 查询系统字典 */
    async getSysDicts() {
      let res = await getSysListWithHome({
        orgId: this.userInfo.orgId,
        userId: this.userInfo.id,
        tags: 'device'
      })
      if (res && !res.error) {
        this.sysDicts = res
      }
    },
    /* 查询设备字典 */
    async getDeviceTypeDicts() {
      let res = await getGloDicts({
        type: 'DEVICE_TYPE'
      })
      if (res && res.data) {
        this.deviceTypeDicts = res.data
      }
    },
    /* 查询地图详情 */
    async getMapInfo(id) {
      let res = await getMapInfo({
        orgId: this.userInfo.orgId,
        id
      })
      if (res && !res.error) {
        this.mapInfo = res
        /* 回显地图 */
        this.getMapDeviceInfo(this.$route.query.id)
      } else {
        this.$toast({
          type: 'error',
          message: '地图加载失败:' + res.message ? res.message : ''
        })
      }
    },
    /* 获取未描点设备 */
    async getNoDrawPointList() {
      let res = await getNoDrawPointList({
        orgId: this.userInfo.orgId,
        ...this.form
      })
      if (res && !res.error) {
        this.deviceList = res
      }
    },
    /* 获取已描点设备 */
    async getDrawPointList() {
      let res = await getDrawPointList({
        orgId: this.userInfo.orgId,
        ...this.form
      })
      if (res && !res.error) {
        this.deviceList = res
      }
    },
 
    /* 获取设备信息 */
    async getMapDeviceInfo(id) {
      let res = await getMapDeviceInfo({
        orgId: this.userInfo.orgId,
        mapId: id
      })
      if (res) {
        this.deviceArr = res
      }
    },
    async saveMapDevice(json) {
      let res = await saveMapDevice(json)
      if (res && !res.error) {
        this.saveLoading = false
        this.$toast({
          type: 'success',
          message: '保存成功'
        })
        this.$router.push({ path: '/mapdevice' })
      }
    },
 
    /* 初始化列表 */
    _initDeviceList() {
      if (this.listType === '未描点') {
        this.getNoDrawPointList()
      }
      if (this.listType === '已描点') {
        this.getDrawPointList()
      }
    },
    saveDevice(evt) {
      evt.preventDefault()
      /* 校验拦截 start */
      if (!this.$route.query.id) {
        this.$notify({
          group: 'foo',
          type: 'error',
          title: '抱歉,缺少重要参数,无法保存',
          text: '地图id是重要参数'
        })
        return false
      }
      if (this.deviceArr.length === 0) {
        this.$notify({
          group: 'foo',
          type: 'error',
          title: '抱歉,设备地图为空,无法保存',
          text: '必须在地图中添加设备,才能保存'
        })
        return false
      }
      /* 处理保存数据 */
      let detailList = this.deviceArr.map(iteam => {
        let icon = {}
        icon.deviceId = iteam.id
        icon.position = iteam.position
        return icon
      })
      let json = {
        orgId: this.userInfo.orgId,
        id: this.$route.query.id,
        detailList
      }
      this.saveLoading = true
      this.saveMapDevice(json)
    },
 
    /* 地图操作 start */
    isMapHas(id) {
      for (let iteam of this.deviceArr) {
        if (iteam.id === id) {
          return true
        }
      }
      return false
    },
    // 地图 添加设备
    addMapIconIteam({ x, y, data, index, tags }) {
      /* 默认返回的x,y为画板坐标 */
      let json = {}
      // 坐标转换
      let deviceX = (x - this.mapLayerX) / this.mapLayerScale
      let deviceY = (y - this.mapLayerY) / this.mapLayerScale
      json = data
      json.position = JSON.stringify({ x: deviceX, y: deviceY })
      json.icon = data.icon
      json.id = data.id
 
      if (findInArrJson(data.id, this.deviceArr, 'id')) {
        this.deviceArr = [...this.deviceArr, json]
      }
    },
    // 地图 设备移动
    iconIteamDrag({ x, y, data }) {
      let deviceArr = this.deviceArr.map(iteam => {
        if (iteam.id === data.id) {
          iteam.position = JSON.stringify({ x, y })
        }
        return iteam
      })
      this.deviceArr = deviceArr
    },
    // 地图 设备删除
    iconIteamDel({ id, data }) {
      this.$swal(
        {
          title: '确定删除吗?',
          type: 'warning',
          showCancelButton: true,
          allowOutsideClick: true,
          confirmButtonText: '确定删除!',
          cancelButtonText: '取消删除!',
          closeOnConfirm: true
        },
        () => {
          /* 删除函数 start */
          let deviceArr = []
          this.deviceArr.map(iteam => {
            if (iteam.id !== id) {
              deviceArr.push(iteam)
            }
          })
          this.deviceArr = deviceArr
          /* 删除函数 end */
          setTimeout(() => {
            this.$toast({
              type: 'success',
              message: '删除成功'
            })
          }, 500)
        }
      )
    }
    /* 地图操作 end */
  },
  created() {
    /* 查询字典 */
    this.getSysDicts()
    this.getDeviceTypeDicts()
    /* 查询描点设备 */
    // this._initDeviceList()  不知哪里已经触发一次
    /* 初始化 */
    if (this.$route.query.id && this.$route.query.type === 'edit') {
      this.isEdit = true
    }
    if (this.$route.query.id && this.$route.query.id !== '') {
      this.getMapInfo(this.$route.query.id)
    }
  },
  mounted() {
    /* 强制管理侧边栏 */
    this.layoutHelpers.setCollapsed(true, false)
  },
  components: {
    svgDIYMapModelEdit,
    LaddaBtn,
    draggerBox,
    elCollapse: Collapse,
    elCollapseItem: CollapseItem,
    PerfectScrollbar
  }
}
</script>
<style lang="scss">
.verticalTabs {
  position: absolute;
  height: 100%;
  width: 30px;
  right: 3px;
  top: 0;
}
.verticalTabs ul,
.verticalTabs ul li {
  list-style: none;
  padding: 0;
  margin: 0;
  a {
    display: inline-block;
    font-size: 14px;
    line-height: 16px;
    padding: 10px 5px;
    width: 30px;
    border: 1px solid #f1f2f2;
    border-radius: 0 5px 5px 0;
    overflow: hidden;
    text-align: center;
    transition-delay: all 1s;
 
    background: rgba(24, 28, 33, 0.06);
    color: #a3a4a6;
  }
}
.verticalTabs ul li.active {
  a {
    background: #fff;
    color: #4e5155;
    border-left: 0;
  }
}
.dragger-li {
  min-width: 150px;
  background: #fff;
  /* 抑制选中 */
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
  .img-icon {
    width: 30px;
    height: 30px;
    background-image: url('/static/assets/img/noneImg.png');
    background-size: cover;
 
    /* 抑制选中 */
    -moz-user-select: none;
    -webkit-user-select: none;
    user-select: none;
  }
  .dragger-content {
    line-height: 30px;
    /* 抑制选中 */
    -moz-user-select: none;
    -webkit-user-select: none;
    user-select: none;
  }
}
.cursor-move {
  cursor: move;
}
</style>