添加本地存储和log

This commit is contained in:
DevWiki 2024-04-18 11:41:12 +08:00
parent a3b73bab26
commit b543224480
3 changed files with 102 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import { router, window } from '@kit.ArkUI';
import { WebPageDialog } from './web/WebPageDialog' import { WebPageDialog } from './web/WebPageDialog'
import promptAction from '@ohos.promptAction'; import promptAction from '@ohos.promptAction';
import { BusinessError } from '@ohos.base'; import { BusinessError } from '@ohos.base';
import { Log } from '../utils/Log';
@CustomDialog @CustomDialog
struct WebViewDialog { struct WebViewDialog {
@ -75,17 +76,17 @@ struct Index {
}).then((result: promptAction.ShowDialogSuccessResponse) => { }).then((result: promptAction.ShowDialogSuccessResponse) => {
if (result.index === 0) { if (result.index === 0) {
// 用户点击了“取消”按钮 // 用户点击了“取消”按钮
console.info('User canceled the operation.'); Log.i('User canceled the operation.');
} else if (result.index === 1) { } else if (result.index === 1) {
// 用户点击了“确认”按钮 // 用户点击了“确认”按钮
console.info('User confirmed the operation.'); Log.i('User confirmed the operation.');
// 调用router.back()方法,返回上一个页面 // 调用router.back()方法,返回上一个页面
router.back(); router.back();
} }
}).catch((err: Error) => { }).catch((err: Error) => {
let message = (err as BusinessError).message let message = (err as BusinessError).message
let code = (err as BusinessError).code let code = (err as BusinessError).code
console.error(`Invoke showDialog failed, code is ${code}, message is ${message}`); Log.i(`Invoke showDialog failed, code is ${code}, message is ${message}`);
}) })
return true; return true;
} }

View File

@ -0,0 +1,65 @@
import { Context } from '@kit.AbilityKit';
import dataPreferences from '@ohos.data.preferences';
import { Log } from './Log';
export class LocalStorageKey {
static readonly KEY_SERVER_CONFIG = "key_config";
}
export class LocalStorage {
private static instance: LocalStorage;
private static lock: boolean = false;
private static readonly XY_DP_Name = "LocalData";
static getInstance(): LocalStorage {
if (!LocalStorage.instance) {
if (!LocalStorage.lock) {
LocalStorage.lock = true;
LocalStorage.instance = new LocalStorage();
LocalStorage.lock = false;
}
}
return LocalStorage.instance;
}
private context: Context | null = null;
private preferences: dataPreferences.Preferences | null = null;
private constructor() {
}
public init(context: Context): void {
if (!this.context) {
this.context = context.getApplicationContext();
let options: dataPreferences.Options = { name: LocalStorage.XY_DP_Name };
this.preferences = dataPreferences.getPreferencesSync(this.context, options);
} else {
Log.i("LocalStorage is already init.")
}
}
public putData(key: string, value: dataPreferences.ValueType) {
Log.i(`put sp data, key:${key}, value:${value}`)
this.preferences?.putSync(key, value);
this.preferences?.flush();
}
public clearData(key: string) {
this.preferences?.delete(key);
this.preferences?.flush();
}
public getData(key: string, defaultValue: dataPreferences.ValueType): dataPreferences.ValueType | undefined {
let value = this.preferences?.getSync(key, defaultValue);
Log.i(`get sp data, key:${key}, value:${value}`)
return value;
}
public getObject<T>(key: string): T {
let value = this.getData(key, "{}") as string;
if (value.toString().length > 0) {
return JSON.parse(value) as T;
}
return "{}" as T;
}
}

View File

@ -0,0 +1,33 @@
import hilog from '@ohos.hilog';
let domain: number = 0xFF00;
let prefix: string = 'HM4Demo';
let format: string = `%{public}s, %{public}s`;
export class Log {
static d(...args: string[]) {
hilog.debug(domain, prefix, format, args);
}
static i(...args: string[]) {
hilog.info(domain, prefix, format, args);
}
static w(...args: string[]) {
hilog.warn(domain, prefix, format, args);
}
static e(...args: string[]) {
hilog.error(domain, prefix, format, args);
}
static f(...args: string[]) {
hilog.fatal(domain,prefix, format, args);
}
static isLoggable(level: number) {
hilog.isLoggable(domain, prefix, level);
}
}