import baseURL from '@/config/baseURL'
import {getTokenHeader} from '../request/header'
import {getObjValue} from "js-fast-way";

export const uploadApi = async (file, form= {}) => {
    return new Promise((resolve) => {
        let url = '/api/blade-resource/oss/endpoint/upload-file';
        //#ifdef APP-PLUS
        url = baseURL + 'blade-resource/oss/endpoint/upload-file'
        //#endif
        uni.uploadFile({
            url: url,
            name: 'file',
            formData: form,
            header: getTokenHeader(),
            filePath: file,
            success:(res) => {
                const {code, msg, data} = JSON.parse(res?.data)
                if (code === 200) {
                    resolve({
                        error: false,
                        msg: msg,
                        data: getObjValue(data)
                    })
                } else {
                    resolve({
                        error: true,
                        msg: msg,
                        data: {}
                    })
                }
            },
            fail:()=> {
                resolve({
                    error: true,
                    msg: '上传失败',
                    data: {}
                })
            }
        });
    })
}
