1.networkmanage 文件夹下的代码进行了格式化

2.将IDataFetch中的loadData接口的 onComplete onError 声明了对象类型

Signed-off-by: zhoulisheng1 <zhoulisheng1@huawei.com>
This commit is contained in:
zhoulisheng1 2023-01-05 00:33:17 -08:00
parent 619103145c
commit 921a61132c
5 changed files with 45 additions and 42 deletions

View File

@ -13,10 +13,10 @@
* limitations under the License.
*/
import { IDataFetch } from "../networkmanage/IDataFetch"
import { RequestOption } from "../RequestOption"
import { Md5 } from "../../cache/Md5"
import { FileUtils } from "../../cache/FileUtils"
import { IDataFetch } from '../networkmanage/IDataFetch'
import { RequestOption } from '../RequestOption'
import { Md5 } from '../../cache/Md5'
import { FileUtils } from '../../cache/FileUtils'
import { NetworkDownloadClient } from './NetworkDownloadClient'
import { LoadLocalFileClient } from './LoadLocalFileClient'
import loadRequest from '@ohos.request';
@ -26,16 +26,15 @@ export class DownloadClient implements IDataFetch {
private networkClient = new NetworkDownloadClient();
private localFileClient = new LoadLocalFileClient();
loadData(request: RequestOption, onCompleteFunction, onErrorFunction) {
if (typeof request.loadSrc == 'string') {
if (request.loadSrc.startsWith(globalThis.ImageKnife.getImageKnifeContext()
.filesDir) || request.loadSrc.startsWith(globalThis.ImageKnife.getImageKnifeContext().cacheDir)) {
// 本地沙盒
this.localFileClient.loadLocalFileData(request,onCompleteFunction,onErrorFunction)
this.localFileClient.loadData(request, onCompleteFunction, onErrorFunction)
} else {
// 网络下载
this.networkClient.downloadNetworkData(request,onCompleteFunction,onErrorFunction)
this.networkClient.loadData(request, onCompleteFunction, onErrorFunction)
}
}
}

View File

@ -13,9 +13,9 @@
* limitations under the License.
*/
import {RequestOption} from "../RequestOption"
import { RequestOption } from '../RequestOption'
// 资源加载接口
export interface IDataFetch {
loadData(request: RequestOption, onCompleteFunction, onErrorFunction);
loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void);
}

View File

@ -13,20 +13,20 @@
* limitations under the License.
*/
import { IDataFetch } from "../networkmanage/IDataFetch"
import { RequestOption } from "../RequestOption"
import { Md5 } from "../../cache/Md5"
import { FileUtils } from "../../cache/FileUtils"
import { IDataFetch } from '../networkmanage/IDataFetch'
import { RequestOption } from '../RequestOption'
import { Md5 } from '../../cache/Md5'
import { FileUtils } from '../../cache/FileUtils'
import loadRequest from '@ohos.request';
export class LoadLocalFileClient {
loadLocalFileData(request: RequestOption, onCompleteFunction, onErrorFunction) {
loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) {
if (typeof request.loadSrc == 'string') {
let fileBuffer = FileUtils.getInstance().readFilePic(request.loadSrc)
if (fileBuffer == null || fileBuffer.byteLength <= 0) {
onErrorFunction('LoadLocalFileClient loadLocalFileData The File Does Not Exist!Check The File!')
onError('LoadLocalFileClient loadLocalFileData The File Does Not Exist!Check The File!')
} else {
onCompleteFunction(fileBuffer);
onComplete(fileBuffer);
}
}
}

View File

@ -13,15 +13,15 @@
* limitations under the License.
*/
import { IDataFetch } from "../networkmanage/IDataFetch"
import { RequestOption } from "../RequestOption"
import { Md5 } from "../../cache/Md5"
import { FileUtils } from "../../cache/FileUtils"
import { IDataFetch } from '../networkmanage/IDataFetch'
import { RequestOption } from '../RequestOption'
import { Md5 } from '../../cache/Md5'
import { FileUtils } from '../../cache/FileUtils'
import loadRequest from '@ohos.request';
// 数据加载器
export class NetworkDownloadClient {
downloadNetworkData(request: RequestOption, onCompleteFunction, onErrorFunction) {
loadData(request: RequestOption, onComplete: (img: ArrayBuffer) => void, onError: (err: string) => void) {
let filename = Md5.hashStr(request.generateDataKey);
let downloadFolder = request.getFilesPath() + "/" + request.networkCacheFolder;
let allpath = request.getFilesPath() + "/" + request.networkCacheFolder + "/" + filename + ".img";
@ -55,7 +55,7 @@ export class NetworkDownloadClient {
let downloadPath = allpath;
request.downloadFilePath = downloadPath;
let arraybuffer = FileUtils.getInstance().readFilePic(downloadPath)
onCompleteFunction(arraybuffer);
onComplete(arraybuffer);
FileUtils.getInstance().deleteFile(downloadPath);
loadTask.off('complete', () => {
@ -83,7 +83,7 @@ export class NetworkDownloadClient {
})
loadTask.on('fail', (err) => {
onErrorFunction('NetworkDownloadClient Download task fail err =' + err)
onError('NetworkDownloadClient Download task fail err =' + err)
if (loadTask) {
loadTask.remove().then(result => {
loadTask.off('complete', () => {
@ -109,10 +109,11 @@ export class NetworkDownloadClient {
})
} else {
onErrorFunction('NetworkDownloadClient downloadTask dismiss!')
onError('NetworkDownloadClient downloadTask dismiss!')
}
}).catch((err) => {
onErrorFunction(err)
})
.catch((err) => {
onError("下载子系统download错误捕获,error="+err.message);
})
}
}

View File

@ -190,7 +190,10 @@ export class RequestManager {
let success = (arraybuffer) => {
this.downloadSuccess(arraybuffer, onComplete, onError)
}
this.mIDataFetch.loadData(request, success, onError);
let error = (errorMsg:string) =>{
onError(errorMsg)
}
this.mIDataFetch.loadData(request, success, error);
}
// 加载本地资源