index.vue 12 KB

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