124 lines
4.3 KiB
JavaScript
124 lines
4.3 KiB
JavaScript
Tea.context(function () {
|
||
this.downloadSDK = function (platform, event) {
|
||
this.checkAndDownload(platform, "sdk", event)
|
||
}
|
||
|
||
this.downloadDoc = function (platform, event) {
|
||
this.checkAndDownload(platform, "doc", event)
|
||
}
|
||
|
||
this.checkAndDownload = function (platform, type, event) {
|
||
if (event != null && typeof event.preventDefault == "function") {
|
||
event.preventDefault()
|
||
}
|
||
|
||
this.$get("/httpdns/apps/sdk/check")
|
||
.params({
|
||
platform: platform,
|
||
type: type
|
||
})
|
||
.success(function (resp) {
|
||
let data = (resp != null && resp.data != null) ? resp.data : {}
|
||
if (!data.exists) {
|
||
teaweb.warn(data.message || "当前暂无可下载文件")
|
||
return
|
||
}
|
||
if (typeof data.url == "string" && data.url.length > 0) {
|
||
this.downloadByBlob(data.url, platform, type)
|
||
return
|
||
}
|
||
teaweb.warn("下载地址生成失败,请稍后重试")
|
||
}.bind(this))
|
||
}
|
||
|
||
this.downloadByBlob = function (url, platform, type) {
|
||
let defaultFileName = "httpdns-sdk-" + platform + (type == "doc" ? ".md" : ".zip")
|
||
|
||
let xhr = new XMLHttpRequest()
|
||
xhr.open("GET", url, true)
|
||
xhr.responseType = "blob"
|
||
|
||
xhr.onload = function () {
|
||
if (xhr.status < 200 || xhr.status >= 300) {
|
||
teaweb.warn("下载失败(HTTP " + xhr.status + ")")
|
||
return
|
||
}
|
||
if (xhr.status == 204) {
|
||
teaweb.warn("下载被浏览器扩展或下载工具拦截,请暂时关闭相关扩展后重试")
|
||
return
|
||
}
|
||
|
||
let contentType = (xhr.getResponseHeader("Content-Type") || "").toLowerCase()
|
||
if (contentType.indexOf("application/json") >= 0) {
|
||
let reader = new FileReader()
|
||
reader.onload = function () {
|
||
try {
|
||
let json = JSON.parse(reader.result)
|
||
teaweb.warn((json && json.message) ? json.message : "下载失败,请稍后重试")
|
||
} catch (_e) {
|
||
teaweb.warn("下载失败,请稍后重试")
|
||
}
|
||
}
|
||
reader.readAsText(xhr.response)
|
||
return
|
||
}
|
||
|
||
let disposition = xhr.getResponseHeader("Content-Disposition") || ""
|
||
let fileName = xhr.getResponseHeader("X-SDK-Filename") || this.parseFileName(disposition) || defaultFileName
|
||
if (xhr.response == null || xhr.response.size <= 0) {
|
||
teaweb.warn("下载文件为空,请检查已上传 SDK 包是否完整")
|
||
return
|
||
}
|
||
this.saveBlob(xhr.response, fileName)
|
||
}.bind(this)
|
||
|
||
xhr.onerror = function () {
|
||
teaweb.warn("下载失败,请检查网络后重试")
|
||
}
|
||
|
||
xhr.send()
|
||
}
|
||
|
||
this.parseFileName = function (disposition) {
|
||
if (typeof disposition != "string" || disposition.length == 0) {
|
||
return ""
|
||
}
|
||
|
||
let utf8Match = disposition.match(/filename\*=UTF-8''([^;]+)/i)
|
||
if (utf8Match != null && utf8Match.length > 1) {
|
||
try {
|
||
return decodeURIComponent(utf8Match[1])
|
||
} catch (_e) {
|
||
}
|
||
}
|
||
|
||
let plainMatch = disposition.match(/filename="?([^";]+)"?/i)
|
||
if (plainMatch != null && plainMatch.length > 1) {
|
||
return plainMatch[1]
|
||
}
|
||
|
||
return ""
|
||
}
|
||
|
||
this.saveBlob = function (blob, fileName) {
|
||
if (window.navigator != null && typeof window.navigator.msSaveOrOpenBlob == "function") {
|
||
window.navigator.msSaveOrOpenBlob(blob, fileName)
|
||
return
|
||
}
|
||
|
||
let objectURL = window.URL.createObjectURL(blob)
|
||
let a = document.createElement("a")
|
||
a.style.display = "none"
|
||
a.href = objectURL
|
||
a.download = fileName
|
||
document.body.appendChild(a)
|
||
a.click()
|
||
setTimeout(function () {
|
||
window.URL.revokeObjectURL(objectURL)
|
||
if (a.parentNode != null) {
|
||
a.parentNode.removeChild(a)
|
||
}
|
||
}, 30000)
|
||
}
|
||
})
|