/* 上传文件 */
import fetch from "@/utils/fetch";

let uploadAPI = (data) => {
    //上传接口定义
    return fetch({
        url: "/File/multipartFileUpload",
        method: "POST",
        headers: {"Content-Type": "multipart/form-data"},
        data,
        onUploadProgress: (progressEvent) => {
        },
    });
};

export default {
    /**
     * 文件上传
     * @param {binary} files
     * @param {array} paths
     * @param {array} names
     * @returns {Array}
     */

    uploadAnyFile(files, paths = [], names = []) {
        //文件列表;根路径;目录;新文件名，必须带后缀名
        let params = new FormData(); // 创建form对象
        files.forEach((element) => {
            params.append("files", element); // 添加不会覆盖旧值
        });
        paths.forEach((element) => {
            params.append("paths", element); // 添加不会覆盖旧值
        });

        names.length > 0
            ? names.forEach((element) => {
                params.append("names", element);
            })
            : params.append("names", []); //数据节点为接口必传，可为空
        return uploadAPI(params)
            .then((res) => {
                return res.data;
            })
            .catch((err) => {
                res.msg;
            });
    },
};
