业务中,尝试将登录的Token信息利用PersistentStorage存储。具体方案为:在EntryAbility的onWindowStageCreate方法中查询PersistentStorage是否存在Token,如有则通过loadContent方法加载首页,否则加载登录页。登录成功后,将Token持久化存储,以实现后续持久化登录。问题是在EntryAbility中获取持久化数据失败,每次退出应用后,都需要重新登录。

方案一:在EntryAbility中loadContent传入的回调中创建PersistentStorage对象,在loadContent加载的首页中判断是否持久化存储了Token信息,如果存储了则保持在当前页面,否则跳转登录页面。示例代码如下:
import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; const DOMAIN = 0x0000; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { try { this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); } catch (err) { hilog.error(DOMAIN, 'testTag', 'Failed to set colorMode. Cause: %{public}s', JSON.stringify(err)); } hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); hilog.info(DOMAIN, 'testTag', want.bundleName); hilog.info(DOMAIN, 'testTag', launchParam.launchReasonMessage); } onDestroy(): void { hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); } onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); windowStage.loadContent('pages/Index', (err) => { // 创建PersistentStorage对象 PersistentStorage.persistProp('token', ''); if (err.code) { hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); return; } hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); }); } onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); } onForeground(): void { // Ability has brought to foreground hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); } onBackground(): void { // Ability has back to background hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); } };
@Entry @Component struct Index { @State message: string = '这是首页'; aboutToAppear(): void { // 获取token信息,判断如果未登录,则跳转登录页 const token = AppStorage.get('token') as string; if (token == '') { this.getUIContext().getRouter().pushUrl({ url: 'pages/Login' }).catch(() => { console.error('跳转失败'); }); } } build() { RelativeContainer() { Text(this.message) .id('HelloWorld') .fontSize($r('app.float.page_text_font_size')) .fontWeight(FontWeight.Bold) .alignRules({ center: { anchor: '__container__', align: VerticalAlign.Center }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) .onClick(() => { this.message = 'Welcome'; }); } .height('100%') .width('100%'); } }
@Entry @Component struct Login { build() { Column({ space: 10 }) { Text('这是登录页') .fontSize($r('app.float.page_text_font_size')) .fontWeight(FontWeight.Bold); Button('登录去首页') .fontSize($r('app.float.page_text_font_size')) .fontWeight(FontWeight.Bold) .onClick(() => { // 在登录页通过AppStorage保存Token信息 AppStorage.setOrCreate('token', '123456'); this.getUIContext().getRouter().pushUrl({ url: 'pages/Index' }).catch(() => { console.error('跳转失败'); }); }); } .height('100%') .width('100%'); } }
方案二:通过用户首选项实现数据持久化。在EntryAbility的onWindowStageCreate方法中查询用户首选项中持久化存储的Token信息,如有则通过loadContent方法加载首页,否则加载登录页。
import { preferences } from '@kit.ArkData'; class TokenPreferences { context: Context | undefined = undefined; tokenName: string = 'TOKEN'; // 获取实例 getStore() { let options: preferences.Options = { name: this.tokenName }; return preferences.getPreferencesSync(this.context, options); } // 增 async insert(value: string) { const dataPreferences = await this.getStore(); dataPreferences.putSync(this.tokenName, value); dataPreferences.flush(); } // 删 async delete() { const dataPreferences = await this.getStore(); dataPreferences.deleteSync(this.tokenName); dataPreferences.flush(); } // 查 async query() { const dataPreferences = await this.getStore(); const value = dataPreferences.getSync(this.tokenName, '') as string; return value; } } const tokenPreferences: TokenPreferences = new TokenPreferences(); export { tokenPreferences };
import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; import { tokenPreferences } from '../utils/TokenPreferences'; const DOMAIN = 0x0000; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { try { this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); } catch (err) { hilog.error(DOMAIN, 'testTag', 'Failed to set colorMode. Cause: %{public}s', JSON.stringify(err)); } hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); hilog.info(DOMAIN, 'testTag', want.bundleName); hilog.info(DOMAIN, 'testTag', launchParam.launchReasonMessage); } onDestroy(): void { hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); } async onWindowStageCreate(windowStage: window.WindowStage): Promise<void> { // Main window is created, set main page for this ability hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); tokenPreferences.context = this.context; const token = await tokenPreferences.query(); if (token == '') { windowStage.loadContent('pages/Login').catch(() => { hilog.error(DOMAIN, 'testTag', '跳转Login页失败'); }); } else { windowStage.loadContent('pages/Index').catch(() => { hilog.error(DOMAIN, 'testTag', '跳转Index页失败'); }); } } onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); } onForeground(): void { // Ability has brought to foreground hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); } onBackground(): void { // Ability has back to background hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); } };
import { tokenPreferences } from '../utils/TokenPreferences'; @Entry @Component struct Login { build() { Column({ space: 10 }) { Text('这是登录页') .fontSize($r('app.float.page_text_font_size')) .fontWeight(FontWeight.Bold); Button('登录去首页') .fontSize($r('app.float.page_text_font_size')) .fontWeight(FontWeight.Bold) .onClick(() => { // 持久化存储 tokenPreferences.insert('654321'); // 跳转首页 this.getUIContext().getRouter().pushUrl({ url: 'pages/Index' }).catch(() => { console.error('跳转失败'); }); }); } .height('100%') .width('100%'); } }
import { tokenPreferences } from '../utils/TokenPreferences'; // 首页 @Entry @Component struct Index { build() { Column({ space: 10 }) { Text('这是首页') .fontSize($r('app.float.page_text_font_size')) .fontWeight(FontWeight.Bold); Text('退出登录') .fontSize($r('app.float.page_text_font_size')) .fontWeight(FontWeight.Bold) .onClick(() => { // 删除首选项 tokenPreferences.delete(); this.getUIContext().getRouter().pushUrl({ url: 'pages/Login' }).catch(() => { console.error('跳转失败'); }); }); }.height('100%') .width('100%'); } }