import promptAction from '@ohos.promptAction'; import { router } from '@kit.ArkUI'; import { contextConstant } from '@kit.AbilityKit'; import { fileIo, storageStatistics } from '@kit.CoreFileKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { yTToast } from '../../utils/YTToast'; @Component export struct MineTab { @State cacheSize: string = '0.00'; aboutToAppear() { // Simulate getting cache size this.getCache() } // 获取应用数据空间大小 getCache(): string { storageStatistics.getCurrentBundleStats((error: BusinessError, bundleStats: storageStatistics.BundleStats) => { if (error) { } else { this.cacheSize = `${(bundleStats.cacheSize / 1024 / 1024).toFixed(2)}MB` } }); return this.cacheSize } clearCacheTask(dir: string): Promise { return new Promise((resolve) => { fileIo.access(dir).then((exist: boolean) => { if (exist) { fileIo.rmdir(dir) } resolve(true) }) }) } clearCache() { yTToast.alertToast({ text: '清理缓存', message: '确定清理当前应用的所有缓存数据?', confirmMessage: '确定', cancelMessage: '取消', isShowClose: false, onConfirm: async () => { this.cacheSize = '0.00'; let context: Context = getContext(this) context.getApplicationContext().area = contextConstant.AreaMode.EL1; context.area = contextConstant.AreaMode.EL1; const el1AppCacheDir = context.getApplicationContext().cacheDir const el1HapCacheDir = context.cacheDir context.getApplicationContext().area = contextConstant.AreaMode.EL2; context.area = contextConstant.AreaMode.EL2; const el2AppCacheDir = context.getApplicationContext().cacheDir const el2HapCacheDir = context.cacheDir const task = [ this.clearCacheTask(el1AppCacheDir), this.clearCacheTask(el1HapCacheDir), this.clearCacheTask(el2AppCacheDir), this.clearCacheTask(el2HapCacheDir) ] await Promise.all(task) let cache = this.getCache() // if (cache == '0.00MB') { // promptAction.showToast({ message: '清除完毕' }) // } else { // //未清理完毕,递归 // await this.clearCache() // } promptAction.showToast({ message: '清理成功' }); yTToast.hide() }, onCancel: () => { yTToast.hide() } }) } build() { Column() { // Top BG (Simulated) Stack() { Image($r('app.media.logo')) // Assuming logo exists .width(118) .height(118) .borderRadius(12) .shadow({ radius: 10, color: '#00000010' }) } .width('100%') .height('30%') .alignContent(Alignment.Center) .backgroundColor('#FBE3EB') // Simple background color Column() { Text('小易旅记').fontSize(16).fontWeight(FontWeight.Medium).margin({ top: 20, bottom: 40 }) // Menu Items this.MenuItem('用户协议', () => { router.pushUrl({ url: '/pages/UserAgreement'.slice(1) }) }) this.MenuItem('隐私协议', () => { router.pushUrl({ url: '/pages/Privacy'.slice(1) }) }) this.MenuItem('意见反馈', () => { router.pushUrl({ url: '/pages/SuggestionPage'.slice(1) }) }) this.MenuItem('数据缓存', () => { this.clearCache() }, this.cacheSize + ' MB') } .width('100%') .layoutWeight(1) .backgroundColor(Color.White) .borderRadius({ topLeft: 30, topRight: 30 }) .margin({ top: -30 }) .padding({ top: 30, left: 24, right: 24 }) } .width('100%') .height('100%') .backgroundColor('#F5F7FA') } @Builder MenuItem(title: string, onClick: () => void, rightText?: string) { Row() { Text(title).fontSize(14).fontColor('#303030') Row() { if (rightText) { Text(rightText).fontSize(14).fontColor('#070202').margin({ right: 8 }) } Image($r('app.media.right_arrow_icon')) .width(24).height(24) } } .width('100%') .height(48) .onClick(onClick) .border({ width: { bottom: 1 }, color: '#F2F2F7' }) .margin({ bottom: 10 }) .justifyContent(FlexAlign.SpaceBetween) } }