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) => {