index.vue 6.5 KB

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