From 07ca7ddaddbd9b021444fdc6f639fdf692588bc3 Mon Sep 17 00:00:00 2001 From: icssoa <2570063477@qq.com> Date: Wed, 17 Aug 2022 00:59:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20upload=20=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/demo/views/upload.vue | 15 ++- src/modules/upload/components/index.vue | 113 +++++++----------- src/modules/upload/components/space.vue | 39 +++--- .../upload/components/space/file-item.vue | 30 ++++- src/modules/upload/utils/index.ts | 36 +++++- 5 files changed, 139 insertions(+), 94 deletions(-) diff --git a/src/modules/demo/views/upload.vue b/src/modules/demo/views/upload.vue index 1e1cdda..b850faa 100644 --- a/src/modules/demo/views/upload.vue +++ b/src/modules/demo/views/upload.vue @@ -24,10 +24,12 @@

自定义内容

- - 上传 - - -
@@ -142,7 +142,7 @@ import { PictureFilled, ZoomIn, Delete } from "@element-plus/icons-vue"; import { useCool, module } from "/@/cool"; import { extname, uuid } from "/@/cool/utils"; import { useBase } from "/$/base"; -import { fileSize, fileName } from "../utils"; +import { fileSize, fileName, fileType } from "../utils"; interface Item { url: string; @@ -257,7 +257,7 @@ function beforeUpload(file: any, item?: Item) { } const d = { - type: file.type?.includes("image") ? "image" : "file", + type: fileType(file.name).value, preload: "", progress: 0, url: "", @@ -423,23 +423,6 @@ function check() { return list.value.find((e) => e.progress != 100); } -// 文件类型 -function fileType(path: string) { - if (props.type == "image") { - return "image"; - } else { - if ( - ["bmp", "jpg", "jpeg", "png", "tif", "gif", "svg", "webp"].includes( - extname(path).toLocaleLowerCase() - ) - ) { - return "image"; - } else { - return "file"; - } - } -} - // 更新 function update() { const check = list.value.find((e) => !e.url); @@ -463,7 +446,7 @@ watch( return item; } else { return { - type: fileType(url), + type: fileType(url).value, progress: 0, uid: uuid(), url, @@ -519,34 +502,26 @@ defineExpose({ box-sizing: border-box; } - .is-drag { - margin: 0 5px 5px 0; - } - - .un-drag { - .cl-upload__item { - margin-right: 5px; - } - - .cl-upload__btn { - margin-bottom: 5px; - } - } - &--file { .cl-upload__list { width: 100%; + margin-top: 10px; + } + } - .un-drag { - width: 100%; - } + &__header { + .cl-upload__item { + margin-right: 5px; } } &__list { display: flex; flex-wrap: wrap; - margin: 0 5px 0 0; + + .cl-upload__item { + margin-right: 5px; + } } &__text { @@ -606,7 +581,7 @@ defineExpose({ } } - &.is-file { + &:not(.is-image) { padding: 10px; .cl-upload { diff --git a/src/modules/upload/components/space.vue b/src/modules/upload/components/space.vue index d97ad8a..4e714e3 100644 --- a/src/modules/upload/components/space.vue +++ b/src/modules/upload/components/space.vue @@ -25,7 +25,7 @@ @drop="onDrop" > - +
@@ -40,6 +40,7 @@ :show-file-list="false" :disabled="disabled" :limit="9999" + :accept="accept" multiple @success="onSuccess" @upload="onUpload" @@ -119,6 +120,8 @@ const props = defineProps({ limit: Number, // 是否禁用 disabled: Boolean, + // 类型 + accept: String, // 显示按钮 showBtn: { type: Boolean, @@ -137,7 +140,7 @@ const { app } = useBase(); const { options } = module.get("upload"); // cl-upload -const Upload = ref(); +const Upload = ref(); // 选择图片的数量 const limit = props.limit || options.limit?.select; @@ -181,14 +184,6 @@ watch( // 是否选中 const isSelected = computed(() => !isEmpty(selection.value)); -// 共享 -provide("space", { - category, - selection, - refresh, - loading -}); - // 打开 function open() { visible.value = true; @@ -225,27 +220,33 @@ function onUpload(data: any) { list.value.unshift(data); } +const reqParams = { + page: 1 +}; + // 刷新资源文件 async function refresh(params: any = {}) { // 清空选择 clear(); - const d = { + // 合并参数 + Object.assign(reqParams, { + type: props.accept?.split("/")[0], ...pagination, ...params, classifyId: category.id - }; + }); // 加载中 - if (d.page == 1) { + if (reqParams.page == 1) { loading.value = true; } - await service.space.info.page(d).then((res) => { + await service.space.info.page(reqParams).then((res) => { // 合并 Object.assign(pagination, res.pagination); - if (d.page == 1) { + if (reqParams.page == 1) { list.value = []; } @@ -331,6 +332,14 @@ function loadmore() { } } +// 共享 +provide("space", { + category, + selection, + refresh, + loading +}); + onMounted(() => { refresh(); }); diff --git a/src/modules/upload/components/space/file-item.vue b/src/modules/upload/components/space/file-item.vue index 3393ab6..b02141e 100644 --- a/src/modules/upload/components/space/file-item.vue +++ b/src/modules/upload/components/space/file-item.vue @@ -30,11 +30,17 @@ {{ fileName(url) }}.{{ extname(url) }} - - - {{ fileSize(info.size) }} + + {{ type.label }} +
index.value >= 0); // 地址 const url = computed(() => info.value.preload || info.value.url); +// 类型 +const type = computed(() => fileType(info.value.url)); + // 选择 function select() { emit("select", info.value); @@ -178,7 +187,7 @@ function onContextMenu(e: any) { } } - &.is-file { + &:not(.is-image) { padding: 10px; .cl-upload-space-file { @@ -220,6 +229,17 @@ function onContextMenu(e: any) { width: calc(100% - 20px); } + &__type { + position: absolute; + top: 5px; + left: 5px; + color: #fff; + background-color: var(--color-primary); + font-size: 12px; + padding: 2px 5px; + border-radius: 3px; + } + &__error { padding: 10px; color: red; diff --git a/src/modules/upload/utils/index.ts b/src/modules/upload/utils/index.ts index 3df30c4..9275ca8 100644 --- a/src/modules/upload/utils/index.ts +++ b/src/modules/upload/utils/index.ts @@ -1,4 +1,4 @@ -import { filename } from "/@/cool/utils"; +import { filename, extname } from "/@/cool/utils"; // 文件大小 export function fileSize(size: number): string { @@ -17,3 +17,37 @@ export function fileSize(size: number): string { export function fileName(url: string) { return filename(url.substring(url.indexOf("_") + 1)); } + +// 文件类型 +export function fileType(path: string) { + const fs = [ + { + label: "图片", + value: "image", + format: ["bmp", "jpg", "jpeg", "png", "tif", "gif", "svg", "webp"], + color: "#67C23A" + }, + { + label: "视频", + value: "video", + format: ["avi", "wmv", "mpg", "mpeg", "mov", "rm", "ram", "swf", "flv", "mp4"], + color: "#409EFF" + }, + { + label: "音频", + value: "audio", + format: ["mp3", "wav", "wma", "mp2", "flac", "midi", "ra", "ape", "aac", "cda"], + color: "#E6A23C" + } + ]; + + return ( + fs.find((e) => { + return e.format.find((a) => a == extname(path).toLocaleLowerCase()); + }) || { + label: "文件", + value: "file", + color: "#909399" + } + ); +}