| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- <template>
- <schema-table
- ref="table"
- row-key="id"
- :schema="schema"
- @row-click="onRowClick"
- >
- <confirm-dialog
- ref="editDialog"
- :title="dialogTitle"
- @confirm="onSave"
- >
- <div class="c-grid-form u-align-self--center">
- <span class="c-grid-form__label u-required">名称</span>
- <el-input
- v-model.trim="currObj.name"
- placeholder="最多50个字符"
- maxlength="50"
- clearable
- />
- <span class="c-grid-form__label u-required">配置</span>
- <schema-select
- v-model="currObj.productId"
- placeholder="请选择配置"
- :schema="productSelectSchema"
- :disabled="isSub"
- />
- <span class="c-grid-form__label u-required">序列号</span>
- <el-input
- v-model.trim="currObj.serialNumber"
- placeholder="最多50个字符"
- maxlength="50"
- clearable
- />
- <span class="c-grid-form__label u-required">MAC</span>
- <el-input
- v-model.trim="currObj.mac"
- placeholder="ff:ff:ff:ff:ff:ff"
- maxlength="17"
- clearable
- />
- <span class="c-grid-form__label u-required">地址</span>
- <el-input
- v-model.trim="currObj.address"
- placeholder="最多100个字符"
- maxlength="100"
- clearable
- />
- <span class="c-grid-form__label">坐标</span>
- <div class="l-flex--row c-grid-form__option">
- <span class="c-sibling-item">经度</span>
- <el-input
- v-model.trim="currObj.longitude"
- class="l-flex__fill c-sibling-item"
- placeholder="-180 ~ +180"
- maxlength="13"
- clearable
- />
- <span class="c-sibling-item far c-grid-form__label">纬度</span>
- <el-input
- v-model.trim="currObj.latitude"
- class="l-flex__fill c-sibling-item"
- placeholder="-90 ~ +90"
- maxlength="12"
- clearable
- />
- </div>
- <span />
- <a
- class="o-link grid"
- href="http://api.map.baidu.com/lbsapi/getpoint/index.html"
- target="_blank"
- />
- </div>
- </confirm-dialog>
- </schema-table>
- </template>
- <script>
- import {
- getDevicesByAdmin,
- addDevice,
- deleteDevice,
- getSubDevices,
- addSubDevice,
- activateDevice,
- deactivateDevice,
- getProducts
- } from '@/api/device'
- import {
- validMAC,
- validLongitude,
- validLatitude
- } from '@/utils/validate'
- export default {
- name: 'Device',
- props: {
- tenant: {
- type: String,
- required: true
- }
- },
- data () {
- const productSelectSchema = {
- remote: this.getProducts,
- pagination: true,
- value: 'id',
- label: 'name'
- }
- return {
- currObj: {},
- isSub: false,
- ratio: null,
- productSelectSchema,
- schema: {
- keepalive: true,
- condition: { productId: void 0, name: '' },
- list: this.getDevicesByAdmin,
- transform: this.transform,
- transformData: __SUB_DEVICE__ && this.transformTableData,
- buttons: [
- { type: 'add', on: this.onAddDevice }
- ],
- filters: [
- { key: 'productId', type: 'select', placeholder: '全部配置', ...productSelectSchema },
- { key: 'name', type: 'search', placeholder: '设备名称' }
- ],
- cols: [
- { type: 'refresh', render: __SUB_DEVICE__
- ? (data, h) => data.isMaster
- ? h('i', {
- staticClass: `o-expand-icon u-pointer ${data.loading ? 'el-icon-loading' : 'el-icon-arrow-right'}`,
- class: { expand: data.expand }
- })
- : null
- : null },
- __SUB_DEVICE__
- ? { label: '设备名称', render: (data, h) => data.empty ? h('span', { staticClass: 'u-color--info' }, '暂无备份设备') : data.name, 'min-width': 120 }
- : { prop: 'name', label: '设备名称', 'min-width': 120 },
- { prop: 'productName', label: '配置' },
- { prop: 'serialNumber', label: '序列号', 'min-width': 140 },
- { prop: 'mac', label: 'MAC', 'min-width': 140 },
- { type: 'tag', render: ({ empty, activate, onlineStatus }) => empty
- ? null
- : activate
- ? onlineStatus === 0
- ? { type: 'primary', label: '已启用' }
- : onlineStatus === 1
- ? { type: 'success', label: '在线' }
- : { type: 'danger', label: '离线' }
- : { type: 'warning', label: '未激活' },
- on: this.onTagClick },
- { type: 'invoke', width: __SUB_DEVICE__ ? 180 : 120, render: [
- { label: '配置', render: ({ isMaster }) => isMaster, on: this.onSettingDevice },
- __SUB_DEVICE__ ? { label: '添加备份', render: ({ isMaster }) => isMaster, on: this.onAddSubDevice } : null,
- { label: '删除', render: ({ empty }) => !empty, on: this.onDelDevice }
- ].filter(Boolean) }
- ]
- }
- }
- },
- computed: {
- dialogTitle () {
- return this.isSub ? '新增备份设备' : '新增设备'
- }
- },
- watch: {
- group () {
- this.$refs.table.pageTo(1)
- }
- },
- methods: {
- getProducts (params) {
- return getProducts({
- tenant: this.tenant,
- ...params
- })
- },
- getDevicesByAdmin (params) {
- return getDevicesByAdmin({
- tenant: this.tenant,
- ...params
- })
- },
- transform (data) {
- return {
- ...data,
- loaded: false,
- loading: false,
- expand: false,
- subs: [],
- isMaster: true
- }
- },
- transformTableData (data) {
- const arr = []
- data.forEach(item => {
- arr.push(item)
- if (item.loaded && item.expand) {
- arr.push(...item.subs)
- }
- })
- return arr
- },
- onAddDevice () {
- this.isSub = false
- this.$master = null
- this.productSelectSchema.option = null
- this.onAdd()
- },
- onAddSubDevice (item) {
- this.isSub = true
- this.$master = item
- this.productSelectSchema.option = { value: item.productId, label: item.productName }
- this.onAdd()
- },
- onAdd () {
- this.currObj = {
- name: '',
- productId: this.$master?.productId,
- serialNumber: '',
- mac: '',
- longitude: '',
- latitude: '',
- address: '',
- tenant: this.tenant
- }
- this.$refs.editDialog.show()
- },
- onSave (done) {
- if (this.check(this.currObj)) {
- if (this.isSub) {
- addSubDevice(this.$master, this.currObj).then(() => {
- done()
- this.reloadSubDevices(this.$master)
- })
- } else {
- addDevice(this.currObj).then(() => {
- done()
- this.$refs.table.resetCondition({ name: this.currObj.name, productId: this.currObj.productId })
- })
- }
- }
- },
- check (item) {
- if (!item.name) {
- this.$message({
- type: 'warning',
- message: '名称不能为空'
- })
- return false
- }
- if (!item.productId) {
- this.$message({
- type: 'warning',
- message: '请选择配置'
- })
- return false
- }
- if (!item.serialNumber) {
- this.$message({
- type: 'warning',
- message: '序列号不能为空'
- })
- return false
- }
- if (!item.mac) {
- this.$message({
- type: 'warning',
- message: 'MAC不能为空'
- })
- return false
- }
- if (!validMAC(item.mac)) {
- this.$message({
- type: 'warning',
- message: 'MAC格式不正确,例 ff:ff:ff:ff:ff:ff'
- })
- return false
- }
- item.mac = item.mac.toLowerCase()
- if (!item.address) {
- this.$message({
- type: 'warning',
- message: '地址不能为空'
- })
- return false
- }
- if (item.longitude && !validLongitude(item.longitude)) {
- this.$message({
- type: 'warning',
- message: '经度格式错误,-180 ~ +180'
- })
- return false
- }
- if (item.latitude && !validLatitude(item.latitude)) {
- this.$message({
- type: 'warning',
- message: '纬度格式错误,-90 ~ +90'
- })
- return false
- }
- return true
- },
- onViewDevice (item) {
- this.$router.push({
- name: 'device-management-detail',
- params: {
- id: item.id
- }
- })
- },
- onDelDevice (item) {
- if (item.isMaster) {
- deleteDevice(item).then(() => {
- this.$refs.table.decrease(1)
- })
- } else {
- deleteDevice(item).then(() => {
- this.reloadSubDevices(item.parent)
- })
- }
- },
- reloadSubDevices (item) {
- item.loaded = false
- item.children = []
- this.getSubDevices(item)
- },
- getSubDevices (item, pageSize = 999) {
- item.loading = true
- getSubDevices({
- id: item.id,
- pageNum: 1,
- pageSize
- }).then(({ data, totalCount }) => {
- if (totalCount > pageSize) {
- this.getSubDevices(item, totalCount)
- } else {
- item.loading = false
- item.loaded = true
- item.expand = true
- if (data.length === 0) {
- item.subs = [{ id: `${Math.random()}`, empty: true }]
- } else {
- item.subs = data.map(device => {
- return {
- ...device,
- isMaster: false,
- empty: false,
- parent: item
- }
- })
- }
- }
- }, () => {
- item.loading = false
- })
- },
- onRowClick (data) {
- if (__SUB_DEVICE__ && data.isMaster) {
- if (data.loaded) {
- data.expand = !data.expand
- } else if (!data.loading) {
- this.getSubDevices(data)
- }
- }
- },
- onTagClick (data) {
- (data.activate ? deactivateDevice : activateDevice)(data).then(() => {
- if (data.isMaster) {
- this.$refs.table.pageTo()
- } else {
- this.reloadSubDevices(data.parent)
- }
- })
- },
- onSettingDevice ({ id }) {
- this.$router.push({
- name: 'tenant-device-management-settings',
- params: { id }
- })
- }
- }
- }
- </script>
|