import {
    request,
    transformRepalceString
} from '@/untils/AxiosUtils.js';
import {
    baseUrl
} from '@/config/env.js'
import {
    getStore
} from '@/untils/store.js'
import {
    Base64
} from 'js-base64'
import website from "@/config/website.js"

export async function uploadFile(file) {
    if (file.size > 20971520) {
        uni.showToast({
            title: '图片过大,应该小于20m',
            duration: 1000,
            icon: 'none',
        })
        return
    }
    uni.showLoading({
        title: '上传中'
    })
    var token = getStore({
        name: 'token'
    })
    return new Promise((resolve, reject) => {
        const header = {
            'Authorization': `Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}`,
        }
        header[website.tokenName] = "bearer " + token
        uni.uploadFile({
            url: `${baseUrl}/api/jobslink-api/resource/file/save?bussinessType=5`,
            header,
            filePath: file.url,
            // formData: formdata,
            name: 'file',
            success: ({
                statusCode,
                data
            }) => {
                const resp = transformRepalceString(JSON.parse(data))
                if (statusCode === 200 && resp.code === 200) {
                    resolve(resp.msg)
                } else {
                    reject(new Error('数据异常'))
                }
            },
            fail: (uploadFileRes) => {
                reject(uploadFileRes)
            },
            complete: () => {
                uni.hideLoading()
            }
        });
    })
}