device.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <div class="tab-content h-full">
  3. <div class="subtitle"><span></span> <el-button type="primary" size="small" @click="addDevice()">添加设备</el-button></div>
  4. <el-table :data="tableData" style="width: 100%" v-loading="loading" max-height="calc(100vh - 280px)">
  5. <el-table-column label="标识" prop="key" min-width="150" show-overflow-tooltip v-col="'key'">
  6. <template #default="{ row }">
  7. <copy :text="row.key"></copy>
  8. </template>
  9. </el-table-column>
  10. <el-table-column label="设备名称" prop="name" min-width="160" show-overflow-tooltip v-col="'name'" />
  11. <el-table-column label="设备类型" prop="product.deviceType" min-width="100" align="center" show-overflow-tooltip v-col="'deviceType'" />
  12. <el-table-column label="所属产品" prop="productName" min-width="120" align="center" show-overflow-tooltip v-col="'productName'" />
  13. <el-table-column prop="status" label="状态" min-width="80" align="center" v-col="'status'">
  14. <template #default="scope">
  15. <el-tag type="info" size="small" v-if="scope.row.status == 1">离线</el-tag>
  16. <el-tag type="success" size="small" v-if="scope.row.status == 2">在线</el-tag>
  17. <el-tag type="info" size="small" v-if="scope.row.status == 0">未启用</el-tag>
  18. </template>
  19. </el-table-column>
  20. <el-table-column prop="lastOnlineTime" label="最后上线时间" align="center" width="160" v-col="'lastOnlineTime'"></el-table-column>
  21. <el-table-column prop="desc" label="说明" show-overflow-tooltip v-col="'desc'"></el-table-column>
  22. <el-table-column label="操作" width="120" align="center">
  23. <template #default="scope">
  24. <el-button size="small" text type="primary" v-auth="'xxx'" @click="$router.push('/iotmanager/device/instance/' + scope.row.key)">设备详情</el-button>
  25. <el-button size="small" text type="warning" v-auth="'xxx'" @click="onDel(scope.row)">解绑</el-button>
  26. </template>
  27. </el-table-column>
  28. </el-table>
  29. <pagination v-if="params.total" :total="params.total" v-model:page="params.pageNum" v-model:limit="params.pageSize" @pagination="getList()" />
  30. <el-dialog title="添加设备" v-model="isShowDialog" width="400">
  31. <el-form v-if="isShowDialog">
  32. <el-form-item label="产品">
  33. <el-select v-model="form.productKey" @change="getDivices" filterable placeholder="选择产品" :clearable="false" style="width: 100%">
  34. <el-option v-for="item in productList" :key="item.value" :label="item.label" :value="item.value" />
  35. </el-select>
  36. </el-form-item>
  37. <el-form-item label="设备" style="margin-bottom: 0;">
  38. <el-select v-model="form.resourcesKey" filterable placeholder="选择设备" :clearable="false" style="width: 100%">
  39. <el-option v-for="item in deviceList" :key="item.value" :label="item.label" :value="item.value" />
  40. </el-select>
  41. </el-form-item>
  42. </el-form>
  43. <template #footer>
  44. <span class="dialog-footer">
  45. <el-button @click="onCancel">取 消</el-button>
  46. <el-button type="primary" @click="onSubmit">确 定</el-button>
  47. </span>
  48. </template>
  49. </el-dialog>
  50. </div>
  51. </template>
  52. <script lang="ts" setup>
  53. import { reactive, ref } from 'vue';
  54. import deviceApi from '/@/api/device';
  55. import api from '/@/api/projects';
  56. import { useSearch } from '/@/hooks/useCommon';
  57. import { useRoute } from 'vue-router';
  58. import { ElMessage, ElMessageBox } from 'element-plus';
  59. const route = useRoute();
  60. const props = defineProps({ resourcesTypes: Number })
  61. const isShowDialog = ref(false)
  62. const productList = ref<any[]>([])
  63. const deviceList = ref<any[]>([])
  64. const resourcesTypes = props.resourcesTypes
  65. const projectsCode = route.params.id
  66. const baseForm = {
  67. productKey: '',
  68. resourcesKey: '',
  69. projectsCode,
  70. resourcesTypes
  71. }
  72. const form = reactive({
  73. ...baseForm
  74. })
  75. const { params, tableData, getList, loading } = useSearch<any[]>(deviceApi.instance.getList, 'device', { keys: [], status: undefined });
  76. getProducts();
  77. getSourceList();
  78. function getSourceList() {
  79. api.getProjectResourcesByCode({ projectsCode }).then((res: any) => {
  80. params.keys = (res || []).filter((item: any) => item.resourcesTypes === resourcesTypes).map((item: any) => item.resourcesKey)
  81. if (!params.keys.length) {
  82. params.pageNum = 1
  83. tableData.value = []
  84. } else {
  85. getList();
  86. }
  87. })
  88. }
  89. function getProducts() {
  90. deviceApi.product.getList({ pageSize: 500, status: 1 }).then((res: any) => {
  91. productList.value = (res?.product || []).map((item: any) => ({ label: item.name, value: item.key }))
  92. })
  93. }
  94. function onCancel() {
  95. Object.assign(form, baseForm)
  96. deviceList.value = []
  97. isShowDialog.value = false
  98. }
  99. function onSubmit() {
  100. if (!form.resourcesKey) return ElMessage('请先选择设备')
  101. api.bindResources(form).then((res: any) => {
  102. onCancel()
  103. getSourceList()
  104. ElMessage.success('添加成功')
  105. })
  106. }
  107. function addDevice() {
  108. isShowDialog.value = true
  109. }
  110. function onDel(row: any) {
  111. ElMessageBox.confirm(`确定要删除?`, '提示', {
  112. confirmButtonText: '确认',
  113. cancelButtonText: '取消',
  114. type: 'warning',
  115. }).then(() => {
  116. api.unbindResources({
  117. projectsCode,
  118. resourcesTypes,
  119. resourcesKey: row.key
  120. }).then(() => {
  121. getSourceList()
  122. ElMessage.success('解绑成功')
  123. })
  124. });
  125. }
  126. function getDivices(productKey: string) {
  127. form.resourcesKey = ''
  128. deviceList.value = []
  129. deviceApi.device.allList({ productKey }).then((res: any) => {
  130. deviceList.value = (res?.device || []).map((item: any) => ({ label: item.name, value: item.key }))
  131. })
  132. }
  133. </script>