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

This commit is contained in:
wangzhongjie 2025-04-09 14:50:19 +08:00
commit 059471385f
2 changed files with 29 additions and 40 deletions

View File

@ -5,24 +5,20 @@ import { GetSyncData } from '../utils/table/Operation';
const cBg = $rawfile('judge/km3/road/luxian_pre.png'); const cBg = $rawfile('judge/km3/road/luxian_pre.png');
const lBg = $rawfile('judge/km3/road/luxian_nor.png'); const lBg = $rawfile('judge/km3/road/luxian_nor.png');
const ctBg = $rawfile('judge/km3/road/luxian_pre.png');
const ltBg = $rawfile('judge/km3/road/luxian_nor.png');
//单机模式才选线路 //单机模式才选线路
@Entry @Entry
@Component @Component
struct Index { struct Index {
@State outFlag: boolean = false; @State outFlag: boolean = false;
@State roadObj: ESObject = {}; @State roadObj: number[] = [];
async aboutToAppear() { async aboutToAppear() {
//读取systemparam表的no1等于4的 //读取systemparam表的no1等于4的
const systemParms: Array<MASYSTEMPARMType> = await GetSyncData('MA_SYSTEMPARM') as MASYSTEMPARMType[]; const systemParms: Array<MASYSTEMPARMType> = await GetSyncData('MA_SYSTEMPARM') as MASYSTEMPARMType[];
systemParms.forEach((systemParm: MASYSTEMPARMType) => { systemParms.forEach((systemParm: MASYSTEMPARMType) => {
//TODO 字段名称待修改
// const { no1, no2, no3, txt1, txt2 } = systemParm;
if (systemParm.no1 == 4) { if (systemParm.no1 == 4) {
this.roadObj[systemParm.no2] = systemParm.no2 this.roadObj.push(systemParm.no2)
} }
}) })
} }
@ -51,14 +47,11 @@ struct Index {
.height(220) .height(220)
.margin({ left: 5, bottom: 10 }) .margin({ left: 5, bottom: 10 })
.onClick(async () => { .onClick(async () => {
const roadArr = Reflect.ownKeys(this.roadObj).map((roadKey) => { const wayno = this.roadObj[Math.floor(Math.random() * this.roadObj.length)];
return this.roadObj[roadKey]
});
const wayno = roadArr[Math.floor(Math.random() * roadArr.length)];
this.goJudge(wayno) this.goJudge(wayno)
}) })
ForEach(Reflect.ownKeys(this.roadObj), (roadIndex) => { ForEach(this.roadObj, (item: number) => {
ListItem() { ListItem() {
Column() { Column() {
Row() { Row() {
@ -68,7 +61,7 @@ struct Index {
.width(90) .width(90)
.height(80) .height(80)
Text(`线路${this.roadObj[roadIndex] + ''}`) { Text(`线路${item}`) {
}.fontColor('#FFF2D9').fontSize(24).padding({ top: 10 }) }.fontColor('#FFF2D9').fontSize(24).padding({ top: 10 })
} }
} }
@ -78,7 +71,7 @@ struct Index {
.height(220) .height(220)
.margin({ left: 5, bottom: 10 }) .margin({ left: 5, bottom: 10 })
.onClick(async () => { .onClick(async () => {
this.goJudge(this.roadObj[roadIndex]) this.goJudge(item)
}) })
}) })
}.lanes(8).margin({ top: 50, left: 15 }) }.lanes(8).margin({ top: 50, left: 15 })
@ -89,7 +82,7 @@ struct Index {
} }
goJudge(wayno: string) { goJudge(wayno: number) {
let currentParams: RouteParamsType = router.getParams() as RouteParamsType; let currentParams: RouteParamsType = router.getParams() as RouteParamsType;
router.replaceUrl({ router.replaceUrl({
url: 'pages/Judge', url: 'pages/Judge',

View File

@ -54,9 +54,9 @@ export class FileHelper {
} }
} }
async getUserAlbumItemByDisplayName(displayName: string): Promise<photoAccessHelper.Album | null> { async getUserAlbumItemByDisplayName(displayName: string): Promise<photoAccessHelper.Album | undefined> {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | null = null; let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | undefined = undefined
let album: photoAccessHelper.Album | null = null let album: photoAccessHelper.Album | undefined = undefined
try { try {
console.log(FileHelpTag, 'getUserAlbumItemByDisplayName'); console.log(FileHelpTag, 'getUserAlbumItemByDisplayName');
let predicates = new dataSharePredicates.DataSharePredicates(); let predicates = new dataSharePredicates.DataSharePredicates();
@ -75,7 +75,7 @@ export class FileHelper {
} catch (err) { } catch (err) {
console.error(FileHelpTag, 'get Album fetchResult failed with err: ' + err); console.error(FileHelpTag, 'get Album fetchResult failed with err: ' + err);
} finally { } finally {
if (fetchResult != null) { if (fetchResult !== undefined) {
fetchResult.close(); fetchResult.close();
} }
} }
@ -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 = null; let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> | undefined = undefined;
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,23 +118,20 @@ export class FileHelper {
fetchColumns: this.FILE_ASSET_FETCH_COLUMNS, fetchColumns: this.FILE_ASSET_FETCH_COLUMNS,
predicates: predicates predicates: predicates
}; };
photoFetchResult = (await album?.getAssets(fetchOptions)) || null photoFetchResult = await album?.getAssets(fetchOptions)
let all_fileAsset = (await photoFetchResult?.getAllObjects()) || null; let all_fileAsset = await photoFetchResult?.getAllObjects();
let uri_array: string[] = [] let uri_array: string[] = []
console.log(FileHelpTag, ' deletePictureOfAlbum iterator begin', album_Name); console.log(FileHelpTag, ' deletePictureOfAlbum iterator begin', album_Name);
if (all_fileAsset) { for (let onfile of all_fileAsset!) {
for (let onfile of all_fileAsset) { console.log(FileHelpTag, album_Name,
console.log(FileHelpTag, album_Name, ' deletePictureOfAlbum uri : ' + onfile.uri + ', photoType : ' + onfile.photoType +
' deletePictureOfAlbum uri : ' + onfile.uri + ', photoType : ' + onfile.photoType + ', displayName : ' + onfile.displayName);
', displayName : ' + onfile.displayName); uri_array.push(onfile.uri);
uri_array.push(onfile.uri);
}
} }
await this.userFileMgr.deleteAssets(uri_array); await this.userFileMgr.deleteAssets(uri_array);
let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | null = null; let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | undefined = undefined;
try { try {
albumFetchResult = albumFetchResult =
await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.TRASH); await this.userFileMgr.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.TRASH);
@ -165,7 +162,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 = null let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> | undefined = undefined;
try { try {
let predicates = new dataSharePredicates.DataSharePredicates(); let predicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = { let fetchOptions: photoAccessHelper.FetchOptions = {
@ -219,9 +216,9 @@ export class FileHelper {
// return album; // return album;
// } // }
async getUserAlbumItemByName(albumName: string): Promise<photoAccessHelper.Album | null> { async getUserAlbumItemByName(albumName: string): Promise<photoAccessHelper.Album | undefined> {
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | null = null let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | undefined = undefined
let album: photoAccessHelper.Album | null = null let album: photoAccessHelper.Album | undefined = undefined
try { try {
console.log(FileHelpTag, 'getUserAlbumItemByName'); console.log(FileHelpTag, 'getUserAlbumItemByName');
let predicates = new dataSharePredicates.DataSharePredicates(); let predicates = new dataSharePredicates.DataSharePredicates();
@ -260,7 +257,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);
return undefined; // Ensure a return value in case of an error return undefined
} }
} }
@ -280,11 +277,10 @@ export class FileHelper {
} }
} }
public async queryAlbum(type: photoAccessHelper.AlbumType, public async queryAlbum(type: photoAccessHelper.AlbumType, subType: photoAccessHelper.AlbumSubtype): Promise<string> {
subType: photoAccessHelper.AlbumSubtype): Promise<string | null> {
let firstUri = ""; let firstUri = "";
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | null = null let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | undefined = undefined;
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());
@ -331,13 +327,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 = null; let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> | undefined = undefined;
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> | undefined = undefined;
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 {