You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.7 KiB

6 years ago
import request from '@/config/request'
6 years ago
import {attachmentUploadUrl, attachmentPrefix} from '@/config'
6 years ago
import {isEmpty, timeFormat} from "@/utils"
const baseUrl = '/file'
const defaultOptions = {
headers: {"Content-Type": "multipart/form-data"},
generateKey(filename) {
return timeFormat('yyyy/MM/dd/') + Date.now() + '/' + filename
},
onUploadProgress(e) {
}
6 years ago
}
//获取七牛云直传需要的token
export function getToken() {
return request.get(baseUrl + '/getToken').then(({data}) => data.data)
}
//下载文件
export function download(url, name) {
const href = typeof url === 'object' ? window.URL.createObjectURL(url) : url
const anchor = document.createElement('a')
6 years ago
anchor.style.visibility = 'hidden'
anchor.href = href
anchor.download = name
document.body.appendChild(anchor)
anchor.click()
document.body.removeChild(anchor)
6 years ago
if (typeof url === 'object') window.URL.revokeObjectURL(url)
}
//删除已上传的文件
6 years ago
export function deleteUpload(url) {
return request.get(baseUrl + '/delete?url=' + encodeURIComponent(url))
}
//七牛云直传
export function upload(blob, filename, options = {}) {
6 years ago
return getToken()
.then(token => {
const param = new FormData()
6 years ago
param.append('token', token)
param.append('key', options.generateKey(filename))
6 years ago
param.append('file', blob, filename)
return request.post(attachmentUploadUrl, param, {...defaultOptions, ...options})
6 years ago
})
.then(({data}) => data)
6 years ago
}
//自动补全附件链接前缀
export function autoCompleteUrl(url) {
if (isEmpty(url)) return ''
6 years ago
if (['http', 'https'].some(i => url.startsWith(i))) return url
6 years ago
return attachmentPrefix + url
}