export const handleRequest = (reqConfig) => {
    //删除reqConfig为undefined的属性
    for (let key in reqConfig.data) {
        if (reqConfig.data[key] === undefined) {
            delete reqConfig.data[key];
        }
    }
    const promises = [];

    /** 普通请求 */
    if (reqConfig.dataType === 'json') {
        const promise = new Promise((resolve, reject) => {
            uni.request({
                ...reqConfig,
                success: function (res) {
                    resolve(res.data);
                },
                fail: function (err) {
                    reject(err);
                },
            });
        });
        promises.push(promise);
    }

    /** 文件上传 */
    if (reqConfig.dataType === 'formData') {
        const promise = new Promise((resolve, reject) => {
            const uploadParams = JSON.parse(JSON.stringify(reqConfig));
            delete uploadParams.data.file;
            uni.uploadFile({
                url: reqConfig.url,
                formData: uploadParams.data,
                file: reqConfig.data.file.file,
                success: function (res) {
                    resolve(JSON.parse(res.data));
                },
                fail: function (err) {
                    reject(err);
                },
            });
        });
        promises.push(promise);
    }

    /** 文件下载 */
    if (reqConfig.dataType === 'base64') {
        const promise = new Promise((resolve, reject) => {
            const url = reqConfig.url + '?' + Object.keys(reqConfig.data).map(key => key + '=' + reqConfig.data[key]).join('&');
            uni.downloadFile({
                url,
                header: {
                    ...reqConfig.header
                },
                success: function (res) {
                    resolve(res.tempFilePath);
                },
                fail: function (err) {
                    reject(err);
                },
            });
        });
        promises.push(promise);
    }

    // 返回一个包含所有异步操作的 Promise 数组
    return Promise.all(promises);
}
