index.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <template>
  2. <div class="pageWrap bgc">
  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, ElMessageBox, 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. import user from '@/api/system/user'
  68. const visible = ref(false) // 弹窗显示隐藏
  69. const isCreate = ref(true)
  70. const activeData = ref({})
  71. const curSelectionRows = ref([]) // 表格选中数据
  72. const formsDialog = ref([
  73. {
  74. prop: 'pid',
  75. label: '上级部门',
  76. itemType: 'cascader',
  77. props: {
  78. value: 'id',
  79. label: 'name',
  80. emitPath: false,
  81. checkStrictly: true
  82. },
  83. options: []
  84. },
  85. {
  86. prop: 'name',
  87. label: '名称',
  88. itemType: 'input',
  89. rules: [{ required: true, message: '请输入名称', trigger: 'blur' }]
  90. },
  91. {
  92. prop: 'code',
  93. label: '编码',
  94. itemType: 'input',
  95. rules: [{ required: true, message: '请输入编码', trigger: 'blur' }]
  96. },
  97. {
  98. prop: 'sort',
  99. label: '排序',
  100. itemType: 'inputNumber',
  101. rules: [{ required: true, message: '请输入排序', trigger: 'blur' }]
  102. },
  103. {
  104. prop: 'status',
  105. label: '状态',
  106. itemType: 'switch',
  107. activeText: '正常',
  108. inactiveText: '禁用'
  109. },
  110. {
  111. prop: 'remark',
  112. label: '备注',
  113. itemType: 'input',
  114. rules: [{ required: true, message: '请输入备注', trigger: 'blur' }]
  115. },
  116. {
  117. prop: 'headId',
  118. label: '主管',
  119. itemType: 'select',
  120. filterable: true,
  121. options: []
  122. }
  123. ])
  124. // 新增的表单 和 编辑的表单
  125. const formOptions = computed(() => {
  126. return {
  127. forms: formsDialog.value,
  128. labelWidth: 120,
  129. span: 30,
  130. showResetBtn: true,
  131. formConfig: {
  132. showCancelBtn: true,
  133. submitLoading: false
  134. }
  135. }
  136. })
  137. const groupFilterText = ref('')
  138. const treeRef = ref<InstanceType<typeof ElTree>>()
  139. const replaceChildren = arr => {
  140. arr.forEach(item => {
  141. item.label = item.name
  142. item.value = item.id
  143. if (item.children && item.children.length > 0) {
  144. replaceChildren(item.children)
  145. }
  146. })
  147. return arr
  148. }
  149. // table列表数据请求
  150. const queryList = async () => {
  151. const { options, searchParams } = tableOpts
  152. options.loading = true
  153. try {
  154. const list = await department.departmentPageApi(searchParams)
  155. // tableOpts.total = total
  156. tableOpts.list = list
  157. formsDialog.value[0].options = list
  158. // const test = replaceChildren(list)
  159. // console.log(test);
  160. // formsDialog.value[1].options = list
  161. } catch {
  162. console.log('获取列表数据失败')
  163. // tableOpts.total = 0
  164. tableOpts.list = []
  165. options.loading = false // 更改加载中的 loading值
  166. } finally {
  167. options.loading = false
  168. }
  169. }
  170. const selectionChange = e => {
  171. // console.error('click 测试', e)
  172. curSelectionRows.value = e
  173. }
  174. // 表格搜索条件
  175. const forms = ref([
  176. {
  177. prop: 'keyword',
  178. label: '角色名称:',
  179. itemType: 'input',
  180. placeholder: '请输入角色名称'
  181. },
  182. {
  183. prop: 'status',
  184. label: '状态:',
  185. itemType: 'select',
  186. placeholder: '请选择状态',
  187. options: [
  188. {
  189. label: '禁用',
  190. value: 0
  191. },
  192. {
  193. label: '正常',
  194. value: 1
  195. }
  196. ]
  197. }
  198. ])
  199. // table 参数
  200. const columns = [
  201. {
  202. prop: 'name',
  203. label: '部门名称',
  204. minWidth: 250
  205. },
  206. {
  207. prop: 'code',
  208. label: '编码',
  209. minWidth: 100
  210. },
  211. {
  212. prop: 'status',
  213. label: '状态',
  214. minWidth: 50,
  215. slots: {
  216. default: 'statusSlot'
  217. }
  218. },
  219. {
  220. prop: 'remark',
  221. label: '备注',
  222. minWidth: 100
  223. },
  224. {
  225. prop: 'sort',
  226. label: '排序',
  227. minWidth: 80
  228. },
  229. {
  230. prop: 'updateBy',
  231. label: '修改人',
  232. minWidth: 100
  233. },
  234. {
  235. prop: 'updateTime',
  236. label: '修改时间',
  237. minWidth: 150
  238. },
  239. {
  240. prop: 'createBy',
  241. label: '创建人',
  242. minWidth: 100
  243. },
  244. {
  245. prop: 'createTime',
  246. label: '创建时间',
  247. minWidth: 150
  248. },
  249. {
  250. prop: 'action',
  251. label: '操作',
  252. width: 100,
  253. fixed: 'right',
  254. slots: {
  255. default: 'actionSlot'
  256. }
  257. }
  258. ]
  259. const { searchData, tableOpts, checkedColumns, activeColumns, updateParams } = useTablePage(
  260. {
  261. options: {
  262. showIndex: false,
  263. defaultExpandAll: false,
  264. onSelectionChange: selectionChange
  265. },
  266. // 需要展示的列
  267. columns,
  268. // 控制列配置
  269. columnsConfig: {
  270. columns
  271. }
  272. },
  273. {
  274. queryList,
  275. fetchImmediate: false
  276. }
  277. )
  278. // 删除
  279. const deleteItem = async ids => {
  280. // try {
  281. await department.departmentDeleteApi(ids)
  282. ElMessage.success(`删除成功~`)
  283. updateParams()
  284. // } catch (e) {
  285. // console.log('删除失败')
  286. // ElMessage.error(`删除失败~`)
  287. // }
  288. }
  289. // 单个删除
  290. const table_del = row => {
  291. deleteItem([row.id])
  292. }
  293. //批量删除
  294. const batch_del = () => {
  295. const ids = curSelectionRows.value.map(item => item.id) // 多选数据
  296. ElMessageBox.confirm('是否删除选中数据?', '提示', {
  297. confirmButtonText: '确认',
  298. cancelButtonText: '取消',
  299. type: 'error',
  300. buttonSize: 'default'
  301. }).then(() => {
  302. deleteItem(ids)
  303. })
  304. }
  305. const table_edit = async row => {
  306. isCreate.value = false
  307. activeData.value = { ...row, status: row.status ? true : false }
  308. visible.value = true
  309. }
  310. // 弹窗事件
  311. const submitHandler = async params => {
  312. formOptions.value.formConfig.submitLoading = true
  313. try {
  314. params.status = params.status ? 1 : 0
  315. params.id = activeData.value.id ? activeData.value.id : null
  316. params.headName = formOptions.value.forms[6].options.find(item => item.value === params.headId).label
  317. await department.departmentAddOrEditSaveApi(params)
  318. ElMessage.success(`${isCreate.value ? '新增' : '修改'}成功~`)
  319. visible.value = false
  320. updateParams()
  321. formOptions.value.formConfig.submitLoading = false
  322. } catch (e) {
  323. console.log(e)
  324. formOptions.value.formConfig.submitLoading = false
  325. }
  326. }
  327. // 获取全部的人员信息
  328. const getUserList = async () => {
  329. try {
  330. var { records } = await user.userPageApi({
  331. page: 1,
  332. pageSize: 9999
  333. })
  334. formOptions.value.forms[6].options = records.map(item => {
  335. return { value: item.id, label: item.realName }
  336. })
  337. } catch (e) {
  338. console.log(e)
  339. }
  340. }
  341. const addHandler = () => {
  342. isCreate.value = true
  343. activeData.value = {}
  344. visible.value = true
  345. }
  346. nextTick(() => {
  347. queryList()
  348. getUserList()
  349. })
  350. watch(groupFilterText, val => {
  351. treeRef.value!.filter(val)
  352. })
  353. </script>