index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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-tree
  12. ref="treeRef"
  13. node-key="id"
  14. default-expand-all
  15. :data="deptOptions"
  16. :props="defaultProps"
  17. :highlight-current="true"
  18. :filter-node-method="filterNode"
  19. :expand-on-click-node="false"
  20. @node-click="handleDeptNodeClick"
  21. />
  22. </el-card>
  23. <el-card class="w-4/5 user" style="margin-left: 10px" :gutter="12" shadow="hover">
  24. <template #header>
  25. <div class="card-header">
  26. <span>{{ tableTitle }}</span>
  27. </div>
  28. </template>
  29. <!-- 列表 -->
  30. <vxe-grid ref="xGrid" v-bind="gridOptions" class="xtable-scrollbar">
  31. <template #toolbar_buttons>
  32. <!-- 操作:新增 -->
  33. <XButton
  34. type="primary"
  35. preIcon="ep:zoom-in"
  36. :title="t('action.add')"
  37. v-hasPermi="['system:user:create']"
  38. @click="handleCreate()"
  39. />
  40. <!-- 操作:导入用户 -->
  41. <XButton
  42. type="warning"
  43. preIcon="ep:upload"
  44. :title="t('action.import')"
  45. v-hasPermi="['system:user:import']"
  46. @click="importDialogVisible = true"
  47. />
  48. <!-- 操作:导出用户 -->
  49. <XButton
  50. type="warning"
  51. preIcon="ep:download"
  52. :title="t('action.export')"
  53. v-hasPermi="['system:user:export']"
  54. @click="exportList('用户数据.xls')"
  55. />
  56. </template>
  57. <template #status_default="{ row }">
  58. <el-switch
  59. v-model="row.status"
  60. :active-value="0"
  61. :inactive-value="1"
  62. @change="handleStatusChange(row)"
  63. />
  64. </template>
  65. <template #actionbtns_default="{ row }">
  66. <!-- 操作:编辑 -->
  67. <XTextButton
  68. preIcon="ep:edit"
  69. :title="t('action.edit')"
  70. v-hasPermi="['system:user:update']"
  71. @click="handleUpdate(row.id)"
  72. />
  73. <!-- 操作:详情 -->
  74. <XTextButton
  75. preIcon="ep:view"
  76. :title="t('action.detail')"
  77. v-hasPermi="['system:user:update']"
  78. @click="handleDetail(row.id)"
  79. />
  80. <el-dropdown
  81. class="p-0.5"
  82. v-hasPermi="[
  83. 'system:user:update-password',
  84. 'system:permission:assign-user-role',
  85. 'system:user:delete'
  86. ]"
  87. >
  88. <XTextButton :title="t('action.more')" postIcon="ep:arrow-down" />
  89. <template #dropdown>
  90. <el-dropdown-menu>
  91. <el-dropdown-item>
  92. <!-- 操作:重置密码 -->
  93. <XTextButton
  94. preIcon="ep:key"
  95. title="重置密码"
  96. v-hasPermi="['system:user:update-password']"
  97. @click="handleResetPwd(row)"
  98. />
  99. </el-dropdown-item>
  100. <el-dropdown-item>
  101. <!-- 操作:分配角色 -->
  102. <XTextButton
  103. preIcon="ep:key"
  104. title="分配角色"
  105. v-hasPermi="['system:permission:assign-user-role']"
  106. @click="handleRole(row)"
  107. />
  108. </el-dropdown-item>
  109. <el-dropdown-item>
  110. <!-- 操作:删除 -->
  111. <XTextButton
  112. preIcon="ep:delete"
  113. :title="t('action.del')"
  114. v-hasPermi="['system:user:delete']"
  115. @click="handleDelete(row.id)"
  116. />
  117. </el-dropdown-item>
  118. </el-dropdown-menu>
  119. </template>
  120. </el-dropdown>
  121. </template>
  122. </vxe-grid>
  123. </el-card>
  124. </div>
  125. <XModal v-model="dialogVisible" :title="dialogTitle">
  126. <!-- 对话框(添加 / 修改) -->
  127. <Form
  128. v-if="['create', 'update'].includes(actionType)"
  129. :rules="rules"
  130. :schema="allSchemas.formSchema"
  131. ref="formRef"
  132. >
  133. <template #deptId="form">
  134. <el-tree-select
  135. node-key="id"
  136. v-model="form['deptId']"
  137. :props="defaultProps"
  138. :data="deptOptions"
  139. check-strictly
  140. />
  141. </template>
  142. <template #postIds="form">
  143. <el-select v-model="form['postIds']" multiple :placeholder="t('common.selectText')">
  144. <el-option
  145. v-for="item in postOptions"
  146. :key="item.id"
  147. :label="item.name"
  148. :value="(item.id as unknown as number)"
  149. />
  150. </el-select>
  151. </template>
  152. </Form>
  153. <!-- 对话框(详情) -->
  154. <Descriptions
  155. v-if="actionType === 'detail'"
  156. :schema="allSchemas.detailSchema"
  157. :data="detailData"
  158. >
  159. <template #deptId="{ row }">
  160. <span>{{ row.dept?.name }}</span>
  161. </template>
  162. <template #postIds="{ row }">
  163. <template v-if="row.postIds !== ''">
  164. <el-tag v-for="(post, index) in row.postIds" :key="index" index="">
  165. <template v-for="postObj in postOptions">
  166. {{ post === postObj.id ? postObj.name : '' }}
  167. </template>
  168. </el-tag>
  169. </template>
  170. <template v-else> </template>
  171. </template>
  172. </Descriptions>
  173. <!-- 操作按钮 -->
  174. <template #footer>
  175. <!-- 按钮:保存 -->
  176. <XButton
  177. v-if="['create', 'update'].includes(actionType)"
  178. type="primary"
  179. :title="t('action.save')"
  180. :loading="loading"
  181. @click="submitForm()"
  182. />
  183. <!-- 按钮:关闭 -->
  184. <XButton :loading="loading" :title="t('dialog.close')" @click="dialogVisible = false" />
  185. </template>
  186. </XModal>
  187. <!-- 分配用户角色 -->
  188. <XModal v-model="roleDialogVisible" title="分配角色">
  189. <el-form :model="userRole" label-width="140px" :inline="true">
  190. <el-form-item label="用户名称">
  191. <el-tag>{{ userRole.username }}</el-tag>
  192. </el-form-item>
  193. <el-form-item label="用户昵称">
  194. <el-tag>{{ userRole.nickname }}</el-tag>
  195. </el-form-item>
  196. <el-form-item label="角色">
  197. <el-transfer
  198. v-model="userRole.roleIds"
  199. :titles="['角色列表', '已选择']"
  200. :props="{
  201. key: 'id',
  202. label: 'name'
  203. }"
  204. :data="roleOptions"
  205. />
  206. </el-form-item>
  207. </el-form>
  208. <!-- 操作按钮 -->
  209. <template #footer>
  210. <!-- 按钮:保存 -->
  211. <XButton type="primary" :title="t('action.save')" :loading="loading" @click="submitRole()" />
  212. <!-- 按钮:关闭 -->
  213. <XButton :title="t('dialog.close')" @click="roleDialogVisible = false" />
  214. </template>
  215. </XModal>
  216. <!-- 导入 -->
  217. <XModal v-model="importDialogVisible" :title="importDialogTitle">
  218. <el-form class="drawer-multiColumn-form" label-width="150px">
  219. <el-form-item label="模板下载 :">
  220. <XButton type="primary" prefix="ep:download" title="点击下载" @click="handleImportTemp()" />
  221. </el-form-item>
  222. <el-form-item label="文件上传 :">
  223. <el-upload
  224. ref="uploadRef"
  225. :action="updateUrl + '?updateSupport=' + updateSupport"
  226. :headers="uploadHeaders"
  227. :drag="true"
  228. :limit="1"
  229. :multiple="true"
  230. :show-file-list="true"
  231. :disabled="uploadDisabled"
  232. :before-upload="beforeExcelUpload"
  233. :on-exceed="handleExceed"
  234. :on-success="handleFileSuccess"
  235. :on-error="excelUploadError"
  236. :auto-upload="false"
  237. accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  238. >
  239. <Icon icon="ep:upload-filled" />
  240. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  241. <template #tip>
  242. <div class="el-upload__tip">请上传 .xls , .xlsx 标准格式文件</div>
  243. </template>
  244. </el-upload>
  245. </el-form-item>
  246. <el-form-item label="是否更新已经存在的用户数据:">
  247. <el-checkbox v-model="updateSupport" />
  248. </el-form-item>
  249. </el-form>
  250. <template #footer>
  251. <!-- 按钮:保存 -->
  252. <XButton
  253. type="warning"
  254. preIcon="ep:upload-filled"
  255. :title="t('action.save')"
  256. @click="submitFileForm()"
  257. />
  258. <!-- 按钮:关闭 -->
  259. <XButton :title="t('dialog.close')" @click="importDialogVisible = false" />
  260. </template>
  261. </XModal>
  262. </template>
  263. <script setup lang="ts" name="User">
  264. import { nextTick, onMounted, reactive, ref, unref, watch } from 'vue'
  265. import {
  266. ElTag,
  267. ElInput,
  268. ElCard,
  269. ElTree,
  270. ElTreeSelect,
  271. ElSelect,
  272. ElOption,
  273. ElTransfer,
  274. ElForm,
  275. ElFormItem,
  276. ElUpload,
  277. ElSwitch,
  278. ElCheckbox,
  279. ElDropdown,
  280. ElDropdownMenu,
  281. ElDropdownItem,
  282. UploadInstance,
  283. UploadRawFile
  284. } from 'element-plus'
  285. import { useRouter } from 'vue-router'
  286. import { VxeGridInstance } from 'vxe-table'
  287. import { handleTree, defaultProps } from '@/utils/tree'
  288. import download from '@/utils/download'
  289. import { CommonStatusEnum } from '@/utils/constants'
  290. import { getAccessToken, getTenantId } from '@/utils/auth'
  291. import { useI18n } from '@/hooks/web/useI18n'
  292. import { useMessage } from '@/hooks/web/useMessage'
  293. import { useVxeGrid } from '@/hooks/web/useVxeGrid'
  294. import { FormExpose } from '@/components/Form'
  295. import { rules, allSchemas } from './user.data'
  296. import * as UserApi from '@/api/system/user'
  297. import { listSimpleDeptApi } from '@/api/system/dept'
  298. import { listSimpleRolesApi } from '@/api/system/role'
  299. import { listSimplePostsApi, PostVO } from '@/api/system/post'
  300. import {
  301. aassignUserRoleApi,
  302. listUserRolesApi,
  303. PermissionAssignUserRoleReqVO
  304. } from '@/api/system/permission'
  305. const { t } = useI18n() // 国际化
  306. const message = useMessage() // 消息弹窗
  307. const queryParams = reactive({
  308. deptId: null
  309. })
  310. // ========== 列表相关 ==========
  311. const tableTitle = ref('用户列表')
  312. // 列表相关的变量
  313. const xGrid = ref<VxeGridInstance>() // 列表 Grid Ref
  314. const { gridOptions, getList, deleteData, exportList } = useVxeGrid<UserApi.UserVO>({
  315. allSchemas: allSchemas,
  316. queryParams: queryParams,
  317. getListApi: UserApi.getUserPageApi,
  318. deleteApi: UserApi.deleteUserApi,
  319. exportListApi: UserApi.exportUserApi
  320. })
  321. // ========== 创建部门树结构 ==========
  322. const filterText = ref('')
  323. const deptOptions = ref<Tree[]>([]) // 树形结构
  324. const treeRef = ref<InstanceType<typeof ElTree>>()
  325. const getTree = async () => {
  326. const res = await listSimpleDeptApi()
  327. deptOptions.value.push(...handleTree(res))
  328. }
  329. const filterNode = (value: string, data: Tree) => {
  330. if (!value) return true
  331. return data.name.includes(value)
  332. }
  333. const handleDeptNodeClick = async (row: { [key: string]: any }) => {
  334. queryParams.deptId = row.id
  335. await getList(xGrid)
  336. }
  337. const { push } = useRouter()
  338. const handleDeptEdit = () => {
  339. push('/system/dept')
  340. }
  341. watch(filterText, (val) => {
  342. treeRef.value!.filter(val)
  343. })
  344. // ========== CRUD 相关 ==========
  345. const loading = ref(false) // 遮罩层
  346. const actionType = ref('') // 操作按钮的类型
  347. const dialogVisible = ref(false) // 是否显示弹出层
  348. const dialogTitle = ref('edit') // 弹出层标题
  349. const formRef = ref<FormExpose>() // 表单 Ref
  350. const postOptions = ref<PostVO[]>([]) //岗位列表
  351. // 获取岗位列表
  352. const getPostOptions = async () => {
  353. const res = await listSimplePostsApi()
  354. postOptions.value.push(...res)
  355. }
  356. // 设置标题
  357. const setDialogTile = async (type: string) => {
  358. dialogTitle.value = t('action.' + type)
  359. actionType.value = type
  360. dialogVisible.value = true
  361. }
  362. // 新增操作
  363. const handleCreate = async () => {
  364. setDialogTile('create')
  365. // 重置表单
  366. await nextTick()
  367. if (allSchemas.formSchema[0].field !== 'username') {
  368. unref(formRef)?.addSchema(
  369. {
  370. field: 'username',
  371. label: '用户账号',
  372. component: 'Input'
  373. },
  374. 0
  375. )
  376. unref(formRef)?.addSchema(
  377. {
  378. field: 'password',
  379. label: '用户密码',
  380. component: 'InputPassword'
  381. },
  382. 1
  383. )
  384. }
  385. }
  386. // 修改操作
  387. const handleUpdate = async (rowId: number) => {
  388. setDialogTile('update')
  389. await nextTick()
  390. unref(formRef)?.delSchema('username')
  391. unref(formRef)?.delSchema('password')
  392. // 设置数据
  393. const res = await UserApi.getUserApi(rowId)
  394. unref(formRef)?.setValues(res)
  395. }
  396. const detailData = ref()
  397. // 详情操作
  398. const handleDetail = async (rowId: number) => {
  399. // 设置数据
  400. const res = await UserApi.getUserApi(rowId)
  401. detailData.value = res
  402. await setDialogTile('detail')
  403. }
  404. // 删除操作
  405. const handleDelete = async (rowId: number) => {
  406. await deleteData(xGrid, rowId)
  407. }
  408. // 提交按钮
  409. const submitForm = async () => {
  410. loading.value = true
  411. // 提交请求
  412. try {
  413. const data = unref(formRef)?.formModel as UserApi.UserVO
  414. if (actionType.value === 'create') {
  415. await UserApi.createUserApi(data)
  416. message.success(t('common.createSuccess'))
  417. } else {
  418. await UserApi.updateUserApi(data)
  419. message.success(t('common.updateSuccess'))
  420. }
  421. dialogVisible.value = false
  422. } finally {
  423. // unref(formRef)?.setSchema(allSchemas.formSchema)
  424. // 刷新列表
  425. await getList(xGrid)
  426. loading.value = false
  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 getList(xGrid)
  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 getList(xGrid)
  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: 900px;
  560. max-height: 960px;
  561. }
  562. .card-header {
  563. display: flex;
  564. justify-content: space-between;
  565. align-items: center;
  566. }
  567. </style>