|
@@ -1,4 +1,4 @@
|
|
|
-<script setup lang="ts">
|
|
|
+<script lang="ts" setup>
|
|
|
/**
|
|
|
* @Name: index.vue
|
|
|
* @Description: 文件上传
|
|
@@ -7,41 +7,32 @@
|
|
|
import { ButtonProps, genFileId, UploadInstance, UploadRawFile } from 'element-plus';
|
|
|
import { BtnPermissionStore } from '/@/plugin/permission/store.permission';
|
|
|
import { uploadFile } from '/@/views/product-manage/product-list/api';
|
|
|
+import { hasPermission } from '/@/utils/hasPermission';
|
|
|
|
|
|
-const { data } = BtnPermissionStore();
|
|
|
|
|
|
const attrs = useAttrs() as any;
|
|
|
const refreshView = inject('refreshView');
|
|
|
|
|
|
const props = defineProps<
|
|
|
- {
|
|
|
- uploadFunction: (url: 'URL') => Promise<any>;
|
|
|
- } & Partial<Omit<ButtonProps, 'disabled' | 'loading' | 'color'>>
|
|
|
+ {
|
|
|
+ uploadFunction: (url: 'URL') => Promise<any>;
|
|
|
+ } & Partial<Omit<ButtonProps, 'disabled' | 'loading' | 'color'>>
|
|
|
>();
|
|
|
|
|
|
-function hasPermission(permissions: string | string[]): boolean {
|
|
|
- if (typeof permissions === 'string') {
|
|
|
- return data.includes(permissions);
|
|
|
- } else if (Array.isArray(permissions)) {
|
|
|
- return permissions.every((permission) => data.includes(permission));
|
|
|
- }
|
|
|
- return false;
|
|
|
-}
|
|
|
-
|
|
|
const upload = ref<UploadInstance>();
|
|
|
const upBtnLoading = ref(false);
|
|
|
-const emits = defineEmits(['handel-error']);
|
|
|
+const emits = defineEmits([ 'handel-error' ]);
|
|
|
|
|
|
/**
|
|
|
* @description 替换文件并上传
|
|
|
* @param files 文件列表
|
|
|
*/
|
|
|
function handleExceed(files: any) {
|
|
|
- upload.value!.clearFiles();
|
|
|
- const file = files[0] as UploadRawFile;
|
|
|
- file.uid = genFileId();
|
|
|
- upload.value!.handleStart(file);
|
|
|
- upload.value!.submit();
|
|
|
+ upload.value!.clearFiles();
|
|
|
+ const file = files[0] as UploadRawFile;
|
|
|
+ file.uid = genFileId();
|
|
|
+ upload.value!.handleStart(file);
|
|
|
+ upload.value!.submit();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -49,22 +40,22 @@ function handleExceed(files: any) {
|
|
|
* @param uploadRequest 上传请求
|
|
|
*/
|
|
|
async function handleCustomUpload(uploadRequest: any) {
|
|
|
- upBtnLoading.value = true;
|
|
|
- try {
|
|
|
- const { file } = uploadRequest;
|
|
|
- const formData = new FormData();
|
|
|
- formData.append('file', file);
|
|
|
- const resp = await uploadFile(formData);
|
|
|
- const fileUrl = resp.data.url;
|
|
|
- const response = await props.uploadFunction({ url: fileUrl });
|
|
|
- handleResponse(response);
|
|
|
- uploadRequest.onSuccess(response);
|
|
|
- } catch (error) {
|
|
|
- console.log('==Error==', error);
|
|
|
- uploadRequest.onError(error);
|
|
|
- } finally {
|
|
|
- upBtnLoading.value = false;
|
|
|
- }
|
|
|
+ upBtnLoading.value = true;
|
|
|
+ try {
|
|
|
+ const { file } = uploadRequest;
|
|
|
+ const formData = new FormData();
|
|
|
+ formData.append('file', file);
|
|
|
+ const resp = await uploadFile(formData);
|
|
|
+ const fileUrl = resp.data.url;
|
|
|
+ const response = await props.uploadFunction({ url: fileUrl });
|
|
|
+ handleResponse(response);
|
|
|
+ uploadRequest.onSuccess(response);
|
|
|
+ } catch (error) {
|
|
|
+ console.log('==Error==', error);
|
|
|
+ uploadRequest.onError(error);
|
|
|
+ } finally {
|
|
|
+ upBtnLoading.value = false;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -72,48 +63,47 @@ async function handleCustomUpload(uploadRequest: any) {
|
|
|
* @param response 后端返回的响应
|
|
|
*/
|
|
|
function handleResponse(response: any) {
|
|
|
- if (response.data && response.data.length !== 0){
|
|
|
- emits('handel-error', response.data);
|
|
|
- return;
|
|
|
- }
|
|
|
- if (response.code === 2000) {
|
|
|
- ElMessage.success({ message: response.msg, plain: true });
|
|
|
- if (refreshView) {
|
|
|
- refreshView();
|
|
|
- }
|
|
|
- }
|
|
|
- else {
|
|
|
- ElMessage.error({ message: '上传失败', plain: true });
|
|
|
- }
|
|
|
+ if (response.data && response.data.length !== 0) {
|
|
|
+ emits('handel-error', response.data);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (response.code === 2000) {
|
|
|
+ ElMessage.success({ message: response.msg, plain: true });
|
|
|
+ if (refreshView) {
|
|
|
+ refreshView();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ ElMessage.error({ message: '上传失败', plain: true });
|
|
|
+ }
|
|
|
}
|
|
|
</script>
|
|
|
|
|
|
<template>
|
|
|
- <div>
|
|
|
- <el-upload
|
|
|
- ref="upload"
|
|
|
- action="#"
|
|
|
- :limit="1"
|
|
|
- :show-file-list="false"
|
|
|
- :auto-upload="true"
|
|
|
- :on-exceed="handleExceed"
|
|
|
- :http-request="handleCustomUpload"
|
|
|
- accept=".xls,.xlsx"
|
|
|
- >
|
|
|
- <template #trigger>
|
|
|
- <el-button
|
|
|
- v-if="attrs.show ? hasPermission(attrs.show) : true"
|
|
|
- :disabled="attrs.permissions ? !hasPermission(attrs.permissions) : false"
|
|
|
- :loading="upBtnLoading"
|
|
|
- :color="attrs.myColor"
|
|
|
- v-bind="props"
|
|
|
- >
|
|
|
- <!--:loading="upBtnLoading" color="#6366f1" v-bind="props">-->
|
|
|
- <slot></slot>
|
|
|
- </el-button>
|
|
|
- </template>
|
|
|
- </el-upload>
|
|
|
- </div>
|
|
|
+ <div>
|
|
|
+ <el-upload
|
|
|
+ ref="upload"
|
|
|
+ :auto-upload="true"
|
|
|
+ :http-request="handleCustomUpload"
|
|
|
+ :limit="1"
|
|
|
+ :on-exceed="handleExceed"
|
|
|
+ :show-file-list="false"
|
|
|
+ accept=".xls,.xlsx"
|
|
|
+ action="#"
|
|
|
+ >
|
|
|
+ <template #trigger>
|
|
|
+ <el-button
|
|
|
+ v-if="attrs.show ? hasPermission(attrs.show) : true"
|
|
|
+ :color="attrs.myColor"
|
|
|
+ :disabled="attrs.permissions ? !hasPermission(attrs.permissions) : false"
|
|
|
+ :loading="upBtnLoading"
|
|
|
+ v-bind="props"
|
|
|
+ >
|
|
|
+ <!--:loading="upBtnLoading" color="#6366f1" v-bind="props">-->
|
|
|
+ <slot></slot>
|
|
|
+ </el-button>
|
|
|
+ </template>
|
|
|
+ </el-upload>
|
|
|
+ </div>
|
|
|
</template>
|
|
|
|
|
|
<style scoped></style>
|