import {
  getToken,getAdminToken
} from '@/utils/auth'
import axios from 'axios';
// ckeditor5上传图片的适配器
export default class UploadImageAdapter {
  constructor(loader) {
    this.loader = loader;
    
  }


  //// imgUploadUrl:'/api/v1/teach/oss/file/oss/upload_files', // 你的图片上传地址  此地址使用base64上传图片
  // imgUploadUrl: base.prefix  + '/api/v1/teach/oss/file/oss/upload_filesSele', // 你的图片上传地址  此地址使用二进制上传
  async upload() {
    // 获取到用户上传的文件
    const img = await this.loader.file;
    const promise = new Promise(async (resolve, reject) => {

      const formData = new FormData();

      formData.append('file', img);
      axios.post('/api/v1/teach/oss/file/oss/upload_filesSele', formData, {
        headers: {
          'Authorization': getToken()
        }
      }).then(res => {
        console.log('上传返回信息', res);
        // 处理上传成功的响应 返回图片的url等下信息
        console.log('上传成功,图片地址是', res.data.data.url);
     
        if (res.status === 200) {
          // 必须要以 default: imgurl形式  ckeditor5 才会显示图片
          let response = {
            default: `${res.data.data.url}` // 图片全链接
            // default:'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAABMhJREFUeF7tW01sG0UU/t7sjtMS2RBFVALRA5yAIKQqhyi9I3JvHQ5wKWoKF0RQzoRSzhFReyqBcIa6Z0AcuDWA1BtFRZyQqx6QosiOIc3O7Dy0G9tZrx1219nYa2d9s3bmm/e+9+bNm59HCP1eLXNh6kVM/LuHM2Ifdvj7iP5XANT9HTRwh9ygDtT+w0xz76CoJzE5okrGEruh0fjjKzRAxF6HAwKYafY9TAOQsVBGv5G6fxvbHgk+AXNvc2ncLR+2me8Jm7RLKLM1O4Vzo2/U5Brs7WCbZq/x0wCeSt599HuceQYNunCFnxVybKJ9IqsoAdfzgOcS9RqzxjkBuQfkU6C/GOBoPSMElbIQEozhesG2H/QjS6IYoF113gBXBfAWEzKhfEtpYtSZ8J0guWYRHsUlIzYBypgysftZ1hTvVpRqgFiVQtyJQ0IsAhztvknCfN0JyI8AqsYZZABtXgBwPjgOk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FQP10G9vUKp1tkTD7GMVxL6L0LP/zTex6ynt6H1aPNyNAuczWn5MoUhFyXMrnvQLJsxJPdqfx5Pfr5ASD3X++/17jpC8IxgAAAABJRU5ErkJggg=='
          };
          resolve(response);
        } else {
          reject(false);
        }
       
      }).catch(error => {
        // 处理上传失败的响应
        console.log('上传失败', error);
        throw error;
      });
      
    });
    return promise;
  }
 
  abort() {
    // 可以书写删除服务器图片的逻辑
  }
}