组件修改

This commit is contained in:
2025-12-08 17:36:20 +08:00
parent bc1ee71fc1
commit 19ff3e2a93
23 changed files with 4036 additions and 82 deletions

View File

@@ -1,13 +1,52 @@
<template>
<button :type="type" :style="style"></button>
<button
:class="[
'btn',
`btn--${variant}`,
`btn--${size}`,
{
'btn--disabled': disabled,
'btn--loading': loading
}
]"
:type="type"
:disabled="disabled || loading"
:style="customStyle"
@click="handleClick"
>
<span v-if="loading" class="btn__spinner"></span>
<span class="btn__content">
<slot></slot>
</span>
</button>
</template>
<script setup lang="ts">
interface Props{
type: 'button' | 'submit' | 'reset',
style: string
type?: 'button' | 'submit' | 'reset'
variant?: 'primary' | 'secondary' | 'danger' | 'success' | 'warning' | 'gradient-blue' | 'gradient-purple' | 'gradient-pink' | 'gradient-orange' | 'gradient-green' | 'gradient-blue-special'
size?: 'small' | 'medium' | 'large'
disabled?: boolean
loading?: boolean
customStyle?: string
}
const props = defineProps<Props>()
const props = withDefaults(defineProps<Props>(), {
type: 'button',
variant: 'primary',
size: 'medium',
disabled: false,
loading: false
})
const emit = defineEmits<{
click: [event: MouseEvent]
}>()
const handleClick = (event: MouseEvent) => {
if (!props.disabled && !props.loading) {
emit('click', event)
}
}
</script>

View File

@@ -0,0 +1,165 @@
<template>
<div class="button-test">
<h2>按钮组件测试</h2>
<!-- 基础按钮 -->
<section class="test-section">
<h3>基础按钮</h3>
<div class="button-group">
<Button variant="primary">Primary</Button>
<Button variant="secondary">Secondary</Button>
<Button variant="danger">Danger</Button>
<Button variant="success">Success</Button>
<Button variant="warning">Warning</Button>
</div>
</section>
<!-- 渐变按钮 -->
<section class="test-section">
<h3>渐变按钮</h3>
<div class="button-group">
<Button variant="gradient-blue">Gradient Blue</Button>
<Button variant="gradient-purple">Gradient Purple</Button>
<Button variant="gradient-pink">Gradient Pink</Button>
<Button variant="gradient-orange">Gradient Orange</Button>
<Button variant="gradient-green">Gradient Green</Button>
<Button variant="gradient-blue-special">Special Blue</Button>
</div>
</section>
<!-- 尺寸测试 -->
<section class="test-section">
<h3>按钮尺寸渐变示例</h3>
<div class="button-group">
<Button variant="gradient-blue" size="small">Small</Button>
<Button variant="gradient-pink" size="medium">Medium</Button>
<Button variant="gradient-green" size="large">Large</Button>
</div>
</section>
<!-- 状态测试 -->
<section class="test-section">
<h3>按钮状态</h3>
<div class="button-group">
<Button variant="gradient-blue" :loading="isLoading" @click="toggleLoading">
{{ isLoading ? '加载中...' : '点击加载' }}
</Button>
<Button variant="gradient-purple" disabled>禁用按钮</Button>
<Button variant="gradient-pink" :loading="true">持续加载</Button>
</div>
</section>
<!-- 混合展示 -->
<section class="test-section">
<h3>混合展示</h3>
<div class="mixed-demo">
<div class="card">
<h4>渐变卡片演示</h4>
<p>这是一个使用渐变按钮的卡片示例</p>
<div class="card-actions">
<Button variant="gradient-blue" size="small">详情</Button>
<Button variant="gradient-green" size="small">确认</Button>
</div>
</div>
</div>
</section>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import Button from './Button.vue'
// 响应式数据
const isLoading = ref(false)
// 方法
const toggleLoading = () => {
isLoading.value = true
setTimeout(() => {
isLoading.value = false
}, 2000)
}
</script>
<style lang="scss" scoped>
.button-test {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
h2 {
text-align: center;
color: #333;
margin-bottom: 30px;
font-size: 28px;
}
.test-section {
margin-bottom: 40px;
padding: 20px;
border: 1px solid #e1e5e9;
border-radius: 8px;
background: white;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
h3 {
margin: 0 0 20px 0;
color: #495057;
font-size: 20px;
border-bottom: 2px solid #f8f9fa;
padding-bottom: 10px;
}
.button-group {
display: flex;
gap: 12px;
flex-wrap: wrap;
align-items: center;
}
}
.mixed-demo {
.card {
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
padding: 24px;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
max-width: 400px;
h4 {
margin: 0 0 12px 0;
color: #374151;
font-size: 18px;
}
p {
margin: 0 0 20px 0;
color: #6b7280;
line-height: 1.6;
}
.card-actions {
display: flex;
gap: 8px;
justify-content: flex-end;
}
}
}
}
// 响应式设计
@media (max-width: 768px) {
.button-test {
padding: 16px;
.test-section {
padding: 16px;
.button-group {
gap: 8px;
}
}
}
}
</style>

