index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <script setup lang="ts">
  2. import { onMounted, ref, unref } from 'vue'
  3. import dayjs from 'dayjs'
  4. import {
  5. ElMessage,
  6. ElCard,
  7. ElTree,
  8. ElTreeSelect,
  9. ElSelect,
  10. ElOption,
  11. ElForm,
  12. ElFormItem,
  13. ElUpload,
  14. ElSwitch,
  15. ElCheckbox,
  16. ElMessageBox,
  17. UploadInstance,
  18. UploadRawFile
  19. } from 'element-plus'
  20. import { handleTree } from '@/utils/tree'
  21. import { DICT_TYPE } from '@/utils/dict'
  22. import { useI18n } from '@/hooks/web/useI18n'
  23. import { useTable } from '@/hooks/web/useTable'
  24. import { FormExpose } from '@/components/Form'
  25. import type { UserVO } from '@/api/system/user/types'
  26. import type { PostVO } from '@/api/system/post/types'
  27. import { listSimpleDeptApi } from '@/api/system/dept'
  28. import { listSimplePostsApi } from '@/api/system/post'
  29. import { rules, allSchemas } from './user.data'
  30. import * as UserApi from '@/api/system/user'
  31. import download from '@/utils/download'
  32. import { useCache } from '@/hooks/web/useCache'
  33. import { CommonStatusEnum } from '@/utils/constants'
  34. const { wsCache } = useCache()
  35. interface Tree {
  36. id: number
  37. name: string
  38. children?: Tree[]
  39. }
  40. const defaultProps = {
  41. children: 'children',
  42. label: 'name',
  43. value: 'id'
  44. }
  45. const { t } = useI18n() // 国际化
  46. // ========== 列表相关 ==========
  47. const tableTitle = ref('用户列表')
  48. const { register, tableObject, methods } = useTable<UserVO>({
  49. getListApi: UserApi.getUserPageApi,
  50. delListApi: UserApi.deleteUserApi,
  51. exportListApi: UserApi.exportUserApi
  52. })
  53. const { getList, setSearchParams, delList, exportList } = methods
  54. // ========== 创建部门树结构 ==========
  55. const deptOptions = ref([]) // 树形结构
  56. const searchForm = ref<FormExpose>()
  57. const treeRef = ref<InstanceType<typeof ElTree>>()
  58. const getTree = async () => {
  59. const res = await listSimpleDeptApi()
  60. deptOptions.value.push(...handleTree(res))
  61. }
  62. const filterNode = (value: string, data: Tree) => {
  63. if (!value) return true
  64. return data.name.includes(value)
  65. }
  66. const handleDeptNodeClick = (data: { [key: string]: any }) => {
  67. tableObject.params = {
  68. deptId: data.id
  69. }
  70. tableTitle.value = data.name
  71. methods.getList()
  72. }
  73. // ========== CRUD 相关 ==========
  74. const loading = ref(false) // 遮罩层
  75. const actionType = ref('') // 操作按钮的类型
  76. const dialogVisible = ref(false) // 是否显示弹出层
  77. const dialogTitle = ref('edit') // 弹出层标题
  78. const formRef = ref<FormExpose>() // 表单 Ref
  79. const deptId = ref(0) // 部门ID
  80. const postIds = ref<string[]>([]) // 岗位ID
  81. const postOptions = ref<PostVO[]>([]) //岗位列表
  82. // 获取岗位列表
  83. const getPostOptions = async () => {
  84. const res = await listSimplePostsApi()
  85. postOptions.value.push(...res)
  86. }
  87. // 设置标题
  88. const setDialogTile = async (type: string) => {
  89. dialogTitle.value = t('action.' + type)
  90. actionType.value = type
  91. dialogVisible.value = true
  92. }
  93. // 新增操作
  94. const handleAdd = () => {
  95. setDialogTile('create')
  96. // 重置表单
  97. deptId.value = 0
  98. unref(formRef)?.getElFormRef()?.resetFields()
  99. }
  100. // 修改操作
  101. const handleUpdate = async (row: UserVO) => {
  102. await setDialogTile('update')
  103. // 设置数据
  104. const res = await UserApi.getUserApi(row.id)
  105. deptId.value = res.deptId
  106. postIds.value = res.postIds
  107. unref(formRef)?.setValues(res)
  108. }
  109. // 提交按钮
  110. const submitForm = async () => {
  111. loading.value = true
  112. // 提交请求
  113. try {
  114. const data = unref(formRef)?.formModel as UserVO
  115. data.deptId = deptId.value
  116. data.postIds = postIds.value
  117. if (actionType.value === 'create') {
  118. await UserApi.createUserApi(data)
  119. ElMessage.success(t('common.createSuccess'))
  120. } else {
  121. await UserApi.updateUserApi(data)
  122. ElMessage.success(t('common.updateSuccess'))
  123. }
  124. // 操作成功,重新加载列表
  125. dialogVisible.value = false
  126. await getList()
  127. } finally {
  128. loading.value = false
  129. }
  130. }
  131. // 改变用户状态操作
  132. const handleStatusChange = async (row: UserVO) => {
  133. const text = row.status === CommonStatusEnum.ENABLE ? '启用' : '停用'
  134. ElMessageBox.confirm('确认要"' + text + '""' + row.username + '"用户吗?', t('common.reminder'), {
  135. confirmButtonText: t('common.ok'),
  136. cancelButtonText: t('common.cancel'),
  137. type: 'warning'
  138. })
  139. .then(async () => {
  140. row.status =
  141. row.status === CommonStatusEnum.ENABLE ? CommonStatusEnum.ENABLE : CommonStatusEnum.DISABLE
  142. await UserApi.updateUserStatusApi(row.id, row.status)
  143. ElMessage.success(text + '成功')
  144. await getList()
  145. })
  146. .catch(() => {
  147. row.status =
  148. row.status === CommonStatusEnum.ENABLE ? CommonStatusEnum.DISABLE : CommonStatusEnum.ENABLE
  149. })
  150. }
  151. // 重置密码
  152. const handleResetPwd = (row: UserVO) => {
  153. ElMessageBox.prompt('请输入"' + row.username + '"的新密码', t('common.reminder'), {
  154. confirmButtonText: t('common.ok'),
  155. cancelButtonText: t('common.cancel')
  156. }).then(({ value }) => {
  157. UserApi.resetUserPwdApi(row.id, value).then(() => {
  158. ElMessage.success('修改成功,新密码是:' + value)
  159. })
  160. })
  161. }
  162. // 删除操作
  163. const handleDelete = (row: UserVO) => {
  164. delList(row.id, false)
  165. }
  166. // 导出操作
  167. const handleExport = async () => {
  168. await exportList('用户数据.xls')
  169. }
  170. // ========== 详情相关 ==========
  171. const detailRef = ref()
  172. // 详情操作
  173. const handleDetail = async (row: UserVO) => {
  174. // 设置数据
  175. detailRef.value = row
  176. await setDialogTile('detail')
  177. }
  178. // ========== 导入相关 ==========
  179. const importDialogVisible = ref(false)
  180. const uploadDisabled = ref(false)
  181. const importDialogTitle = ref('用户导入')
  182. const updateSupport = ref(0)
  183. let updateUrl = import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/system/user/import'
  184. const uploadHeaders = ref()
  185. // 下载导入模版
  186. const handleImportTemp = async () => {
  187. const res = await UserApi.importUserTemplateApi()
  188. download.excel(res, '用户导入模版.xls')
  189. }
  190. // 文件上传之前判断
  191. const beforeExcelUpload = (file: UploadRawFile) => {
  192. const isExcel =
  193. file.type === 'application/vnd.ms-excel' ||
  194. file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  195. const isLt5M = file.size / 1024 / 1024 < 5
  196. if (!isExcel) ElMessage.error('上传文件只能是 xls / xlsx 格式!')
  197. if (!isLt5M) ElMessage.error('上传文件大小不能超过 5MB!')
  198. return isExcel && isLt5M
  199. }
  200. // 文件上传
  201. const uploadRef = ref<UploadInstance>()
  202. const submitFileForm = () => {
  203. uploadHeaders.value = {
  204. Authorization: 'Bearer ' + wsCache.get('ACCESS_TOKEN'),
  205. 'tenant-id': wsCache.get('tenantId')
  206. }
  207. uploadDisabled.value = true
  208. uploadRef.value!.submit()
  209. }
  210. // 文件上传成功
  211. const handleFileSuccess = (response: any): void => {
  212. if (response.code !== 0) {
  213. ElMessage.error(response.msg)
  214. return
  215. }
  216. importDialogVisible.value = false
  217. uploadDisabled.value = false
  218. const data = response.data
  219. let text = '上传成功数量:' + data.createUsernames.length + ';'
  220. for (let username of data.createUsernames) {
  221. text += '< ' + username + ' >'
  222. }
  223. text += '更新成功数量:' + data.updateUsernames.length + ';'
  224. for (const username of data.updateUsernames) {
  225. text += '< ' + username + ' >'
  226. }
  227. text += '更新失败数量:' + Object.keys(data.failureUsernames).length + ';'
  228. for (const username in data.failureUsernames) {
  229. text += '< ' + username + ': ' + data.failureUsernames[username] + ' >'
  230. }
  231. ElMessageBox.alert(text)
  232. }
  233. // 文件数超出提示
  234. const handleExceed = (): void => {
  235. ElMessage.error('最多只能上传一个文件!')
  236. }
  237. // 上传错误提示
  238. const excelUploadError = (): void => {
  239. ElMessage.error('导入数据失败,请您重新上传!')
  240. }
  241. // ========== 初始化 ==========
  242. onMounted(async () => {
  243. await getTree()
  244. await getPostOptions()
  245. })
  246. getList()
  247. </script>
  248. <template>
  249. <div class="flex">
  250. <el-card class="w-1/5 user" :gutter="12" shadow="always">
  251. <template #header>
  252. <div class="card-header">
  253. <span>部门列表</span>
  254. </div>
  255. </template>
  256. <el-tree
  257. ref="treeRef"
  258. node-key="id"
  259. default-expand-all
  260. :data="deptOptions"
  261. :props="defaultProps"
  262. :highlight-current="true"
  263. :filter-method="filterNode"
  264. :expand-on-click-node="false"
  265. @node-click="handleDeptNodeClick"
  266. />
  267. </el-card>
  268. <!-- 搜索工作区 -->
  269. <el-card class="w-4/5 user" style="margin-left: 10px" :gutter="12" shadow="hover">
  270. <template #header>
  271. <div class="card-header">
  272. <span>{{ tableTitle }}</span>
  273. </div>
  274. </template>
  275. <Search
  276. :schema="allSchemas.searchSchema"
  277. @search="setSearchParams"
  278. @reset="setSearchParams"
  279. ref="searchForm"
  280. />
  281. <!-- 操作工具栏 -->
  282. <div class="mb-10px">
  283. <el-button type="primary" v-hasPermi="['system:user:create']" @click="handleAdd">
  284. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  285. </el-button>
  286. <el-button
  287. type="info"
  288. v-hasPermi="['system:user:import']"
  289. @click="importDialogVisible = true"
  290. >
  291. <Icon icon="ep:upload" class="mr-5px" /> {{ t('action.import') }}
  292. </el-button>
  293. <el-button type="warning" v-hasPermi="['system:user:export']" @click="handleExport">
  294. <Icon icon="ep:download" class="mr-5px" /> {{ t('action.export') }}
  295. </el-button>
  296. </div>
  297. <!-- 列表 -->
  298. <Table
  299. :columns="allSchemas.tableColumns"
  300. :selection="false"
  301. :data="tableObject.tableList"
  302. :loading="tableObject.loading"
  303. :pagination="{
  304. total: tableObject.total
  305. }"
  306. v-model:pageSize="tableObject.pageSize"
  307. v-model:currentPage="tableObject.currentPage"
  308. @register="register"
  309. >
  310. <template #sex="{ row }">
  311. <DictTag :type="DICT_TYPE.SYSTEM_USER_SEX" :value="row.sex" />
  312. </template>
  313. <template #status="{ row }">
  314. <el-switch
  315. v-model="row.status"
  316. :active-value="0"
  317. :inactive-value="1"
  318. @change="handleStatusChange(row)"
  319. />
  320. </template>
  321. <template #loginDate="{ row }">
  322. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  323. </template>
  324. <template #action="{ row }">
  325. <el-button
  326. link
  327. type="primary"
  328. v-hasPermi="['system:user:update']"
  329. @click="handleUpdate(row)"
  330. >
  331. <Icon icon="ep:edit" class="mr-5px" /> {{ t('action.edit') }}
  332. </el-button>
  333. <el-button
  334. link
  335. type="primary"
  336. v-hasPermi="['system:user:update']"
  337. @click="handleDetail(row)"
  338. >
  339. <Icon icon="ep:view" class="mr-5px" /> {{ t('action.detail') }}
  340. </el-button>
  341. <el-button
  342. link
  343. type="primary"
  344. v-hasPermi="['system:user:update-password']"
  345. @click="handleResetPwd(row)"
  346. >
  347. <Icon icon="ep:key" class="mr-5px" /> 重置密码
  348. </el-button>
  349. <el-button
  350. link
  351. type="primary"
  352. v-hasPermi="['system:user:delete']"
  353. @click="handleDelete(row)"
  354. >
  355. <Icon icon="ep:delete" class="mr-5px" /> {{ t('action.del') }}
  356. </el-button>
  357. </template>
  358. </Table>
  359. </el-card>
  360. </div>
  361. <Dialog v-model="dialogVisible" :title="dialogTitle">
  362. <!-- 对话框(添加 / 修改) -->
  363. <Form
  364. v-if="['create', 'update'].includes(actionType)"
  365. :rules="rules"
  366. :schema="allSchemas.formSchema"
  367. ref="formRef"
  368. >
  369. <template #deptId>
  370. <el-tree-select
  371. node-key="id"
  372. v-model="deptId"
  373. :props="defaultProps"
  374. :data="deptOptions"
  375. check-strictly
  376. />
  377. </template>
  378. <template #postIds>
  379. <el-select v-model="postIds" multiple placeholder="Select">
  380. <el-option
  381. v-for="item in postOptions"
  382. :key="item.id"
  383. :label="item.name"
  384. :value="item.id"
  385. />
  386. </el-select>
  387. </template>
  388. </Form>
  389. <!-- 对话框(详情) -->
  390. <Descriptions
  391. v-if="actionType === 'detail'"
  392. :schema="allSchemas.detailSchema"
  393. :data="detailRef"
  394. >
  395. <template #deptId="{ row }">
  396. <span>{{ row.dept.name }}</span>
  397. </template>
  398. <template #postIds="{ row }">
  399. <span>{{ row.dept.name }}</span>
  400. </template>
  401. <template #sex="{ row }">
  402. <DictTag :type="DICT_TYPE.SYSTEM_USER_SEX" :value="row.sex" />
  403. </template>
  404. <template #status="{ row }">
  405. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  406. </template>
  407. <template #loginDate="{ row }">
  408. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  409. </template>
  410. </Descriptions>
  411. <!-- 操作按钮 -->
  412. <template #footer>
  413. <el-button
  414. v-if="['create', 'update'].includes(actionType)"
  415. type="primary"
  416. :loading="loading"
  417. @click="submitForm"
  418. >
  419. {{ t('action.save') }}
  420. </el-button>
  421. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  422. </template>
  423. </Dialog>
  424. <!-- 导入 -->
  425. <Dialog
  426. v-model="importDialogVisible"
  427. :title="importDialogTitle"
  428. :destroy-on-close="true"
  429. maxHeight="350px"
  430. >
  431. <el-form class="drawer-multiColumn-form" label-width="150px">
  432. <el-form-item label="模板下载 :">
  433. <el-button type="primary" @click="handleImportTemp">
  434. <Icon icon="ep:download" />
  435. 点击下载
  436. </el-button>
  437. </el-form-item>
  438. <el-form-item label="文件上传 :">
  439. <el-upload
  440. ref="uploadRef"
  441. :action="updateUrl + '?updateSupport=' + updateSupport"
  442. :headers="uploadHeaders"
  443. :drag="true"
  444. :limit="1"
  445. :multiple="true"
  446. :show-file-list="true"
  447. :disabled="uploadDisabled"
  448. :before-upload="beforeExcelUpload"
  449. :on-exceed="handleExceed"
  450. :on-success="handleFileSuccess"
  451. :on-error="excelUploadError"
  452. :auto-upload="false"
  453. accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  454. >
  455. <Icon icon="ep:upload-filled" />
  456. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  457. <template #tip>
  458. <div class="el-upload__tip">请上传 .xls , .xlsx 标准格式文件</div>
  459. </template>
  460. </el-upload>
  461. </el-form-item>
  462. <el-form-item label="是否更新已经存在的用户数据:">
  463. <el-checkbox v-model="updateSupport" />
  464. </el-form-item>
  465. </el-form>
  466. <template #footer>
  467. <el-button type="primary" @click="submitFileForm">
  468. <Icon icon="ep:upload-filled" />
  469. {{ t('action.save') }}
  470. </el-button>
  471. <el-button @click="importDialogVisible = false">{{ t('dialog.close') }}</el-button>
  472. </template>
  473. </Dialog>
  474. </template>
  475. <style scoped>
  476. .user {
  477. height: 900px;
  478. max-height: 960px;
  479. }
  480. .card-header {
  481. display: flex;
  482. justify-content: space-between;
  483. align-items: center;
  484. }
  485. </style>