فهرست منبع

用户角色关联导入功能

xy 22 ساعت پیش
والد
کامیت
d99eaca039

+ 5 - 0
yudao-ui-admin-vue3/src/api/system/user/index.ts

@@ -61,6 +61,11 @@ export const importUserTemplate = () => {
   return request.download({ url: '/system/user/get-import-template' })
 }
 
+// 下载用户角色关联导入模板
+export const importUserRoleTemplate = () => {
+  return request.download({ url: '/system/user/get-import-role-template' })
+}
+
 // 用户密码重置
 export const resetUserPwd = (id: number, password: string) => {
   const data = {

+ 123 - 0
yudao-ui-admin-vue3/src/views/system/dept/DeptImportForm.vue

@@ -0,0 +1,123 @@
+<template>
+  <Dialog v-model="dialogVisible" title="部门导入" width="400">
+    <el-upload
+      ref="uploadRef"
+      v-model:file-list="fileList"
+      :action="importUrl"
+      :auto-upload="false"
+      :disabled="formLoading"
+      :headers="uploadHeaders"
+      :limit="1"
+      :on-error="submitFormError"
+      :on-exceed="handleExceed"
+      :on-success="submitFormSuccess"
+      accept=".xlsx, .xls"
+      drag
+    >
+      <Icon icon="ep:upload" />
+      <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
+      <template #tip>
+        <div class="el-upload__tip text-center">
+          <span>仅允许导入 xls、xlsx 格式文件。</span>
+          <el-link
+            :underline="false"
+            style="font-size: 12px; vertical-align: baseline"
+            type="primary"
+            @click="importTemplate"
+          >
+            下载模板
+          </el-link>
+        </div>
+      </template>
+    </el-upload>
+    <template #footer>
+      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
+      <el-button @click="dialogVisible = false">取 消</el-button>
+    </template>
+  </Dialog>
+</template>
+<script lang="ts" setup>
+import * as DeptApi from '@/api/system/dept'
+import { getAccessToken, getTenantId } from '@/utils/auth'
+import download from '@/utils/download'
+
+defineOptions({ name: 'SystemDeptImportForm' })
+
+const message = useMessage()
+
+const dialogVisible = ref(false)
+const formLoading = ref(false)
+const uploadRef = ref()
+const importUrl = import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/system/dept/import'
+const uploadHeaders = ref()
+const fileList = ref([])
+
+/** 打开弹窗 */
+const open = () => {
+  dialogVisible.value = true
+  fileList.value = []
+  resetForm()
+}
+defineExpose({ 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.createDeptNames.length + ';'
+  for (const name of data.createDeptNames) {
+    text += '< ' + name + ' >'
+  }
+  text += '导入失败数量:' + Object.keys(data.failureDeptNames).length + ';'
+  for (const name in data.failureDeptNames) {
+    text += '< ' + name + ': ' + data.failureDeptNames[name] + ' >'
+  }
+  message.alert(text)
+  formLoading.value = false
+  dialogVisible.value = false
+  emits('success')
+}
+
+/** 上传错误提示 */
+const submitFormError = (): void => {
+  message.error('上传失败,请您重新上传!')
+  formLoading.value = false
+}
+
+/** 重置表单 */
+const resetForm = async (): Promise<void> => {
+  formLoading.value = false
+  await nextTick()
+  uploadRef.value?.clearFiles()
+}
+
+/** 文件数超出提示 */
+const handleExceed = (): void => {
+  message.error('最多只能上传一个文件!')
+}
+
+/** 下载模板操作 */
+const importTemplate = async () => {
+  const res = await DeptApi.importDeptTemplate()
+  download.excel(res, '部门导入模版.xls')
+}
+</script>

+ 123 - 0
yudao-ui-admin-vue3/src/views/system/user/UserRoleImportForm.vue

@@ -0,0 +1,123 @@
+<template>
+  <Dialog v-model="dialogVisible" title="导入用户角色关联" width="400">
+    <el-upload
+      ref="uploadRef"
+      v-model:file-list="fileList"
+      :action="importUrl"
+      :auto-upload="false"
+      :disabled="formLoading"
+      :headers="uploadHeaders"
+      :limit="1"
+      :on-error="submitFormError"
+      :on-exceed="handleExceed"
+      :on-success="submitFormSuccess"
+      accept=".xlsx, .xls"
+      drag
+    >
+      <Icon icon="ep:upload" />
+      <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
+      <template #tip>
+        <div class="el-upload__tip text-center">
+          <span>仅允许导入 xls、xlsx 格式文件。</span>
+          <el-link
+            :underline="false"
+            style="font-size: 12px; vertical-align: baseline"
+            type="primary"
+            @click="importTemplate"
+          >
+            下载模板
+          </el-link>
+        </div>
+      </template>
+    </el-upload>
+    <template #footer>
+      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
+      <el-button @click="dialogVisible = 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'
+
+defineOptions({ name: 'SystemUserRoleImportForm' })
+
+const message = useMessage()
+
+const dialogVisible = 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-role'
+const uploadHeaders = ref()
+const fileList = ref([])
+
+/** 打开弹窗 */
+const open = () => {
+  dialogVisible.value = true
+  fileList.value = []
+  resetForm()
+}
+defineExpose({ 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 (const name of data.createUsernames) {
+    text += '< ' + name + ' >'
+  }
+  text += '关联失败数量:' + Object.keys(data.failureUsernames).length + ';'
+  for (const name in data.failureUsernames) {
+    text += '< ' + name + ': ' + data.failureUsernames[name] + ' >'
+  }
+  message.alert(text)
+  formLoading.value = false
+  dialogVisible.value = false
+  emits('success')
+}
+
+/** 上传错误提示 */
+const submitFormError = (): void => {
+  message.error('上传失败,请您重新上传!')
+  formLoading.value = false
+}
+
+/** 重置表单 */
+const resetForm = async (): Promise<void> => {
+  formLoading.value = false
+  await nextTick()
+  uploadRef.value?.clearFiles()
+}
+
+/** 文件数超出提示 */
+const handleExceed = (): void => {
+  message.error('最多只能上传一个文件!')
+}
+
+/** 下载模板操作 */
+const importTemplate = async () => {
+  const res = await UserApi.importUserRoleTemplate()
+  download.excel(res, '用户角色关联导入模版.xls')
+}
+</script>

+ 18 - 1
yudao-ui-admin-vue3/src/views/system/user/index.vue

@@ -106,7 +106,15 @@
               @click="handleImport"
               v-hasPermi="['system:user:import']"
             >
-              <Icon icon="ep:upload" /> 导入
+              <Icon icon="ep:upload" /> 导入用户
+            </el-button>
+            <el-button
+              type="warning"
+              plain
+              @click="handleImportRole"
+              v-hasPermi="['system:user:import']"
+            >
+              <Icon icon="ep:upload" /> 导入角色关联
             </el-button>
             <el-button
               type="success"
@@ -221,6 +229,8 @@
   <UserForm ref="formRef" @success="getList" />
   <!-- 用户导入对话框 -->
   <UserImportForm ref="importFormRef" @success="getList" />
+  <!-- 用户角色关联导入对话框 -->
+  <UserRoleImportForm ref="importRoleFormRef" @success="getList" />
   <!-- 分配角色 -->
   <UserAssignRoleForm ref="assignRoleFormRef" @success="getList" />
 </template>
@@ -234,6 +244,7 @@ import * as UserApi from '@/api/system/user'
 import * as RoleApi from '@/api/system/role'
 import UserForm from './UserForm.vue'
 import UserImportForm from './UserImportForm.vue'
+import UserRoleImportForm from './UserRoleImportForm.vue'
 import UserAssignRoleForm from './UserAssignRoleForm.vue'
 import DeptTree from './DeptTree.vue'
 import SparkMD5 from 'spark-md5'
@@ -303,6 +314,12 @@ const handleImport = () => {
   importFormRef.value.open()
 }
 
+/** 用户角色关联导入 */
+const importRoleFormRef = ref()
+const handleImportRole = () => {
+  importRoleFormRef.value.open()
+}
+
 /** 修改用户状态 */
 const handleStatusChange = async (row: UserApi.UserVO) => {
   try {