Explorar o código

REVIEW 用户管理(导入用户)

YunaiV %!s(int64=2) %!d(string=hai) anos
pai
achega
cebe6f93db

+ 1 - 1
src/api/system/user/index.ts

@@ -48,7 +48,7 @@ export const exportUser = (params) => {
 }
 
 // 下载用户导入模板
-export const importUserTemplateApi = () => {
+export const importUserTemplate = () => {
   return request.download({ url: '/system/user/get-import-template' })
 }
 

+ 3 - 3
src/views/bpm/model/ModelImportForm.vue

@@ -8,7 +8,7 @@
         :data="formData"
         name="bpmnFile"
         v-model:file-list="fileList"
-        :drag="true"
+        drag
         :auto-upload="false"
         accept=".bpmn, .xml"
         :limit="1"
@@ -77,7 +77,7 @@ const open = async () => {
 }
 defineExpose({ open }) // 提供 open 方法,用于打开弹窗
 
-/** 重置表单 */
+/** 提交表单 */
 const submitForm = async () => {
   // 校验表单
   if (!formRef) return
@@ -98,7 +98,7 @@ const submitForm = async () => {
 
 /** 文件上传成功 */
 const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
-const submitFormSuccess = async (response: any): Promise<void> => {
+const submitFormSuccess = async (response: any) => {
   if (response.code !== 0) {
     message.error(response.msg)
     formLoading.value = false

+ 129 - 0
src/views/system/user/UserImportForm.vue

@@ -0,0 +1,129 @@
+<template>
+  <Dialog title="用户导入" v-model="modelVisible" width="400">
+    <el-upload
+      ref="uploadRef"
+      :action="importUrl + '?updateSupport=' + updateSupport"
+      :headers="uploadHeaders"
+      v-model:file-list="fileList"
+      drag
+      accept=".xlsx, .xls"
+      :limit="1"
+      :on-success="submitFormSuccess"
+      :on-exceed="handleExceed"
+      :on-error="submitFormError"
+      :auto-upload="false"
+      :disabled="formLoading"
+    >
+      <Icon icon="ep:upload" />
+      <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
+      <template #tip>
+        <div class="el-upload__tip text-center">
+          <div class="el-upload__tip">
+            <el-checkbox v-model="updateSupport" /> 是否更新已经存在的用户数据
+          </div>
+          <span>仅允许导入 xls、xlsx 格式文件。</span>
+          <el-link
+            type="primary"
+            :underline="false"
+            style="font-size: 12px; vertical-align: baseline"
+            @click="importTemplate"
+          >
+            下载模板
+          </el-link>
+        </div>
+      </template>
+    </el-upload>
+    <template #footer>
+      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
+      <el-button @click="modelVisible = false">取 消</el-button>
+    </template>
+  </Dialog>
+</template>
+<script lang="ts" setup>
+import * as UserApi from '@/api/system/user'
+import { getAccessToken, getTenantId } from '@/utils/auth'
+import download from '@/utils/download'
+const message = useMessage() // 消息弹窗
+
+const modelVisible = ref(false) // 弹窗的是否展示
+const formLoading = ref(false) // 表单的加载中
+const uploadRef = ref()
+const importUrl =
+  import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/system/user/import'
+const uploadHeaders = ref() // 上传 Header 头
+const fileList = ref([]) // 文件列表
+const updateSupport = ref(0) // 是否更新已经存在的用户数据
+
+/** 打开弹窗 */
+const open = () => {
+  modelVisible.value = true
+  resetForm()
+}
+defineExpose({ open }) // 提供 open 方法,用于打开弹窗
+
+/** 提交表单 */
+const submitForm = async () => {
+  if (fileList.value.length == 0) {
+    message.error('请上传文件')
+    return
+  }
+  // 提交请求
+  uploadHeaders.value = {
+    Authorization: 'Bearer ' + getAccessToken(),
+    'tenant-id': getTenantId()
+  }
+  formLoading.value = true
+  uploadRef.value!.submit()
+}
+
+/** 文件上传成功 */
+const emits = defineEmits(['success'])
+const submitFormSuccess = (response: any) => {
+  if (response.code !== 0) {
+    message.error(response.msg)
+    formLoading.value = false
+    return
+  }
+  // 拼接提示语
+  const data = response.data
+  let text = '上传成功数量:' + data.createUsernames.length + ';'
+  for (let username of data.createUsernames) {
+    text += '< ' + username + ' >'
+  }
+  text += '更新成功数量:' + data.updateUsernames.length + ';'
+  for (const username of data.updateUsernames) {
+    text += '< ' + username + ' >'
+  }
+  text += '更新失败数量:' + Object.keys(data.failureUsernames).length + ';'
+  for (const username in data.failureUsernames) {
+    text += '< ' + username + ': ' + data.failureUsernames[username] + ' >'
+  }
+  message.alert(text)
+  // 发送操作成功的事件
+  emits('success')
+}
+
+/** 上传错误提示 */
+const submitFormError = (): void => {
+  message.error('上传失败,请您重新上传!')
+  formLoading.value = false
+}
+
+/** 重置表单 */
+const resetForm = () => {
+  // 重置上传状态和文件
+  formLoading.value = false
+  uploadRef.value?.clearFiles()
+}
+
+/** 文件数超出提示 */
+const handleExceed = (): void => {
+  message.error('最多只能上传一个文件!')
+}
+
+/** 下载模板操作 */
+const importTemplate = async () => {
+  const res = await UserApi.importUserTemplate()
+  download.excel(res, '用户导入模版.xls')
+}
+</script>

+ 0 - 154
src/views/system/user/components/UserImportForm.vue

@@ -1,154 +0,0 @@
-<template>
-  <Dialog
-    :title="upload.title"
-    :modelValue="showDialog"
-    width="400px"
-    append-to-body
-    @close="closeDialog"
-  >
-    <el-upload
-      ref="uploadRef"
-      accept=".xlsx, .xls"
-      :limit="1"
-      :headers="upload.headers"
-      :action="upload.url + '?updateSupport=' + upload.updateSupport"
-      :disabled="upload.isUploading"
-      :on-progress="handleFileUploadProgress"
-      :on-success="handleFileSuccess"
-      :on-exceed="handleExceed"
-      :on-error="excelUploadError"
-      :auto-upload="false"
-      drag
-    >
-      <Icon icon="ep:upload" />
-      <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
-      <template #tip>
-        <div class="el-upload__tip text-center">
-          <div class="el-upload__tip">
-            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
-          </div>
-
-          <span>仅允许导入xls、xlsx格式文件。</span>
-          <el-link
-            type="primary"
-            :underline="false"
-            style="font-size: 12px; vertical-align: baseline"
-            @click="importTemplate"
-            >下载模板</el-link
-          >
-        </div>
-      </template>
-    </el-upload>
-    <template #footer>
-      <div class="dialog-footer">
-        <el-button type="primary" @click="submitFileForm">确 定</el-button>
-        <el-button @click="cancel">取 消</el-button>
-      </div>
-    </template>
-  </Dialog>
-</template>
-
-<script lang="ts" setup>
-import { importUserTemplateApi } from '@/api/system/user'
-import { getAccessToken, getTenantId } from '@/utils/auth'
-import download from '@/utils/download'
-
-const emits = defineEmits(['success'])
-
-const message = useMessage() // 消息弹窗
-
-const showDialog = ref(false)
-const uploadRef = ref()
-
-// 用户导入参数
-const upload = reactive({
-  // // 是否显示弹出层(用户导入)
-  // open: false,
-  // 弹出层标题(用户导入)
-  title: '用户导入',
-  // 是否禁用上传
-  isUploading: false,
-  // 是否更新已经存在的用户数据
-  updateSupport: 0,
-  // 设置上传的请求头部
-  headers: {
-    Authorization: 'Bearer ' + getAccessToken(),
-    'tenant-id': getTenantId()
-  },
-  // 上传的地址
-  url: import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/system/user/import'
-})
-
-// 文件上传中处理
-const handleFileUploadProgress = () => {
-  upload.isUploading = true
-}
-// 文件上传成功处理
-const handleFileSuccess = (response: any) => {
-  if (response.code !== 0) {
-    message.error(response.msg)
-    return
-  }
-  upload.isUploading = false
-  uploadRef.value?.clearFiles()
-  // 拼接提示语
-  const data = response.data
-  let text = '上传成功数量:' + data.createUsernames.length + ';'
-  for (let username of data.createUsernames) {
-    text += '< ' + username + ' >'
-  }
-  text += '更新成功数量:' + data.updateUsernames.length + ';'
-  for (const username of data.updateUsernames) {
-    text += '< ' + username + ' >'
-  }
-  text += '更新失败数量:' + Object.keys(data.failureUsernames).length + ';'
-  for (const username in data.failureUsernames) {
-    text += '< ' + username + ': ' + data.failureUsernames[username] + ' >'
-  }
-  message.alert(text)
-  emits('success')
-  closeDialog()
-}
-
-// 文件数超出提示
-const handleExceed = (): void => {
-  message.error('最多只能上传一个文件!')
-}
-// 上传错误提示
-const excelUploadError = (e): void => {
-  console.log(e)
-  message.error('导入数据失败,请您重新上传!')
-}
-
-/** 下载模板操作 */
-const importTemplate = async () => {
-  try {
-    const res = await importUserTemplateApi()
-    download.excel(res, '用户导入模版.xls')
-  } catch (error) {
-    console.error(error)
-  }
-}
-
-/* 弹框按钮操作 */
-// 点击取消
-const cancel = () => {
-  closeDialog()
-}
-// 关闭弹窗
-const closeDialog = () => {
-  showDialog.value = false
-}
-// 提交上传文件
-const submitFileForm = () => {
-  uploadRef.value?.submit()
-}
-
-const openForm = () => {
-  uploadRef.value?.clearFiles()
-  showDialog.value = true
-}
-defineExpose({
-  openForm
-})
-</script>

+ 3 - 3
src/views/system/user/index.vue

@@ -194,7 +194,7 @@ import download from '@/utils/download'
 import { CommonStatusEnum } from '@/utils/constants'
 import * as UserApi from '@/api/system/user'
 import UserForm from './UserForm.vue'
-import UserImportForm from './components/UserImportForm.vue'
+import UserImportForm from './UserImportForm.vue'
 import UserAssignRoleForm from './UserAssignRoleForm.vue'
 import DeptTree from './DeptTree.vue'
 const message = useMessage() // 消息弹窗
@@ -250,10 +250,10 @@ const openForm = (type: string, id?: number) => {
   formRef.value.open(type, id)
 }
 
-// 用户导入
+/** 用户导入 */
 const importFormRef = ref()
 const handleImport = () => {
-  importFormRef.value?.openForm()
+  importFormRef.value.open()
 }
 
 /** 修改用户状态 */