index.vue 11 KB

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