index.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <template>
  2. <div class="pageWrap">
  3. <div class="content-warp flex-column-page-wrap">
  4. <!-- 公用搜索组件 -->
  5. <LeSearchForm ref="searchForm" v-model:searchData="searchData" :forms="forms" :loading="tableOpts.options.loading"> </LeSearchForm>
  6. <!-- LeTable 组件使用 -->
  7. <LeTable
  8. ref="tableRef"
  9. v-model:searchParams="tableOpts.searchParams"
  10. v-bind="tableOpts"
  11. v-model:curRow="tableOpts.curRow"
  12. v-model:checked-options="checkedColumns"
  13. :columns="activeColumns"
  14. >
  15. <template #toolLeft>
  16. <el-button type="primary" @click="addHandler">
  17. <el-icon class="btn-icon">
  18. <Plus />
  19. </el-icon>
  20. </el-button>
  21. <el-button type="danger" :disabled="curSelectionRows.length === 0" @click="batch_del">
  22. <el-icon class="btn-icon">
  23. <Delete />
  24. </el-icon>
  25. </el-button>
  26. </template>
  27. <template #statusSlot="scope">
  28. <status-indicator v-if="scope.row.status === 1" pulse type="success"></status-indicator>
  29. <status-indicator v-else pulse type="danger"></status-indicator>
  30. </template>
  31. <template #actionSlot="scope">
  32. <el-tooltip content="编辑" placement="bottom" effect="light">
  33. <el-icon class="ibt0" @click="table_edit(scope.row)">
  34. <Edit />
  35. </el-icon>
  36. </el-tooltip>
  37. <el-divider direction="vertical"></el-divider>
  38. <el-popconfirm title="确定删除吗?" @confirm="table_del(scope.row)">
  39. <template #reference>
  40. <el-icon class="ibt0">
  41. <Delete />
  42. </el-icon>
  43. </template>
  44. </el-popconfirm>
  45. </template>
  46. </LeTable>
  47. </div>
  48. <LeFormConfigDialog
  49. v-if="visible"
  50. ref="dialogUserRef"
  51. v-model="visible"
  52. :title="`${isCreate ? '新增' : '编辑'}部门`"
  53. width="600px"
  54. :form-data="activeData"
  55. :form-options="formOptions"
  56. @submit="submitHandler"
  57. />
  58. </div>
  59. </template>
  60. <script lang="tsx" setup>
  61. import department from '@/api/system/department'
  62. import { computed, nextTick, ref, watch } from 'vue'
  63. import { ElMessage, ElTree } from 'element-plus'
  64. import { useTablePage } from '@/hooks/useTablePage'
  65. import { Plus, Delete } from '@element-plus/icons-vue'
  66. import StatusIndicator from '@/components/StatusIndicator'
  67. const visible = ref(false) // 弹窗显示隐藏
  68. const isCreate = ref(true)
  69. const activeData = ref({})
  70. const curSelectionRows = ref([]) // 表格选中数据
  71. const formsDialog = ref([
  72. {
  73. prop: 'pid',
  74. label: '上级部门',
  75. itemType: 'cascader',
  76. props: {
  77. value: 'id',
  78. label: 'name',
  79. emitPath: false,
  80. checkStrictly: true
  81. },
  82. options: []
  83. },
  84. {
  85. prop: 'name',
  86. label: '名称',
  87. itemType: 'input',
  88. rules: [{ required: true, message: '请输入名称', trigger: 'blur' }]
  89. },
  90. {
  91. prop: 'code',
  92. label: '编码',
  93. itemType: 'input',
  94. rules: [{ required: true, message: '请输入编码', trigger: 'blur' }]
  95. },
  96. {
  97. prop: 'sort',
  98. label: '排序',
  99. itemType: 'inputNumber',
  100. rules: [{ required: true, message: '请输入排序', trigger: 'blur' }]
  101. },
  102. {
  103. prop: 'status',
  104. label: '状态',
  105. itemType: 'switch',
  106. activeText: '是',
  107. inactiveText: '否'
  108. },
  109. {
  110. prop: 'remark',
  111. label: '备注',
  112. itemType: 'input',
  113. rules: [{ required: true, message: '请输入备注', trigger: 'blur' }]
  114. }
  115. ])
  116. // 新增的表单 和 编辑的表单
  117. const formOptions = computed(() => {
  118. return {
  119. forms: formsDialog.value,
  120. labelWidth: 120,
  121. span: 30,
  122. showResetBtn: true,
  123. formConfig: {
  124. showCancelBtn: true,
  125. submitLoading: false
  126. }
  127. }
  128. })
  129. const groupFilterText = ref('')
  130. const treeRef = ref<InstanceType<typeof ElTree>>()
  131. const replaceChildren = arr => {
  132. arr.forEach(item => {
  133. item.label = item.name
  134. item.value = item.id
  135. if (item.children && item.children.length > 0) {
  136. replaceChildren(item.children)
  137. }
  138. })
  139. return arr
  140. }
  141. // table列表数据请求
  142. const queryList = async () => {
  143. const { options, searchParams } = tableOpts
  144. options.loading = true
  145. try {
  146. const list = await department.departmentPageApi(searchParams)
  147. // tableOpts.total = total
  148. tableOpts.list = list
  149. formsDialog.value[0].options = list
  150. // const test = replaceChildren(list)
  151. // console.log(test);
  152. // formsDialog.value[1].options = list
  153. } catch {
  154. console.log('获取列表数据失败')
  155. // tableOpts.total = 0
  156. tableOpts.list = []
  157. options.loading = false // 更改加载中的 loading值
  158. } finally {
  159. options.loading = false
  160. }
  161. }
  162. const selectionChange = e => {
  163. // console.error('click 测试', e)
  164. curSelectionRows.value = e
  165. }
  166. // 表格搜索条件
  167. const forms = ref([
  168. {
  169. prop: 'keyword',
  170. label: '角色名称:',
  171. itemType: 'input',
  172. placeholder: '请输入角色名称'
  173. }
  174. ])
  175. // table 参数
  176. const columns = [
  177. {
  178. prop: 'name',
  179. label: '部门名称',
  180. minWidth: 250
  181. },
  182. {
  183. prop: 'code',
  184. label: '编码',
  185. minWidth: 100
  186. },
  187. {
  188. prop: 'status',
  189. label: '状态',
  190. minWidth: 50,
  191. slots: {
  192. default: 'statusSlot'
  193. }
  194. },
  195. {
  196. prop: 'secretKey',
  197. label: '密钥',
  198. minWidth: 100
  199. },
  200. {
  201. prop: 'sort',
  202. label: '排序',
  203. minWidth: 80
  204. },
  205. {
  206. prop: 'updateBy',
  207. label: '修改人',
  208. minWidth: 100
  209. },
  210. {
  211. prop: 'updateTime',
  212. label: '修改时间',
  213. minWidth: 150
  214. },
  215. {
  216. prop: 'createBy',
  217. label: '创建人',
  218. minWidth: 100
  219. },
  220. {
  221. prop: 'createTime',
  222. label: '创建时间',
  223. minWidth: 150
  224. },
  225. {
  226. prop: 'action',
  227. label: '操作',
  228. width: 100,
  229. fixed: 'right',
  230. slots: {
  231. default: 'actionSlot'
  232. }
  233. }
  234. ]
  235. const { searchData, tableOpts, checkedColumns, activeColumns, updateParams } = useTablePage(
  236. {
  237. options: {
  238. showIndex: false,
  239. defaultExpandAll: true,
  240. onSelectionChange: selectionChange
  241. },
  242. // 需要展示的列
  243. columns,
  244. // 控制列配置
  245. columnsConfig: {
  246. columns
  247. }
  248. },
  249. {
  250. queryList,
  251. fetchImmediate: false
  252. }
  253. )
  254. // 删除
  255. const deleteItem = async ids => {
  256. try {
  257. await department.departmentDeleteApi(ids)
  258. updateParams()
  259. } catch (e) {
  260. console.log('删除失败')
  261. ElMessage.error(`删除失败~`)
  262. }
  263. }
  264. // 单个删除
  265. const table_del = row => {
  266. deleteItem([row.id])
  267. }
  268. //批量删除
  269. const batch_del = () => {
  270. const ids = curSelectionRows.value.map(item => item.id) // 多选数据
  271. deleteItem(ids)
  272. }
  273. const table_edit = async row => {
  274. isCreate.value = false
  275. activeData.value = { ...row, status: row.status ? true : false }
  276. visible.value = true
  277. }
  278. // 弹窗事件
  279. const submitHandler = async params => {
  280. formOptions.value.formConfig.submitLoading = true
  281. try {
  282. params.status = params.status ? 1 : 0
  283. params.id = activeData.value.id ? activeData.value.id : null
  284. await department.departmentAddOrEditSaveApi(params)
  285. ElMessage.success(`${isCreate.value ? '新增' : '修改'}成功~`)
  286. visible.value = false
  287. updateParams()
  288. formOptions.value.formConfig.submitLoading = false
  289. } catch (e) {
  290. console.log(e)
  291. formOptions.value.formConfig.submitLoading = false
  292. }
  293. }
  294. const addHandler = () => {
  295. isCreate.value = true
  296. activeData.value = {}
  297. visible.value = true
  298. }
  299. nextTick(() => {
  300. queryList()
  301. })
  302. watch(groupFilterText, val => {
  303. treeRef.value!.filter(val)
  304. })
  305. </script>
  306. <style scoped lang="scss">
  307. .pageWrap {
  308. flex: 1;
  309. display: flex;
  310. height: 100%;
  311. //background: #fff;
  312. }
  313. .content-warp {
  314. flex: 1;
  315. //width: calc(100% - 250px);
  316. width: calc(100% - 210px);
  317. }
  318. // 单独自己写的
  319. /*:deep(.box-card) {
  320. height: 100%;
  321. .el-card__body {
  322. padding: 0;
  323. }
  324. }*/
  325. // 应用的树结构样式
  326. :deep(.menu-tree) {
  327. .el-tree-node__content {
  328. height: 36px;
  329. }
  330. .el-tree-node__content .el-tree-node__label .icon {
  331. margin-right: 5px;
  332. }
  333. }
  334. .nopadding {
  335. padding: 0px;
  336. }
  337. </style>