Browse Source

refactor: device status

Casper Dai 3 years ago
parent
commit
5295e02f2e

+ 13 - 2
src/api/base.js

@@ -69,8 +69,19 @@ export function addScope (data) {
   return data
 }
 
-export function addOrg (data) {
-  data.tenant = store.getters.tenant
+export function addTenantOrOrg (data) {
+  if (store.getters.isGroupAdmin && store.getters.isTopGroupAdmin) {
+    data.tenant = store.getters.tenant
+  } else {
+    data.org = store.getters.org
+  }
+  return data
+}
+
+export function addOrg (data, needTenant = true) {
+  if (needTenant) {
+    data.tenant = store.getters.tenant
+  }
   data.org = store.getters.org
   return data
 }

+ 35 - 36
src/api/device.js

@@ -8,8 +8,9 @@ import {
   send,
   confirmAndSend,
   addTenant,
-  addScope,
-  addUser
+  addTenantOrOrg,
+  addUser,
+  addOrg
 } from './base'
 import {
   AssetType,
@@ -20,7 +21,7 @@ export function getRatiosWithUser () {
   return tenantRequest({
     url: '/device/resolutionRatio',
     method: 'GET',
-    params: addScope({})
+    params: addTenantOrOrg({})
   }).then(({ data }) => {
     return {
       data: Object.keys(data).map(key => {
@@ -93,77 +94,75 @@ export function deleteDevice ({ id, name }) {
 }
 
 export function getDevices (query, options) {
+  const scope = addTenantOrOrg({})
+  if (scope.tenant) {
+    return getDevicesByAdmin({
+      ...query,
+      ...scope
+    }, options)
+  }
   const { pageNum: pageIndex, pageSize, ...params } = query
   return tenantRequest({
     url: '/device/relation/page',
     method: 'GET',
-    params: addScope({
+    params: {
       pageIndex, pageSize,
-      ...params
-    }),
+      ...params,
+      ...scope
+    },
     ...options
   })
 }
 
-export function getDevicesByAdmin (query) {
+export function getDevicesByAdmin (query, options) {
   const { pageNum: pageIndex, pageSize, ...params } = query
   return tenantRequest({
-    url: '/device/relation/page',
+    url: params.tenant ? '/device/tenant/page' : '/device/relation/page',
     method: 'GET',
     params: {
       pageIndex, pageSize,
       ...params
-    }
+    },
+    ...options
   })
 }
 
-export function getDevicesByRelation (query) {
+export function getBoundDevices (query, options) {
   const { pageNum: pageIndex, pageSize, ...params } = query
-  return tenantRequest({
-    url: '/device/queryRelation/page',
+  return send({
+    url: '/device/allocate/page',
     method: 'GET',
     params: {
       pageIndex, pageSize,
       ...params
-    }
+    },
+    ...options
   })
 }
 
-export function bindDeviceToObject (deviceId, org, user) {
-  return request({
-    url: `/device/relation/${deviceId}/save`,
+export function bindDeviceToObject (deviceId, org) {
+  return send({
+    url: '/device/allocate/department',
     method: 'POST',
-    data: {
-      org,
-      user
-    }
+    data: { deviceId, org }
   })
 }
 
-export function unbindDevice ({ deviceRelaId, name }) {
+export function unbindDevice ({ deviceRelationId, name }) {
   return confirmAndSend('移除', name, {
-    url: `/device/relation/${deviceRelaId}/remove`,
+    url: `/device/allocate/${deviceRelationId}`,
     method: 'DELETE'
   })
 }
 
 export function unbindDevices (params) {
   return request({
-    url: '/device/relation/batchRemove',
+    url: '/device/allocate/department',
     method: 'DELETE',
     params
   })
 }
 
-export function updateBindDevices (user, org) {
-  return send({
-    url: '/device/relation/update',
-    method: 'POST',
-    params: { user },
-    data: { org }
-  })
-}
-
 export function activateDevice ({ id, name }) {
   return confirmAndSend('激活', name, {
     url: '/device/batch/activate',
@@ -281,7 +280,7 @@ export function addDeviceGroup (data) {
   return add({
     url: '/deviceGroup',
     method: 'POST',
-    data
+    data: addOrg(data, false)
   })
 }
 
@@ -316,7 +315,7 @@ export function getDevicesByGroup (id) {
   return tenantRequest({
     url: `/deviceGroup/${id}/device`,
     method: 'GET',
-    params: addScope({})
+    params: addTenantOrOrg({})
   })
 }
 
@@ -324,7 +323,7 @@ export function getDeviceTree () {
   return tenantRequest({
     url: '/deviceGroup/deviceTree',
     method: 'GET',
-    params: addUser(addScope({}))
+    params: addUser(addTenantOrOrg({}))
   })
 }
 
@@ -348,7 +347,7 @@ export function getDeviceStatistics (productId) {
   return tenantRequest({
     url: '/device/listDeviceTotal',
     method: 'GET',
-    params: addScope({ productId })
+    params: addTenantOrOrg({ productId })
   })
 }
 

+ 2 - 9
src/api/external.js

@@ -331,17 +331,10 @@ export function deleteSensor ({ id, name }) {
 
 export function getSensorRecords (params, options) {
   return request({
-    url: '/device/thirdPartyGateway/sensorRecord',
-    method: 'GET',
-    params,
-    ...options
-  })
-}
-
-export function getSensorRecordsForOld (options) {
-  return request({
+    // url: '/device/thirdPartyGateway/sensorRecord',
     url: '/device/sensorType',
     method: 'GET',
+    params,
     ...options
   })
 }

+ 1 - 1
src/components/service/external/DevicePlayer/index.vue

@@ -131,7 +131,7 @@ export default {
   },
   computed: {
     online () {
-      return this.device.activate === 2 && this.device.onlineStatus === 1
+      return this.device.activate && this.device.onlineStatus === 1
     },
     canPlay () {
       return !this.loading && !this.needReset

+ 6 - 6
src/router/index.js

@@ -274,18 +274,18 @@ export const asyncRoutes = [
     access: [Access.MANAGE_TENANTS, Access.MANAGE_TENANT],
     meta: { title: '平台管理', icon: 'pm' },
     children: [
-      {
-        name: 'settings',
-        path: 'settings',
-        component: () => import('@/views/realm/settings/index'),
-        meta: { title: '功能管理' }
-      },
       {
         name: 'org',
         path: 'org',
         component: () => import('@/views/realm/tenant/index'),
         meta: { title: '组织管理' }
       },
+      {
+        name: 'settings',
+        path: 'settings',
+        component: () => import('@/views/realm/settings/index'),
+        meta: { title: '功能管理' }
+      },
       {
         name: 'account',
         path: 'account',

+ 9 - 11
src/views/broadcast/deploy/components/BroadcasDeployPanel.vue

@@ -47,6 +47,15 @@
           placeholder="请选择"
           @change="onTemplateChange"
         />
+        <span class="c-grid-form__label">失效时间</span>
+        <el-date-picker
+          v-model="endDateTime"
+          type="datetime"
+          placeholder="请选择失效时间"
+          value-format="yyyy-MM-dd HH:mm:ss"
+          :picker-options="endDatePickerOptions"
+          @change="onEndDateTimeChange"
+        />
         <div
           v-if="templateId && !templateContent"
           class="c-grid-form__row u-text-center"
@@ -54,15 +63,6 @@
           <i class="el-icon-loading" />
         </div>
         <template v-if="templateContent">
-          <span class="c-grid-form__label">失效时间</span>
-          <el-date-picker
-            v-model="endDateTime"
-            type="datetime"
-            placeholder="请选择失效时间"
-            value-format="yyyy-MM-dd HH:mm:ss"
-            :picker-options="endDatePickerOptions"
-            @change="onEndDateTimeChange"
-          />
           <span class="c-grid-form__label">内容</span>
           <div class="l-flex--row c-grid-form__option c-grid-form__text">{{ templateContent }}</div>
           <div
@@ -73,8 +73,6 @@
             <span class="c-grid-form__label required c-grid-form__text">{{ value.keywordName }}</span>
             <el-input
               v-model.trim="value.content"
-              placeholder="最多50个字符"
-              maxlength="50"
               clearable
             />
           </div>

+ 7 - 7
src/views/dashboard/Dashboard.vue

@@ -187,15 +187,15 @@ export default {
       clearTimeout(this.$timer)
       this.deviceOptions = { loaded: false }
       getDeviceStatistics().then(({ data }) => {
-        const { notEnabledTotal, offLineTotal, onLineTotal, total } = data
-        monitor.total = total || 0
-        monitor.online = onLineTotal || 0
-        monitor.offline = offLineTotal || 0
-        monitor.inactive = notEnabledTotal || 0
+        const { deactivatedTotal, notConnectedTotal, offLineTotal, onLineTotal, total } = data
+        monitor.total = total
+        monitor.online = onLineTotal
+        monitor.offline = offLineTotal + notConnectedTotal
+        monitor.inactive = deactivatedTotal
       }).finally(() => {
         monitor.loading = false
         if (!this.monitor.loading) {
-          this.getDevices(this.monitor.total)
+          this.getDevices(this.monitor.total - this.monitor.inactive)
         }
       })
     },
@@ -215,7 +215,7 @@ export default {
       getDevices({
         pageNum: 1,
         pageSize: total,
-        activate: 2
+        activate: 1
       }, { custom: true }).then(
         ({ data }) => {
           options.list = data.sort(this.sort)

+ 1 - 1
src/views/dashboard/v0/DeviceInfo.vue

@@ -75,7 +75,7 @@ export default {
   },
   data () {
     return {
-      online: this.device.activate === 2 && this.device.onlineStatus === 1,
+      online: this.device.activate && this.device.onlineStatus === 1,
       rebooting: false,
       rows: [
         [

+ 1 - 1
src/views/dashboard/v0/Record.vue

@@ -106,7 +106,7 @@ export default {
   },
   data () {
     return {
-      options: createListOptions({ activate: 2, pageSize: 1 })
+      options: createListOptions({ activate: 1, pageSize: 1 })
     }
   },
   computed: {

+ 11 - 14
src/views/dashboard/v0/index.vue

@@ -217,25 +217,22 @@ export default {
     },
     getDeviceStatistics () {
       getDeviceStatistics().then(({ data }) => {
-        const { notEnabledTotal, offLineTotal, onLineTotal, total } = data
+        const { deactivatedTotal, notConnectedTotal, offLineTotal, onLineTotal, total } = data
         this.statusData = [
-          { label: '设备总数', value: total || 0 },
-          { label: '设备在线数', value: onLineTotal || 0 },
-          { label: '设备离线数', value: offLineTotal || 0 },
-          { label: '设备未启用数', value: notEnabledTotal || 0 }
+          { label: '设备总数', value: total },
+          { label: '设备在线数', value: onLineTotal },
+          { label: '设备离线数', value: offLineTotal + notConnectedTotal },
+          { label: '设备未启用数', value: deactivatedTotal }
         ]
-        this.getDevices(total - (notEnabledTotal || 0))
+        this.getDevices(total - deactivatedTotal)
       })
     },
     getDevices (total) {
-      getDevices(
-        {
-          pageNum: 1,
-          pageSize: total,
-          activate: 2
-        },
-        { custom: true }
-      ).then(({ data }) => {
+      getDevices({
+        pageNum: 1,
+        pageSize: total,
+        activate: 1
+      }, { custom: true }).then(({ data }) => {
         this.deviceList = data
       })
     }

+ 1 - 1
src/views/dashboard/v1/Record.vue

@@ -34,7 +34,7 @@ export default {
   },
   data () {
     return {
-      options: createListOptions({ activate: 2, onlineStatus: 1, pageSize: 9 })
+      options: createListOptions({ activate: 1, onlineStatus: 1, pageSize: 9 })
     }
   },
   created () {

+ 11 - 14
src/views/dashboard/v1/index.vue

@@ -184,25 +184,22 @@ export default {
     },
     getDeviceStatistics () {
       getDeviceStatistics().then(({ data }) => {
-        const { notEnabledTotal, offLineTotal, onLineTotal, total } = data
+        const { deactivatedTotal, notConnectedTotal, offLineTotal, onLineTotal, total } = data
         this.statusData = [
-          { label: '设备总数', value: total || 0 },
-          { label: '设备在线数', value: onLineTotal || 0 },
-          { label: '设备离线数', value: offLineTotal || 0 },
-          { label: '设备未启用数', value: notEnabledTotal || 0 }
+          { label: '设备总数', value: total },
+          { label: '设备在线数', value: onLineTotal },
+          { label: '设备离线数', value: offLineTotal + notConnectedTotal },
+          { label: '设备未启用数', value: deactivatedTotal }
         ]
-        this.getDevices(total)
+        this.getDevices(total - deactivatedTotal)
       })
     },
     getDevices (total) {
-      getDevices(
-        {
-          pageNum: 1,
-          pageSize: total,
-          activate: 2
-        },
-        { custom: true }
-      ).then(({ data }) => {
+      getDevices({
+        pageNum: 1,
+        pageSize: total,
+        activate: 1
+      }, { custom: true }).then(({ data }) => {
         this.deviceList = data
       })
     }

+ 1 - 1
src/views/device/detail/dashboard/DeviceInfo.vue

@@ -49,7 +49,7 @@ export default {
   },
   data () {
     return {
-      online: this.device.activate === 2 && this.device.onlineStatus === 1,
+      online: this.device.activate && this.device.onlineStatus === 1,
       rebooting: false,
       openFunctionKey: 'bootDevice',
       closeFunctionKey: 'shutdownDevice',

+ 1 - 1
src/views/device/detail/dashboard/LinkState.vue

@@ -179,7 +179,7 @@ export default {
         : { transform: `scale(${this.scale})` }
     },
     online () {
-      return this.device.activate === 2 && this.device.onlineStatus === 1
+      return this.device.activate && this.device.onlineStatus === 1
     },
     linkState () {
       const map = {

+ 1 - 1
src/views/device/detail/index.vue

@@ -171,7 +171,7 @@ export default {
         if (this.deviceId === id) {
           if (data) {
             this.device = data
-            this.isActivated = data.activate === 2
+            this.isActivated = data.activate
             this.isOnline = this.isActivated && data.onlineStatus === 1
             if (!this.isOnline) {
               ScreenshotCache.remove(id)

+ 2 - 2
src/views/device/detail/monitor.js

@@ -1,4 +1,4 @@
-import { getSensorRecordsForOld } from '@/api/external'
+import { getSensorRecords } from '@/api/external'
 import {
   publish,
   subscribe,
@@ -303,7 +303,7 @@ export function stopSensor () {
 }
 
 function updateSensors (inst) {
-  inst.running && getSensorRecordsForOld({ custom: true }).then(({ data }) => {
+  inst.running && getSensorRecords({ deviceId }, { custom: true }).then(({ data }) => {
     inst.running && onUpdate(inst, data)
     return 5000
   }, () => 2000).then(delay => {

+ 2 - 2
src/views/device/index.vue

@@ -69,8 +69,8 @@ export default {
             return empty
               ? null
               : activate
-                ? activate === 1
-                  ? { type: 'primary', label: '已激活' }
+                ? onlineStatus === 0
+                  ? { type: 'primary', label: '已启用' }
                   : onlineStatus === 1
                     ? { type: 'success', label: '在线' }
                     : { type: 'danger', label: '离线' }

+ 1 - 1
src/views/device/record/index.vue

@@ -80,7 +80,7 @@ export default {
   name: 'DeviceRecord',
   data () {
     return {
-      options: createListOptions({ activate: 2, pageSize: 4, name: '' })
+      options: createListOptions({ activate: 1, pageSize: 4, name: '' })
     }
   },
   computed: {

+ 1 - 1
src/views/device/timeline/index.vue

@@ -207,7 +207,7 @@ export default {
     return {
       deviceOptions: createListOptions({
         name: '',
-        activate: 2,
+        activate: 1,
         pageSize: 5
       }),
       style: null,

+ 8 - 34
src/views/realm/assign/Device.vue

@@ -13,10 +13,9 @@
 </template>
 
 <script>
-import { getUserGroups } from '@/api/user'
 import {
   getDevicesByAdmin,
-  getDevicesByRelation,
+  getBoundDevices,
   bindDeviceToObject,
   unbindDevice
 } from '@/api/device'
@@ -24,10 +23,6 @@ import {
 export default {
   name: 'Device',
   props: {
-    isGroup: {
-      type: [Boolean, String],
-      default: false
-    },
     tenant: {
       type: Object,
       required: true
@@ -84,43 +79,22 @@ export default {
   },
   methods: {
     getDevices (params) {
-      const queryParams = { ...params }
-      if (this.isGroup) {
-        queryParams.org = this.value
-      } else {
-        queryParams.user = this.value
-      }
-      return getDevicesByRelation(queryParams)
+      return getBoundDevices({
+        ...params,
+        org: this.value
+      })
     },
     onAdd () {
       this.$refs.tableDialog.show()
     },
-    async onChoosen ({ value: { id } }) {
-      const loading = this.$showLoading()
-      try {
-        const key = this.value
-        let org
-        let user
-        if (this.isGroup) {
-          org = key
-        } else {
-          if (!this.$userGroup) {
-            const groups = await getUserGroups(this.value)
-            this.$userGroup = groups[groups.length - 1].path
-          }
-          org = this.$userGroup
-          user = key
-        }
-        await bindDeviceToObject(id, org, user)
+    onChoosen ({ value: { id } }) {
+      bindDeviceToObject(id, this.value).then(() => {
         this.$message({
           type: 'success',
           message: '分配成功'
         })
         this.$refs.table.pageTo(1)
-      } catch (e) {
-        console.log(e)
-      }
-      this.$closeLoading(loading)
+      })
     },
     onDel (device) {
       unbindDevice(device).then(() => {

+ 13 - 36
src/views/realm/assign/index.vue

@@ -22,39 +22,22 @@
               @node-click="onTenantTreeClick"
             />
           </div>
-          <div class="l-flex__auto l-flex--col">
-            <el-tabs
-              v-model="active"
-              class="c-tabs has-bottom-padding"
+          <div class="l-flex__fill l-flex">
+            <div
+              class="c-tree-sidebar u-overflow-y--auto"
+              :class="{ large: !isSuperAdmin }"
             >
-              <el-tab-pane
-                label="设备到部门"
-                name="Group"
-              />
-              <el-tab-pane
-                label="设备到人"
-                name="User"
-              />
-            </el-tabs>
-            <div class="l-flex__fill l-flex">
-              <div
-                class="c-tree-sidebar u-overflow-y--auto"
-                :class="{ large: !isSuperAdmin }"
-              >
-                <component
-                  :is="active"
-                  :tenant="tenant"
-                  :groups="groups"
-                  @change="onChange"
-                />
-              </div>
-              <device
-                v-if="value"
-                :is-group="isGroup"
+              <group
                 :tenant="tenant"
-                :value="value"
+                :groups="groups"
+                @change="onChange"
               />
             </div>
+            <device
+              v-if="value"
+              :tenant="tenant"
+              :value="value"
+            />
           </div>
         </template>
       </div>
@@ -85,19 +68,16 @@
 import { mapGetters } from 'vuex'
 import { getTenantTree } from '@/api/user'
 import Group from './Group'
-import User from './User'
 import Device from './Device'
 
 export default {
   name: 'DeviceAssign',
   components: {
     Group,
-    User,
     Device
   },
   data () {
     return {
-      active: 'Group',
       tenants: null,
       tenant: null,
       groups: null,
@@ -106,10 +86,7 @@ export default {
     }
   },
   computed: {
-    ...mapGetters(['isSuperAdmin']),
-    isGroup () {
-      return this.active === 'Group'
-    }
+    ...mapGetters(['isSuperAdmin'])
   },
   created () {
     this.getTreeData()

+ 2 - 2
src/views/realm/device/Device.vue

@@ -146,8 +146,8 @@ export default {
             return empty
               ? null
               : activate
-                ? activate === 1
-                  ? { type: 'primary', label: '已激活' }
+                ? onlineStatus === 0
+                  ? { type: 'primary', label: '已启用' }
                   : onlineStatus === 1
                     ? { type: 'success', label: '在线' }
                     : { type: 'danger', label: '离线' }

+ 2 - 2
src/views/realm/device/Group.vue

@@ -55,8 +55,8 @@ export default {
             return empty
               ? null
               : activate
-                ? activate === 1
-                  ? { type: 'primary', label: '已激活' }
+                ? onlineStatus === 0
+                  ? { type: 'primary', label: '已启用' }
                   : onlineStatus === 1
                     ? { type: 'success', label: '在线' }
                     : { type: 'danger', label: '离线' }

+ 22 - 29
src/views/realm/user/Settings.vue

@@ -143,10 +143,6 @@ import {
   getUserRoleMapping,
   updateUserRoles
 } from '@/api/user'
-import {
-  unbindDevices,
-  updateBindDevices
-} from '@/api/device'
 import { Role } from '@/constant'
 
 export default {
@@ -289,8 +285,7 @@ export default {
         { type: 'warning' }
       ).then(() => {
         const loading = this.$showLoading()
-        unbindDevices({ user: this.user.id })
-          .then(() => deleteUser(this.user))
+        deleteUser(this.user)
           .then(() => this.$emit('del'))
           .finally(() => {
             this.$closeLoading(loading)
@@ -343,29 +338,27 @@ export default {
         { type: 'warning' }
       ).then(() => {
         const loading = this.$showLoading()
-        updateBindDevices(this.user.id, this.$group.path).then(() => {
-          moveUserGroup(this.user.id, this.group, this.$group).then(
-            () => {
-              this.$closeLoading(loading)
-              this.group = this.$group
-              this.$group = null
-              this.$message({
-                type: 'success',
-                message: '修改成功'
-              })
-              this.$emit('group', this.group)
-            },
-            () => {
-              this.group = null
-              this.groupChanged = false
-              this.getSettings().then(() => {
-                if (!this.error) {
-                  this.expandTree()
-                }
-              })
-            }
-          )
-        })
+        moveUserGroup(this.user.id, this.group, this.$group).then(
+          () => {
+            this.$closeLoading(loading)
+            this.group = this.$group
+            this.$group = null
+            this.$message({
+              type: 'success',
+              message: '修改成功'
+            })
+            this.$emit('group', this.group)
+          },
+          () => {
+            this.group = null
+            this.groupChanged = false
+            this.getSettings().then(() => {
+              if (!this.error) {
+                this.expandTree()
+              }
+            })
+          }
+        )
       })
     },
     onSaveRole () {