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

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


export function getTime(timestamp: string) {
  if (!timestamp)
    return ''

  const date = timestamp.length === 10 ? new Date(Number(timestamp) * 1000) : new Date(timestamp)

  const Year = `${date.getFullYear()}年`
  const Month = `${date.getMonth() < 9 ? `0${date.getMonth() + 1}` : date.getMonth() + 1}月`

  function format(time: number) {
    return time < 10 ? `0${time}` : time
  }

  const D = `${format(date.getDate())}日 `
  const h = `${format(date.getHours())}:` // 小时
  const m = `${format(date.getMinutes())}:` // 分钟
  const s = format(date.getSeconds()) // 秒
  return Year + Month + D + h + m + s
}


export function getFileType(str: string) {
  if (str) {
    str = str += ''.toLocaleLowerCase()
    const arr = str.split('.');
    const ext = arr[arr.length - 1].toLowerCase()
    if (['pdf'].includes(ext))
      return 'pdf'
    else if (['mp4'].includes(ext))
      return 'video'
    else if (['ppt', 'pptx', 'xls', 'xlsx', 'doc', 'docx'].includes(ext))
      return 'office'
    else if (['mp3'].includes(ext))
      return 'audio'
    else if (['png', 'jpg', 'jpeg', 'webp'].includes(ext))
      return 'image'
    return 'other'
  }
  return 'other'
}


// 手机号码带*处理
export function dealPhone(str: string) {
  if (str) {
    let reg = /(\d{3})\d*(\d{4})/;
    let phone = str.replace(reg, '$1****$2')
    return phone
  } else {
    return ""
  }
}
