export function resolveFileString(str) {
  return str ? str.split(';').map((s) => resolveSingleFileString(s)) : []
}

export function resolveSingleFileString(str, sign = '|') {
  const [url, name] = str.split(sign)
  return {
    name,
    url: url.startsWith('http') ? url : window.GLOBAL_CONFIG.oss + '/' + url,
    origin: url
  }
}

// 获取完整文件地址
export function getFullUrl(url) {
  if (!url) return '';
  return url.startsWith('http') ? url : window.GLOBAL_CONFIG.oss + '/' + url
}

// 获取不完整文件地址
export function getPartUrl(url) {
  if (!url) return '';
  return url.startsWith('http') ? url.startsWith(window.GLOBAL_CONFIG.oss) ? url.substring(window.GLOBAL_CONFIG.oss.length) : Error('oss 地址错误') : url
}

export function getAvatarUrl(id) {
  return window.GLOBAL_CONFIG.oss + '/user/main/user_avatar?user_id=' + id
}


export function base64toBlob(dataURI) {
  var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]; // mime类型
  var byteString = window.atob(dataURI.split(',')[1]); //base64 解码
  var arrayBuffer = new ArrayBuffer(byteString.length); //创建缓冲数组
  var intArray = new Uint8Array(arrayBuffer); //创建视图

  for (var i = 0; i < byteString.length; i++) {
    intArray[i] = byteString.charCodeAt(i);
  }

  return URL.createObjectURL(new Blob([intArray], { type: mimeString }));
}
