index.vue 6.1 KB

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