index.vue 8.9 KB

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