fix: 优化多个组件中的类型声明和逻辑,提升代码一致性

This commit is contained in:
wangzhongjie 2025-04-09 14:50:01 +08:00
parent 4330fcfd5f
commit 2f683c70e2
4 changed files with 89 additions and 68 deletions

View File

@ -190,12 +190,12 @@ export default struct FaceCompare {
console.log('mmmmm0', 2) console.log('mmmmm0', 2)
this.param.pztd = this.param.rlls this.param.pztd = this.param.rlls
const data = await takePhoto(this.param, this.context, 'jt/', 1,) const data = await takePhoto(this.param, this.context, 'jt/', 1,)
this.base64 = this.imageBase64 + (data.base64 || "") this.base64 = this.imageBase64 + (data?.base64 || "")
console.log('mmmmt', this.base64) console.log('mmmmt', this.base64)
faceCompare({ faceCompare({
sfzh: this.sfzh, sfzh: this.sfzh,
firstImage: this.firstImage.substr(22), firstImage: this.firstImage.substr(22),
secondImage: data.base64 || "", secondImage: data?.base64 || "",
type: "2", type: "2",
verifyType: "1" verifyType: "1"
}) })

View File

@ -1,8 +1,6 @@
import Prompt from '@system.prompt'
const TAG = 'SURENJUN_JUDGE' const TAG = 'SURENJUN_JUDGE'
interface DelayConfig{ interface DelayConfig {
isDelay: boolean isDelay: boolean
delayTime?: number delayTime?: number
} }
@ -12,47 +10,46 @@ interface QUEUE {
delayConfig?: DelayConfig delayConfig?: DelayConfig
} }
type GoTask = (currentTask:QUEUE)=>Promise<boolean> type GoTask = (currentTask: QUEUE) => Promise<boolean>
export default class JudgeTask { export default class JudgeTask {
private queue?:QUEUE[] = []
private status: string
constructor() {
this.queue = []
this.status = 'end'
}
//执行任务 //执行任务
goTask:GoTask = async (currentTask:QUEUE) => { goTask: GoTask = async (currentTask: QUEUE) => {
const fn = currentTask.fn; const fn = currentTask.fn;
const delayConfig = currentTask.delayConfig || {isDelay:false,delayTime:1000} const delayConfig = currentTask.delayConfig || {
isDelay: false, delayTime: 1000
}
const isDelay = delayConfig.isDelay const isDelay = delayConfig.isDelay
const delayTime = delayConfig.delayTime const delayTime = delayConfig.delayTime
return false return false
} }
private queue?: QUEUE[] = []
private status: string
executeQueue = async () => { executeQueue = async () => {
const queue = this.queue; const queue = this.queue;
const executeQueue = this.executeQueue const executeQueue = this.executeQueue
if (queue.length) { if (queue?.length) {
for (const currentTask of queue) { for (const currentTask of queue) {
await this.goTask(currentTask) await this.goTask(currentTask)
this.queue.shift() this.queue?.shift()
await executeQueue() await executeQueue()
} }
} else { } else {
this.status = 'end' this.status = 'end'
} }
} }
addTask = async (fn: Function, delayConfig?: DelayConfig) => {
addTask = async (fn:Function, delayConfig?: DelayConfig) => { this.queue?.push({
this.queue.push({
fn, delayConfig fn, delayConfig
}); });
if (this.status == 'end' && this.queue.length === 1) { if (this.status == 'end' && this.queue?.length === 1) {
await this.executeQueue(); await this.executeQueue();
} }
} }
constructor() {
this.queue = []
this.status = 'end'
}
} }

View File

