index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. <template>
  2. <div class="flex">
  3. <el-card class="w-1/5 user" :gutter="12" shadow="always">
  4. <template #header>
  5. <div class="card-header">
  6. <span>部门列表</span>
  7. <XTextButton title="修改部门" @click="handleDeptEdit()" />
  8. </div>
  9. </template>
  10. <el-input v-model="filterText" placeholder="搜索部门" />
  11. <el-scrollbar height="650">
  12. <el-tree
  13. ref="treeRef"
  14. node-key="id"
  15. default-expand-all
  16. :data="deptOptions"
  17. :props="defaultProps"
  18. :highlight-current="true"
  19. :filter-node-method="filterNode"
  20. :expand-on-click-node="false"
  21. @node-click="handleDeptNodeClick"
  22. />
  23. </el-scrollbar>
  24. </el-card>
  25. <el-card class="w-4/5 user" style="margin-left: 10px" :gutter="12" shadow="hover">
  26. <template #header>
  27. <div class="card-header">
  28. <span>{{ tableTitle }}</span>
  29. </div>
  30. </template>
  31. <!-- 列表 -->
  32. <XTable @register="registerTable">
  33. <template #toolbar_buttons>
  34. <!-- 操作:新增 -->
  35. <XButton
  36. type="primary"
  37. preIcon="ep:zoom-in"
  38. :title="t('action.add')"
  39. v-hasPermi="['system:user:create']"
  40. @click="handleCreate()"
  41. />
  42. <!-- 操作:导入用户 -->
  43. <XButton
  44. type="warning"
  45. preIcon="ep:upload"
  46. :title="t('action.import')"
  47. v-hasPermi="['system:user:import']"
  48. @click="importDialogVisible = true"
  49. />
  50. <!-- 操作:导出用户 -->
  51. <XButton
  52. type="warning"
  53. preIcon="ep:download"
  54. :title="t('action.export')"
  55. v-hasPermi="['system:user:export']"
  56. @click="exportList('用户数据.xls')"
  57. />
  58. </template>
  59. <template #status_default="{ row }">
  60. <el-switch
  61. v-model="row.status"
  62. :active-value="0"
  63. :inactive-value="1"
  64. @change="handleStatusChange(row)"
  65. />
  66. </template>
  67. <template #actionbtns_default="{ row }">
  68. <!-- 操作:编辑 -->
  69. <XTextButton
  70. preIcon="ep:edit"
  71. :title="t('action.edit')"
  72. v-hasPermi="['system:user:update']"
  73. @click="handleUpdate(row.id)"
  74. />
  75. <!-- 操作:详情 -->
  76. <XTextButton
  77. preIcon="ep:view"
  78. :title="t('action.detail')"
  79. v-hasPermi="['system:user:update']"
  80. @click="handleDetail(row.id)"
  81. />
  82. <el-dropdown
  83. class="p-0.5"
  84. v-hasPermi="[
  85. 'system:user:update-password',
  86. 'system:permission:assign-user-role',
  87. 'system:user:delete'
  88. ]"
  89. >
  90. <XTextButton :title="t('action.more')" postIcon="ep:arrow-down" />
  91. <template #dropdown>
  92. <el-dropdown-menu>
  93. <el-dropdown-item>
  94. <!-- 操作:重置密码 -->
  95. <XTextButton
  96. preIcon="ep:key"
  97. title="重置密码"
  98. v-hasPermi="['system:user:update-password']"
  99. @click="handleResetPwd(row)"
  100. />
  101. </el-dropdown-item>
  102. <el-dropdown-item>
  103. <!-- 操作:分配角色 -->
  104. <XTextButton
  105. preIcon="ep:key"
  106. title="分配角色"
  107. v-hasPermi="['system:permission:assign-user-role']"
  108. @click="handleRole(row)"
  109. />
  110. </el-dropdown-item>
  111. <el-dropdown-item>
  112. <!-- 操作:删除 -->
  113. <XTextButton
  114. preIcon="ep:delete"
  115. :title="t('action.del')"
  116. v-hasPermi="['system:user:delete']"
  117. @click="deleteData(row.id)"
  118. />
  119. </el-dropdown-item>
  120. </el-dropdown-menu>
  121. </template>
  122. </el-dropdown>
  123. </template>
  124. </XTable>
  125. </el-card>
  126. </div>
  127. <XModal v-model="dialogVisible" :title="dialogTitle">
  128. <!-- 对话框(添加 / 修改) -->
  129. <Form
  130. v-if="['create', 'update'].includes(actionType)"
  131. :rules="rules"
  132. :schema="allSchemas.formSchema"
  133. ref="formRef"
  134. >
  135. <template #deptId="form">
  136. <el-tree-select
  137. node-key="id"
  138. v-model="form['deptId']"
  139. :props="defaultProps"
  140. :data="deptOptions"
  141. check-strictly
  142. />
  143. </template>
  144. <template #postIds="form">
  145. <el-select v-model="form['postIds']" multiple :placeholder="t('common.selectText')">
  146. <el-option
  147. v-for="item in postOptions"
  148. :key="item.id"
  149. :label="item.name"
  150. :value="(item.id as unknown as number)"
  151. />
  152. </el-select>
  153. </template>
  154. </Form>
  155. <!-- 对话框(详情) -->
  156. <Descriptions
  157. v-if="actionType === 'detail'"
  158. :schema="allSchemas.detailSchema"
  159. :data="detailData"
  160. >
  161. <template #deptId="{ row }">
  162. <el-tag>{{ dataFormater(row.deptId) }}</el-tag>
  163. </template>
  164. <template #postIds="{ row }">
  165. <template v-if="row.postIds !== ''">
  166. <el-tag v-for="(post, index) in row.postIds" :key="index" index="">
  167. <template v-for="postObj in postOptions">
  168. {{ post === postObj.id ? postObj.name : '' }}
  169. </template>
  170. </el-tag>
  171. </template>
  172. <template v-else> </template>
  173. </template>
  174. </Descriptions>
  175. <!-- 操作按钮 -->
  176. <template #footer>
  177. <!-- 按钮:保存 -->
  178. <XButton
  179. v-if="['create', 'update'].includes(actionType)"
  180. type="primary"
  181. :title="t('action.save')"
  182. :loading="loading"
  183. @click="submitForm()"
  184. />
  185. <!-- 按钮:关闭 -->
  186. <XButton :loading="loading" :title="t('dialog.close')" @click="dialogVisible = false" />
  187. </template>
  188. </XModal>
  189. <!-- 分配用户角色 -->
  190. <XModal v-model="roleDialogVisible" title="分配角色">
  191. <el-form :model="userRole" label-width="140px" :inline="true">
  192. <el-form-item label="用户名称">
  193. <el-tag>{{ userRole.username }}</el-tag>
  194. </el-form-item>
  195. <el-form-item label="用户昵称">
  196. <el-tag>{{ userRole.nickname }}</el-tag>
  197. </el-form-item>
  198. <el-form-item label="角色">
  199. <el-transfer
  200. v-model="userRole.roleIds"
  201. :titles="['角色列表', '已选择']"
  202. :props="{
  203. key: 'id',
  204. label: 'name'
  205. }"
  206. :data="roleOptions"
  207. />
  208. </el-form-item>
  209. </el-form>
  210. <!-- 操作按钮 -->
  211. <template #footer>
  212. <!-- 按钮:保存 -->
  213. <XButton type="primary" :title="t('action.save')" :loading="loading" @click="submitRole()" />
  214. <!-- 按钮:关闭 -->
  215. <XButton :title="t('dialog.close')" @click="roleDialogVisible = false" />
  216. </template>
  217. </XModal>
  218. <!-- 导入 -->
  219. <XModal v-model="importDialogVisible" :title="importDialogTitle">
  220. <el-form class="drawer-multiColumn-form" label-width="150px">
  221. <el-form-item label="模板下载 :">
  222. <XButton type="primary" prefix="ep:download" title="点击下载" @click="handleImportTemp()" />
  223. </el-form-item>
  224. <el-form-item label="文件上传 :">
  225. <el-upload
  226. ref="uploadRef"
  227. :action="updateUrl + '?updateSupport=' + updateSupport"
  228. :headers="uploadHeaders"
  229. :drag="true"
  230. :limit="1"
  231. :multiple="true"
  232. :show-file-list="true"
  233. :disabled="uploadDisabled"
  234. :before-upload="beforeExcelUpload"
  235. :on-exceed="handleExceed"
  236. :on-success="handleFileSuccess"
  237. :on-error="excelUploadError"
  238. :auto-upload="false"
  239. accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  240. >
  241. <Icon icon="ep:upload-filled" />
  242. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  243. <template #tip>
  244. <div class="el-upload__tip">请上传 .xls , .xlsx 标准格式文件</div>
  245. </template>
  246. </el-upload>
  247. </el-form-item>
  248. <el-form-item label="是否更新已经存在的用户数据:">
  249. <el-checkbox v-model="updateSupport" />
  250. </el-form-item>
  251. </el-form>
  252. <template #footer>
  253. <!-- 按钮:保存 -->
  254. <XButton
  255. type="warning"
  256. preIcon="ep:upload-filled"
  257. :title="t('action.save')"
  258. @click="submitFileForm()"
  259. />
  260. <!-- 按钮:关闭 -->
  261. <XButton :title="t('dialog.close')" @click="importDialogVisible = false" />
  262. </template>
  263. </XModal>
  264. </template>
  265. <script setup lang="ts" name="User">
  266. import type { ElTree, UploadRawFile, UploadInstance } from 'element-plus'
  267. import { handleTree, defaultProps } from '@/utils/tree'
  268. import download from '@/utils/download'
  269. import { CommonStatusEnum } from '@/utils/constants'
  270. import { getAccessToken, getTenantId } from '@/utils/auth'
  271. import type { FormExpose } from '@/components/Form'
  272. import { rules, allSchemas } from './user.data'
  273. import * as UserApi from '@/api/system/user'
  274. import { listSimpleDeptApi } from '@/api/system/dept'
  275. import { listSimpleRolesApi } from '@/api/system/role'
  276. import { listSimplePostsApi, PostVO } from '@/api/system/post'
  277. import {
  278. aassignUserRoleApi,
  279. listUserRolesApi,
  280. PermissionAssignUserRoleReqVO
  281. } from '@/api/system/permission'
  282. const { t } = useI18n() // 国际化
  283. const message = useMessage() // 消息弹窗
  284. const queryParams = reactive({
  285. deptId: null
  286. })
  287. // ========== 列表相关 ==========
  288. const tableTitle = ref('用户列表')
  289. // 列表相关的变量
  290. const [registerTable, { reload, deleteData, exportList }] = useXTable({
  291. allSchemas: allSchemas,
  292. params: queryParams,
  293. getListApi: UserApi.getUserPageApi,
  294. deleteApi: UserApi.deleteUserApi,
  295. exportListApi: UserApi.exportUserApi
  296. })
  297. // ========== 创建部门树结构 ==========
  298. const filterText = ref('')
  299. const deptOptions = ref<Tree[]>([]) // 树形结构
  300. const treeRef = ref<InstanceType<typeof ElTree>>()
  301. const getTree = async () => {
  302. const res = await listSimpleDeptApi()
  303. deptOptions.value.push(...handleTree(res))
  304. }
  305. const filterNode = (value: string, data: Tree) => {
  306. if (!value) return true
  307. return data.name.includes(value)
  308. }
  309. const handleDeptNodeClick = async (row: { [key: string]: any }) => {
  310. queryParams.deptId = row.id
  311. await reload()
  312. }
  313. const { push } = useRouter()
  314. const handleDeptEdit = () => {
  315. push('/system/dept')
  316. }
  317. watch(filterText, (val) => {
  318. treeRef.value!.filter(val)
  319. })
  320. // ========== CRUD 相关 ==========
  321. const loading = ref(false) // 遮罩层
  322. const actionType = ref('') // 操作按钮的类型
  323. const dialogVisible = ref(false) // 是否显示弹出层
  324. const dialogTitle = ref('edit') // 弹出层标题
  325. const formRef = ref<FormExpose>() // 表单 Ref
  326. const postOptions = ref<PostVO[]>([]) //岗位列表
  327. // 获取岗位列表
  328. const getPostOptions = async () => {
  329. const res = await listSimplePostsApi()
  330. postOptions.value.push(...res)
  331. }
  332. const dataFormater = (val) => {
  333. return deptFormater(deptOptions.value, val)
  334. }
  335. //部门回显
  336. const deptFormater = (ary, val: any) => {
  337. var o = ''
  338. if (ary && val) {
  339. for (const v of ary) {
  340. if (v.id == val) {
  341. o = v.name
  342. if (o) return o
  343. } else if (v.children?.length) {
  344. o = deptFormater(v.children, val)
  345. if (o) return o
  346. }
  347. }
  348. return o
  349. } else {
  350. return val
  351. }
  352. }
  353. // 设置标题
  354. const setDialogTile = async (type: string) => {
  355. dialogTitle.value = t('action.' + type)
  356. actionType.value = type
  357. dialogVisible.value = true
  358. }
  359. // 新增操作
  360. const handleCreate = async () => {
  361. setDialogTile('create')
  362. // 重置表单
  363. await nextTick()
  364. if (allSchemas.formSchema[0].field !== 'username') {
  365. unref(formRef)?.addSchema(
  366. {
  367. field: 'username',
  368. label: '用户账号',
  369. component: 'Input'
  370. },
  371. 0
  372. )
  373. unref(formRef)?.addSchema(
  374. {
  375. field: 'password',
  376. label: '用户密码',
  377. component: 'InputPassword'
  378. },
  379. 1
  380. )
  381. }
  382. }
  383. // 修改操作
  384. const handleUpdate = async (rowId: number) => {
  385. setDialogTile('update')
  386. await nextTick()
  387. unref(formRef)?.delSchema('username')
  388. unref(formRef)?.delSchema('password')
  389. // 设置数据
  390. const res = await UserApi.getUserApi(rowId)
  391. unref(formRef)?.setValues(res)
  392. }
  393. const detailData = ref()
  394. // 详情操作
  395. const handleDetail = async (rowId: number) => {
  396. // 设置数据
  397. const res = await UserApi.getUserApi(rowId)
  398. detailData.value = res
  399. await setDialogTile('detail')
  400. }
  401. // 提交按钮
  402. const submitForm = async () => {
  403. // 提交请求
  404. const elForm = unref(formRef)?.getElFormRef()
  405. if (!elForm) return
  406. elForm.validate(async (valid) => {
  407. if (valid) {
  408. try {
  409. const data = unref(formRef)?.formModel as UserApi.UserVO
  410. if (actionType.value === 'create') {
  411. await UserApi.createUserApi(data)
  412. loading.value = true
  413. message.success(t('common.createSuccess'))
  414. } else {
  415. await UserApi.updateUserApi(data)
  416. loading.value = true
  417. message.success(t('common.updateSuccess'))
  418. }
  419. dialogVisible.value = false
  420. } finally {
  421. // unref(formRef)?.setSchema(allSchemas.formSchema)
  422. // 刷新列表
  423. await reload()
  424. loading.value = false
  425. }
  426. }
  427. })
  428. }
  429. // 改变用户状态操作
  430. const handleStatusChange = async (row: UserApi.UserVO) => {
  431. const text = row.status === CommonStatusEnum.ENABLE ? '启用' : '停用'
  432. message
  433. .confirm('确认要"' + text + '""' + row.username + '"用户吗?', t('common.reminder'))
  434. .then(async () => {
  435. row.status =
  436. row.status === CommonStatusEnum.ENABLE ? CommonStatusEnum.ENABLE : CommonStatusEnum.DISABLE
  437. await UserApi.updateUserStatusApi(row.id, row.status)
  438. message.success(text + '成功')
  439. // 刷新列表
  440. await reload()
  441. })
  442. .catch(() => {
  443. row.status =
  444. row.status === CommonStatusEnum.ENABLE ? CommonStatusEnum.DISABLE : CommonStatusEnum.ENABLE
  445. })
  446. }
  447. // 重置密码
  448. const handleResetPwd = (row: UserApi.UserVO) => {
  449. message.prompt('请输入"' + row.username + '"的新密码', t('common.reminder')).then(({ value }) => {
  450. UserApi.resetUserPwdApi(row.id, value).then(() => {
  451. message.success('修改成功,新密码是:' + value)
  452. })
  453. })
  454. }
  455. // 分配角色
  456. const roleDialogVisible = ref(false)
  457. const roleOptions = ref()
  458. const userRole = reactive({
  459. id: 0,
  460. username: '',
  461. nickname: '',
  462. roleIds: []
  463. })
  464. const handleRole = async (row: UserApi.UserVO) => {
  465. userRole.id = row.id
  466. userRole.username = row.username
  467. userRole.nickname = row.nickname
  468. // 获得角色拥有的权限集合
  469. const roles = await listUserRolesApi(row.id)
  470. userRole.roleIds = roles
  471. // 获取角色列表
  472. const roleOpt = await listSimpleRolesApi()
  473. roleOptions.value = roleOpt
  474. roleDialogVisible.value = true
  475. }
  476. // 提交
  477. const submitRole = async () => {
  478. const data = ref<PermissionAssignUserRoleReqVO>({
  479. userId: userRole.id,
  480. roleIds: userRole.roleIds
  481. })
  482. await aassignUserRoleApi(data.value)
  483. message.success(t('common.updateSuccess'))
  484. roleDialogVisible.value = false
  485. }
  486. // ========== 导入相关 ==========
  487. // TODO @星语:这个要不要把导入用户,封装成一个小组件?可选哈
  488. const importDialogVisible = ref(false)
  489. const uploadDisabled = ref(false)
  490. const importDialogTitle = ref('用户导入')
  491. const updateSupport = ref(0)
  492. let updateUrl = import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/system/user/import'
  493. const uploadHeaders = ref()
  494. // 下载导入模版
  495. const handleImportTemp = async () => {
  496. const res = await UserApi.importUserTemplateApi()
  497. download.excel(res, '用户导入模版.xls')
  498. }
  499. // 文件上传之前判断
  500. const beforeExcelUpload = (file: UploadRawFile) => {
  501. const isExcel =
  502. file.type === 'application/vnd.ms-excel' ||
  503. file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  504. const isLt5M = file.size / 1024 / 1024 < 5
  505. if (!isExcel) message.error('上传文件只能是 xls / xlsx 格式!')
  506. if (!isLt5M) message.error('上传文件大小不能超过 5MB!')
  507. return isExcel && isLt5M
  508. }
  509. // 文件上传
  510. const uploadRef = ref<UploadInstance>()
  511. const submitFileForm = () => {
  512. uploadHeaders.value = {
  513. Authorization: 'Bearer ' + getAccessToken(),
  514. 'tenant-id': getTenantId()
  515. }
  516. uploadDisabled.value = true
  517. uploadRef.value!.submit()
  518. }
  519. // 文件上传成功
  520. const handleFileSuccess = async (response: any): Promise<void> => {
  521. if (response.code !== 0) {
  522. message.error(response.msg)
  523. return
  524. }
  525. importDialogVisible.value = false
  526. uploadDisabled.value = false
  527. const data = response.data
  528. let text = '上传成功数量:' + data.createUsernames.length + ';'
  529. for (let username of data.createUsernames) {
  530. text += '< ' + username + ' >'
  531. }
  532. text += '更新成功数量:' + data.updateUsernames.length + ';'
  533. for (const username of data.updateUsernames) {
  534. text += '< ' + username + ' >'
  535. }
  536. text += '更新失败数量:' + Object.keys(data.failureUsernames).length + ';'
  537. for (const username in data.failureUsernames) {
  538. text += '< ' + username + ': ' + data.failureUsernames[username] + ' >'
  539. }
  540. message.alert(text)
  541. await reload()
  542. }
  543. // 文件数超出提示
  544. const handleExceed = (): void => {
  545. message.error('最多只能上传一个文件!')
  546. }
  547. // 上传错误提示
  548. const excelUploadError = (): void => {
  549. message.error('导入数据失败,请您重新上传!')
  550. }
  551. // ========== 初始化 ==========
  552. onMounted(async () => {
  553. await getPostOptions()
  554. await getTree()
  555. })
  556. </script>
  557. <style scoped>
  558. .user {
  559. height: 780px;
  560. max-height: 800px;
  561. }
  562. .card-header {
  563. display: flex;
  564. justify-content: space-between;
  565. align-items: center;
  566. }
  567. </style>