提交 b611bf16 编写于 作者: Q qiang

feat(App): downloadFile

上级 a76887a4
......@@ -39,7 +39,7 @@
"node": ">=10.0.0"
},
"devDependencies": {
"@dcloudio/types": "^2.2.9",
"@dcloudio/types": "^2.2.10",
"@microsoft/api-extractor": "^7.13.2",
"@rollup/plugin-alias": "^3.1.1",
"@rollup/plugin-commonjs": "^17.0.0",
......
......@@ -5,3 +5,4 @@ export * from './device/accelerometer'
export * from './media/getImageInfo'
export * from './media/getVideoInfo'
export * from './keyboard/keyboard'
export * from './network/downloadFile'
import { TEMP_PATH } from '../constants'
import { hasOwn } from '@vue/shared'
import {
defineTaskApi,
API_DOWNLOAD_FILE,
API_TYPE_DOWNLOAD_FILE,
DownloadFileProtocol,
DownloadFileOptions,
} from '@dcloudio/uni-api'
type Downloader = ReturnType<typeof plus.downloader.createDownload>
class DownloadTask implements UniApp.DownloadTask {
private _downloader: Downloader
private _callbacks: Function[] = []
constructor(downloader: Downloader) {
this._downloader = downloader
downloader.addEventListener('statechanged', (download, status) => {
if (download.downloadedSize && download.totalSize) {
this._callbacks.forEach((callback) => {
callback({
progress: Math.round(
(download.downloadedSize! / download.totalSize!) * 100
),
totalBytesWritten: download.downloadedSize,
totalBytesExpectedToWrite: download.totalSize,
})
})
}
})
}
abort() {
this._downloader.abort()
}
onProgressUpdate(callback: (result: any) => void) {
if (typeof callback !== 'function') {
return
}
this._callbacks.push(callback)
}
offProgressUpdate(callback: (result: any) => void) {
const index = this._callbacks.indexOf(callback)
if (index >= 0) {
this._callbacks.splice(index, 1)
}
}
onHeadersReceived(callback: (result: any) => void): void {
throw new Error('Method not implemented.')
}
offHeadersReceived(callback: (result: any) => void): void {
throw new Error('Method not implemented.')
}
}
export const downloadFile = defineTaskApi<API_TYPE_DOWNLOAD_FILE>(
API_DOWNLOAD_FILE,
({ url, header, timeout }, { resolve, reject }) => {
timeout =
(timeout ||
(__uniConfig.networkTimeout && __uniConfig.networkTimeout.request) ||
60 * 1000) / 1000
const downloader = plus.downloader.createDownload(
url,
{
timeout,
filename: TEMP_PATH + '/download/',
// 需要与其它平台上的表现保持一致,不走重试的逻辑。
retry: 0,
retryInterval: 0,
},
(download, statusCode) => {
if (statusCode) {
resolve({
tempFilePath: download.filename!,
statusCode,
})
} else {
reject(`statusCode: ${statusCode}`)
}
}
)
const downloadTask = new DownloadTask(downloader)
for (const name in header) {
if (hasOwn(header, name)) {
downloader.setRequestHeader(name, header[name])
}
}
downloader.start()
return downloadTask
},
DownloadFileProtocol,
DownloadFileOptions
)
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册