Source: packages/UpLoadFile/utils/newUpload.js

import Vue from 'vue'
import { put } from './ossClient'

//oss文件名称截取方法
export function fileNameInterception(name){
  let index = name.indexOf("_");
  let result = name.substr(index + 1,name.length);
  let aName = decodeURIComponent(result);
  let newName;

  //处理文件名
  var infix = aName.substring(aName.lastIndexOf('.')+1);  //获取文件名尾缀
  if ((aName.length-infix.length) <= 10){
    newName = aName;
  }else {
    newName = aName.substring(0,10) + '···' + '.' + infix
  }
  return newName
}


/**
 *  //oss请求方法 ocr识别方法
 * @param action 上传action值
 * @param dir 储存路劲
 */

export async function customRequestNew (action,dir,that) {
  let params = new FormData();
  params.append("file", action.file);
  const file = action.file;
  let name = new Date().valueOf() + parseInt(Math.random() * 10000) +'_'+ file.name;
  // put(name, file, process.env.VUE_APP_OSS_BUCKET,dir).then(result => {
  //   action.onSuccess(result, action.file) //解决一直loading情况,调用onSuccess
  //   action.loading = false;
  //   progress.percent = 99;
  //   that.fileObj = result;
  //   that.lockType =true;
  //   return that.fileObj;
  //   // that.$message.success(`${file.name} 上传成功`);
  // }).catch(e => {
  //   console.log(e)
  // })
  let progress = { percent: 1 }
  let speed = 100/(action.file.size/65000)//上传速度
  const intervalId = setInterval(() => {
    // 控制进度条防止在未上传成功时进度条达到100
    if (progress.percent < 99 && progress.percent+speed < 100 ) {
      progress.percent+=speed//控制进度条速度
      action.onProgress(progress)//onProgress接收一个对象{ percent: 进度 }在进度条上显示
    } else if((progress.percent = 99) ) {
      progress.percent++
    } else if (progress.percent=100){
      clearInterval(intervalId)
    }
  }, 100)


  const result = await put(name, file, process.env.VUE_APP_OSS_BUCKET,dir)
  action.onSuccess(result, action.file) //解决一直loading情况,调用onSuccess
  action.loading = false;
  progress.percent = 99;
  // that.fileObj = result;
  // that.lockType =true;
  return {...result,name:file.name}


}

/**
 *  //oss请求方法 ocr识别方法
 * @param action 上传action值
 * @param dir 储存路劲
 */

 export async function customRequest (action,dir,that) {
  let params = new FormData();
  params.append("file", action.file);
  const file = action.file;
  let name = new Date().valueOf() + parseInt(Math.random() * 10000) +'_'+ file.name;
  put(name, file, process.env.VUE_APP_OSS_BUCKET,dir).then(result => {
    action.onSuccess(result, action.file) //解决一直loading情况,调用onSuccess
    action.loading = false;
    progress.percent = 99;
    that.fileObj = result;
    that.lockType =true;
    return that.fileObj;
    // that.$message.success(`${file.name} 上传成功`);
  }).catch(e => {
    console.log(e)
  })
  let progress = { percent: 1 }
  let speed = 100/(action.file.size/65000)//上传速度
  const intervalId = setInterval(() => {
    // 控制进度条防止在未上传成功时进度条达到100
    if (progress.percent < 99 && progress.percent+speed < 100 ) {
      progress.percent+=speed//控制进度条速度
      action.onProgress(progress)//onProgress接收一个对象{ percent: 进度 }在进度条上显示
    } else if((progress.percent = 99) ) {
      progress.percent++
    } else if (progress.percent=100){
      clearInterval(intervalId)
    }
  }, 100)


  // const result = await put(name, file, process.env.VUE_APP_OSS_BUCKET,dir)
  // action.onSuccess(result, action.file) //解决一直loading情况,调用onSuccess
  // action.loading = false;
  // progress.percent = 99;
  // // that.fileObj = result;
  // // that.lockType =true;
  // return result


}


/**
 * 下载文件
 * @param {String} url 文件下载地址
 * @param {*} name 下载后文件名
 */
export function download(url, name) {
  const a = document.createElement('a')
  a.download = name
  a.href = url
  document.body.appendChild(a)
  a.click()
  document.body.removeChild(a)
}