Files
cz-web/src/views/demand/cpns/DemandAttachments.vue
2026-10-01 10:53:13 +08:00

345 lines
8.5 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
// 附件上传行 + 附件列表(真实接口:POST /web/workOrderDraft/upload → SysOssVo)
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import type { DemandAttachment } from '@/types/business/demand'
import { formatFileSize } from '@/utils/format'
interface Props {
files: DemandAttachment[]
uploading?: boolean
}
const props = withDefaults(defineProps<Props>(), { uploading: false })
const emit = defineEmits<{
upload: [file: File]
remove: [file: DemandAttachment]
}>()
const inputRef = ref<HTMLInputElement | null>(null)
/** 与后端/UploadArea 同口径:单文件不超过 1G */
const MAX_SIZE = 1024 * 1024 * 1024
/** 文件类型标识色(设计稿里 PDF 红、Word 蓝等,按后缀归类) */
const FILE_TONES: Record<string, string> = {
pdf: '#f54a45',
doc: '#2f6bff',
docx: '#2f6bff',
xls: '#1f9d55',
xlsx: '#1f9d55',
csv: '#1f9d55',
ppt: '#f08a24',
pptx: '#f08a24',
zip: '#f08a24',
rar: '#f08a24',
'7z': '#f08a24',
png: '#7a3fd6',
jpg: '#7a3fd6',
jpeg: '#7a3fd6',
gif: '#7a3fd6',
webp: '#7a3fd6',
}
/** 缩写展示(长后缀截断,压缩包统一 ZIP) */
const FILE_LABELS: Record<string, string> = {
doc: 'DOC',
docx: 'DOC',
xls: 'XLS',
xlsx: 'XLS',
csv: 'CSV',
ppt: 'PPT',
pptx: 'PPT',
zip: 'ZIP',
rar: 'ZIP',
'7z': 'ZIP',
}
function suffixOf(file: DemandAttachment): string {
if (file.type) return file.type.replace(/^\./, '').toLowerCase()
const name = file.originalName ?? ''
const dot = name.lastIndexOf('.')
return dot >= 0 ? name.slice(dot + 1).toLowerCase() : ''
}
function toneOf(file: DemandAttachment): string {
return FILE_TONES[suffixOf(file)] ?? '#8a94a6'
}
function labelOf(file: DemandAttachment): string {
const suffix = suffixOf(file)
return FILE_LABELS[suffix] ?? (suffix ? suffix.slice(0, 3).toUpperCase() : 'FILE')
}
function pick(): void {
inputRef.value?.click()
}
function onChange(event: Event): void {
const input = event.target as HTMLInputElement
for (const file of Array.from(input.files ?? [])) {
if (file.size > MAX_SIZE) {
ElMessage.warning(`「${file.name}」超过1G,未上传`)
continue
}
emit('upload', file)
}
// 清空以便再次选择同一个文件
input.value = ''
}
/** 预览:OSS 地址浏览器可直接打开(图片 / PDF) */
function onPreview(file: DemandAttachment): void {
if (!file.url) return
window.open(file.url, '_blank', 'noopener')
}
/** 下载:a[download] 对跨域地址可能被忽略,加 target=_blank 兜底 */
function onDownload(file: DemandAttachment): void {
if (!file.url) return
const link = document.createElement('a')
link.href = file.url
link.download = file.originalName ?? ''
link.target = '_blank'
link.rel = 'noopener'
link.click()
}
</script>
<template>
<div class="demand-attach">
<div class="demand-attach__head">
<span class="demand-attach__label">附件上传</span>
<span class="demand-attach__hint">支持图片/文档/压缩包,大小不超过1G</span>
<button
class="demand-attach__pick"
type="button"
:disabled="props.uploading"
@click="pick"
>
{{ props.uploading ? '上传中…' : '上传文档' }}
</button>
<input
ref="inputRef"
class="demand-attach__input"
type="file"
multiple
@change="onChange"
>
</div>
<ul v-if="props.files.length > 0" class="demand-attach__list">
<li
v-for="(file, index) in props.files"
:key="`${file.ossId ?? file.originalName}-${index}`"
class="demand-attach__item"
>
<span
class="demand-attach__badge"
:style="{ color: toneOf(file), backgroundColor: `${toneOf(file)}1a` }"
>
{{ labelOf(file) }}
</span>
<div class="demand-attach__meta">
<span class="demand-attach__name" :title="file.originalName ?? ''">
{{ file.originalName }}
</span>
<span class="demand-attach__sub">
<template v-if="file.fileSize">{{ formatFileSize(file.fileSize) }} · </template>上传于 {{ file.createTime || '—' }}
</span>
</div>
<div class="demand-attach__acts">
<button
class="demand-attach__act"
type="button"
:disabled="!file.url"
@click="onPreview(file)"
>
<svg width="14" height="14" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<path
d="M1.5 8s2.4-4.5 6.5-4.5S14.5 8 14.5 8s-2.4 4.5-6.5 4.5S1.5 8 1.5 8Z"
stroke="currentColor"
stroke-width="1.2"
/>
<circle cx="8" cy="8" r="1.8" stroke="currentColor" stroke-width="1.2" />
</svg>
预览
</button>
<button
class="demand-attach__act"
type="button"
:disabled="!file.url"
@click="onDownload(file)"
>
<svg width="14" height="14" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<path
d="M8 2v8m0 0 3-3m-3 3L5 7M3 13h10"
stroke="currentColor"
stroke-width="1.2"
stroke-linecap="round"
stroke-linejoin="round"
/>
</svg>
下载
</button>
<button
class="demand-attach__act demand-attach__act--danger"
type="button"
@click="emit('remove', file)"
>
<svg width="14" height="14" viewBox="0 0 16 16" fill="none" aria-hidden="true">
<path
d="M3 4.5h10M6.5 4.5V3h3v1.5M4.5 4.5 5 13h6l.5-8.5M6.8 7v3.5M9.2 7v3.5"
stroke="currentColor"
stroke-width="1.2"
stroke-linecap="round"
stroke-linejoin="round"
/>
</svg>
删除
</button>
</div>
</li>
</ul>
</div>
</template>
<style lang="less" scoped>
.demand-attach {
.flex(column, flex-start, stretch);
gap: 14px;
&__head {
.flex(row, flex-start, center);
}
&__label {
flex-shrink: 0;
font-size: 20px; // 设计稿 20/500 @(194,1376)
font-weight: 500;
line-height: 29px;
color: @demand-heading;
}
// 设计稿:提示文案 @(294,1379) 16/400,起点 = 标签列 80 + 20
&__hint {
flex: 1;
min-width: 0;
margin-left: 20px;
font-size: 16px;
font-weight: 400;
color: @demand-line;
}
// 设计稿该行未标注按钮,按钮设计稿外置(沿用站内次级按钮规格)
&__pick {
flex-shrink: 0;
height: 40px;
padding: 0 @spacing-lg;
border: 1px solid @demand-line;
border-radius: @radius-sm;
background: @white;
font-size: 16px;
color: @demand-line;
cursor: pointer;
transition: border-color 0.2s, color 0.2s;
&:hover:not(:disabled) {
border-color: @demand-accent;
color: @demand-accent;
}
&:disabled {
opacity: 0.6;
cursor: not-allowed;
}
}
&__input {
display: none;
}
&__list {
.flex(column, flex-start, stretch);
// 设计稿:两条 90 高灰条,间距 20
gap: 20px;
margin: 0;
padding: 0;
list-style: none;
}
// 设计稿附件条:实底 #EDEDED、无描边、90 高
&__item {
.flex(row, flex-start, center);
gap: @spacing-base;
min-height: 90px;
padding: 0 @spacing-lg;
border-radius: @radius-sm;
background: @demand-upload-bg;
}
&__badge {
.flex(row, center);
flex-shrink: 0;
width: 44px;
height: 44px;
border-radius: @radius-sm;
font-size: 12px;
font-weight: 700;
letter-spacing: 0.2px;
}
&__meta {
.flex(column, flex-start, stretch);
flex: 1;
gap: 4px;
min-width: 0;
}
&__name {
font-size: 20px;
color: @demand-ink;
.ellipsis();
}
&__sub {
font-size: 16px;
color: @text-color-secondary;
}
&__acts {
.flex(row, flex-end, center);
gap: @spacing-base;
flex-shrink: 0;
}
&__act {
.flex(row, center, center);
gap: 4px;
font-size: @font-size-sm;
color: @text-color;
cursor: pointer;
transition: color 0.2s;
&:hover:not(:disabled) {
color: @primary-color;
}
&:disabled {
color: @text-color-disabled;
cursor: not-allowed;
}
&--danger {
color: @danger-color;
&:hover:not(:disabled) {
color: @danger-color;
opacity: 0.8;
}
}
}
}
</style>