index.vue 5.5 KB

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