index.vue 8.8 KB

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