调整上传图片尺寸
This commit is contained in:
parent
fb86d5f8fd
commit
08230c5343
@ -5,7 +5,7 @@
|
||||
:id="uuid"
|
||||
:class="['upload', self_disabled ? 'disabled' : '', drag ? 'no-border' : '']"
|
||||
:multiple="false"
|
||||
:disabled="loading?true:self_disabled"
|
||||
:disabled="loading ? true : self_disabled"
|
||||
:show-file-list="false"
|
||||
:http-request="handleHttpUpload"
|
||||
:before-upload="beforeUpload"
|
||||
@ -14,8 +14,8 @@
|
||||
:accept="fileType.join(',')"
|
||||
>
|
||||
<template v-if="imageUrl">
|
||||
<img v-if="imageUrl.indexOf('.pdf')<0" :src="imageUrl" class="upload-image" />
|
||||
<div v-else class="upload-image">{{imageUrl}}</div>
|
||||
<img v-if="imageUrl.indexOf('.pdf') < 0" :src="imageUrl" class="upload-image" />
|
||||
<div v-else class="upload-image">{{ imageUrl }}</div>
|
||||
<div class="upload-handle" @click.stop>
|
||||
<div class="handle-icon" @click="editImg" v-if="!self_disabled">
|
||||
<el-icon :size="props.iconSize">
|
||||
@ -38,7 +38,7 @@
|
||||
<div class="upload-empty">
|
||||
<slot name="empty">
|
||||
<el-icon v-if="!loading"><Plus /></el-icon>
|
||||
<span v-else>上传中{{progress}}%</span>
|
||||
<span v-else>上传中{{ progress }}%</span>
|
||||
<!-- <span>请上传图片</span> -->
|
||||
</slot>
|
||||
</div>
|
||||
@ -61,18 +61,18 @@
|
||||
:oImg="oImg"
|
||||
@success="cropperSuccess"
|
||||
:fileName="fileName"
|
||||
></Cropper>
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts" name="UploadImg">
|
||||
import { ref, computed, inject } from 'vue';
|
||||
import { ElNotification, formContextKey, formItemContextKey } from 'element-plus';
|
||||
import type { UploadProps, UploadRequestOptions } from 'element-plus';
|
||||
import { generateUUID } from '@/utils/auth';
|
||||
import {upload} from '@/api/common'
|
||||
import Cropper from '@/components/Upload/cropper.vue'
|
||||
import { ref, computed, inject } from 'vue';
|
||||
import { ElNotification, formContextKey, formItemContextKey } from 'element-plus';
|
||||
import type { UploadProps, UploadRequestOptions } from 'element-plus';
|
||||
import { generateUUID } from '@/utils/auth';
|
||||
import { upload } from '@/api/common';
|
||||
import Cropper from '@/components/Upload/cropper.vue';
|
||||
|
||||
interface UploadFileProps {
|
||||
interface UploadFileProps {
|
||||
imageUrl?: string; // 图片地址 ==> 必传
|
||||
uploadFileUrl?: string; // 上传图片的 api 方法,一般项目上传都是同一个 api 方法,在组件里直接引入即可 ==> 非必传
|
||||
drag?: boolean; // 是否支持拖拽上传 ==> 非必传(默认为 true)
|
||||
@ -84,92 +84,92 @@ interface UploadFileProps {
|
||||
borderRadius?: string; // 组件边框圆角 ==> 非必传(默认为 8px)
|
||||
iconSize?: number;
|
||||
name?: string;
|
||||
cropper:Boolean,
|
||||
cropperSize?:Object
|
||||
}
|
||||
cropper: Boolean;
|
||||
cropperSize?: Object;
|
||||
}
|
||||
|
||||
const cropperVisible=ref(false)
|
||||
// 接受父组件参数
|
||||
const props = withDefaults(defineProps<UploadFileProps>(), {
|
||||
const cropperVisible = ref(false);
|
||||
// 接受父组件参数
|
||||
const props = withDefaults(defineProps<UploadFileProps>(), {
|
||||
imageUrl: '',
|
||||
uploadFileUrl: '/admin/file/upload',
|
||||
drag: true,
|
||||
disabled: false,
|
||||
fileSize: 5,
|
||||
fileType: () => ['image/jpeg', 'image/png', 'image/jpg', 'image/gif'],
|
||||
height: '150px',
|
||||
width: '150px',
|
||||
height: '120px',
|
||||
width: '120px',
|
||||
borderRadius: '8px',
|
||||
name:'',
|
||||
cropper:false,
|
||||
cropperSize:null
|
||||
});
|
||||
name: '',
|
||||
cropper: false,
|
||||
cropperSize: null,
|
||||
});
|
||||
|
||||
// 生成组件唯一id
|
||||
const uuid = ref('id-' + generateUUID());
|
||||
// 生成组件唯一id
|
||||
const uuid = ref('id-' + generateUUID());
|
||||
|
||||
const viewImg=()=>{
|
||||
if(props.imageUrl.indexOf('.pdf')<0){
|
||||
imgViewVisible.value = true
|
||||
}else{
|
||||
window.open(props.imageUrl)
|
||||
const viewImg = () => {
|
||||
if (props.imageUrl.indexOf('.pdf') < 0) {
|
||||
imgViewVisible.value = true;
|
||||
} else {
|
||||
window.open(props.imageUrl);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 查看图片
|
||||
const imgViewVisible = ref(false);
|
||||
// 获取 el-form 组件上下文
|
||||
const formContext = inject(formContextKey, void 0);
|
||||
// 获取 el-form-item 组件上下文
|
||||
const formItemContext = inject(formItemContextKey, void 0);
|
||||
// 判断是否禁用上传和删除
|
||||
const self_disabled = computed(() => {
|
||||
// 查看图片
|
||||
const imgViewVisible = ref(false);
|
||||
// 获取 el-form 组件上下文
|
||||
const formContext = inject(formContextKey, void 0);
|
||||
// 获取 el-form-item 组件上下文
|
||||
const formItemContext = inject(formItemContextKey, void 0);
|
||||
// 判断是否禁用上传和删除
|
||||
const self_disabled = computed(() => {
|
||||
return props.disabled || formContext?.disabled;
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
/**
|
||||
* @description 图片上传
|
||||
* @param options upload 所有配置项
|
||||
* */
|
||||
interface UploadEmits {
|
||||
(e: 'update:imageUrl', value: string): void,
|
||||
interface UploadEmits {
|
||||
(e: 'update:imageUrl', value: string): void;
|
||||
(e: 'changeFileName', value: string): void;
|
||||
}
|
||||
}
|
||||
|
||||
const fileName=ref('')
|
||||
const oImg=ref('')
|
||||
const emit = defineEmits<UploadEmits>();
|
||||
const loading=ref(false)
|
||||
const progress=ref(0)
|
||||
const cropperSuccess=(fileChile:any)=>{
|
||||
actionFile(fileChile)
|
||||
}
|
||||
const actionFile=async (fileChild:any)=>{
|
||||
loading.value=true
|
||||
const fileName = ref('');
|
||||
const oImg = ref('');
|
||||
const emit = defineEmits<UploadEmits>();
|
||||
const loading = ref(false);
|
||||
const progress = ref(0);
|
||||
const cropperSuccess = (fileChile: any) => {
|
||||
actionFile(fileChile);
|
||||
};
|
||||
const actionFile = async (fileChild: any) => {
|
||||
loading.value = true;
|
||||
try {
|
||||
const formData = new FormData()
|
||||
formData.append('file',fileChild)
|
||||
formData.append('name',props.name)
|
||||
const res =await upload(formData)
|
||||
emit('update:imageUrl',res.fileUrl);
|
||||
emit('changeFileName',res.originalName)
|
||||
const formData = new FormData();
|
||||
formData.append('file', fileChild);
|
||||
formData.append('name', props.name);
|
||||
const res = await upload(formData);
|
||||
emit('update:imageUrl', res.fileUrl);
|
||||
emit('changeFileName', res.originalName);
|
||||
// 调用 el-form 内部的校验方法(可自动校验)
|
||||
formItemContext?.prop && formContext?.validateField([formItemContext.prop as string]);
|
||||
} catch (error) {
|
||||
emit('update:imageUrl','');
|
||||
emit('changeFileName','')
|
||||
}finally {
|
||||
progress.value=0
|
||||
loading.value=false
|
||||
emit('update:imageUrl', '');
|
||||
emit('changeFileName', '');
|
||||
} finally {
|
||||
progress.value = 0;
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleHttpUploadOptions=ref({})
|
||||
const handleHttpUploadOptions = ref({});
|
||||
|
||||
const handleHttpUpload = async (options: UploadRequestOptions) => {
|
||||
handleHttpUploadOptions.value=options
|
||||
if(props.cropper){
|
||||
fileName.value= options.file.name
|
||||
const handleHttpUpload = async (options: UploadRequestOptions) => {
|
||||
handleHttpUploadOptions.value = options;
|
||||
if (props.cropper) {
|
||||
fileName.value = options.file.name;
|
||||
let reader = new FileReader();
|
||||
reader.readAsDataURL(options.file);
|
||||
let img = new Image();
|
||||
@ -177,37 +177,37 @@ const handleHttpUpload = async (options: UploadRequestOptions) => {
|
||||
img.src = this.result;
|
||||
img.onload = function () {
|
||||
let base = e.target.result;
|
||||
oImg.value= base;
|
||||
event.target.value = "";
|
||||
oImg.value = base;
|
||||
event.target.value = '';
|
||||
cropperVisible.value = true;
|
||||
};
|
||||
};
|
||||
return
|
||||
return;
|
||||
}
|
||||
actionFile(options.file)
|
||||
};
|
||||
actionFile(options.file);
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* @description 删除图片
|
||||
* */
|
||||
const deleteImg = () => {
|
||||
const deleteImg = () => {
|
||||
emit('update:imageUrl', '');
|
||||
emit('changeFileName','')
|
||||
};
|
||||
emit('changeFileName', '');
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* @description 编辑图片
|
||||
* */
|
||||
const editImg = () => {
|
||||
const editImg = () => {
|
||||
const dom = document.querySelector(`#${uuid.value} .el-upload__input`);
|
||||
dom && dom.dispatchEvent(new MouseEvent('click'));
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* @description 文件上传之前判断
|
||||
* @param rawFile 选择的文件
|
||||
* */
|
||||
const beforeUpload: UploadProps['beforeUpload'] = (rawFile) => {
|
||||
const beforeUpload: UploadProps['beforeUpload'] = (rawFile) => {
|
||||
const imgSize = rawFile.size / 1024 / 1024 < props.fileSize;
|
||||
const imgType = props.fileType.includes(rawFile.type as File.ImageMimeType);
|
||||
if (!imgType)
|
||||
@ -225,21 +225,21 @@ const beforeUpload: UploadProps['beforeUpload'] = (rawFile) => {
|
||||
});
|
||||
}, 0);
|
||||
return imgType && imgSize;
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* @description 图片上传错误
|
||||
* */
|
||||
const uploadError = () => {
|
||||
const uploadError = () => {
|
||||
ElNotification({
|
||||
title: '温馨提示',
|
||||
message: '图片上传失败,请您重新上传!',
|
||||
type: 'error',
|
||||
});
|
||||
};
|
||||
};
|
||||
</script>
|
||||
<style scoped lang="scss">
|
||||
.is-error {
|
||||
.is-error {
|
||||
.upload {
|
||||
:deep(.el-upload),
|
||||
:deep(.el-upload-dragger) {
|
||||
@ -249,8 +249,8 @@ const uploadError = () => {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
:deep(.disabled) {
|
||||
}
|
||||
:deep(.disabled) {
|
||||
.el-upload,
|
||||
.el-upload-dragger {
|
||||
cursor: not-allowed !important;
|
||||
@ -260,8 +260,8 @@ const uploadError = () => {
|
||||
border: 1px dashed var(--el-border-color-darker) !important;
|
||||
}
|
||||
}
|
||||
}
|
||||
.upload-box {
|
||||
}
|
||||
.upload-box {
|
||||
.no-border {
|
||||
:deep(.el-upload) {
|
||||
border: none !important;
|
||||
@ -305,8 +305,8 @@ const uploadError = () => {
|
||||
border: 2px dashed var(--el-color-primary) !important;
|
||||
}
|
||||
.upload-image {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
width: 90%;
|
||||
height: 90%;
|
||||
object-fit: contain;
|
||||
}
|
||||
.upload-empty {
|
||||
@ -361,5 +361,5 @@ const uploadError = () => {
|
||||
line-height: 18px;
|
||||
text-align: center;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
@ -1,8 +1,7 @@
|
||||
<template>
|
||||
<div class="upload-box">
|
||||
<div v-for="(item, index) in fileList" class="items">
|
||||
<img v-if="item.filePath" :src="item.filePath"
|
||||
class="upload-image" />
|
||||
<img v-if="item.filePath" :src="item.filePath" class="upload-image" />
|
||||
<div class="upload-handle" @click.stop>
|
||||
<div class="handle-icon" @click="handleEdit(index)" v-if="!self_disabled">
|
||||
<el-icon :size="props.iconSize">
|
||||
@ -21,213 +20,227 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<el-upload action="#" :id="uuid" class="upload-demo my-upload-images"
|
||||
:class="fileList.length > 0 && !loading ? 'success-file' : ''" :multiple="multiple" ref="uploadRef"
|
||||
:disabled="loading ? true : (self_disabled || fileList.length >= props.limit)" :http-request="handleHttpUpload"
|
||||
:before-upload="beforeUpload" list-type="picture-card" :limit="limit" :file-list="fileList"
|
||||
:show-file-list="false" v-if="!self_disabled" :on-exceed="onExceed" :accept="props.fileType">
|
||||
<el-upload
|
||||
action="#"
|
||||
:id="uuid"
|
||||
class="upload-demo my-upload-images"
|
||||
:class="fileList.length > 0 && !loading ? 'success-file' : ''"
|
||||
:multiple="multiple"
|
||||
ref="uploadRef"
|
||||
:disabled="loading ? true : self_disabled || fileList.length >= props.limit"
|
||||
:http-request="handleHttpUpload"
|
||||
:before-upload="beforeUpload"
|
||||
list-type="picture-card"
|
||||
:limit="limit"
|
||||
:file-list="fileList"
|
||||
:show-file-list="false"
|
||||
v-if="!self_disabled"
|
||||
:on-exceed="onExceed"
|
||||
:accept="props.fileType"
|
||||
>
|
||||
<el-icon v-if="!loading">
|
||||
<Plus />
|
||||
</el-icon>
|
||||
<el-progress type="circle" v-else :percentage="progress"></el-progress>
|
||||
<el-progress type="circle" v-else :percentage="progress" />
|
||||
</el-upload>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed,ref } from "vue";
|
||||
import { ElNotification, UploadFile, UploadInstance } from "element-plus";
|
||||
import type { UploadProps, UploadRequestOptions } from "element-plus";
|
||||
import { Plus } from '@element-plus/icons-vue'
|
||||
import { generateUUID } from "@/utils/auth";
|
||||
import { computed, ref } from 'vue';
|
||||
import { ElNotification, UploadFile, UploadInstance } from 'element-plus';
|
||||
import type { UploadProps, UploadRequestOptions } from 'element-plus';
|
||||
import { Plus } from '@element-plus/icons-vue';
|
||||
import { generateUUID } from '@/utils/auth';
|
||||
|
||||
const uploadRef = ref<UploadInstance>();
|
||||
import {upload} from '@/api/common'
|
||||
const uploadRef = ref<UploadInstance>();
|
||||
import { upload } from '@/api/common';
|
||||
|
||||
// 接受父组件参数
|
||||
const props = defineProps({
|
||||
// 接受父组件参数
|
||||
const props = defineProps({
|
||||
zIndex: {
|
||||
default: -1
|
||||
default: -1,
|
||||
},
|
||||
multiple: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
default: false,
|
||||
},
|
||||
btnTip: {
|
||||
type: String,
|
||||
default: ''
|
||||
default: '',
|
||||
},
|
||||
limit: {
|
||||
type: Number,
|
||||
default: undefined
|
||||
default: undefined,
|
||||
},
|
||||
width: {
|
||||
type: String,
|
||||
default: '150px'
|
||||
default: '150px',
|
||||
},
|
||||
height: {
|
||||
type: String,
|
||||
default: '150px'
|
||||
default: '150px',
|
||||
},
|
||||
borderRadius: {
|
||||
type: String,
|
||||
default: '8px'
|
||||
default: '8px',
|
||||
},
|
||||
fileList: {
|
||||
type: Array,
|
||||
required: true,
|
||||
default: []
|
||||
default: [],
|
||||
},
|
||||
disabled: {
|
||||
default: false
|
||||
default: false,
|
||||
},
|
||||
fileSize: {
|
||||
default: 200
|
||||
default: 200,
|
||||
},
|
||||
orderNo: {
|
||||
default: ''
|
||||
default: '',
|
||||
},
|
||||
fileType: {
|
||||
default: ""
|
||||
default: '',
|
||||
},
|
||||
name: {
|
||||
default: ""
|
||||
}
|
||||
});
|
||||
// 生成组件唯一id
|
||||
const uuid = ref("id-" + generateUUID());
|
||||
default: '',
|
||||
},
|
||||
});
|
||||
// 生成组件唯一id
|
||||
const uuid = ref('id-' + generateUUID());
|
||||
|
||||
// 判断是否禁用上传和删除
|
||||
const self_disabled = computed(() => {
|
||||
// 判断是否禁用上传和删除
|
||||
const self_disabled = computed(() => {
|
||||
return props.disabled;
|
||||
});
|
||||
let editIndex = ''
|
||||
});
|
||||
let editIndex = '';
|
||||
|
||||
const emit = defineEmits(["upload"]);
|
||||
const progress = ref(0)
|
||||
const loading = ref(false);
|
||||
const handleHttpUpload = async (options: UploadRequestOptions) => {
|
||||
const emit = defineEmits(['upload']);
|
||||
const progress = ref(0);
|
||||
const loading = ref(false);
|
||||
const handleHttpUpload = async (options: UploadRequestOptions) => {
|
||||
loading.value = true;
|
||||
try {
|
||||
const formData = new FormData()
|
||||
formData.append('file',options.file)
|
||||
formData.append('name',props.name)
|
||||
const res =await upload(formData)
|
||||
const formData = new FormData();
|
||||
formData.append('file', options.file);
|
||||
formData.append('name', props.name);
|
||||
const res = await upload(formData);
|
||||
if (props.multiple) {
|
||||
let list = JSON.parse(JSON.stringify(props.fileList))
|
||||
let list = JSON.parse(JSON.stringify(props.fileList));
|
||||
if (editIndex !== '') {
|
||||
list.splice(editIndex, 1, {
|
||||
url: res.fileUrl,
|
||||
name: res.originalName,
|
||||
filePath: res.fileUrl,
|
||||
fileName: res.originalName
|
||||
})
|
||||
editIndex = ''
|
||||
fileName: res.originalName,
|
||||
});
|
||||
editIndex = '';
|
||||
} else {
|
||||
list.push({
|
||||
url: res.fileUrl,
|
||||
name: res.originalName,
|
||||
filePath: res.fileUrl,
|
||||
fileName: res.originalName
|
||||
})
|
||||
fileName: res.originalName,
|
||||
});
|
||||
}
|
||||
emit('upload', list,props.zIndex)
|
||||
emit('upload', list, props.zIndex);
|
||||
} else {
|
||||
emit("upload", res, props.zIndex);
|
||||
emit('upload', res, props.zIndex);
|
||||
}
|
||||
} catch (error) {
|
||||
ElNotification({
|
||||
title: "温馨提示",
|
||||
message: "上传文件失败!",
|
||||
type: "error"
|
||||
title: '温馨提示',
|
||||
message: '上传文件失败!',
|
||||
type: 'error',
|
||||
});
|
||||
if (props.multiple) {
|
||||
emit('upload', props.fileList,props.zIndex)
|
||||
emit('upload', props.fileList, props.zIndex);
|
||||
} else {
|
||||
uploadRef.value!.clearFiles();
|
||||
emit("upload", "", props.zIndex);
|
||||
emit('upload', '', props.zIndex);
|
||||
}
|
||||
options.onError(error as any);
|
||||
} finally {
|
||||
progress.value = 0
|
||||
progress.value = 0;
|
||||
loading.value = false;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const onPreview = (index: any) => {
|
||||
window.open(props.fileList[index].filePath)
|
||||
}
|
||||
const onPreview = (index: any) => {
|
||||
window.open(props.fileList[index].filePath);
|
||||
};
|
||||
|
||||
const handleEdit = (index: any) => {
|
||||
editIndex = index
|
||||
const handleEdit = (index: any) => {
|
||||
editIndex = index;
|
||||
const dom = document.querySelector(`#${uuid.value} .el-upload__input`);
|
||||
dom && dom.dispatchEvent(new MouseEvent('click'));
|
||||
}
|
||||
const handleRemove = (index: any) => {
|
||||
editIndex = index
|
||||
};
|
||||
const handleRemove = (index: any) => {
|
||||
editIndex = index;
|
||||
if (props.multiple) {
|
||||
emit("upload", props.fileList.filter((f, i) => !(editIndex == i)),props.zIndex)
|
||||
emit(
|
||||
'upload',
|
||||
props.fileList.filter((f, i) => !(editIndex == i)),
|
||||
props.zIndex,
|
||||
);
|
||||
} else {
|
||||
uploadRef.value!.clearFiles();
|
||||
emit("upload", "" ,props.zIndex);
|
||||
emit('upload', '', props.zIndex);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const onExceed = () => {
|
||||
const onExceed = () => {
|
||||
if (props.limit) {
|
||||
ElNotification({
|
||||
title: "温馨提示",
|
||||
title: '温馨提示',
|
||||
message: `最多支持上传${props.limit}张`,
|
||||
type: "warning"
|
||||
type: 'warning',
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
/**
|
||||
* @description 文件上传之前判断
|
||||
* @param rawFile 选择的文件
|
||||
* */
|
||||
const beforeUpload: UploadProps["beforeUpload"] = (rawFile) => {
|
||||
const beforeUpload: UploadProps['beforeUpload'] = (rawFile) => {
|
||||
const imgSize = rawFile.size / 1024 / 1024 < props.fileSize;
|
||||
if (!imgSize) {
|
||||
ElNotification({
|
||||
title: "温馨提示",
|
||||
title: '温馨提示',
|
||||
message: `上传文件大小不能超过 ${props.fileSize}M!`,
|
||||
type: "warning"
|
||||
type: 'warning',
|
||||
});
|
||||
return false;
|
||||
}
|
||||
if (props.fileType) {
|
||||
let fileType = props.fileType.replace(/\s/g, "");
|
||||
fileType.split('.').join('|')
|
||||
let fileIndex = rawFile.name.lastIndexOf('.')
|
||||
let substrName = rawFile.name.substr(fileIndex)
|
||||
let fileType = props.fileType.replace(/\s/g, '');
|
||||
fileType.split('.').join('|');
|
||||
let fileIndex = rawFile.name.lastIndexOf('.');
|
||||
let substrName = rawFile.name.substr(fileIndex);
|
||||
if (fileType.indexOf(substrName) == -1) {
|
||||
ElNotification({
|
||||
title: "温馨提示",
|
||||
message: "上传文件不符合所需的格式!",
|
||||
type: "warning"
|
||||
title: '温馨提示',
|
||||
message: '上传文件不符合所需的格式!',
|
||||
type: 'warning',
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
};
|
||||
</script>
|
||||
<style scoped lang="scss">
|
||||
.success-file {
|
||||
.success-file {
|
||||
.el-upload {
|
||||
.el-upload-dragger {
|
||||
border-width: 3px;
|
||||
border-color: var(--el-color-success) !important;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.my-upload-images {
|
||||
.my-upload-images {
|
||||
.el-upload-dragger {
|
||||
padding: var(--el-upload-dragger-padding-vertical);
|
||||
}
|
||||
@ -239,24 +252,24 @@ const beforeUpload: UploadProps["beforeUpload"] = (rawFile) => {
|
||||
.el-icon--close-tip {
|
||||
display: none !important;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.el-form-item.is-error {
|
||||
.el-form-item.is-error {
|
||||
.el-upload-dragger {
|
||||
border-width: 3px;
|
||||
border-color: var(--el-color-error) !important;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
:deep(.el-upload-list__item){
|
||||
:deep(.el-upload-list__item) {
|
||||
transition: none !important;
|
||||
}
|
||||
}
|
||||
|
||||
.upload-box {
|
||||
.upload-box {
|
||||
position: relative;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
>div {
|
||||
> div {
|
||||
margin-right: 10px;
|
||||
margin-bottom: 10px;
|
||||
position: relative;
|
||||
@ -316,5 +329,5 @@ const beforeUpload: UploadProps["beforeUpload"] = (rawFile) => {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
@ -9,7 +9,7 @@
|
||||
:before-close="dialogClose"
|
||||
>
|
||||
<div class="cropper_content">
|
||||
<div class="cropper" style="text-align: center;">
|
||||
<div class="cropper" style="text-align: center">
|
||||
<vueCropper
|
||||
ref="cropperRef"
|
||||
:img="options.img"
|
||||
@ -25,11 +25,11 @@
|
||||
:fixedBox="options.fixedBox"
|
||||
:enlarge="options.enlarge"
|
||||
:fixedNumber="options.fixedNumber"
|
||||
></vueCropper>
|
||||
/>
|
||||
</div>
|
||||
<div class="cropper_btns">
|
||||
<el-button @click="rotateLeft" icon="RefreshLeft" size="mini" title="左旋转"></el-button>
|
||||
<el-button @click="rotateRight" icon="RefreshRight" size="mini" title="右旋转"></el-button>
|
||||
<el-button @click="rotateLeft" icon="RefreshLeft" size="mini" title="左旋转" />
|
||||
<el-button @click="rotateRight" icon="RefreshRight" size="mini" title="右旋转" />
|
||||
<el-button @click="changeScale(1)" size="mini" title="放大">+</el-button>
|
||||
<el-button @click="changeScale(-1)" size="mini" title="缩小">-</el-button>
|
||||
</div>
|
||||
@ -44,42 +44,42 @@
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import VueCropper from "vue-cropper/src/vue-cropper.vue";
|
||||
import { ref, reactive} from 'vue';
|
||||
import VueCropper from 'vue-cropper/src/vue-cropper.vue';
|
||||
import { ref, reactive } from 'vue';
|
||||
|
||||
const props = defineProps({
|
||||
const props = defineProps({
|
||||
visible: {
|
||||
type: Boolean,
|
||||
required: true,
|
||||
default: false
|
||||
default: false,
|
||||
},
|
||||
cropperSize: {
|
||||
type: Object,
|
||||
default() {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
},
|
||||
oImg: {
|
||||
type: String,
|
||||
default: ""
|
||||
default: '',
|
||||
},
|
||||
fileName: {
|
||||
type: String,
|
||||
default: ""
|
||||
default: '',
|
||||
},
|
||||
folder: {
|
||||
type: String,
|
||||
default: ""
|
||||
}
|
||||
});
|
||||
default: '',
|
||||
},
|
||||
});
|
||||
|
||||
const dialogClose = () => {
|
||||
emit("update:visible", false);
|
||||
};
|
||||
const options = reactive({
|
||||
const dialogClose = () => {
|
||||
emit('update:visible', false);
|
||||
};
|
||||
const options = reactive({
|
||||
img: props.oImg, // 裁剪图片的地址
|
||||
outputSize: 1, // 裁剪生成图片的质量
|
||||
outputType: "png", // 裁剪生成图片的格式
|
||||
outputType: 'png', // 裁剪生成图片的格式
|
||||
info: true, // 裁剪框的大小信息
|
||||
canScale: true, // 图片是否允许滚动缩放
|
||||
autoCrop: true, // 是否默认生成截图框
|
||||
@ -97,31 +97,31 @@ const options = reactive({
|
||||
infoTrue: true, // true为展示真实输出图片宽高false展示看到的截图框宽高
|
||||
maximgSize: 100, // 限制图片最大宽度和高度
|
||||
enlarge: 1, // 图片根据截图框输出比例倍数
|
||||
mode: "contain" // 图片默认渲染方式(contain, cover, 100px, 100% auto)
|
||||
});
|
||||
const cropperRef = ref();
|
||||
mode: 'contain', // 图片默认渲染方式(contain, cover, 100px, 100% auto)
|
||||
});
|
||||
const cropperRef = ref();
|
||||
|
||||
const addImgLoading = ref(false);
|
||||
const addImgLoading = ref(false);
|
||||
|
||||
// 左旋转
|
||||
const rotateLeft = () => {
|
||||
// 左旋转
|
||||
const rotateLeft = () => {
|
||||
cropperRef.value.rotateLeft();
|
||||
};
|
||||
// 右旋转
|
||||
const rotateRight = () => {
|
||||
};
|
||||
// 右旋转
|
||||
const rotateRight = () => {
|
||||
cropperRef.value.rotateRight();
|
||||
};
|
||||
// 放大缩小
|
||||
const changeScale = (num: any) => {
|
||||
};
|
||||
// 放大缩小
|
||||
const changeScale = (num: any) => {
|
||||
num = num || 1;
|
||||
cropperRef.value.changeScale(num);
|
||||
};
|
||||
};
|
||||
|
||||
const emit = defineEmits(["success", "update:visible"]);
|
||||
const emit = defineEmits(['success', 'update:visible']);
|
||||
|
||||
// base64转图片文件
|
||||
const dataURLtoFile=(dataurl:any, filename:any)=> {
|
||||
let arr = dataurl.split(",");
|
||||
// base64转图片文件
|
||||
const dataURLtoFile = (dataurl: any, filename: any) => {
|
||||
let arr = dataurl.split(',');
|
||||
let mime = arr[0].match(/:(.*?);/)[1];
|
||||
let bstr = atob(arr[1]);
|
||||
let len = bstr.length;
|
||||
@ -129,25 +129,25 @@ const dataURLtoFile=(dataurl:any, filename:any)=> {
|
||||
while (len--) {
|
||||
u8arr[len] = bstr.charCodeAt(len);
|
||||
}
|
||||
return new File([u8arr], filename, {type: mime});
|
||||
}
|
||||
return new File([u8arr], filename, { type: mime });
|
||||
};
|
||||
|
||||
const submit=()=>{
|
||||
cropperRef.value.getCropData(fileData => {
|
||||
const submit = () => {
|
||||
cropperRef.value.getCropData((fileData) => {
|
||||
let file = dataURLtoFile(fileData, props.fileName);
|
||||
emit("update:visible", false);
|
||||
emit('success',file)
|
||||
})
|
||||
}
|
||||
emit('update:visible', false);
|
||||
emit('success', file);
|
||||
});
|
||||
};
|
||||
|
||||
// 暴露变量
|
||||
defineExpose({});
|
||||
// 暴露变量
|
||||
defineExpose({});
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
.cropper_model_dlg {
|
||||
.cropper_model_dlg {
|
||||
.el-dialog__body {
|
||||
padding: 0px 20px 60px !important
|
||||
padding: 0px 20px 60px !important;
|
||||
}
|
||||
|
||||
.cropper_content {
|
||||
@ -193,5 +193,5 @@ defineExpose({});
|
||||
.cropper_btns {
|
||||
margin-top: 20px;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
@ -3,22 +3,22 @@
|
||||
action="#"
|
||||
:id="uuid"
|
||||
class="upload-demo my-upload-file"
|
||||
:class="fileList.length>0&&!loading?'success-file':''"
|
||||
:class="fileList.length > 0 && !loading ? 'success-file' : ''"
|
||||
:multiple="multiple"
|
||||
ref="uploadRef"
|
||||
:disabled="loading?true:self_disabled"
|
||||
:disabled="loading ? true : self_disabled"
|
||||
:http-request="handleHttpUpload"
|
||||
:file-list="fileList"
|
||||
:on-preview="onPreview"
|
||||
:on-remove="handleRemove"
|
||||
:before-upload="beforeUpload"
|
||||
:show-file-list="showFileList"
|
||||
:drag="isBtn?false:true"
|
||||
:drag="isBtn ? false : true"
|
||||
:limit="limit"
|
||||
>
|
||||
<template v-if="isBtn">
|
||||
<el-tooltip
|
||||
v-if="isBtn&&btnTip"
|
||||
v-if="isBtn && btnTip"
|
||||
class="box-item"
|
||||
effect="dark"
|
||||
:content="btnTip"
|
||||
@ -30,197 +30,201 @@
|
||||
</template>
|
||||
<template v-else>
|
||||
<el-icon class="el-icon--upload">
|
||||
<Finished style="color: var(--el-color-success)" v-if="fileList.length>0&&!loading"/>
|
||||
<upload-filled v-else/>
|
||||
<Finished style="color: var(--el-color-success)" v-if="fileList.length > 0 && !loading" />
|
||||
<upload-filled v-else />
|
||||
</el-icon>
|
||||
<div v-if="!self_disabled" class="el-upload__text">{{loading?'上传中':(fileList.length>0?'上传成功':'点击或将文件拖拽到这里上传')}}</div>
|
||||
<div v-if="!self_disabled" class="el-upload__text">{{
|
||||
loading ? '上传中' : fileList.length > 0 ? '上传成功' : '点击或将文件拖拽到这里上传'
|
||||
}}</div>
|
||||
</template>
|
||||
<template #tip>
|
||||
<template v-if="!isBtn">
|
||||
<div v-if="props.fileType" class="el-upload__tip">支持扩展名:{{ props.fileType }}</div>
|
||||
<div v-if="props.multiple && props.limit" class="el-upload__tip">最多上传{{props.limit}}个文件</div>
|
||||
<div v-if="props.multiple && props.limit" class="el-upload__tip"
|
||||
>最多上传{{ props.limit }}个文件</div
|
||||
>
|
||||
</template>
|
||||
</template>
|
||||
</el-upload>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed,ref} from "vue";
|
||||
import {ElNotification, UploadFile, UploadInstance} from "element-plus";
|
||||
import type {UploadProps, UploadRequestOptions} from "element-plus";
|
||||
import {generateUUID} from "@/utils/auth";
|
||||
import { computed, ref } from 'vue';
|
||||
import { ElNotification, UploadFile, UploadInstance } from 'element-plus';
|
||||
import type { UploadProps, UploadRequestOptions } from 'element-plus';
|
||||
import { generateUUID } from '@/utils/auth';
|
||||
|
||||
const uploadRef = ref<UploadInstance>();
|
||||
import {upload} from '@/api/common'
|
||||
const uploadRef = ref<UploadInstance>();
|
||||
import { upload } from '@/api/common';
|
||||
|
||||
// 接受父组件参数
|
||||
const props = defineProps({
|
||||
// 接受父组件参数
|
||||
const props = defineProps({
|
||||
zIndex: {
|
||||
default: -1
|
||||
default: -1,
|
||||
},
|
||||
multiple: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
default: false,
|
||||
},
|
||||
isBtn: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
default: false,
|
||||
},
|
||||
showFileList: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
default: true,
|
||||
},
|
||||
btnTip: {
|
||||
type: String,
|
||||
default: ''
|
||||
default: '',
|
||||
},
|
||||
limit: {
|
||||
type: Number,
|
||||
default: undefined
|
||||
default: undefined,
|
||||
},
|
||||
fileList: {
|
||||
type: Array,
|
||||
required: true,
|
||||
default: []
|
||||
default: [],
|
||||
},
|
||||
disabled: {
|
||||
default: false
|
||||
default: false,
|
||||
},
|
||||
fileSize: {
|
||||
default: 200
|
||||
default: 200,
|
||||
},
|
||||
orderNo: {
|
||||
default: ''
|
||||
default: '',
|
||||
},
|
||||
fileType: {
|
||||
default: ""
|
||||
default: '',
|
||||
},
|
||||
name: {
|
||||
default: ""
|
||||
}
|
||||
});
|
||||
default: '',
|
||||
},
|
||||
});
|
||||
|
||||
// 生成组件唯一id
|
||||
const uuid = ref("id-" + generateUUID());
|
||||
// 生成组件唯一id
|
||||
const uuid = ref('id-' + generateUUID());
|
||||
|
||||
// 判断是否禁用上传和删除
|
||||
const self_disabled = computed(() => {
|
||||
// 判断是否禁用上传和删除
|
||||
const self_disabled = computed(() => {
|
||||
return props.disabled;
|
||||
});
|
||||
});
|
||||
|
||||
const emit = defineEmits(['upload']);
|
||||
|
||||
const emit = defineEmits(["upload"]);
|
||||
|
||||
const loading = ref(false);
|
||||
const handleHttpUpload = async (options: UploadRequestOptions) => {
|
||||
|
||||
const loading = ref(false);
|
||||
const handleHttpUpload = async (options: UploadRequestOptions) => {
|
||||
loading.value = true;
|
||||
try {
|
||||
const formData = new FormData()
|
||||
formData.append('file',options.file)
|
||||
formData.append('name',props.name)
|
||||
const res =await upload(formData)
|
||||
if(props.multiple){
|
||||
let list=JSON.parse(JSON.stringify(props.fileList))
|
||||
const formData = new FormData();
|
||||
formData.append('file', options.file);
|
||||
formData.append('name', props.name);
|
||||
const res = await upload(formData);
|
||||
if (props.multiple) {
|
||||
let list = JSON.parse(JSON.stringify(props.fileList));
|
||||
list.push({
|
||||
url: res.fileUrl,
|
||||
name: res.originalName,
|
||||
filePath: res.fileUrl,
|
||||
fileName: res.originalName,
|
||||
})
|
||||
emit('upload', list,props.zIndex)
|
||||
}else{
|
||||
emit("upload", res.fileUrl,res.originalName,props.zIndex);
|
||||
});
|
||||
emit('upload', list, props.zIndex);
|
||||
} else {
|
||||
emit('upload', res.fileUrl, res.originalName, props.zIndex);
|
||||
}
|
||||
} catch (error) {
|
||||
ElNotification({
|
||||
title: "温馨提示",
|
||||
message: "上传文件失败!",
|
||||
type: "error"
|
||||
title: '温馨提示',
|
||||
message: '上传文件失败!',
|
||||
type: 'error',
|
||||
});
|
||||
if(props.multiple){
|
||||
emit('upload', props.fileList,props.zIndex)
|
||||
}else{
|
||||
if (props.multiple) {
|
||||
emit('upload', props.fileList, props.zIndex);
|
||||
} else {
|
||||
uploadRef.value!.clearFiles();
|
||||
emit("upload", "", "",props.zIndex);
|
||||
emit('upload', '', '', props.zIndex);
|
||||
}
|
||||
options.onError(error as any);
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const onPreview=(uploadFile: UploadFile)=> {
|
||||
window.open( uploadFile?.url)
|
||||
}
|
||||
const onPreview = (uploadFile: UploadFile) => {
|
||||
window.open(uploadFile?.url);
|
||||
};
|
||||
|
||||
const handleRemove: UploadProps["onRemove"] = (file:any) => {
|
||||
if(props.multiple){
|
||||
emit("upload",props.fileList.filter((f) => !(f === file.url)),props.zIndex)
|
||||
}else{
|
||||
const handleRemove: UploadProps['onRemove'] = (file: any) => {
|
||||
if (props.multiple) {
|
||||
emit(
|
||||
'upload',
|
||||
props.fileList.filter((f) => !(f === file.url)),
|
||||
props.zIndex,
|
||||
);
|
||||
} else {
|
||||
uploadRef.value!.clearFiles();
|
||||
emit("upload", "", "", props.zIndex);
|
||||
emit('upload', '', '', props.zIndex);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
/**
|
||||
* @description 文件上传之前判断
|
||||
* @param rawFile 选择的文件
|
||||
* */
|
||||
const beforeUpload: UploadProps["beforeUpload"] = (rawFile) => {
|
||||
const beforeUpload: UploadProps['beforeUpload'] = (rawFile) => {
|
||||
const imgSize = rawFile.size / 1024 / 1024 < props.fileSize;
|
||||
if (!imgSize) {
|
||||
ElNotification({
|
||||
title: "温馨提示",
|
||||
title: '温馨提示',
|
||||
message: `上传文件大小不能超过 ${props.fileSize}M!`,
|
||||
type: "warning"
|
||||
type: 'warning',
|
||||
});
|
||||
return false;
|
||||
}
|
||||
if( props.fileType){
|
||||
let fileType = props.fileType.replace(/\s/g, "");
|
||||
fileType.split('.').join('|')
|
||||
let fileIndex = rawFile.name.lastIndexOf('.')
|
||||
let substrName = rawFile.name.substr(fileIndex)
|
||||
if (fileType.indexOf(substrName)==-1) {
|
||||
if (props.fileType) {
|
||||
let fileType = props.fileType.replace(/\s/g, '');
|
||||
fileType.split('.').join('|');
|
||||
let fileIndex = rawFile.name.lastIndexOf('.');
|
||||
let substrName = rawFile.name.substr(fileIndex);
|
||||
if (fileType.indexOf(substrName) == -1) {
|
||||
ElNotification({
|
||||
title: "温馨提示",
|
||||
message: "上传文件不符合所需的格式!",
|
||||
type: "warning"
|
||||
title: '温馨提示',
|
||||
message: '上传文件不符合所需的格式!',
|
||||
type: 'warning',
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
};
|
||||
</script>
|
||||
<style lang="scss">
|
||||
.success-file {
|
||||
.success-file {
|
||||
.el-upload {
|
||||
.el-upload-dragger {
|
||||
border-width: 3px;
|
||||
border-color: var(--el-color-success) !important;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.my-upload-file{
|
||||
.el-upload-dragger{
|
||||
.my-upload-file {
|
||||
.el-upload-dragger {
|
||||
padding: var(--el-upload-dragger-padding-vertical);
|
||||
}
|
||||
.el-upload-list__item.is-success.el-list-leave-active.el-list-leave-to{
|
||||
.el-upload-list__item.is-success.el-list-leave-active.el-list-leave-to {
|
||||
display: none;
|
||||
}
|
||||
.el-icon--close-tip{
|
||||
display: none!important;
|
||||
.el-icon--close-tip {
|
||||
display: none !important;
|
||||
}
|
||||
}
|
||||
.el-form-item.is-error{
|
||||
}
|
||||
.el-form-item.is-error {
|
||||
.el-upload-dragger {
|
||||
border-width: 3px;
|
||||
border-color: var(--el-color-error) !important;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user