index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <div class="page page-full border bg padding">
  3. <el-form inline ref="queryRef" @keyup.enter="getList(1)">
  4. <el-form-item label="名称" prop="keyWord">
  5. <el-input v-model="params.keyWord" placeholder="请输入名称" clearable style="width: 240px" />
  6. </el-form-item>
  7. <el-form-item>
  8. <el-button type="primary" class="ml10" @click="getList(1)">
  9. <el-icon>
  10. <ele-Search />
  11. </el-icon>
  12. 查询
  13. </el-button>
  14. </el-form-item>
  15. <el-form-item>
  16. <el-button type="success" @click="addOrEdit()" v-auth="'add'" v-if="productIno">
  17. <el-icon>
  18. <ele-FolderAdd />
  19. </el-icon>
  20. 新增档案
  21. </el-button>
  22. <!-- <el-button type="primary" @click="addOrEdit()" v-if="productIno">
  23. <el-icon>
  24. <ele-FolderAdd />
  25. </el-icon>
  26. 批量添加
  27. </el-button> -->
  28. <el-button type="danger" @click="batchdel()" v-auth="'batchdel'">
  29. <el-icon>
  30. <ele-FolderAdd />
  31. </el-icon>
  32. 批量删除
  33. </el-button>
  34. </el-form-item>
  35. </el-form>
  36. <div class="page page-full-part flex-row gap-4">
  37. <el-card style="width: 250px;" shadow="nover">
  38. <el-tree :data="mergedData" :props="defaultProps" accordion default-expand-all @node-click="handleNodeClick" :node-key="'id'" highlight-current>
  39. <template #default="{ node, data }">
  40. <div class="custom-tree-node">
  41. <span class="tree-label">
  42. <el-icon v-if="data.is_type == '2'">
  43. <Expand />
  44. </el-icon>
  45. {{ node.label }}
  46. </span>
  47. </div>
  48. </template>
  49. </el-tree>
  50. </el-card>
  51. <el-card class="flex1" shadow="nover">
  52. <div class="page page-full">
  53. <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange" row-key="id" v-loading="loading">
  54. <el-table-column type="selection" width="55" align="center" />
  55. <el-table-column prop="deviceName" v-col="'deviceName'" label="设备名称" min-width="100" show-overflow-tooltip></el-table-column>
  56. <el-table-column prop="deviceKey" v-col="'deviceKey'" label="设备KEY" show-overflow-tooltip></el-table-column>
  57. <el-table-column prop="deviceNumber" v-col="'deviceNumber'" label="设备编码" show-overflow-tooltip></el-table-column>
  58. <el-table-column prop="deviceCategory" v-col="'deviceCategory'" label="设备类型" show-overflow-tooltip></el-table-column>
  59. <el-table-column prop="installTime" v-col="'installTime'" label="安装时间" width="160" align="center"></el-table-column>
  60. <el-table-column label="操作" width="120" align="center">
  61. <template #default="scope">
  62. <el-button size="small" text type="warning" v-auth="'edit'" @click="addOrEdit(scope.row)">编辑</el-button>
  63. <el-button size="small" text type="info" v-auth="'del'" @click="del(scope.row)">删除</el-button>
  64. </template>
  65. </el-table-column>
  66. </el-table>
  67. <pagination v-if="params.total" :total="params.total" v-model:page="params.pageNum" v-model:limit="params.pageSize" @pagination="getList()" />
  68. </div>
  69. </el-card>
  70. </div>
  71. <EditForm ref="editFormRef" @getList="getList(1)"></EditForm>
  72. </div>
  73. </template>
  74. <script lang="ts" setup>
  75. import device from '/@/api/device'
  76. import { useSearch } from '/@/hooks/useCommon'
  77. import { Expand } from '@element-plus/icons-vue'
  78. import { ElMessageBox, ElMessage } from 'element-plus'
  79. import EditForm from './edit.vue'
  80. interface Tree {
  81. label: string
  82. children?: Tree[]
  83. }
  84. import { ref, onMounted } from 'vue'
  85. const defaultProps = {
  86. children: 'children',
  87. label: 'label',
  88. }
  89. const queryRef = ref()
  90. const productData = ref([])
  91. const mergedData = ref()
  92. const cateData = ref()
  93. const editFormRef = ref()
  94. const productIno = ref()
  95. const ids = ref<number[]>([])
  96. const { params, tableData, getList, loading } = useSearch<any[]>(device.dev_asset.getList, 'Data', { keyWord: '' })
  97. getList()
  98. const handleSelectionChange = (selection: any[]) => {
  99. ids.value = selection.map((item) => item.id);
  100. };
  101. onMounted(() => {
  102. getCateList()
  103. })
  104. const addOrEdit = async (row?: any) => {
  105. if (row) {
  106. editFormRef.value.open(row, productIno.value)
  107. return
  108. } else {
  109. editFormRef.value.open({}, productIno.value)
  110. }
  111. }
  112. const getCateList = () => {
  113. device.category.getList({}).then((res: any) => {
  114. cateData.value = res.category
  115. device.product.getLists({}).then((res: any) => {
  116. productData.value = res.product
  117. mergedData.value = matchProductsToCategories(productData.value, cateData.value)
  118. // 默认加载第一个设备对应属性
  119. if (productData.value.length > 0) {
  120. handleNodeClick(mergedData.value[0].children[0])
  121. }
  122. })
  123. })
  124. }
  125. const handleNodeClick = (data: any) => {
  126. if (data.is_type === '2') {
  127. productIno.value = data
  128. params.productKey = data.key
  129. getList()
  130. } else {
  131. productIno.value = ''
  132. }
  133. }
  134. const matchProductsToCategories = (productData: any, cateData: any) => {
  135. const treeData = []
  136. for (let category of cateData) {
  137. const treeNode = buildTree(category, productData)
  138. treeData.push(treeNode)
  139. }
  140. return treeData
  141. }
  142. const buildTree = (category: any, productData: any) => {
  143. const treeNode = {
  144. id: category.id,
  145. label: category.name,
  146. key: category.key,
  147. is_type: '1', // 1是分类
  148. children: [],
  149. }
  150. if (category.children && category.children.length > 0) {
  151. for (let child of category.children) {
  152. const childNode = buildTree(child, productData)
  153. treeNode.children.push(childNode)
  154. }
  155. } else {
  156. const products = productData.filter((product: any) => product.categoryId === category.id)
  157. for (let product of products) {
  158. const productNode = {
  159. id: product.id,
  160. label: product.name,
  161. key: product.key,
  162. is_type: '2', // 2是产品
  163. }
  164. treeNode.children.push(productNode)
  165. }
  166. }
  167. return treeNode
  168. }
  169. const batchdel = () => {
  170. ElMessageBox.confirm('是否确认要批量删除这些数据吗?', '提示', {
  171. confirmButtonText: '确认',
  172. cancelButtonText: '取消',
  173. type: 'warning',
  174. }).then(async () => {
  175. await device.dev_asset.delete({ ids: ids.value })
  176. ElMessage.success('删除成功')
  177. getList()
  178. })
  179. }
  180. const del = (row: any) => {
  181. ElMessageBox.confirm('是否确认删除名称为:"' + row.deviceName + '"的数据项?', '提示', {
  182. confirmButtonText: '确认',
  183. cancelButtonText: '取消',
  184. type: 'warning',
  185. }).then(async () => {
  186. await device.dev_asset.delete({ ids: row.id })
  187. ElMessage.success('删除成功')
  188. getList()
  189. })
  190. }
  191. // getCateList()
  192. </script>
  193. <style scoped lang="scss">
  194. .custom-tree-node {
  195. width: 100%;
  196. display: flex;
  197. align-items: center;
  198. justify-content: space-between;
  199. font-size: 14px;
  200. padding-right: 8px;
  201. overflow: hidden;
  202. .tree-label {
  203. width: 100%;
  204. overflow: hidden;
  205. text-overflow: ellipsis;
  206. white-space: nowrap;
  207. margin-right: 10px;
  208. }
  209. &:hover {
  210. .tree-options {
  211. display: block;
  212. }
  213. }
  214. }
  215. </style>