index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <template>
  2. <div class="pageWrap bgc">
  3. <div :class="['h-full mr-2 aside-wrap', isCollapsed ? 'aside-wrap--collapsed' : '']">
  4. <div :class="['trigger-bar', isCollapsed ? 'trigger-bar--collapsed' : '']" @click="isCollapsed = !isCollapsed">
  5. <div class="bar__top"></div>
  6. <div class="bar__bottom"></div>
  7. </div>
  8. <el-card class="h-full border-none menu-card" shadow="never">
  9. <template #header>
  10. <div class="flex justify-between">
  11. <div class="card-header">
  12. <span>字典分类</span>
  13. </div>
  14. <div class="">
  15. <el-space>
  16. <el-icon :style="{ color: 'var(--el-color-primary)' }" class="cursor-pointer" size="22" @click="addHandler('tree')"
  17. ><Plus />
  18. </el-icon>
  19. <el-icon class="cursor-pointer" size="22" @click="getGroup"><Refresh /></el-icon>
  20. </el-space>
  21. </div>
  22. </div>
  23. </template>
  24. <el-input v-model="groupFilterText" placeholder="输入关键字进行过滤" clearable />
  25. <el-tree
  26. ref="treeRef"
  27. v-loading="showGroupLoading"
  28. class="menu-tree-box py-3"
  29. :data="treeData"
  30. node-key="id"
  31. :current-node-key="''"
  32. :highlight-current="true"
  33. :expand-on-click-node="false"
  34. :props="{ label: 'name' }"
  35. default-expand-all
  36. :filter-node-method="filterNode"
  37. @node-click="leftTreeClick"
  38. >
  39. <template #default="{ node, data }">
  40. <span class="custom-tree-node">
  41. <span class="label">{{ node.label }}</span>
  42. <span class="code">{{ data.code }}</span>
  43. <span v-if="data.id" class="do">
  44. <el-icon :style="{ color: 'var(--el-color-primary)' }" size="24" @click.stop="dicEdit(data)"><Edit /></el-icon>
  45. <el-icon :style="{ color: 'var(--el-color-error)' }" size="24" @click.stop="dictDel(node, data)"><Delete /></el-icon>
  46. </span>
  47. </span>
  48. </template>
  49. </el-tree>
  50. </el-card>
  51. </div>
  52. <div class="content-warp flex-column-page-wrap">
  53. <!-- 公用搜索组件 -->
  54. <LeSearchForm ref="searchForm" v-model:searchData="searchData" :forms="forms" :loading="tableOpts.options.loading"></LeSearchForm>
  55. <!-- LeTable 组件使用 -->
  56. <LeTable
  57. ref="tableRef"
  58. v-model:searchParams="tableOpts.searchParams"
  59. v-bind="tableOpts"
  60. v-model:curRow="tableOpts.curRow"
  61. v-model:checked-options="checkedColumns"
  62. :columns="activeColumns"
  63. >
  64. <template #toolLeft>
  65. <el-button plain :icon="Plus" type="primary" @click="addHandler('table')">新增</el-button>
  66. <el-button plain :icon="Delete" :disabled="curSelectionRows.length === 0" type="danger" @click="batch_del">删除</el-button>
  67. </template>
  68. <template #filterAvatarSlot="scope">
  69. <el-avatar :src="scope.row.avatar" size="small"></el-avatar>
  70. </template>
  71. <template #statusSlot="scope">
  72. <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" @change="() => handleStatusChange(scope.row)"></el-switch>
  73. </template>
  74. <template #actionSlot="{ row }">
  75. <div class="flex flex-align-pack-center">
  76. <LeIcon
  77. class="text-lg text-icon-color cursor-pointer icon-mage--edit"
  78. icon-class="icon-processInfo-mage--edit"
  79. @click="table_edit(row)"
  80. />
  81. <LeIcon class="text-lg ml-2 text-rose-700 cursor-pointer" icon-class="icon-processInfo-iconoir--trash" @click="table_del(row)" />
  82. </div>
  83. </template>
  84. </LeTable>
  85. </div>
  86. <LeFormConfigDialog
  87. v-if="visible"
  88. ref="dialogDictRef"
  89. v-model="visible"
  90. :title="`${isCreate ? '新增' : '编辑'}项`"
  91. width="600px"
  92. :form-data="activeData"
  93. :form-options="formOptions"
  94. @submit="submitHandler"
  95. />
  96. </div>
  97. </template>
  98. <script lang="tsx" setup>
  99. import dict from '@/api/system/dict'
  100. import { computed, nextTick, ref, watch } from 'vue'
  101. import { ElMessage, ElTree, ElMessageBox } from 'element-plus'
  102. import { useTablePage } from '@/hooks/useTablePage'
  103. import { Plus, Delete, Edit, Refresh } from '@element-plus/icons-vue'
  104. const isCollapsed = ref(false)
  105. const visible = ref(false) // 弹窗显示隐藏
  106. const isCreate = ref(true)
  107. const activeData = ref({})
  108. const current_mode = ref('') // 当前模式 (tree | table)
  109. const formsDialog = [
  110. {
  111. prop: 'pid',
  112. label: '所属字典',
  113. itemType: 'select',
  114. filterable: true,
  115. options: [],
  116. rules: [{ required: true, message: '请选择所属字典', trigger: 'blur' }]
  117. },
  118. {
  119. prop: 'name',
  120. label: '字典名称',
  121. itemType: 'input',
  122. rules: [{ required: true, message: '请输入字典名称', trigger: 'blur' }]
  123. },
  124. {
  125. prop: 'content',
  126. label: '内容',
  127. itemType: 'input',
  128. rules: [{ required: true, message: '请输入内容', trigger: 'blur' }]
  129. },
  130. {
  131. prop: 'code',
  132. label: '字典编码',
  133. itemType: 'input',
  134. rules: [{ required: true, message: '请输入字典编码', trigger: 'blur' }]
  135. },
  136. {
  137. prop: 'status',
  138. label: '字典状态',
  139. itemType: 'switch',
  140. activeText: '正常',
  141. inactiveText: '禁用'
  142. },
  143. {
  144. prop: 'sort',
  145. label: '排序',
  146. itemType: 'inputNumber',
  147. rules: [{ required: true, message: '请输入排序', trigger: 'blur' }]
  148. },
  149. {
  150. prop: 'remark',
  151. label: '备注',
  152. itemType: 'input'
  153. }
  154. ]
  155. // 新增的表单 和 编辑的表单
  156. const formOptions = computed(() => {
  157. let tempForm = []
  158. if (current_mode.value === 'tree') {
  159. for (let i in formsDialog) {
  160. let fd = formsDialog[i]
  161. if (fd.prop == 'pid') {
  162. continue
  163. }
  164. tempForm.push(fd)
  165. }
  166. } else {
  167. tempForm = formsDialog
  168. }
  169. return {
  170. forms: tempForm,
  171. labelWidth: 120,
  172. span: 30,
  173. showResetBtn: true,
  174. formConfig: {
  175. showCancelBtn: true,
  176. submitLoading: false
  177. }
  178. }
  179. })
  180. const showGroupLoading = ref(false)
  181. const groupFilterText = ref('')
  182. const treeRef = ref<InstanceType<typeof ElTree>>()
  183. const filterNode = (value: string, data: Tree) => {
  184. if (!value) return true
  185. return data.name.includes(value)
  186. }
  187. const treeData = ref([])
  188. // 获取左侧菜单数据
  189. const getGroup = async () => {
  190. showGroupLoading.value = true
  191. let data = await dict.dictListParentApi()
  192. formOptions.value.forms[0].options = data.map(item => {
  193. return { value: item.id, label: item.name }
  194. })
  195. showGroupLoading.value = false
  196. data.unshift({ id: '', name: '所有' })
  197. treeData.value = data
  198. }
  199. // 左侧菜单点击
  200. const leftTreeClick = data => {
  201. searchData.value = { ...searchData.value, pid: data.id ? data.id : null }
  202. }
  203. // 表格搜索条件
  204. const forms = ref([
  205. {
  206. prop: 'name',
  207. label: '字典名称:',
  208. itemType: 'input',
  209. placeholder: '请输入字典名称'
  210. },
  211. {
  212. prop: 'code',
  213. label: '字典编码:',
  214. itemType: 'input',
  215. placeholder: '请输入字典编码'
  216. },
  217. {
  218. prop: 'status',
  219. label: '字典状态:',
  220. itemType: 'select',
  221. placeholder: '请选择字典状态',
  222. options: [
  223. {
  224. label: '禁用',
  225. value: 0
  226. },
  227. {
  228. label: '正常',
  229. value: 1
  230. }
  231. ]
  232. }
  233. ])
  234. // table列表数据请求
  235. const queryList = async () => {
  236. const { options, searchParams } = tableOpts
  237. options.loading = true
  238. try {
  239. const { records: list, total } = await dict.dictPageApi(searchParams)
  240. tableOpts.total = total
  241. tableOpts.list = list
  242. } catch {
  243. tableOpts.total = 0
  244. tableOpts.list = []
  245. } finally {
  246. options.loading = false
  247. }
  248. }
  249. // table 参数
  250. const columns = [
  251. {
  252. prop: 'name',
  253. label: '字典名称',
  254. minWidth: 80
  255. },
  256. {
  257. prop: 'code',
  258. label: '字典编码',
  259. minWidth: 100
  260. },
  261. {
  262. prop: 'content',
  263. label: '内容',
  264. minWidth: 150
  265. },
  266. {
  267. prop: 'sort',
  268. label: '排序'
  269. },
  270. {
  271. prop: 'status',
  272. label: '字典状态',
  273. slots: {
  274. default: 'statusSlot'
  275. }
  276. },
  277. {
  278. prop: 'remark',
  279. label: '备注',
  280. minWidth: 100
  281. },
  282. {
  283. prop: 'action',
  284. label: '操作',
  285. width: 100,
  286. fixed: 'right',
  287. align: 'center',
  288. slots: {
  289. default: 'actionSlot'
  290. }
  291. }
  292. ]
  293. const { searchData, tableOpts, checkedColumns, activeColumns, curSelectionRows, updateParams } = useTablePage(
  294. {
  295. options: {
  296. showIndex: false
  297. },
  298. // 需要展示的列
  299. columns,
  300. // 控制列配置
  301. columnsConfig: {
  302. columns
  303. }
  304. },
  305. {
  306. queryList,
  307. fetchImmediate: false
  308. }
  309. )
  310. // 删除
  311. const deleteItem = async ids => {
  312. // try {
  313. await dict.dictDeleteApi(ids)
  314. if (current_mode.value === 'tree') {
  315. getGroup()
  316. }
  317. updateParams()
  318. ElMessage.success(`删除成功~`)
  319. // } catch (e) {
  320. // ElMessage.error(`删除失败~`)
  321. // }
  322. }
  323. // 单个删除
  324. const table_del = (row: any) => {
  325. ElMessageBox.confirm(`确认删除「${row.name}」这条数据?`, '提示', {
  326. confirmButtonText: '确认',
  327. cancelButtonText: '取消',
  328. type: 'error'
  329. }).then(async () => {
  330. deleteItem([row.id])
  331. })
  332. }
  333. //批量删除
  334. const batch_del = () => {
  335. const ids = curSelectionRows.value.map(item => item.id) // 多选数据
  336. ElMessageBox.confirm('是否删除选中数据?', '提示', {
  337. confirmButtonText: '确认',
  338. cancelButtonText: '取消',
  339. type: 'error',
  340. buttonSize: 'default'
  341. }).then(() => {
  342. deleteItem(ids)
  343. })
  344. }
  345. const table_edit = async row => {
  346. isCreate.value = false
  347. activeData.value = { ...row, status: row.status ? true : false }
  348. visible.value = true
  349. }
  350. // 弹窗事件
  351. const submitHandler = async params => {
  352. try {
  353. formOptions.value.formConfig.submitLoading = true
  354. params.status = params.status ? 1 : 0
  355. params.id = activeData.value.id ? activeData.value.id : null
  356. await dict.dictAddOrEditSaveApi(params)
  357. ElMessage.success(`${isCreate.value ? '新增' : '修改'}成功~`)
  358. visible.value = false
  359. if (current_mode.value === 'tree') {
  360. getGroup()
  361. }
  362. updateParams()
  363. } finally {
  364. formOptions.value.formConfig.submitLoading = false
  365. }
  366. }
  367. const addHandler = mode => {
  368. isCreate.value = true
  369. activeData.value = {}
  370. visible.value = true
  371. current_mode.value = mode
  372. }
  373. // 删除左侧树
  374. const dictDel = (node, data) => {
  375. current_mode.value = 'tree'
  376. ElMessageBox.confirm(`确定删除 ${data.name} 项吗?`, '提示')
  377. .then(async () => {
  378. // try {
  379. deleteItem([data.id])
  380. // } catch (e) {
  381. // ElMessage.error(`删除失败~`)
  382. // }
  383. })
  384. .catch(() => {})
  385. }
  386. const dicEdit = data => {
  387. current_mode.value = 'tree'
  388. table_edit(data)
  389. }
  390. const handleStatusChange = (row: any) => {
  391. // 字典状态 0、禁用 1、正常
  392. const status = row.status
  393. let text = Number(row.status) === 0 ? '禁用' : '启用'
  394. ElMessageBox.confirm('请确认是否' + text + '当前数据?', '提示', { type: 'warning' })
  395. .then(async () => {
  396. await dict.dictStatusApi({ id: row.id, status }) // 接口请求
  397. ElMessage.success(`${text}成功`)
  398. })
  399. .catch(status => {
  400. if (status == 'cancel') {
  401. row.status = Number(row.status) === 0 ? 1 : 0
  402. }
  403. })
  404. }
  405. nextTick(() => {
  406. getGroup()
  407. queryList()
  408. })
  409. watch(groupFilterText, val => {
  410. treeRef.value!.filter(val)
  411. })
  412. </script>
  413. <style scoped lang="scss">
  414. .custom-tree-node {
  415. display: flex;
  416. flex: 1;
  417. align-items: center;
  418. justify-content: space-between;
  419. font-size: 14px;
  420. padding-right: 24px;
  421. height: 100%;
  422. &:hover {
  423. .code {
  424. display: none;
  425. }
  426. .do {
  427. display: inline-block;
  428. }
  429. }
  430. .do {
  431. display: none;
  432. }
  433. .code {
  434. font-size: 12px;
  435. color: var(--el-color-primary);
  436. }
  437. .do i {
  438. margin-left: 3px;
  439. color: #999;
  440. padding: 3px;
  441. font-size: 20px;
  442. &:hover {
  443. color: #333;
  444. }
  445. }
  446. }
  447. .content-warp {
  448. // 300 + 28
  449. width: calc(100% - 328px);
  450. }
  451. .aside-wrap--collapsed {
  452. margin-left: -10px;
  453. .menu-card {
  454. width: 0;
  455. }
  456. }
  457. @media (max-width: 750px) {
  458. .pageWrap {
  459. //overflow-y: auto;
  460. flex-direction: column;
  461. overflow-y: auto;
  462. :deep(.aside-wrap) {
  463. margin-bottom: 10px;
  464. margin-right: 0;
  465. //height: 50%;
  466. .menu-card {
  467. .el-card__body {
  468. width: 100%;
  469. }
  470. }
  471. .trigger-bar {
  472. display: none;
  473. }
  474. }
  475. .content-warp {
  476. width: 100%;
  477. }
  478. }
  479. }
  480. </style>