import Http from './http'
export default {
decode(e) {
this._keyStr =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
var t = ''
var n, r, i
var s, o, u, a
var f = 0
e = e.replace(/[^A-Za-z0-9+/=]/g, '')
while (f < e.length) {
s = this._keyStr.indexOf(e.charAt(f++))
o = this._keyStr.indexOf(e.charAt(f++))
u = this._keyStr.indexOf(e.charAt(f++))
a = this._keyStr.indexOf(e.charAt(f++))
n = (s << 2) | (o >> 4)
r = ((o & 15) << 4) | (u >> 2)
i = ((u & 3) << 6) | a
t = t + String.fromCharCode(n)
if (u != 64) {
t = t + String.fromCharCode(r)
}
if (a != 64) {
t = t + String.fromCharCode(i)
}
}
t = this._utf8_decode(t)
return t
},
_utf8_decode(e) {
e = e.replace(/rn/g, 'n')
var t = ''
for (var n = 0; n < e.length; n++) {
var r = e.charCodeAt(n)
if (r < 128) {
t += String.fromCharCode(r)
} else if (r > 127 && r < 2048) {
t += String.fromCharCode((r >> 6) | 192)
t += String.fromCharCode((r & 63) | 128)
} else {
t += String.fromCharCode((r >> 12) | 224)
t += String.fromCharCode(((r >> 6) & 63) | 128)
t += String.fromCharCode((r & 63) | 128)
}
}
return t
},
UploadImage(file) {
if (!file) {
return
}
let reader = new FileReader()
let imgBase64
let url
reader.readAsArrayBuffer(file)
reader.onloadend = function(e) {
imgBase64 = e.target.result
}
let kind = file.type.split('/')[1]
let ossurl = ''
let BaseUrl
if (process.env.NODE_ENV === 'development') {
BaseUrl = 'https://xxx-test.xxx.com'
} else {
BaseUrl = 'https://api.xxx.com'
}
let urlto =
BaseUrl + `/api/tank/xxx/?origin_name=${file.name}&kind=${kind}`
return new Promise((resolve, reject) => {
Http.get(urlto)
.then((res) => {
ossurl = this.decode(res.body.oss_url)
const xhr = new XMLHttpRequest()
xhr.open('PUT', ossurl)
xhr.send(imgBase64)
xhr.onload = function() {
url = xhr.responseURL.split('?')[0]
resolve(url)
}
})
.catch((err) => {
reject(err)
})
})
}
}
封装Oss上传
最新推荐文章于 2024-02-21 17:13:32 发布