This commit is contained in:
zlwu committed 2026-06-06 19:44:18 +08:00
1 parent dacfdc9ff6
commit f9676f8759
906 files changed
+533306 -12946

No files matched your search

@@ -6,94 +6,22 @@
align-center
append-to-body
destroy-on-close
class="dialog-form-layout"
class="dialog-form-layout upload-scan-dialog"
modal-class="order-module-dialog-mask"
:close-on-click-modal="false"
>
<div class="attachment-upload-container">
<el-upload
ref="uploadRef"
:file-list="attachmentList"
class="upload-demo"
drag
list-type="picture-card"
:on-preview="handleFilePreview"
:action="uploadAction"
:headers="uploadHeaders"
:data="uploadData"
:auto-upload="true"
:before-upload="handleBeforeUpload"
:on-success="handleUploadSuccess"
:on-error="handleUploadError"
:on-remove="handleRemoveAttachmentConfirm"
:on-change="handleFileChange"
multiple
:limit="10"
:show-file-list="true"
accept="image/*,.pdf,.doc,.docx"
>
<template #tip>
<div class="el-upload__tip">支持图片、PDF、Word文档等格式,最多上传10个文件。</div>
</template>
<ContractAttachmentPanel
v-model="attachmentList"
:biz-type="CRM_UPLOAD_BIZ_TYPE.CONTRACT_ATTACHMENT"
hint="支持上传图片、PDF、Word文档等,最多10个文件"
/>
<!-- 调整拖拽上传图标与文字在同一行、居中对齐 -->
<div class="el-upload-dragger1">
<el-icon class="el-icon--upload"><UploadFilled /></el-icon>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
</div>
<!-- 自定义文件卡片缩略图(含文件名) -->
<template #file="{ file }">
<div class="upload-file-card">
<!-- 缩略图区域 -->
<div class="upload-file-thumb">
<ImagePreview v-if="isImageFile(file) && getFileOssId(file)" :src="getFileOssId(file)" class="upload-thumb-image" :isOssId="true" />
<div v-else class="upload-file-placeholder">
<el-icon><Document /></el-icon>
<span class="file-ext">{{ getFileExt(file.name) }}</span>
</div>
<!-- loading 遮罩只在上传中显示 -->
<div v-if="file.status === 'uploading'" class="upload-file-loading">
<el-icon class="is-loading"><Loading /></el-icon>
<span class="loading-text">上传中...</span>
</div>
</div>
<!-- 文件名称 -->
<div class="upload-file-name" :title="file.name">
{{ getShortFileName(file.name) }}
</div>
<!-- 附件分类选择 -->
<div class="upload-file-category" v-if="file.status && file.status !== 'ready'">
<el-select v-model="file.category" placeholder="选择附件分类" size="small" style="width: 100%" @change="handleCategoryChange(file)">
<el-option label="单章附件" value="single_seal" />
<el-option label="双章附件" value="double_seal" />
<el-option label="其他附件" value="other" />
</el-select>
</div>
<!-- 操作按钮区域 - 固定在文件名下方 -->
<div class="upload-file-card-actions" v-if="file.status && file.status !== 'ready'">
<el-button-group size="small">
<el-button type="success" @click.stop="handleDownloadAttachment(file)" title="下载" :disabled="file.status !== 'success'">
<el-icon><Download /></el-icon>
</el-button>
<el-button type="danger" @click.stop="handleRemoveAttachmentConfirm(file)" title="删除">
<el-icon><Delete /></el-icon>
</el-button>
</el-button-group>
</div>
</div>
</template>
</el-upload>
</div>
<template #footer>
<div class="upload-dialog-actions">
<el-button @click="handleClose">
<el-icon><Close /></el-icon>关闭
</el-button>
<el-button type="primary" @click="handleSaveAndClose">
<el-button type="primary" :loading="saveLoading" @click="handleSaveAndClose">
<el-icon><Check /></el-icon>保存并关闭
</el-button>
</div>
@@ -102,480 +30,95 @@
</template>
<script setup lang="ts" name="UploadDialog">
import { ref, computed } from 'vue'
import { UploadFilled, Document, Loading, Download, Delete, Close, Check } from '@element-plus/icons-vue'
import ImagePreview from '@/components/ImagePreview/index.vue'
import { ref } from 'vue'
import { Close, Check } from '@element-plus/icons-vue'
import ContractAttachmentPanel from './ContractAttachmentPanel.vue'
import { uploadAttachments, getContract } from '@/api/crm/contract'
import { $modal } from '@/utils/modal'
import { useUserStore } from '@/store/modules/user'
import {
buildUploadFileListFromField,
downloadCrmAttachment
mapContractAttachmentsPayload,
type ContractAttachmentItem
} from '@/utils/crm-attachment'
import { CRM_UPLOAD_BIZ_TYPE } from '@/utils/crm-upload'
const userStore = useUserStore()
const emit = defineEmits(['success'])
// 定义事件
const emit = defineEmits(['success']);
const dialogVisible = ref(false)
const contractId = ref<number>()
const attachmentList = ref<ContractAttachmentItem[]>([])
const saveLoading = ref(false)
// 对话框显示状态
const dialogVisible = ref(false);
// 合同ID
const contractId = ref<number>();
// 文件列表
const attachmentList = ref<any[]>([]);
// 已上传成功的附件列表(包含ossId和category)
const uploadedAttachments = ref<Array<{ ossId: string; category: string }>>([]);
// 上传地址
const uploadAction = computed(() => '/web/admin/attachment/upload');
// 上传请求头
const uploadHeaders = computed(() => ({
Authorization: `Bearer ${userStore.accessToken || ''}`
}));
// 上传额外数据
const uploadData = computed(() => {
return {
bizType: 'crm_contract_file'
};
});
const uploadRef = ref();
/** 打开对话框 */
const open = async (id: number) => {
contractId.value = id;
dialogVisible.value = true;
attachmentList.value = [];
uploadedAttachments.value = [];
contractId.value = id
dialogVisible.value = true
attachmentList.value = []
await loadExistingAttachments()
}
// 加载已有的附件数据
await loadExistingAttachments();
};
/** 加载已有的附件数据 */
const loadExistingAttachments = async () => {
if (!contractId.value) return;
if (!contractId.value) return
try {
const res = await getContract(contractId.value);
const contract = res.data;
if (!contract?.attachmentFiles) return;
attachmentList.value = buildUploadFileListFromField(contract.attachmentFiles);
uploadedAttachments.value = attachmentList.value
.filter((file) => file.ossId)
.map((file) => ({
ossId: String(file.ossId),
category: String(file.category || 'other')
}));
const res = await getContract(contractId.value)
const contract = res.data
if (!contract?.attachmentFiles) return
attachmentList.value = buildUploadFileListFromField(
contract.attachmentFiles
) as ContractAttachmentItem[]
} catch (error) {
console.error('[附件回显] 加载附件数据失败:', error);
console.error('[附件回显] 加载附件数据失败:', error)
}
};
}
/** 文件类型判断 */
const isImageFile = (file: any) => {
const imageTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/webp'];
// 1. 检查 raw.type(新上传的文件)
if (file.raw?.type && imageTypes.includes(file.raw.type)) {
return true;
const validateAttachments = () => {
if (attachmentList.value.some((file) => file.status === 'uploading')) {
$modal.msgWarning('仍有附件正在上传,请稍候')
return false
}
// 2. 检查文件名扩展名
if (file.name && /\.(jpg|jpeg|png|gif|bmp|webp)$/i.test(file.name)) {
return true;
}
// 3. 检查 URL 扩展名(回显的文件)
if (file.url && /\.(jpg|jpeg|png|gif|bmp|webp)(\?|$)/i.test(file.url)) {
return true;
}
// 4. 如果有 ossId,尝试通过 URL 判断(即使 URL 可能被清空)
if (file.ossId && file.url && file.url.includes('image')) {
return true;
}
return false;
};
/** 获取文件OSS ID */
const getFileOssId = (file: any) => {
return file.ossId || '';
};
/** 获取文件扩展名 */
const getFileExt = (fileName: string) => {
const ext = fileName.split('.').pop()?.toUpperCase();
return ext || 'FILE';
};
/** 获取缩写的文件名 */
const getShortFileName = (fileName: string) => {
if (!fileName) return '';
const lastDotIndex = fileName.lastIndexOf('.');
if (lastDotIndex === -1) {
return fileName.length > 12 ? fileName.substring(0, 12) + '...' : fileName;
}
const name = fileName.substring(0, lastDotIndex);
const ext = fileName.substring(lastDotIndex);
if (name.length <= 8) {
return fileName;
}
return name.substring(0, 8) + '...' + ext;
};
/** 上传前检查 */
const handleBeforeUpload = (file: any) => {
const isValidType = /\.(jpg|jpeg|png|gif|bmp|webp|pdf|doc|docx|txt)$/i.test(file.name);
if (!isValidType) {
$modal.msgError('上传文件格式不正确!')
return false;
}
return true;
};
/** 上传成功 */
const handleUploadSuccess = (response: any, file: any) => {
const payload = response?.data ?? response;
const url = payload?.url || payload?.fileUrl || payload;
if (url) {
file.url = url;
file.ossId = url;
file.status = 'success';
file.category = file.category || 'other';
uploadedAttachments.value.push({
ossId: url,
category: file.category
});
$modal.msgSuccess('文件上传成功');
} else {
file.status = 'error';
$modal.msgError(response?.msg || '文件上传失败');
}
};
/** 附件分类变更 */
const handleCategoryChange = (file: any) => {
// 更新已上传附件列表中的分类
const index = uploadedAttachments.value.findIndex((item) => item.ossId === file.ossId);
if (index !== -1) {
uploadedAttachments.value[index].category = file.category;
}
};
/** 保存附件到数据库 */
const saveAttachments = async () => {
if (!contractId.value) {
return;
}
// 检查是否有附件未选择分类
const unclassifiedFiles = attachmentList.value.filter((file) => file.status === 'success' && !file.category);
if (unclassifiedFiles.length > 0) {
const unclassified = attachmentList.value.filter(
(file) => file.status === 'success' && file.ossId && !file.category
)
if (unclassified.length > 0) {
$modal.msgWarning('请为所有附件选择分类')
return;
return false
}
return true
}
// 即使没有附件也允许保存(用于清空附件的场景)
await uploadAttachments({
id: contractId.value,
attachments: uploadedAttachments.value
});
};
/** 上传失败 */
const handleUploadError = (error: any) => {
console.error('文件上传失败:', error);
$modal.msgError('文件上传失败')
};
/** 文件预览 */
const handleFilePreview = (file: any) => {
if (isImageFile(file)) {
window.open(file.url);
} else {
handleDownloadAttachment(file);
}
};
/** 文件变更 */
const handleFileChange = (file: any, fileList: any) => {
attachmentList.value = fileList;
};
/** 删除附件确认 */
const handleRemoveAttachmentConfirm = (file: any) => {
$modal
.confirm('确定要删除这个文件吗?')
.then(() => {
handleRemoveAttachment(file);
})
.catch(() => {
// 取消删除
});
};
/** 删除附件 */
const handleRemoveAttachment = (file: any) => {
const index = attachmentList.value.findIndex((item) => item.uid === file.uid);
if (index !== -1) {
attachmentList.value.splice(index, 1);
}
// 从已上传附件列表中移除
if (file.ossId) {
const uploadIndex = uploadedAttachments.value.findIndex((item) => item.ossId === file.ossId);
if (uploadIndex !== -1) {
uploadedAttachments.value.splice(uploadIndex, 1);
}
}
};
/** 下载附件 */
const handleDownloadAttachment = async (file: any) => {
if (!file.ossId && !file.url) {
$modal.msgError('文件信息不完整,无法下载');
return;
}
try {
await downloadCrmAttachment(file);
$modal.msgSuccess('下载成功');
} catch (error) {
console.error('下载附件失败:', error);
$modal.msgError('下载失败');
}
};
/** 保存并关闭 */
const handleSaveAndClose = async () => {
// 检查是否有附件未选择分类
const unclassifiedFiles = attachmentList.value.filter((file) => file.status === 'success' && !file.category);
if (unclassifiedFiles.length > 0) {
$modal.msgWarning('请为所有附件选择分类')
return;
}
if (!contractId.value) return
if (!validateAttachments()) return
saveLoading.value = true
try {
await saveAttachments();
await uploadAttachments({
id: contractId.value,
attachments: mapContractAttachmentsPayload(attachmentList.value)
})
$modal.msgSuccess('保存成功')
emit('success');
dialogVisible.value = false;
attachmentList.value = [];
uploadedAttachments.value = [];
emit('success')
handleClose()
} catch (error) {
console.error('保存附件失败:', error);
console.error('保存附件失败:', error)
$modal.msgError('保存附件失败')
} finally {
saveLoading.value = false
}
};
}
/** 关闭对话框 */
const handleClose = () => {
dialogVisible.value = false;
attachmentList.value = [];
uploadedAttachments.value = [];
};
dialogVisible.value = false
attachmentList.value = []
}
// 暴露方法给父组件调用
defineExpose({
open
});
defineExpose({ open })
</script>
<style scoped lang="scss">
/* 上传区域容器样式 */
.attachment-upload-container {
width: 100%;
max-height: calc(100vh - 300px);
min-height: 200px;
overflow-y: auto;
padding-right: 8px;
border: 1px solid var(--border, #d9e8e2);
border-radius: 8px;
background: var(--primary-bg, #ecfdf5);
}
.attachment-upload-container .upload-demo {
padding: 12px;
min-height: 180px;
}
.attachment-upload-container .upload-demo .el-upload {
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
transition: all 0.3s;
}
.attachment-upload-container .upload-demo .el-upload:hover {
border-color: var(--primary, #10b981);
}
.attachment-upload-container .upload-demo .el-upload__tip {
font-size: 12px;
color: var(--text-muted, #64748b);
margin-top: 7px;
}
/* 调整拖拽上传图标与文字在同一行、居中对齐 */
.attachment-upload-container .upload-demo .el-upload-dragger1 {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 15px;
padding: 0px !important;
}
.attachment-upload-container .upload-demo .el-upload-dragger1 .el-icon--upload {
margin: 0;
padding: 0px !important;
}
/* 文件卡片占位封面样式 */
.attachment-upload-container .upload-demo .upload-file-placeholder {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: #909399;
font-size: 14px;
}
.attachment-upload-container .upload-demo .upload-file-placeholder .el-icon {
font-size: 28px;
margin-bottom: 4px;
}
.attachment-upload-container .upload-demo .upload-file-placeholder .file-ext {
font-size: 12px;
text-transform: uppercase;
text-align: center;
}
/* 让附件卡片一行展示 5 个 */
.attachment-upload-container :deep(.el-upload-list--picture-card) {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: 9px;
margin-top: 12px;
padding-bottom: 16px;
}
/* 确保卡片宽度自适应,增加高度以容纳分类选择框 */
.attachment-upload-container :deep(.el-upload-list--picture-card) .el-upload-list__item {
margin: 0;
height: 220px !important;
width: 100% !important;
}
:deep(.el-upload-dragger) {
border: 0px !important;
padding: 0px !important;
}
:deep(.el-upload-dragger) .el-upload-dragger1 {
border: 0px !important;
padding: 0px !important;
}
/* 文件卡片样式 */
.upload-file-card {
position: relative;
width: 100%;
height: 100%;
border-radius: 6px;
overflow: hidden;
background: #fff;
transition: all 0.3s;
}
.upload-file-card:hover {
}
.upload-file-thumb {
position: relative;
width: 100%;
height: 110px;
background: #f5f5f5;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
}
.upload-thumb-image {
width: 100%;
height: 100%;
object-fit: cover;
}
.upload-file-loading {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: #fff;
font-size: 12px;
}
.upload-file-loading .el-icon {
font-size: 24px;
margin-bottom: 4px;
}
.upload-file-loading .loading-text {
margin-top: 4px;
}
.upload-file-name {
padding: 8px;
font-size: 12px;
color: #333;
text-align: center;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
background: #fff;
border-top: 1px solid #e4e7ed;
}
.upload-file-category {
padding: 4px 8px;
background: #fff;
border-top: 1px solid #e4e7ed;
}
.upload-file-card-actions {
padding: 6px 8px;
display: flex;
justify-content: center;
gap: 8px;
background: #fafafa;
border-top: 1px solid #e4e7ed;
transition: all 0.3s;
height: 38px;
}
.upload-file-card-actions .el-button {
padding: 4px 8px;
font-size: 14px;
.upload-scan-dialog {
:deep(.el-dialog__body) {
padding-top: 12px;
}
}
.upload-dialog-actions {