From 06ca1d227d0de6adb9e625d30219e2325a7c41ac Mon Sep 17 00:00:00 2001 From: WUSIJIAN <13825895+wsj0228@user.noreply.gitee.com> Date: Wed, 10 Dec 2025 11:27:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=A7=9F=E6=88=B7=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=8A=9F=E8=83=BD?= =?UTF-8?q?,=E6=94=AF=E6=8C=81FormData=E6=A0=BC=E5=BC=8F=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E8=90=A5=E4=B8=9A=E6=89=A7=E7=85=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/system/tenant/index.ts | 6 +++ .../system/tenant/component/editTenant.vue | 44 +++++++++++++++---- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/api/system/tenant/index.ts b/src/api/system/tenant/index.ts index dae12d2..4792ac9 100644 --- a/src/api/system/tenant/index.ts +++ b/src/api/system/tenant/index.ts @@ -20,19 +20,25 @@ export function getTenant(id: number) { // 新增租户 export function addTenant(data: object) { + // 如果是 FormData,需要让浏览器自动设置 Content-Type(包含 boundary) + const isFormData = data instanceof FormData; return request({ url: '/api/v1/system/tenant/add', method: 'post', data: data, + headers: isFormData ? { 'Content-Type': undefined } : undefined, }); } // 修改租户 export function editTenant(data: object) { + // 如果是 FormData,需要让浏览器自动设置 Content-Type(包含 boundary) + const isFormData = data instanceof FormData; return request({ url: '/api/v1/system/tenant/edit', method: 'put', data: data, + headers: isFormData ? { 'Content-Type': undefined } : undefined, }); } diff --git a/src/views/system/tenant/component/editTenant.vue b/src/views/system/tenant/component/editTenant.vue index f00acf7..1c1a6a2 100644 --- a/src/views/system/tenant/component/editTenant.vue +++ b/src/views/system/tenant/component/editTenant.vue @@ -76,11 +76,11 @@ class="avatar-uploader" action="#" :auto-upload="false" + :on-change="handleFileChange" :show-file-list="false" - :on-change="handleAvatarSuccess" :before-upload="beforeAvatarUpload" > - +
点击上传营业执照 @@ -108,6 +108,7 @@ import { ElMessage, UploadProps } from 'element-plus'; import { Plus } from '@element-plus/icons-vue'; import { addTenant, editTenant } from '/@/api/system/tenant'; import { pcTextArr,provinceAndCityData } from 'element-china-area-data'; +import { getUpFileUrl } from '/@/utils/gfast'; // 定义父组件传递的事件 const emit = defineEmits(['getTenantList']); @@ -117,6 +118,9 @@ const formRef = ref(null); // 省市数据(使用 element-china-area-data) const cityOptions = ref([]); +// 单独定义文件对象,避免reactive深层代理导致FormData识别错误 +const fileRaw = ref(null); + const initCityData = () => { const data = JSON.parse(JSON.stringify(provinceAndCityData)); cityOptions.value = data.map((item: any) => { @@ -285,8 +289,25 @@ const onSubmit = () => { submitForm.city = ''; } + // 转换为FormData + const formData = new FormData(); + for (const key in submitForm) { + // 如果是文件字段,跳过(后面单独处理) + if (key === 'businessLicense') continue; + formData.append(key, submitForm[key]); + } + // 添加文件 + if (fileRaw.value) { + // 【重要】必须append raw file(二进制文件流),不能是element-plus的uploadFile包装对象 + // 否则后端接收到的会是 { uid: ..., status: ... } 这样的json结构 + formData.append('businessLicense', fileRaw.value); + } else if (submitForm.businessLicense && !submitForm.businessLicense.startsWith('blob:')) { + // 如果没有新文件,且原值不是blob预览地址,则传递原url字符串 + formData.append('businessLicense', submitForm.businessLicense); + } + if (state.ruleForm.id === 0) { - addTenant(submitForm).then(() => { + addTenant(formData).then(() => { ElMessage.success('添加成功'); closeDialog(); emit('getTenantList'); @@ -294,8 +315,11 @@ const onSubmit = () => { } else { // 过滤掉密码字段,或者后端接口决定是否更新密码 // 根据需求,修改时不能修改密码 - const { password, confirmPassword, tenantAccount, ...rest } = submitForm; - editTenant({ ...rest, id: state.ruleForm.id }).then(() => { + if (formData.has('password')) formData.delete('password'); + if (formData.has('confirmPassword')) formData.delete('confirmPassword'); + if (formData.has('tenantAccount')) formData.delete('tenantAccount'); + + editTenant(formData).then(() => { ElMessage.success('修改成功'); closeDialog(); emit('getTenantList'); @@ -319,11 +343,15 @@ const resetForm = () => { businessLicense: '', }; state.passwordStrength = 0; + fileRaw.value = null; }; -// 模拟上传图片 -const handleAvatarSuccess: UploadProps['onChange'] = (uploadFile) => { - state.ruleForm.businessLicense = URL.createObjectURL(uploadFile.raw!); +// 文件改变时触发 +const handleFileChange: UploadProps['onChange'] = (uploadFile) => { + if (uploadFile.raw) { + fileRaw.value = uploadFile.raw; + state.ruleForm.businessLicense = URL.createObjectURL(uploadFile.raw); + } }; const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {