|
@@ -3,15 +3,19 @@ import preferences from '@ohos.data.preferences';
|
|
|
import common from '@ohos.app.ability.common';
|
|
import common from '@ohos.app.ability.common';
|
|
|
|
|
|
|
|
export class Storage {
|
|
export class Storage {
|
|
|
- private static prefs: preferences.Preferences;
|
|
|
|
|
|
|
+ // 操作对象
|
|
|
|
|
+ private static instance: preferences.Preferences;
|
|
|
|
|
|
|
|
- static async init(context: common.UIAbilityContext) {
|
|
|
|
|
- if (!Storage.prefs) {
|
|
|
|
|
- try {
|
|
|
|
|
- Storage.prefs = await preferences.getPreferences(context, 'app_prefs');
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 初始化轻量级存储工具
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param context
|
|
|
|
|
+ */
|
|
|
|
|
+ static init(context: common.UIAbilityContext) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Storage.instance = preferences.getPreferencesSync(context, { name: 'app_kv_data' })
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.log("Storage", "init() failed", error)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -19,11 +23,12 @@ export class Storage {
|
|
|
* 设置当前登录状态
|
|
* 设置当前登录状态
|
|
|
* @param value
|
|
* @param value
|
|
|
*/
|
|
*/
|
|
|
- static async setLogin(value: boolean) {
|
|
|
|
|
|
|
+ static setLogin(value: boolean) {
|
|
|
try {
|
|
try {
|
|
|
- Storage.prefs.put("is_login", value).catch()
|
|
|
|
|
- await Storage.prefs.flush()
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
|
|
+ Storage.instance.putSync("is_login", value)
|
|
|
|
|
+ Storage.instance.flushSync()
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.log("Storage", "setLogin() failed", error)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -34,40 +39,65 @@ export class Storage {
|
|
|
*/
|
|
*/
|
|
|
static isLogin(): boolean {
|
|
static isLogin(): boolean {
|
|
|
try {
|
|
try {
|
|
|
- return Storage.prefs.getSync('is_login', false) === true
|
|
|
|
|
|
|
+ return Storage.instance.getSync('is_login', false) === true
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
|
|
+ console.log("Storage", "isLogin() failed", error)
|
|
|
return false
|
|
return false
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- static async setToken(value: string) {
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 将登录的token存储到本地
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param value
|
|
|
|
|
+ */
|
|
|
|
|
+ static setToken(value: string) {
|
|
|
try {
|
|
try {
|
|
|
- Storage.prefs.put("token", value).catch()
|
|
|
|
|
- await Storage.prefs.flush()
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
|
|
+ Storage.instance.putSync("token", value)
|
|
|
|
|
+ Storage.instance.flushSync()
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.log("Storage", "setToken() failed", error)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取存储本地的Token
|
|
|
|
|
+ *
|
|
|
|
|
+ * @returns
|
|
|
|
|
+ */
|
|
|
static getToken(): string {
|
|
static getToken(): string {
|
|
|
try {
|
|
try {
|
|
|
- return Storage.prefs.getSync('token', "") as string
|
|
|
|
|
|
|
+ return Storage.instance.getSync('token', "") as string
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
|
|
+ console.log("Storage", "getToken() failed", error)
|
|
|
return ""
|
|
return ""
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- static async setUsername(value: string) {
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 将用户名存储到本地
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param value
|
|
|
|
|
+ */
|
|
|
|
|
+ static setUsername(value: string) {
|
|
|
try {
|
|
try {
|
|
|
- Storage.prefs.put("username", value).catch()
|
|
|
|
|
- await Storage.prefs.flush()
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
|
|
+ Storage.instance.putSync("username", value)
|
|
|
|
|
+ Storage.instance.flushSync()
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.log("Storage", "setUsername() failed", error)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取用户名
|
|
|
|
|
+ *
|
|
|
|
|
+ * @returns
|
|
|
|
|
+ */
|
|
|
static getUsername(): string {
|
|
static getUsername(): string {
|
|
|
try {
|
|
try {
|
|
|
- return Storage.prefs.getSync('username', "") as string
|
|
|
|
|
|
|
+ return Storage.instance.getSync('username', "") as string
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
|
|
+ console.log("Storage", "getUsername() failed", error)
|
|
|
return ""
|
|
return ""
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|