index.vue 10 KB

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