@ -54,9 +54,9 @@ export class FileHelper {
} }
} }
async getUserAlbumItemByDisplayName(displayName: string): Promise<photoAccessHelper.Album> { async getUserAlbumItemByDisplayName(displayName: string): Promise<photoAccessHelper.Album | null> {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | null = null;
let album: photoAccessHelper.Album = null let album: photoAccessHelper.Album | null = null
try { try {
console.log(FileHelpTag, 'getUserAlbumItemByDisplayName'); console.log(FileHelpTag, 'getUserAlbumItemByDisplayName');
let predicates = new dataSharePredicates.DataSharePredicates(); let predicates = new dataSharePredicates.DataSharePredicates();
@ -98,7 +98,7 @@ export class FileHelper {
let asset = await assetsResult.getFirstObject(); let asset = await assetsResult.getFirstObject();
console.log(FileHelpTag, ' addAssetToAlbum one asset uri : ' + asset.uri + ', photoType : ' + asset.photoType + console.log(FileHelpTag, ' addAssetToAlbum one asset uri : ' + asset.uri + ', photoType : ' + asset.photoType +
', displayName : ' + asset.displayName); ', displayName : ' + asset.displayName);
await album.addAssets([asset]); await album?.addAssets([asset]);
console.log(FileHelpTag, ' addAssetToAlbum success '); console.log(FileHelpTag, ' addAssetToAlbum success ');
} }
} catch (err) { } catch (err) {
@ -109,7 +109,7 @@ export class FileHelper {
public async deleteFileOfAlbum(album_Name: string, file_type: number | string | boolean): Promise<void> { public async deleteFileOfAlbum(album_Name: string, file_type: number | string | boolean): Promise<void> {
console.log(FileHelpTag, ' deletePictureOfAlbum album_Name' + album_Name); console.log(FileHelpTag, ' deletePictureOfAlbum album_Name' + album_Name);
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = null; let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> | null = null;
try { try {
let album = await this.getUserAlbumItemByName(album_Name); let album = await this.getUserAlbumItemByName(album_Name);
let predicates = new dataSharePredicates.DataSharePredicates(); let predicates = new dataSharePredicates.DataSharePredicates();
@ -118,20 +118,23 @@ export class FileHelper {
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS, fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
predicates: predicates predicates: predicates
}; };
photoFetchResult = await album.getAssets(fetchOptions) photoFetchResult = (await album?.getAssets(fetchOptions)) || null
let all_fileAsset = await photoFetchResult.getAllObjects(); let all_fileAsset = (await photoFetchResult?.getAllObjects()) || null;
let uri_array: string[] = [] let uri_array: string[] = []
console.log(FileHelpTag, ' deletePictureOfAlbum iterator begin', album_Name); console.log(FileHelpTag, ' deletePictureOfAlbum iterator begin', album_Name);
for (let onfile of all_fileAsset) { if (all_fileAsset) {
console.log(FileHelpTag, album_Name, for (let onfile of all_fileAsset) {
' deletePictureOfAlbum uri : ' + onfile.uri + ', photoType : ' + onfile.photoType + console.log(FileHelpTag, album_Name,
', displayName : ' + onfile.displayName); ' deletePictureOfAlbum uri : ' + onfile.uri + ', photoType : ' + onfile.photoType +
uri_array.push(onfile.uri); ', displayName : ' + onfile.displayName);
uri_array.push(onfile.uri);
}
} }
await this.userFileMgr.deleteAssets(uri_array); await this.userFileMgr.deleteAssets(uri_array);
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null; let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | null = null;
try { try {
albumFetchResult = albumFetchResult =
await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.TRASH); await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.TRASH);
@ -162,7 +165,7 @@ export class FileHelper {
public async deleteAllPictures(): Promise<void> { public async deleteAllPictures(): Promise<void> {
console.log(FileHelpTag, ' deleteAllPictures'); console.log(FileHelpTag, ' deleteAllPictures');
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = null; let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> | null = null
try { try {
let predicates = new dataSharePredicates.DataSharePredicates(); let predicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = { let fetchOptions: photoAccessHelper.FetchOptions = {
@ -216,9 +219,9 @@ export class FileHelper {
// return album; // return album;
// } // }
async getUserAlbumItemByName(albumName: string): Promise<photoAccessHelper.Album> { async getUserAlbumItemByName(albumName: string): Promise<photoAccessHelper.Album | null> {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | null = null
let album: photoAccessHelper.Album = null let album: photoAccessHelper.Album | null = null
try { try {
console.log(FileHelpTag, 'getUserAlbumItemByName'); console.log(FileHelpTag, 'getUserAlbumItemByName');
let predicates = new dataSharePredicates.DataSharePredicates(); let predicates = new dataSharePredicates.DataSharePredicates();
@ -246,7 +249,7 @@ export class FileHelper {
} }
public async createAlbum(albumName: string): Promise<string> { public async createAlbum(albumName: string): Promise<string | undefined> {
try { try {
console.log(FileHelpTag, " createAlbum beging"); console.log(FileHelpTag, " createAlbum beging");
let albumAsset = await this.userFileMgr.createAlbum(albumName); let albumAsset = await this.userFileMgr.createAlbum(albumName);
@ -257,7 +260,7 @@ export class FileHelper {
return albumAsset.albumUri; return albumAsset.albumUri;
} catch (err) { } catch (err) {
console.error(FileHelpTag, ' createAlbum failed with err: ' + err); console.error(FileHelpTag, ' createAlbum failed with err: ' + err);
} finally { return undefined; // Ensure a return value in case of an error
} }
} }
@ -277,10 +280,11 @@ export class FileHelper {
} }
} }
public async queryAlbum(type: photoAccessHelper.AlbumType, subType: photoAccessHelper.AlbumSubtype): Promise<string> { public async queryAlbum(type: photoAccessHelper.AlbumType,
subType: photoAccessHelper.AlbumSubtype): Promise<string | null> {
let firstUri = ""; let firstUri = "";
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null; let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | null = null
try { try {
fetchResult = await this.userFileMgr.getAlbums(type, subType); fetchResult = await this.userFileMgr.getAlbums(type, subType);
console.log(FileHelpTag, ' queryAlbum success count: ' + fetchResult.getCount()); console.log(FileHelpTag, ' queryAlbum success count: ' + fetchResult.getCount());
@ -327,13 +331,13 @@ export class FileHelper {
public async deleteAllVideos(type: photoAccessHelper.AlbumType, public async deleteAllVideos(type: photoAccessHelper.AlbumType,
subType: photoAccessHelper.AlbumSubtype): Promise<void> { subType: photoAccessHelper.AlbumSubtype): Promise<void> {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null; let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | null = null;
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> | null = null;
try { try {
fetchResult = await this.userFileMgr.getAlbums(type, subType); fetchResult = await this.userFileMgr.getAlbums(type, subType);
console.log(FileHelpTag, ' get Album fetchResult, count: ' + fetchResult.getCount()); console.log(FileHelpTag, ' get Album fetchResult, count: ' + fetchResult.getCount());
for (let i = 0; i < fetchResult.getCount(); i++) { for (let i = 0; i < fetchResult.getCount(); i++) {
let albumAsset: photoAccessHelper.Album = await fetchResult.getObjectByPosition(i); let albumAsset: photoAccessHelper.Album = await fetchResult.getObjectByPosition(i);
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = null;
let count = 0; let count = 0;
console.log(FileHelpTag, ' get one Album name : ' + albumAsset.albumName); console.log(FileHelpTag, ' get one Album name : ' + albumAsset.albumName);
try { try {

View File

@ -78,7 +78,7 @@ export async function endRecordVideo(record_handleObj: RecordHandleType) {
} }
export async function saveStartRecordVideo(path: string, context?: common.UIAbilityContext): Promise<RecordHandleType> { export async function saveStartRecordVideo(path: string, context: common.UIAbilityContext): Promise<RecordHandleType> {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
const fileUtil = new FileUtils(context) const fileUtil = new FileUtils(context)
const fileHelper = new FileHelper(context); const fileHelper = new FileHelper(context);
@ -130,7 +130,7 @@ interface takePhotoParam {
* @returns * @returns
*/ */
export async function delPic(day: number, type: number, context?: common.UIAbilityContext) { export async function delPic(day: number, type: number, context: common.UIAbilityContext) {
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
console.log('albumGetAssetsDemoCallback'); console.log('albumGetAssetsDemoCallback');
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
@ -155,41 +155,61 @@ export async function delPic(day: number, type: number, context?: common.UIAbili
} }
export async function takePhoto(param: VideoConfig, context: common.UIAbilityContext, dir: string, flag = 1, export async function takePhoto(param: VideoConfig, context: common.UIAbilityContext, dir: string, flag = 1,
callback?: (data: TakePhotoCallbackData) => void) { callback?: (data: TakePhotoCallbackData) => void): Promise<takePhotoParam | void> {
let video_uri = `rtsp://${param.userName}:${param.pwd}@${param.ip}:${param.port}/h264/ch${param.pztd}/main/av_stream`; let video_uri = `rtsp://${param.userName}:${param.pwd}@${param.ip}:${param.port}/h264/ch${param.pztd}/main/av_stream`;
const num = Math.floor(Math.random() * 10000) const num = Math.floor(Math.random() * 10000);
const fileName = `picture_record${num}.jpg` const fileName = `picture_record${num}.jpg`;
if (flag == 0) { if (flag == 0) {
rtsp_server.detectVideoSnapshotSize(video_uri, fileName, (err: BusinessError, snapResult: record.RtspResult) => { return new Promise<void>((resolve, reject) => {
callback({ fileSize: snapResult.fileSize, errorCode: snapResult.errorCode }) rtsp_server.detectVideoSnapshotSize(video_uri, fileName, (err: BusinessError, snapResult: record.RtspResult) => {
if (snapResult && snapResult.errorCode === 0) {
if (callback) {
callback({
fileSize: snapResult.fileSize,
errorCode: snapResult.errorCode,
});
}
resolve();
} else {
promptAction.showToast({
message: `拍照失败`,
duration: 3000,
});
reject(err);
}
});
}); });
} else { } else {
return new Promise<takePhotoParam>(async (resolve, reject) => { return new Promise<takePhotoParam>(async (resolve, reject) => {
const time = GetCurrentTime() const time = GetCurrentTime();
const date = time.split(' ')[0] const date = time.split(' ')[0];
let dirName = dir ? dir : date let dirName = dir ? dir : date;
rtsp_server.getVideoSnapshot(context, video_uri, fileName, dirName, true, rtsp_server.getVideoSnapshot(
context,
video_uri,
fileName,
dirName,
true,
async (err: BusinessError, snapResult: record.RtspResult) => { async (err: BusinessError, snapResult: record.RtspResult) => {
if (snapResult.result && snapResult.errorCode == 0) { if (snapResult.result && snapResult.errorCode === 0) {
resolve({ resolve({
base64: snapResult.dataString, base64: snapResult.dataString,
name: snapResult.fileName, name: snapResult.fileName,
fileSize: snapResult.fileSize, fileSize: snapResult.fileSize,
errorCode: snapResult.errorCode errorCode: snapResult.errorCode,
}) });
} else { } else {
promptAction.showToast({ promptAction.showToast({
message: `拍照失败`, message: `拍照失败`,
duration: 3000 duration: 3000,
}); });
reject(false) reject(false);
// reject()
} }
}
}); );
}) });
} }
} }
@ -202,14 +222,14 @@ export async function deleteAllFileByPiC(dirName: string, type = 1, context: com
export async function deleteAllVideos(context: common.UIAbilityContext, type: photoAccessHelper.AlbumType, export async function deleteAllVideos(context: common.UIAbilityContext, type: photoAccessHelper.AlbumType,
subType: photoAccessHelper.AlbumSubtype): Promise<void> { subType: photoAccessHelper.AlbumSubtype): Promise<void> {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null; let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | null = null;
let count = 0; let count = 0;
try { try {
const userFileMgr = photoAccessHelper.getPhotoAccessHelper(context); const userFileMgr = photoAccessHelper.getPhotoAccessHelper(context);
fetchResult = await userFileMgr.getAlbums(type, subType); fetchResult = await userFileMgr.getAlbums(type, subType);
for (let i = 0; i < fetchResult.getCount(); i++) { for (let i = 0; i < fetchResult.getCount(); i++) {
let albumAsset: photoAccessHelper.Album = await fetchResult.getObjectByPosition(i); let albumAsset: photoAccessHelper.Album = await fetchResult.getObjectByPosition(i);
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = null; let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> | null = null;
try { try {
let predicates = new dataSharePredicates.DataSharePredicates(); let predicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = { let fetchOptions: photoAccessHelper.FetchOptions = {