LuoSong
LuoSong
Published on 2023-10-17 / 51 Visits
0
0

前后端FormData数据传递

前后端FormData数据传递

数据结构

数据结构

前端构造数据

// list<object>添加属性
const addProptypes = (data, dataIndex, value) => {
    if (data.length) {
      data.map((item) => {
        item[dataIndex] = value;
      });
    };
    return data;
    };

    // formdata添加list
    const addDataToFormData = (formdata, data, name) => {
    if (data.length) {
      data.map((item, index) => {
        for (let key in item) {
          formdata.append(`${name}[${index}].${key}`, item[key]);
        };
      });
    };
    return formdata;
    };

    // 调整类型转换
    const statusToString = (data) => {
    let statusArray = [];
    if (data.margin) {
      statusArray.push(1);
    };
    if (data.effective) {
      statusArray.push(2);
    };
    if (data.expenses) {
      statusArray.push(3);
    };
    return statusArray.join(',');
    }

    // 添加文件list
    const addFiles = (data, formdata, name) => {
    if (data.length) {
      data.map((item) => {
        formdata.append(name, item);
      });
    };
    return formdata;
    }

    const submitApply = () => {
    if (stepThreeBeforeData.length === 0) {
      message.error('当前订购及使用情况不能为空');
      return;
    };
    if (stepThreeAfterData.length === 0) {
      message.error('申请调整情况不能为空');
      return;
    };
    if (!applyAdjustStatus.margin && !applyAdjustStatus.effective && !applyAdjustStatus.expenses) {
      message.error('请勾选调整类型,至少一项');
      return;
    };
    if (reasonDetail === '') {
      message.error('调账原因不能为空');
      return;
    };
    if (fileList.length === 0) {
      message.error('请上传附件,至少一个');
      return;
    };
    let fileAllSize = 0;
    fileList.map((item) => {
      fileAllSize += item.size;
    });
    if (fileAllSize > 100 * 1024 * 1024) {
      message.error('附件大小超过最大限制100MB,请重新选择附件');
      return;
    };
    let formdata = new FormData();
    let before = [...stepThreeBeforeData];
    let after = [...stepThreeAfterData];
    before = addProptypes(before, 'adjustStatus', '0');
    after = addProptypes(after, 'adjustStatus', '1');
    after = addProptypes(after, 'isOrder', '0');
    formdata = addDataToFormData(formdata, [...before, ...after], 'dataList');
    formdata.append('adjustType', statusToString(applyAdjustStatus));
    formdata.append('adjustReason', reasonDetail.trim());
    formdata = addFiles(fileList, formdata, 'adjustFileList');
    dispatch({
      type: `billAdjustmentApplyModels/applySubmit`,
      payload: formdata
    }).then(res => {

    });
}

后端接收数据

@RequestMapping(value = "/api")
public RestResponse apiTest(@ModelAttribute TestParamsPojo testParamsPojo){
    String str = testParamsPojo.getAdjustReason();
    AdjustmentPojo name = testParamsPojo.getDataList().get(0);
    String name_1 = name.getCustName();
    System.out.println(name_1);
    if(str.compareTo("1") > 0){
        return RestResponse.success();
    } else {
        return RestResponse.error(ExceptionEnum.REQUEST_PARAM_ERROR);
    }
}

TestParamsPojo

import lombok.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @program: YouGos
 * @description: 测试类
 * @author: LuoSong
 * @create: 2023-10-17 17:57:36
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
public class TestParamsPojo {
    private List<AdjustmentPojo> dataList;
    private String adjustReason;
    private List<MultipartFile> adjustFileList;
    private String adjustType;
}

AdjustmentPojo

import lombok.*;

/**
 * @program: YouGos
 * @description: 测试类
 * @author: LuoSong
 * @create: 2023-10-17 18:32:58
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
public class AdjustmentPojo {
    private String sourceId;
    private String custName;
    private String goodsCode;
}

Comment