import api from '@/api/api'
export default {
    //file: 类似this.$refs.file.files[0]
    upload: function (file, int) {
        return new Promise((resolve, reject) => {
            var datas = {
                imageStatus: int
            }
            api.getPolicy(datas).then(res => {
                var ossToken = res.data.resultContent
                // const filename = file.name;
                // const fileExtension = filename.split('.').pop().toLowerCase()
                let formData = new FormData()
                // formData.append('key', ossToken.dir + "." + fileExtension); //存储在oss的文件路径
                formData.append('key', ossToken.dir); //存储在oss的文件路径
                formData.append('OSSAccessKeyId', ossToken.accessid); //accessKeyId
                formData.append('policy', ossToken.policy); //policy
                formData.append('Signature', ossToken.signature); //签名
                formData.append('callback', ossToken.callback); //回调
                formData.append('success_action_status', 200); //成功后返回的操作码
                //如果是base64文件，那么直接把base64字符串转成blob对象进行上传就可以了
                formData.append("file", file);
                api.uploadFile(ossToken.host, formData).then(resp => {
                    resolve(ossToken.dir)
                    // resolve(ossToken.host + '/' + ossToken.dir)
                }).catch(error => {
                    reject(error)
                })

            }).catch(err => {
                reject(err)
            })
        })

    },
}