index.vue 15 KB

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