helper.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. export function resolveFileString(str: string) {
  2. return str ? str.split(';').map(s => resolveSingleFileString(s)) : []
  3. }
  4. export function resolveSingleFileString(str: string, sign = '|') {
  5. const [url, name] = str.split(sign)
  6. return {
  7. name,
  8. url: url.startsWith('http') ? url : `${(window as any).GLOBAL_CONFIG.oss}/${url}`,
  9. origin: url,
  10. }
  11. }
  12. // 获取完整文件地址
  13. export function getFullUrl(url: string) {
  14. if (!url)
  15. return ''
  16. return url.startsWith('http') ? url : `${(window as any).GLOBAL_CONFIG.oss}/${url}`
  17. }
  18. // 获取不完整文件地址
  19. export function getPartUrl(url: string) {
  20. if (!url)
  21. return ''
  22. return url.startsWith('http') ? url.startsWith((window as any).GLOBAL_CONFIG.oss) ? url.substring((window as any).GLOBAL_CONFIG.oss.length) : Error('oss 地址错误') : url
  23. }
  24. export function getAvatarUrl(id: string) {
  25. return `${(window as any).GLOBAL_CONFIG.oss}/user/main/user_avatar?user_id=${id}`
  26. }
  27. export function getTime(timestamp: string) {
  28. if (!timestamp)
  29. return ''
  30. const date = timestamp.length === 10 ? new Date(Number(timestamp) * 1000) : new Date(timestamp)
  31. const Year = `${date.getFullYear()}年`
  32. const Month = `${date.getMonth() < 9 ? `0${date.getMonth() + 1}` : date.getMonth() + 1}月`
  33. function format(time: number) {
  34. return time < 10 ? `0${time}` : time
  35. }
  36. const D = `${format(date.getDate())}日 `
  37. const h = `${format(date.getHours())}:` // 小时
  38. const m = `${format(date.getMinutes())}:` // 分钟
  39. const s = format(date.getSeconds()) // 秒
  40. return Year + Month + D + h + m + s
  41. }
  42. export function getFileType(str: string) {
  43. if (str) {
  44. str = str += ''.toLocaleLowerCase()
  45. const arr = str.split('.')
  46. const ext = arr[arr.length - 1].toLowerCase()
  47. if (['pdf'].includes(ext))
  48. return 'pdf'
  49. else if (['mp4'].includes(ext))
  50. return 'video'
  51. else if (['ppt', 'pptx', 'xls', 'xlsx', 'doc', 'docx'].includes(ext))
  52. return 'office'
  53. else if (['mp3'].includes(ext))
  54. return 'audio'
  55. else if (['png', 'jpg', 'jpeg', 'webp'].includes(ext))
  56. return 'image'
  57. return 'other'
  58. }
  59. return 'other'
  60. }
  61. // 手机号码带*处理
  62. export function dealPhone(str: string) {
  63. if (str) {
  64. const reg = /(\d{3})\d*(\d{4})/
  65. const phone = str.replace(reg, '$1****$2')
  66. return phone
  67. }
  68. else {
  69. return ''
  70. }
  71. }