123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- <template>
- <div class="upload_wrap">
- <block v-if="waitFileList.length > 0">
- <div class="el-upload-list el-upload-list--picture-card" v-if="listType == 'picture-card' || listType == 'picture'">
- <div class="el-upload-list__item is-success" v-for="( item, index ) in waitFileList " :key="index">
- <el-image style="width:148px;height:148px;" :src="item.url" fit="fill" :lazy="true"></el-image>
- <div v-if="!props.isDisableUpload" class="el-upload-list__item-actions">
- <el-icon class="cursor-pointer el-upload-list__item-delete" @click="removeFile(item)">
- <DeleteFilled />
- </el-icon>
- </div>
- <span v-if="isDownLoad" style="paddingleft: 5px">
- <el-icon @click="handleDownLoad(item)">
- <Download />
- </el-icon>
- </span>
- </div>
- </div>
- <div class="template_list" v-if="listType == 'text'">
- <div class="template" v-for="(item, index) in waitFileList" :key="index">
- <span>
- <el-icon>
- <Link />
- </el-icon>
- </span>
- <span class="documentName">{{ item.name }}</span>
- <span v-if="!props.isDisableUpload">
- <el-icon color="#000000a6" size="16" @click="removeFile(item)">
- <Close />
- </el-icon>
- </span>
- <span v-if="isDownLoad" style="paddingleft: 5px">
- <el-icon @click="handleDownLoad(item)">
- <Download />
- </el-icon>
- </span>
- </div>
- </div>
- </block>
- <el-upload v-else class="w-auto upload" ref="uploadRef" :file-list="waitFileList" :multiple="props.isMultiple"
- :limit="props.limitNum" :list-type="listType" :accept="props.acceptType" :auto-upload="false"
- :show-file-list="false" :disabled="props.isDisableUpload" :on-change="handleChange" :on-remove="handleRemove">
- <block>
- <div class="el-upload__text" v-if="listType == 'text'">
- <el-icon>
- <Upload />
- </el-icon>
- <span>上传文件</span>
- </div>
- <div v-else>
- <el-icon>
- <Plus />
- </el-icon>
- </div>
- </block>
- <template #tip v-if="!props.isDisableUpload && isShowTips">
- <div class="el-upload__tip">
- <span>支持{{ acceptTypeDesc }};</span> <span v-if="isLimitSize">文件大小不能超过{{
- props.maxFileSize }}M</span>
- </div>
- </template>
- </el-upload>
- <el-dialog v-model="dialogVisible">
- <img w-full :src="dialogImageUrl" alt="Preview Image" />
- </el-dialog>
- </div>
- </template>
- <script lang="ts" setup>
- import { ref, watch } from "vue";
- import { ElLoading, ElMessage } from "element-plus";
- import http from '@/api/http';
- import { Close, Upload, Download, Plus, Link, DeleteFilled } from "@element-plus/icons-vue";
- import type { UploadProps, UploadUserFile } from 'element-plus'
- const emits = defineEmits(["fileSuccess", "fileRemove"]);
- interface Props {
- acceptType?: string; // 上传文件类型
- acceptTypeDesc?: string; // 描述 - 上传文件类型
- isMultiple?: boolean; // 是否可批量上传
- isCheckName?: boolean; // 是否检查文件名
- limitNum?: number; // 允许上传文件的最大数量
- isDisableUpload?: boolean; // 是否禁用上传
- maxFileSize?: number; // 文件大小
- isLimitSize?: boolean;//是否限制大小
- isShowTips?: boolean;
- action?: string;
- formType?: string;//上传的文件字段名称
- fileList?: any; // 回显的文件
- isDownLoad?: boolean; // 是否可以下载
- listType?: string;//文件上传样式类型
- }
- // 接收父组件传递过来的参数
- const props = withDefaults(defineProps<Props>(), {
- acceptType: ".jpeg,.png",
- acceptTypeDesc: ".png/.jpeg",
- formType: "photo",
- isMultiple: false,
- isCheckName: false,
- limitNum: 10,
- isDisableUpload: false,
- maxFileSize: 0.3,
- isShowTips: true,
- isLimitSize: true,
- action: "/qiniu/upload/image",
- fileList: [],
- isDownLoad: false,
- listType: 'picture-card'
- });
- let waitFileList = ref<any[]>([]);
- waitFileList.value = props.fileList;
- // waitFileList.value?.forEach((item: any) => {
- // item.name = item.original;
- // });
- const dialogImageUrl = ref('')
- const dialogVisible = ref(false)
- const handlePictureCardPreview: UploadProps['onPreview'] = (uploadFile) => {
- dialogImageUrl.value = uploadFile.url!
- dialogVisible.value = true
- }
- const handleRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
- console.log(uploadFile, uploadFiles)
- }
- watch(
- () => props.fileList,
- () => {
- console.log("props.fileList====>", props.fileList);
- waitFileList.value = props.fileList;
- // waitFileList.value?.forEach((item: any) => {
- // item.name = item.original;
- // });
- }
- );
- // 文件变化Handle 这里监听上传文件的变化是一个一个接收到变化的,所以文件也是一个一个上传到服务器上面的
- const handleChange = async (file: any, fileList: any[]) => {
- console.log(fileList, 'fileListfileList');
- console.log(file, 'filefilefile', /^[0-9_][0-9_]*$/.test(file.name));
- if (!/^[0-9]+_/.test(file.name) && props.isCheckName) {
- ElMessage.error(`文件上传格式错误`);
- return false
- }
- // 防止多次执行change
- const rawFile = file.raw;
- const list = props.acceptTypeDesc.split("/");
- let acceptTypeList = list.map((its: string) => {
- return getType(its)
- })
- // 如果要检索的字符串值没有出现,则该方法返回 -1
- const ars = acceptTypeList.filter((q: string) => {
- return rawFile.type.indexOf(q) > -1
- })
- // 用于校验是否符合上传条件
- const type = props.acceptTypeDesc.replace("/", ", ");
- if (ars.length < 1) {
- ElMessage.error(`仅支持格式为${type}的图片`);
- return false;
- } else if (rawFile.size / 1024 / 1024 > props.maxFileSize && props.isLimitSize) {
- ElMessage.error(`文件大小不能超过${props.maxFileSize}MB!`);
- const arr = [...waitFileList.value];
- waitFileList.value = arr.filter((item: any) => {
- return item.uid != rawFile.uid;
- });
- return false;
- } else {
- let formData = new FormData();
- formData.append(props.formType, rawFile);
- const loadingInstance = ElLoading.service({
- text: "正在上传",
- background: "rgba(0,0,0,.2)",
- });
- // 上传到服务器上面
- const requestURL: string = props.action;
- http.post(requestURL, formData)
- .then(async (res: any) => {
- if (res.code == 10000) {
- loadingInstance.close();
- waitFileList.value.push({ uid: file.uid, url: res.data })
- emits("fileSuccess", res.data);
- } else {
- loadingInstance.close();
- ElMessage.warning(`文件上传失败`);
- }
- })
- .catch(() => {
- loadingInstance.close();
- // ElMessage.warning(`文件上传失败`);
- });
- }
- return true;
- };
- // 校验上传文件格式
- const getType = (acceptType: string) => {
- let val = "";
- switch (acceptType) {
- case "xls":
- val = "excel";
- break;
- case "doc":
- val = "word";
- break;
- case "pdf":
- val = "pdf";
- break;
- case "zip":
- val = "zip";
- break;
- case "xlsx":
- val = "sheet";
- break;
- case "pptx":
- val = "presentation";
- break;
- case "docx":
- val = "document";
- break;
- case "text":
- val = "text";
- break;
- case "jpeg":
- val = "jpeg";
- break;
- case "png":
- val = "png";
- break;
- case "gif":
- val = "gif";
- break;
- case "mp4":
- val = "mp4";
- break;
- case "mp3":
- val = "mp3";
- break;
- }
- return val
- };
- // 移除文件
- const removeFile = (file: any) => {
- const arr: any[] = [...waitFileList.value];
- waitFileList.value = arr.filter((its: any) => {
- console.log(its, file);
- return its.uid != file.uid;
- });
-
- console.log(file, 'file', arr, waitFileList.value);
- emits("fileRemove", file);
- };
- const handleDownLoad = (row: { ossFile: string }) => {
- const str = window.location.href.split("#")[0];
- const herf = str.slice(0, str.length - 1);
- window.location.href = herf + row.ossFile;
- };
- </script>
- <style lang="scss" scoped>
- .upload_wrap {
- .upload {
- width: auto;
- padding-bottom: 10px;
- }
- .tips {
- display: block;
- font-size: 14px;
- font-family: PingFangSC-Regular, PingFang SC;
- font-weight: 400;
- color: rgba(0, 0, 0, 0.65);
- }
- }
- :deep().el-upload__text {
- width: 106px;
- height: 32px;
- display: flex;
- align-items: center;
- justify-content: center;
- background: #ffffff;
- border: 1px solid rgba(0, 0, 0, 0.15);
- img {
- display: block;
- width: 14px;
- height: 14px;
- }
- span {
- font-size: 14px;
- padding-left: 6px;
- font-family: PingFangSC-Regular, PingFang SC;
- font-weight: 400;
- color: rgba(0, 0, 0, 0.65);
- }
- }
- .template_list {
- padding-bottom: 4px;
- }
- .template {
- display: flex;
- align-items: center;
- padding: 5px 0;
- span {
- line-height: 16px;
- }
- img {
- margin-right: 8px;
- width: 16px;
- height: 16px;
- }
- .documentName {
- margin-right: 12px;
- font-size: 14px;
- color: rgba(0, 0, 0, 0.65);
- }
- }
- </style>
|