import co from 'co'

const OSSConfig = {
  uploadHost: 'https://oss-cn-shanghai.aliyuncs.com', //OSS上传地址
  showUrl:'http://fresh-life.oss-cn-shanghai.aliyuncs.com',
  type: 'scs',
  ossParams: {
    region: 'oss-cn-shanghai',
    success_action_status: '200', // 默认200
    accessKeyId: 'LTAI88rwaldHLQei',
    accessKeySecret: 'TblIpmrYwpsbTrJowCQHKFi0vOEwkF',
    bucket: 'fresh-life',
  },
}
/**
 *
 * @param event 上传图片的资源路径
 * @param type 图片资源类型
 * @param dir oss要保存的文件夹
 * @returns {OSS文件路径}
 */

function uploadOSS(event,type, dir = '/') {
  debugger
  return new Promise((resolve, reject) => {
    var client = new OSS.Wrapper({
      region: OSSConfig.ossParams.region,
      accessKeyId: OSSConfig.ossParams.accessKeyId,
      accessKeySecret: OSSConfig.ossParams.accessKeySecret,
      bucket: OSSConfig.ossParams.bucket,
    });
    // debugger;
    var file;
    if (type == 0) {
      file = event;
    } else {
      file = event.file;
    }
    // 获取当前时间
    let dateT = new Date();
    // 获取当前年
    let year = dateT.getFullYear();
    // 获取当前月
    let month = dateT.getMonth() + 1;
    if (month < 10) {
      month = "0" + month;
    }
    
    // 获取当前日
    let date = dateT.getDate();
    if(date < 10){
      date = "0"+date;
    }
    //随机数
    let datetime = new Date().getTime();
    let i = Math.floor((Math.random() * 900 + 100));

    let myStr = 'video/'+"/"+ year + month + date + "/" + datetime+i+"/"
    let names = new String(file.name.substring(0,file.name.indexOf('.')), "UTF-8");;
    let suffix = file.name.substring(file.name.lastIndexOf('.'));
    let randomName = `${dir}${myStr}${names}${suffix}`;
    debugger
    co(function* () {
      var result = yield client.multipartUpload(randomName, file);
      resolve( `${OSSConfig.showUrl}${randomName}`);
    }).catch(function (err) {
      console.log(err);
      reject(err);
    });
  });
}

export { uploadOSS }
