|
|
|
@ -1,5 +1,6 @@ |
|
|
|
import request from "@/api/request" |
|
|
|
import request from "@/api/request" |
|
|
|
import {attachmentUploadUrl, attachmentPrefix, filePreviewPrefix} from '@/config' |
|
|
|
import {getToken} from "@/api/file" |
|
|
|
|
|
|
|
import {file as fileConfig} from '@/config' |
|
|
|
import {isEmpty, timeFormat} from "@/util" |
|
|
|
import {isEmpty, timeFormat} from "@/util" |
|
|
|
import {isTxt} from "@/util/validate" |
|
|
|
import {isTxt} from "@/util/validate" |
|
|
|
|
|
|
|
|
|
|
|
@ -18,21 +19,16 @@ export function preview(url) { |
|
|
|
if (isTxt(url)) return window.open(url) |
|
|
|
if (isTxt(url)) return window.open(url) |
|
|
|
|
|
|
|
|
|
|
|
const connectChar = url.includes('?') ? '&' : '?' |
|
|
|
const connectChar = url.includes('?') ? '&' : '?' |
|
|
|
url = url + connectChar + 'fullfilename=' + url.replace(attachmentPrefix, '') |
|
|
|
url = url + connectChar + 'fullfilename=' + url.replace(fileConfig.storePrefix, '') |
|
|
|
const anchor = document.createElement('a') |
|
|
|
const anchor = document.createElement('a') |
|
|
|
anchor.style.opacity = '0' |
|
|
|
anchor.style.opacity = '0' |
|
|
|
anchor.href = `${filePreviewPrefix}/onlinePreview?url=${encodeURIComponent(url)}` |
|
|
|
anchor.href = `${fileConfig.previewPrefix}/onlinePreview?url=${encodeURIComponent(url)}` |
|
|
|
anchor.target = '_blank' |
|
|
|
anchor.target = '_blank' |
|
|
|
document.body.appendChild(anchor) |
|
|
|
document.body.appendChild(anchor) |
|
|
|
anchor.click() |
|
|
|
anchor.click() |
|
|
|
document.body.removeChild(anchor) |
|
|
|
document.body.removeChild(anchor) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//获取七牛云直传需要的token
|
|
|
|
|
|
|
|
export function getToken() { |
|
|
|
|
|
|
|
return request.get('/file/getToken').then(({data}) => data.data) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//下载文件
|
|
|
|
//下载文件
|
|
|
|
export function download(url, name) { |
|
|
|
export function download(url, name) { |
|
|
|
const href = typeof url === 'object' ? window.URL.createObjectURL(url) : url |
|
|
|
const href = typeof url === 'object' ? window.URL.createObjectURL(url) : url |
|
|
|
@ -48,29 +44,25 @@ export function download(url, name) { |
|
|
|
if (typeof url === 'object') window.URL.revokeObjectURL(url) |
|
|
|
if (typeof url === 'object') window.URL.revokeObjectURL(url) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//删除已上传的文件
|
|
|
|
|
|
|
|
export function deleteUpload(url) { |
|
|
|
|
|
|
|
return request.get('/file/delete?url=' + encodeURIComponent(url)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//七牛云直传
|
|
|
|
//七牛云直传
|
|
|
|
export function upload(blob, filename, options = {}) { |
|
|
|
export function upload(blob, filename, options = {}) { |
|
|
|
return getToken() |
|
|
|
return getToken |
|
|
|
|
|
|
|
.request() |
|
|
|
.then(token => { |
|
|
|
.then(token => { |
|
|
|
if (!options.generateKey) options.generateKey = defaultOptions.generateKey |
|
|
|
if (!options.generateKey) { |
|
|
|
|
|
|
|
options.generateKey = defaultOptions.generateKey |
|
|
|
|
|
|
|
} |
|
|
|
const param = new FormData() |
|
|
|
const param = new FormData() |
|
|
|
param.append('token', token) |
|
|
|
param.append('token', token) |
|
|
|
param.append('key', options.generateKey(filename)) |
|
|
|
param.append('key', options.generateKey(filename)) |
|
|
|
param.append('file', blob, filename) |
|
|
|
param.append('file', blob, filename) |
|
|
|
return request.post(attachmentUploadUrl, param, {...defaultOptions, ...options}) |
|
|
|
return request.post(fileConfig.uploadUrl, param, {...defaultOptions, ...options}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
.then(({data}) => data) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//自动补全附件链接前缀
|
|
|
|
//自动补全附件链接前缀
|
|
|
|
export function autoCompleteUrl(url) { |
|
|
|
export function autoCompleteUrl(url) { |
|
|
|
if (isEmpty(url)) return '' |
|
|
|
if (isEmpty(url)) return '' |
|
|
|
if (['http', 'https'].some(i => url.startsWith(i))) return url |
|
|
|
if (['http', 'https'].some(i => url.startsWith(i))) return url |
|
|
|
return attachmentPrefix + url |
|
|
|
return fileConfig.storePrefix + url |
|
|
|
} |
|
|
|
} |
|
|
|
|