index.vue 5.5 KB

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