import request from '@/request'
export default {
    // 上传图片标识 0头像 1身份证 2题库 3指南指引图片 4广告图片 5身份证或学信网图片 6文件excel,word,zip等
    //file: 类似this.$refs.file.files[0]
    upload: function (file, int) {
        return new Promise((resolve, reject) => {
            if(typeof file != 'object') {
              resolve(file)
              return;
            }
            var datas = {
                imageStatus: int
            }
            request.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);
                request.uploadFile(ossToken.host, formData).then(resp => {
                    resolve(ossToken.dir)
                    // resolve(ossToken.host + '/' + ossToken.dir)
                }).catch(error => {
                  console.log(error,'1231')
                    reject(error)
                })

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

    },
}