const contentTypeMap: any = { jpg: 'image', jpeg: 'image', svg: 'image', png: 'image', pdf: 'pdf', doc: 'word', docx: 'word', xls: 'excel', xlsx: 'excel', cvs: 'excel', ppt: 'ppt', pptx: 'ppt' } export function getFileType (name = ''): 'pdf' | 'image' | 'word' | 'excel' | 'ppt' | 'other' { const isUrl = name.startsWith('http://') || name.startsWith('https://') let filename = name if (isUrl) { const url = new URL(name) const path = url.pathname filename = path } const suffix = (filename.substr(filename.lastIndexOf('.') + 1) || '').toLowerCase() return contentTypeMap[suffix] || 'unknown' } export function previewPdf (url: string) { // window.open(url) window.open(`https://officeapps.honjo168.com/op/view.aspx?src=${encodeURIComponent(url)}`) } export function previewOffice (url: string) { // http://view.officeapps.live.com/op/view.aspx 官方 window.open(`https://officeapps.honjo168.com/op/view.aspx?src=${encodeURIComponent(url)}`) } export function download (name: string, url: string) { const link = document.createElement('a') link.style.display = 'none' link.href = url link.setAttribute('download', name) link.setAttribute('target', '_blank') document.body.appendChild(link) link.click() document.body.removeChild(link) }