View File

@@ -0,0 +1,189 @@
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
border: none;
border-radius: 6px;
cursor: pointer;
font-weight: 500;
transition: all 0.2s ease;
text-decoration: none;
white-space: nowrap;
&:focus {
outline: 2px solid rgba(59, 130, 246, 0.5);
outline-offset: 2px;
}
// 尺寸
&--small {
padding: 6px 12px;
font-size: 12px;
line-height: 1.4;
}
&--medium {
padding: 8px 16px;
font-size: 14px;
line-height: 1.5;
}
&--large {
padding: 12px 20px;
font-size: 16px;
line-height: 1.5;
}
// 变体
&--primary {
background-color: #3b82f6;
color: white;
&:hover:not(.btn--disabled):not(.btn--loading) {
background-color: #2563eb;
}
}
&--secondary {
background-color: #f1f5f9;
color: #475569;
border: 1px solid #e2e8f0;
&:hover:not(.btn--disabled):not(.btn--loading) {
background-color: #e2e8f0;
}
}
&--danger {
background-color: #ef4444;
color: white;
&:hover:not(.btn--disabled):not(.btn--loading) {
background-color: #dc2626;
}
}
&--success {
background-color: #10b981;
color: white;
&:hover:not(.btn--disabled):not(.btn--loading) {
background-color: #059669;
}
}
&--warning {
background-color: #f59e0b;
color: white;
&:hover:not(.btn--disabled):not(.btn--loading) {
background-color: #d97706;
}
}
// 渐变变体
&--gradient-blue {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
&:hover:not(.btn--disabled):not(.btn--loading) {
background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
transform: translateY(-1px);
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}
}
&--gradient-purple {
background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
color: #374151;
border: none;
&:hover:not(.btn--disabled):not(.btn--loading) {
background: linear-gradient(135deg, #96e6e2 0%, #fcc4d1 100%);
transform: translateY(-1px);
box-shadow: 0 4px 12px rgba(168, 237, 234, 0.4);
}
}
&--gradient-pink {
background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%);
color: white;
border: none;
&:hover:not(.btn--disabled):not(.btn--loading) {
background: linear-gradient(135deg, #ff888c 0%, #feb7dd 100%);
transform: translateY(-1px);
box-shadow: 0 4px 12px rgba(255, 154, 158, 0.4);
}
}
&--gradient-orange {
background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
color: #374151;
border: none;
&:hover:not(.btn--disabled):not(.btn--loading) {
background: linear-gradient(135deg, #ffe4c0 0%, #fba88d 100%);
transform: translateY(-1px);
box-shadow: 0 4px 12px rgba(252, 182, 159, 0.4);
}
}
&--gradient-green {
background: linear-gradient(135deg, #a8e6cf 0%, #dcedc1 100%);
color: #374151;
border: none;
&:hover:not(.btn--disabled):not(.btn--loading) {
background: linear-gradient(135deg, #96dfbf 0%, #d0e6af 100%);
transform: translateY(-1px);
box-shadow: 0 4px 12px rgba(168, 230, 207, 0.4);
}
}
&--gradient-blue-special {
background: linear-gradient(281.59deg, #162C8E 8.51%, #1D72D3 66.15%, #AEF1FF 100.83%);
color: white;
border: none;
&:hover:not(.btn--disabled):not(.btn--loading) {
background: linear-gradient(281.59deg, #142558 8.51%, #1a64b8 66.15%, #9ce8f5 100.83%);
transform: translateY(-1px);
box-shadow: 0 4px 12px rgba(29, 114, 211, 0.4);
}
}
// 状态
&--disabled {
opacity: 0.5;
cursor: not-allowed;
}
&--loading {
cursor: wait;
}
// 加载动画
&__spinner {
width: 16px;
height: 16px;
border: 2px solid transparent;
border-top: 2px solid currentColor;
border-radius: 50%;
animation: spin 1s linear infinite;
}
&__content {
display: flex;
align-items: center;
gap: 8px;
}
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}

View File

@@ -0,0 +1,8 @@
<template>
</template>
<script setup lang="ts">
</script>
<style lang="scss" scoped>
@import url("./DynamicComponent.scss");
</style>

View File

@@ -1,9 +1,7 @@
<template>
</template>
<script setup lang="ts">
</script>
<style lang="scss" scoped>
</style>

View File

@@ -0,0 +1,402 @@
// 文件上传组件 - 精简版样式
.file-upload {
display: inline-block;
width: 100%;
// 封面模式
&.cover {
width: 300px;
height: 200px;
.image-wrapper {
position: relative;
width: 300px;
height: 200px;
border-radius: 6px;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
background: #f5f7fa;
&:hover .actions {
opacity: 1;
}
.image {
display: block;
object-fit: contain;
max-width: 100%;
max-height: 100%;
&.horizontal {
width: 100%;
height: auto;
}
&.vertical {
height: 100%;
width: auto;
}
}
.actions {
position: absolute;
top: 8px;
right: 8px;
opacity: 0;
transition: opacity 0.3s;
}
}
}
// 弹窗模式
&.dialog {
.overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
}
.modal {
background: white;
border-radius: 12px;
padding: 24px;
width: 90%;
max-width: 500px;
max-height: 80vh;
overflow-y: auto;
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
padding-bottom: 12px;
border-bottom: 1px solid #e2e8f0;
h3 {
margin: 0;
font-size: 18px;
font-weight: 600;
color: #1e293b;
}
}
.footer {
display: flex;
gap: 12px;
justify-content: flex-end;
margin-top: 20px;
padding-top: 20px;
border-top: 1px solid #e2e8f0;
}
}
// 内容模式
&.content {}
// 通用容器
.container {
padding: 20px 0;
}
// 上传区域
.area {
border: 2px dashed #dcdfe6;
border-radius: 8px;
padding: 60px 20px;
text-align: center;
cursor: pointer;
transition: all 0.3s;
background: #fafafa;
min-height: 160px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
position: relative;
// 封面模式特殊样式
&.cover {
border: 1px dashed #d9d9d9;
border-radius: 6px;
background: #ffffff;
padding: 30px 20px;
&:hover {
box-shadow: 0 4px 12px rgba(64, 158, 255, 0.1);
transform: translateY(-2px);
.icon {
transform: scale(1.1);
}
}
}
&:hover {
border-color: #409eff;
background: #f0f9ff;
.icon .plus {
&::before,
&::after {
background-color: #409eff;
}
}
.text {
color: #409eff;
}
}
&.dragover {
border-color: #409eff;
background: #e6f7ff;
border-style: solid;
}
&.disabled {
cursor: not-allowed;
opacity: 0.6;
}
}
// 内容区域
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
width: 100%;
}
// 图标
.icon {
margin-bottom: 16px;
transition: all 0.3s;
.plus {
width: 80px;
height: 80px;
position: relative;
margin: 0 auto;
&::before,
&::after {
content: '';
position: absolute;
background-color: #c0c4cc;
transition: all 0.3s;
}
// 竖线
&::before {
width: 2px;
height: 80px;
left: 50%;
top: 0;
transform: translateX(-50%);
}
// 横线
&::after {
width: 80px;
height: 2px;
left: 0;
top: 50%;
transform: translateY(-50%);
}
}
}
// 封面模式的小图标
&.cover .icon .plus {
width: 48px;
height: 48px;
&::before {
width: 1px;
height: 48px;
}
&::after {
width: 48px;
height: 1px;
}
}
// 文字
.text {
color: #606266;
font-size: 16px;
margin-bottom: 8px;
font-weight: 500;
transition: color 0.3s;
.link {
color: #409eff;
cursor: pointer;
text-decoration: underline;
}
}
// 提示
.tip {
color: #909399;
font-size: 13px;
line-height: 1.5;
max-width: 240px;
}
// 加载状态
.loading {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(255, 255, 255, 0.9);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
.spinner {
font-size: 32px;
color: #409eff;
margin-bottom: 8px;
animation: rotate 1s linear infinite;
}
div {
font-size: 14px;
color: #606266;
}
}
// 文件列表
.files {
margin-top: 20px;
h4 {
margin: 0 0 12px 0;
font-size: 14px;
font-weight: 600;
color: #374151;
}
}
// 单个文件
.file {
display: flex;
align-items: center;
gap: 12px;
padding: 12px;
background-color: #f8fafc;
border-radius: 6px;
margin-bottom: 8px;
transition: background-color 0.2s ease;
&:hover {
background-color: #e2e8f0;
}
.preview {
width: 60px;
height: 60px;
border-radius: 4px;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
background: #f5f7fa;
flex-shrink: 0;
.thumb {
width: 100%;
height: 100%;
object-fit: cover;
}
.type-icon {
font-size: 24px;
}
}
.info {
flex: 1;
min-width: 0;
.name {
font-size: 14px;
font-weight: 500;
color: #1e293b;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
margin-bottom: 4px;
}
.size {
font-size: 12px;
color: #64748b;
}
}
.actions {
display: flex;
gap: 8px;
}
}
// 操作按钮区域
.actions {
margin-top: 16px;
text-align: center;
&:not(.file .actions) {
display: flex;
gap: 12px;
justify-content: center;
}
}
}
// 动画
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
// 响应式设计
@media (max-width: 768px) {
.file-upload {
&.dialog .modal {
margin: 16px;
padding: 20px;
width: calc(100% - 32px);
}
.area.cover {
width: 250px;
height: 160px;
}
}
}

View File

@@ -1,26 +1,404 @@
<template>
<div v-if="mode==='cover'">
<!-- 封面的文件上传只可传一张图片上传成功后封面图片会替换 -->
<!-- 封面模式 -->
<div v-if="mode === 'cover'" class="file-upload cover">
<!-- 已上传的封面 -->
<div v-if="currentCoverImg" class="image-wrapper">
<img :src="currentCoverImg" class="image" :class="coverImageClass" @load="handleCoverImageLoad"
alt="封面图片" />
<div class="actions">
<Button variant="danger" size="small" @click="handleRemoveCover">
×
</Button>
</div>
</div>
<!-- 上传区域 -->
<div v-else class="area cover" :class="{ dragover: isDragover, disabled: uploading }" @click="triggerFileInput"
@drop.prevent="handleDrop" @dragover.prevent="handleDragOver" @dragleave.prevent="handleDragLeave">
<div v-if="!uploading" class="content">
<div class="icon">
<div class="plus"></div>
</div>
<div class="text">点击上传封面</div>
<div class="tip">
{{ `支持 ${getAcceptText()} 格式,最大 ${(maxSize / 1024 / 1024).toFixed(0)}MB` }}
</div>
</div>
<div v-if="uploading" class="loading">
<div class="spinner"></div>
<div>上传中...</div>
</div>
</div>
<input ref="fileInputRef" type="file" :accept="accept" @change="handleFileSelect" hidden />
</div>
<div v-if="mode==='dialog'">
<!-- 文件上传弹窗可传多个文件上传成功后文件列表会更新 -->
<!-- 弹窗模式 -->
<div v-else-if="mode === 'dialog'" class="file-upload dialog">
<Button @click="showDialog = true" variant="primary">
{{ buttonText || '上传文件' }}
</Button>
<div v-if="showDialog" class="overlay" @click="closeDialog">
<div class="modal" @click.stop>
<div class="header">
<h3>{{ title || '文件上传' }}</h3>
<Button variant="secondary" size="small" @click="closeDialog">×</Button>
</div>
<div class="container">
<div class="area" :class="{ dragover: isDragover, disabled: uploading }" @click="triggerFileInput"
@drop.prevent="handleDrop" @dragover.prevent="handleDragOver"
@dragleave.prevent="handleDragLeave">
<div class="icon">
<div class="plus"></div>
</div>
<div class="text">
将文件拖到此处<span class="link">点击上传</span>
</div>
<div class="tip">{{ getUploadTip() }}</div>
</div>
<input ref="fileInputRef" type="file" :accept="accept" :multiple="maxCount > 1"
@change="handleFileSelect" hidden />
<!-- 文件列表 -->
<div v-if="selectedFiles.length > 0" class="files">
<div v-for="(file, index) in selectedFiles" :key="index" class="file">
<div class="preview">
<img v-if="isImageFile(file)" :src="getFilePreviewUrl(file)" class="thumb" />
<span v-else class="type-icon">{{ getFileTypeIcon(file) }}</span>
</div>
<div class="info">
<div class="name">{{ file.name }}</div>
<div class="size">{{ formatFileSize(file.size) }}</div>
</div>
<div class="actions">
<Button variant="danger" size="small" @click="removeFile(index)" :disabled="uploading">
删除
</Button>
</div>
</div>
</div>
</div>
<div class="footer">
<Button variant="secondary" @click="closeDialog" :disabled="uploading">取消</Button>
<Button variant="primary" @click="uploadFiles" :loading="uploading"
:disabled="selectedFiles.length === 0">
{{ uploading ? '上传中...' : '确定上传' }}
</Button>
</div>
</div>
</div>
</div>
<div v-if="mode==='content'">
<!-- 嵌入原div文件上传内容可传多个文件上传成功后文件列表会更新 -->
<!-- 内容模式 -->
<div v-else class="file-upload content">
<div class="container">
<div class="area" :class="{ dragover: isDragover, disabled: uploading }" @click="triggerFileInput"
@drop.prevent="handleDrop" @dragover.prevent="handleDragOver" @dragleave.prevent="handleDragLeave">
<div class="icon">
<div class="plus"></div>
</div>
<div class="text">
将文件拖到此处<span class="link">点击上传</span>
</div>
<div class="tip">{{ getUploadTip() }}</div>
</div>
<input ref="fileInputRef" type="file" :accept="accept" :multiple="maxCount > 1" @change="handleFileSelect"
hidden />
<!-- 文件列表 -->
<div v-if="selectedFiles.length > 0" class="files">
<h4>待上传文件</h4>
<div v-for="(file, index) in selectedFiles" :key="index" class="file">
<div class="preview">
<img v-if="isImageFile(file)" :src="getFilePreviewUrl(file)" class="thumb" />
<span v-else class="type-icon">{{ getFileTypeIcon(file) }}</span>
</div>
<div class="info">
<div class="name">{{ file.name }}</div>
<div class="size">{{ formatFileSize(file.size) }}</div>
</div>
<div class="actions">
<Button variant="danger" size="small" @click="removeFile(index)" :disabled="uploading">
删除
</Button>
</div>
</div>
</div>
<!-- 上传按钮 -->
<div v-if="selectedFiles.length > 0" class="actions">
<Button variant="primary" @click="uploadFiles" :loading="uploading"
:disabled="selectedFiles.length === 0">
{{ uploading ? '上传中...' : '确定上传' }}
</Button>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue'
import { Button } from '@/components'
import { FILE_DOWNLOAD_URL } from '@/config'
interface Props{
import { fileAPI } from '@/api/file/file'
import type { TbSysFileDTO } from '@/types/file/file'
import {
formatFileSize,
isImageFile,
getFileTypeIcon,
isValidFileType,
getFilePreviewUrl
} from '@/utils/file'
interface Props {
mode: 'cover' | 'dialog' | 'content'
coverImg: string
fileList: FileList
coverImg?: string
fileList?: TbSysFileDTO[]
accept?: string
maxSize?: number
maxCount?: number
title?: string
buttonText?: string
}
const props = withDefaults(defineProps<Props>(), {
coverImg: '',
fileList: () => [],
accept: '',
maxSize: 10 * 1024 * 1024,
maxCount: 10,
title: '文件上传',
buttonText: '上传文件'
})
const emit = defineEmits<{
'update:coverImg': [value: string]
'update:fileList': [value: TbSysFileDTO[]]
'upload-success': [files: TbSysFileDTO[]]
'upload-error': [error: string]
}>()
// 响应式数据
const showDialog = ref(false)
const uploading = ref(false)
const selectedFiles = ref<File[]>([])
const isDragover = ref(false)
const coverImageClass = ref('')
// 文件输入引用
const fileInputRef = ref<HTMLInputElement>()
// 计算属性
const currentCoverImg = computed({
get: () => props.coverImg,
set: (value: string) => emit('update:coverImg', value)
})
// 触发文件输入
const triggerFileInput = () => {
fileInputRef.value?.click()
}
// 拖拽相关
const handleDragOver = (event: DragEvent) => {
event.preventDefault()
isDragover.value = true
}
const handleDragLeave = () => {
isDragover.value = false
}
const handleDrop = (event: DragEvent) => {
event.preventDefault()
isDragover.value = false
if (uploading.value) return
const files = event.dataTransfer?.files
if (files && files.length > 0) {
addFiles(Array.from(files))
// cover模式和content模式下拖拽文件后立即上传
if (props.mode === 'cover' || props.mode === 'content') {
uploadFiles()
}
}
}
// 处理封面图片加载
const handleCoverImageLoad = (event: Event) => {
const img = event.target as HTMLImageElement
const width = img.naturalWidth
const height = img.naturalHeight
if (width > height) {
coverImageClass.value = 'horizontal'
} else {
coverImageClass.value = 'vertical'
}
}
// 删除封面
const handleRemoveCover = () => {
emit('update:coverImg', '')
coverImageClass.value = ''
selectedFiles.value = []
}
// 获取接受文件类型文本
const getAcceptText = (): string => {
if (!props.accept || props.accept === '*/*') return '所有'
if (props.accept.includes('image')) return '图片'
return props.accept
}
// 获取上传提示文本
const getUploadTip = (): string => {
const acceptText = getAcceptText()
const sizeText = (props.maxSize / 1024 / 1024).toFixed(0)
return `支持 ${acceptText} 格式,单个文件不超过 ${sizeText}MB`
}
// 添加文件
const addFiles = (files: File[]) => {
files.forEach(file => {
// 验证文件大小
if (file.size > props.maxSize) {
emit('upload-error', `文件 ${file.name} 大小超过 ${(props.maxSize / 1024 / 1024).toFixed(0)}MB`)
return
}
// 验证文件类型
if (props.accept && !isValidFileType(file, props.accept)) {
emit('upload-error', `文件 ${file.name} 类型不符合要求`)
return
}
// 检查是否重复
if (selectedFiles.value.some(f => f.name === file.name && f.size === file.size)) {
emit('upload-error', `文件 ${file.name} 已添加`)
return
}
// 如果不允许多选或封面模式,清空之前的文件
if (props.maxCount === 1 || props.mode === 'cover') {
selectedFiles.value = [file]
} else {
// 检查数量限制
if (selectedFiles.value.length >= props.maxCount) {
emit('upload-error', `最多只能上传 ${props.maxCount} 个文件`)
return
}
selectedFiles.value.push(file)
}
})
}
// 处理文件选择
const handleFileSelect = (event: Event) => {
const target = event.target as HTMLInputElement
const files = Array.from(target.files || [])
if (files.length === 0) return
addFiles(files)
// 清空 input允许重复选择同一文件
target.value = ''
// cover模式和content模式下选择文件后立即上传
if (props.mode === 'cover' || props.mode === 'content') {
uploadFiles()
}
}
// 移除文件
const removeFile = (index: number) => {
selectedFiles.value.splice(index, 1)
}
// 关闭弹窗
const closeDialog = () => {
showDialog.value = false
selectedFiles.value = []
isDragover.value = false
}
// 上传文件
const uploadFiles = async () => {
if (selectedFiles.value.length === 0) return
uploading.value = true
const uploadedFilesList: TbSysFileDTO[] = []
try {
if (selectedFiles.value.length === 1) {
const file = selectedFiles.value[0]
const result = await fileAPI.uploadFile({
file: file,
module: props.mode,
optsn: ''
})
if (result.success && result.data) {
uploadedFilesList.push(result.data)
if (props.mode === 'cover' && result.data.url) {
emit('update:coverImg', result.data.url)
}
} else {
emit('upload-error', result.message || '上传失败,请重试')
return
}
} else {
const result = await fileAPI.batchUploadFiles({
files: selectedFiles.value,
module: props.mode,
optsn: ''
})
if (result.success) {
const files = result.dataList || []
uploadedFilesList.push(...files)
} else {
emit('upload-error', result.message || '上传失败,请重试')
return
}
}
// 上传成功
if (uploadedFilesList.length > 0) {
emit('upload-success', uploadedFilesList)
emit('update:fileList', [...props.fileList, ...uploadedFilesList])
}
// 清空文件列表
selectedFiles.value = []
if (props.mode === 'dialog') {
closeDialog()
}
} catch (error) {
console.error('上传失败:', error)
emit('upload-error', '上传失败,请重试')
} finally {
uploading.value = false
}
}
const props = defineProps<Props>()
</script>
<style lang="scss" scoped>
@import './FileUpload.scss';
</style>

View File

@@ -0,0 +1,312 @@
<template>
<div class="file-upload-test">
<h2>文件上传组件测试</h2>
<!-- 封面模式测试 -->
<section class="test-section">
<h3>1. 封面模式测试</h3>
<div class="test-content">
<div class="cover-upload-wrapper">
<FileUpload
mode="cover"
v-model:coverImg="coverImage"
accept="image/*"
:maxSize="5 * 1024 * 1024"
@upload-success="handleCoverUploadSuccess"
@upload-error="handleUploadError"
style="height: auto;"
/>
</div>
<div class="test-info">
<p><strong>当前封面:</strong> {{ coverImage || '无' }}</p>
<p><strong>说明:</strong> 点击上传封面图片支持JPGPNGGIF格式最大5MB</p>
</div>
</div>
</section>
<!-- 弹窗模式测试 -->
<section class="test-section">
<h3>2. 弹窗模式测试</h3>
<div class="test-content">
<FileUpload
mode="dialog"
v-model:fileList="dialogFileList"
:maxCount="5"
:maxSize="10 * 1024 * 1024"
@upload-success="handleDialogUploadSuccess"
@upload-error="handleUploadError"
/>
<div class="test-info">
<p><strong>已上传文件数:</strong> {{ dialogFileList.length }}</p>
<p><strong>说明:</strong> 点击按钮打开弹窗支持多文件上传最多5个文件每个文件最大10MB</p>
<div v-if="dialogFileList.length > 0" class="uploaded-files">
<h4>已上传的文件:</h4>
<ul>
<li v-for="file in dialogFileList" :key="file.fileId">
<strong>{{ file.name }}</strong>
<span class="file-size">({{ formatFileSize(file.size || 0) }})</span>
<span class="file-type">{{ file.type }}</span>
</li>
</ul>
</div>
</div>
</div>
</section>
<!-- 内容模式测试 -->
<section class="test-section">
<h3>3. 内容模式测试</h3>
<div class="test-content">
<FileUpload
mode="content"
v-model:fileList="contentFileList"
:maxCount="3"
:maxSize="20 * 1024 * 1024"
@upload-success="handleContentUploadSuccess"
@upload-error="handleUploadError"
/>
<div class="test-info">
<p><strong>已上传文件数:</strong> {{ contentFileList.length }}</p>
<p><strong>说明:</strong> 直接在页面中显示上传区域最多3个文件每个文件最大20MB</p>
<div v-if="contentFileList.length > 0" class="uploaded-files">
<h4>已上传的文件:</h4>
<ul>
<li v-for="file in contentFileList" :key="file.fileId">
<strong>{{ file.name }}</strong>
<span class="file-size">({{ formatFileSize(file.size || 0) }})</span>
<span class="file-type">{{ file.type }}</span>
</li>
</ul>
</div>
</div>
</div>
</section>
<!-- 消息提示 -->
<div v-if="message" class="message" :class="messageType">
<p>{{ message }}</p>
<button @click="clearMessage">×</button>
</div>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import FileUpload from './FileUpload.vue'
import type { TbSysFileDTO } from '@/types/file/file'
// 响应式数据
const coverImage = ref<string>('')
const dialogFileList = ref<TbSysFileDTO[]>([])
const contentFileList = ref<TbSysFileDTO[]>([])
const message = ref<string>('')
const messageType = ref<'success' | 'error'>('success')
// 处理封面上传成功
const handleCoverUploadSuccess = (files: TbSysFileDTO[]) => {
console.log('封面上传成功:', files)
showMessage('封面上传成功!', 'success')
}
// 处理弹窗上传成功
const handleDialogUploadSuccess = (files: TbSysFileDTO[]) => {
console.log('弹窗上传成功:', files)
showMessage(`成功上传 ${files.length} 个文件!`, 'success')
}
// 处理内容上传成功
const handleContentUploadSuccess = (files: TbSysFileDTO[]) => {
console.log('内容上传成功:', files)
showMessage(`成功上传 ${files.length} 个文件!`, 'success')
}
// 处理上传错误
const handleUploadError = (error: string) => {
console.error('上传错误:', error)
showMessage(error, 'error')
}
// 显示消息
const showMessage = (msg: string, type: 'success' | 'error') => {
message.value = msg
messageType.value = type
// 3秒后自动清除消息
setTimeout(() => {
clearMessage()
}, 3000)
}
// 清除消息
const clearMessage = () => {
message.value = ''
}
// 格式化文件大小
const formatFileSize = (bytes: number): string => {
if (bytes === 0) return '0 B'
const k = 1024
const sizes = ['B', 'KB', 'MB', 'GB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}
</script>
<style lang="scss" scoped>
.file-upload-test {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
h2 {
text-align: center;
color: #333;
margin-bottom: 30px;
font-size: 28px;
}
.test-section {
margin-bottom: 40px;
border: 1px solid #e1e5e9;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
h3 {
background-color: #f8f9fa;
margin: 0;
padding: 15px 20px;
color: #495057;
font-size: 20px;
border-bottom: 1px solid #e1e5e9;
}
.test-content {
padding: 20px;
display: grid;
grid-template-columns: 1fr 1fr;
gap: 30px;
align-items: start;
@media (max-width: 768px) {
grid-template-columns: 1fr;
gap: 20px;
}
}
.test-info {
background-color: #f8f9fa;
padding: 15px;
border-radius: 6px;
border-left: 4px solid #007bff;
p {
margin: 8px 0;
strong {
color: #495057;
}
}
.uploaded-files {
margin-top: 15px;
h4 {
margin: 0 0 10px 0;
color: #495057;
font-size: 16px;
}
ul {
list-style: none;
padding: 0;
margin: 0;
li {
padding: 8px 12px;
background-color: white;
border-radius: 4px;
margin-bottom: 5px;
border: 1px solid #e1e5e9;
.file-size {
color: #6c757d;
font-size: 12px;
margin-left: 8px;
}
.file-type {
color: #28a745;
font-size: 12px;
margin-left: 8px;
}
}
}
}
}
}
.message {
position: fixed;
top: 20px;
right: 20px;
padding: 15px 20px;
border-radius: 6px;
color: white;
display: flex;
align-items: center;
gap: 15px;
z-index: 1000;
min-width: 300px;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
&.success {
background-color: #28a745;
}
&.error {
background-color: #dc3545;
}
p {
margin: 0;
flex: 1;
}
button {
background: none;
border: none;
color: white;
font-size: 18px;
cursor: pointer;
padding: 0;
width: 24px;
height: 24px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
&:hover {
background-color: rgba(255,255,255,0.2);
}
}
}
// // 为封面模式设置默认尺寸,可被外部覆盖
// .cover-upload-wrapper .file-upload.cover .area.cover {
// width: 300px;
// height: 200px;
// }
// .cover-upload-wrapper .file-upload.cover .image-wrapper {
// width: 300px;
// height: 200px;
// }
// // 为内容模式也设置相同的默认尺寸,演示统一行为
// .file-upload.content .area {
// width: 300px;
// height: 200px;
// }
}
</style>