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
<template>
  <div>
    <VueDragResize
      v-for="(item, index) in deviceList"
      :key="item.equipmentId"
      :w="item.vw"
      :h="item.vh"
      :x="item.coordinateLeft"
      :y="item.coordinateTop"
      v-on:resizing="resize($event, index)"
      v-on:dragging="resize($event, index)"
      :parentLimitation="true"
      :parentH="1065"
      :parentW="1500"
      :minw="70"
      :minh="70"
      :isDraggable="isSwitchChecked"
      :isResizable="isSwitchChecked"
      :stickSize="6"
      @deactivated="isShowGuideline = false"
    >
      <div class="single-device" :style="{ width: item.vw + 'px', height: item.vh + 'px' }">
        <div class="device-status">
          <div
            :style="{ backgroundImage: `url(${getImgView(item.equipmentImage) || require('@/assets/WorskhopSignage/default.png')})` }"
            class="device-image"
          >
            <div
              :style="{ backgroundImage: `url(${getCurrentDeviceStatusImage(item.oporation)})` }"
              class="status-image"
            ></div>
          </div>
        </div>
        <div :style="{backgroundColor:item.equipmentStatus!==0?'#f00':''}">{{ item.equipmentId }}</div>
      </div>
    </VueDragResize>
 
    <template v-if="isShowGuideline">
      <div class="guideline guidelineX" :style="{top:guidelineXTop+'px'}"></div>
      <div class="guideline guidelineY" :style="{left:guidelineYLeft+'px'}"></div>
    </template>
 
    <a-form layout="inline" v-has="'isCanDragAndResize'">
      <a-form-item label="功能开关">
        <a-switch checked-children="开" un-checked-children="关" v-model="isSwitchChecked" @change="handleSwitchChange"
                  :disabled="isHasResizeOrDragDevice"/>
      </a-form-item>
 
      <a-form-item label="保存坐标">
        <a-button type="primary" icon="save" @click="saveDevicePositionAndSizeByApi"
                  :disabled="!isHasResizeOrDragDevice">保存
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
 
<script>
  import signageApi from '@/api/signage'
  import VueDragResize from 'vue-drag-resize'
 
  export default {
    name: 'DeviceDragLayout',
    components: {
      VueDragResize
    },
    props: {
      currentProductionId: {
        type: String
      },
      getImgView: {
        type: Function
      },
      equipmentStatusList: {
        type: Array
      }
    },
    data() {
      return {
        deviceList: [],
        isShowGuideline: false,
        guidelineXTop: 0,
        guidelineYLeft: 0,
        isSwitchChecked: false,
        isHasResizeOrDragDevice: false
      }
    },
    created() {
      if (!this.currentProductionId) return
      this.getDeviceListByApi()
    },
    methods: {
      // 通过车间Id调用接口获取设备信息列表
      getDeviceListByApi() {
        const that = this
        signageApi.getDeviceListInWorkshopSignagePageApi(that.currentProductionId)
          .then((res) => {
            if (res.success && res.result && res.result.length > 0) that.deviceList = res.result
          })
      },
 
      handleSwitchChange(checked) {
        console.log('checked--------', checked)
        this.$emit('handleTimeIntervalForShortOpen', checked)
      },
 
      /**
       * 设备拖拽或缩放时触发事件
       * @param newRect 拖拽或缩放后的尺寸及距离
       * @param index 拖拽设备在数组中的下标
       */
      resize(newRect, index) {
        this.isShowGuideline = true
        // 当设备缩放或拖拽后禁用switch组件并开放保存按钮功能(避免无效请求)
        if (!this.isHasResizeOrDragDevice) {
          if (this.deviceList[index].vw !== newRect.width || this.deviceList[index].vh !== newRect.height || this.deviceList[index].coordinateTop !== newRect.top || this.deviceList[index].coordinateLeft !== newRect.left) {
            this.isHasResizeOrDragDevice = true
          }
        }
        // this.isHasResizeOrDragDevice = true
        this.deviceList[index].vw = newRect.width
        this.deviceList[index].vh = newRect.height
        this.deviceList[index].coordinateTop = newRect.top
        this.deviceList[index].coordinateLeft = newRect.left
        this.guidelineXTop = newRect.top + newRect.height / 2
        this.guidelineYLeft = newRect.left + newRect.width / 2
      },
 
      // 点击保存按钮调用接口保存拖拽后的位置与设备图标尺寸
      saveDevicePositionAndSizeByApi() {
        const that = this
        signageApi.saveDevicePositionAndSizeApi(that.deviceList)
          .then((res) => {
            if (!res.success) return
            that.$notification.success({
              message: '消息',
              description: res.message
            })
            that.isSwitchChecked = that.isHasResizeOrDragDevice = false
            that.getDeviceListByApi(that.currentProductionId)
          })
      },
 
      /**
       * 获取当前设备状态图片
       * @param oporation 状态码
       * @returns {any} 设备状态图片资源
       */
      getCurrentDeviceStatusImage(oporation) {
        const currentStatus = this.equipmentStatusList.find(item => item.value.includes(oporation))
        return currentStatus.statusImage
      }
    }
  }
</script>
 
<style scoped lang="less">
  .single-device {
    position: absolute;
    border: 1px solid transparent;
    padding: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    cursor: default;
 
    &:active {
      border: 1px solid #1890ff;
    }
 
    .device-status {
      width: 100%;
      height: 100%;
      display: flex;
      -webkit-align-items: flex-end;
      -moz-align-items: flex-end;
      -ms-align-items: flex-end;
 
      .status-image {
        background-size: 100% 100%;
        background-repeat: no-repeat;
        width: 10px;
        height: 80%;
        margin-right: 5px;
      }
 
      .device-image {
        background-size: 100% 100%;
        background-repeat: no-repeat;
        width: 100%;
        height: 100%;
      }
    }
  }
 
  .guideline {
    position: absolute;
    border: 1px dashed #ccc;
  }
 
  .guidelineX {
    width: 9999px;
    left: 0;
  }
 
  .guidelineY {
    top: 0;
    height: 9999px;
  }
</style>