index.vue 6.8 KB

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