liuxiaolong
2019-05-06 2418a2d61feea858e9e63aa52fd64ddfe17e392a
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
<template>
  <div>
    <h5 class="pt-2 table-title">
      设备列表
      <small class="text-primary pl-2">( {{total}} )</small>
    </h5>
    <div class="pb30">
      <Table :data="newtableList" :border="true" :fit="true" ref="leftTable">
        <TableColumn type="index" prop="index" label="序号" :index="snMethod" width="50"/>
        <TableColumn label="设备类型" prop="typeName"/>
        <TableColumn label="设备名称" prop="name"/>
        <!-- <TableColumn label="设备编码" prop="code"/> -->
        <TableColumn label="设备位置" prop="address"/>
        <TableColumn label="经纬度">
          <template slot-scope="scope">
            <span>{{scope.row|lngAndLat}}</span>
          </template>
        </TableColumn>
        <TableColumn label="IP地址" prop="ip"/>
        <TableColumn label="端口" prop="port"/>
        <TableColumn label="操作" width="180">
          <template slot-scope="scope">
            <div @click.stop>
              <fButton
                type="link"
                style="padding:2px;"
                authority="device:localList:update"
                @click.native.self="$emit('add-device', { data:scope.row, type: 'edit' })"
              >编辑</fButton>
              <fButton
                type="link"
                style="padding:2px;"
                @click.native="handleDel(scope.row.id)"
                authority="device:localList:delete"
              >删除</fButton>
              <!-- <fButton
                size="sm"
                type="link"
                @click.native="handlEdeitOrg(scope.row)"
                authority="device:localList:editOrg"
                >
                  编辑组织机构
              </fButton>-->
            </div>
          </template>
        </TableColumn>
      </Table>
      <div class="pt20 pb20">
        <b-pagination
          class="justify-content-center justify-content-sm-end m-0"
          v-if="total"
          v-model="activePage"
          :total-rows="total"
          :per-page="pageSize"
        />
      </div>
    </div>
    <!-- <editOrg
      ref="editOrg"
      title="配置机构"
      :isShowFooter="false"
    />-->
  </div>
</template>
<script>
import { Table, TableColumn, Collapse, CollapseItem } from 'element-ui'
import {
  getLocalDeviceList,
  getDeviceType,
  getDeviceBrand,
  delDevice
} from '@/server/home.js'
import editOrg from '../components/editOrg'
export default {
  name: 'table-local',
  metaInfo: {
    title: '平台设备'
  },
  props: {
    orgId: { default: '0', type: String },
    searchName: {
      default: '',
      type: String
    },
    currentOrgItem: {
      default: () => ({ id: '0', type: 'MENU', name: '平台设备' }),
      type: Object
    }
  },
  components: {
    Table,
    TableColumn,
    Collapse,
    CollapseItem,
    editOrg
  },
  data() {
    return {
      deviceTypeList: [],
      deviceBrandList: [],
      tableList: [],
      total: 0,
      pageSize: 10,
      activePage: 1
    }
  },
  computed: {
    newtableList() {
      let list = this.tableList.map(item => {
        const json = { ...item }
        this.deviceTypeList.map(iteam => {
          if (iteam.value === item.type) {
            json.typeName = iteam.name
          }
        })
        return json
      })
      return list
    }
  },
  filters: {
    lngAndLat(value) {
      if (!value) return ''
      if (value.longitude && value.latitude) {
        return `${value.longitude} , ${value.latitude}`
      }
      return ''
    }
  },
  methods: {
    /* 设备列表初始化 */
    async getDeviceType() {
      const res = await getDeviceType()
      this.deviceTypeList =
        res.success && res.code === 200 && res.data ? res.data : []
    },
    async getDeviceBrand() {
      const res = await getDeviceBrand()
      this.deviceBrandList =
        res.success && res.code === 200 && res.data ? res.data : []
    },
    async _initData() {
      let res = await getLocalDeviceList({
        orgId: this.currentOrgItem.id,
        type: this.currentOrgItem.type,
        length: this.pageSize,
        start: this.pageSize * (this.activePage - 1),
        condition: this.searchName
      })
      if (res && res.data) {
        this.tableList = res.data
        this.total = res.total
      } else {
        this.$toast({
          type: 'error',
          message: res && res.msg ? res.msg : '列表查询失败!'
        })
      }
    },
    async delDevice(id) {
      const res = await delDevice({ id })
      if (res.success && res.code === 200) {
        /* 成功操作 */
        this._initData()
        this.$emit('del-device-success')
        this.$toast({
          type: 'success',
          message: res && res.msg ? res.msg : '删除设备成功'
        })
      } else {
        this.$toast({
          type: 'error',
          message: res && res.msg ? res.msg : '删除设备失败!'
        })
      }
      console.log(res, 'delDevice---res')
    },
    snMethod(index) {
      return this.activePage > 1
        ? this.pageSize * (this.activePage - 1) + index + 1
        : index + 1
    },
    handleDel(id) {
      this.$swal(
        {
          title: '确定删除?',
          text: '你将要删除当前设备!',
          type: 'warning',
          showCancelButton: true,
          allowOutsideClick: true,
          // confirmButtonColor: '#d9534f',
          confirmButtonText: '确定删除!',
          cancelButtonText: '取消',
          closeOnConfirm: true
        },
        () => {
          this.delDevice(id)
        }
      )
    },
    handlEdeitOrg(data) {
      this.$refs.editOrg.showModel(data)
    }
  },
  created() {
    // 查询设备类型
    this.getDeviceType()
    this.getDeviceBrand()
    // this._initData()
  },
  watch: {
    activePage(val, oldVal) {
      if (val !== oldVal) {
        this.activePage = val
        this._initData()
      }
    },
    currentOrgItem: {
      handler(newVal, oldVal) {
        if (newVal !== oldVal) {
          // 触发 org 查询平台设备列表
          this._initData()
        }
      },
      deep: true,
      immediate: true
    }
    // orgId: {
    //   handler(newVal, oldVal) {
    //     if (newVal !== oldVal) {
    //       // 触发 org 查询平台设备列表
    //       this._initData()
    //     }
    //   },
    //   deep: true,
    //   immediate: true
    // }
  }
}
</script>
<style lang="scss">
.table-title {
  font-size: 16px;
  font-weight: 600;
}
</style>