HarmonyOS 修改变量值

下列方法中data的值始终为{},该怎么修改:

getUserInfo(): string {
  let data = '{}'
  PreferenceUtil.readBoolean(PreferenceKey.Key_Login, false).then((isLogin: boolean) => {
    if (isLogin) {
      PreferenceUtil.readModel<UserInfo>(PreferenceKey.Key_User_Info).then((userInfo) => {
        if (userInfo != null) {
          data = JSON.stringify(userInfo)
        }
      })
    } else {
      data = '{}'
    }
  })
  return data
}

PreferenceUtil代码如下:

import dataPreferences from '@ohos.data.preferences'
import common from '@ohos.app.ability.common'
import { Constant } from '../constant/Constant'
import { Logger, StrUtil } from '@yunkss/eftool'

let preference: dataPreferences.Preferences
const context = getContext(this) as common.UIAbilityContext
let TAG: string = 'PreferenceUtil'

/**
 * 持久化存储数据
 * 使用方式1:PreferenceUtil.readModel<UserLoginDTO>(PreferenceKey.USER_INFO).then((userInfo: UserLoginDTO) => {
 Logger.d('userId:'+userInfo.userId)
 })
 *使用方式2:let userInfo = await PreferenceUtil.readModel<UserLoginDTO>(PreferenceKey.USER_INFO)
 *         Logger.d('userId:'+userInfo.userId)
 */
export class PreferenceUtil {
  static async writeString(key: string, value?: string) {
    if (preference == null) {
      await PreferenceUtil.getPreferences()
    }
    try {
      await preference.put(key, value)
    } catch (e) {
      Logger.error(TAG, 'Failed to write value, cause:' + e)
    }
    await preference.flush()
  }

  static async readString(key: string, defaultValue?: string) {
    if (preference == null) {
      await PreferenceUtil.getPreferences()
    }
    let value: string = ''
    try {
      value = await preference.get(key, defaultValue).then()
    } catch (e) {
      Logger.error(TAG, 'Failed to read value, cause:' + e)
    }
    return value
  }

  static async writeNumber(key: string, value?: number) {
    if (preference == null) {
      await PreferenceUtil.getPreferences()
    }
    try {
      await preference.put(key, value)
    } catch (e) {
      Logger.error(TAG, 'Failed to write value, cause:' + e)
    }
    await preference.flush()
  }

  static async readNumber(key: string, defaultValue?: number) {
    if (preference == null) {
      await PreferenceUtil.getPreferences()
    }
    let value: number = 0
    try {
      value = await preference.get(key, defaultValue).then()
    } catch (e) {
      Logger.error(TAG, 'Failed to read value, cause:' + e)
    }
    return value
  }

  static async writeBoolean(key: string, value?: boolean) {
    if (preference == null) {
      await PreferenceUtil.getPreferences()
    }
    try {
      await preference.put(key, value)
    } catch (e) {
      Logger.error(TAG, 'Failed to write value, cause:' + e)
    }
    await preference.flush()
  }

  static async readBoolean(key: string, defaultValue?: boolean) {
    if (preference == null) {
      await PreferenceUtil.getPreferences()
    }
    let value: boolean = false
    try {
      value = await preference.get(key, defaultValue).then()
    } catch (e) {
      Logger.error(TAG, 'Failed to read value, cause:' + e)
    }
    return value
  }

  static async writeModel<T>(key: string, value?: T) {
    if (preference == null) {
      await PreferenceUtil.getPreferences()
    }
    try {
      await preference.put(key, JSON.stringify(value))
    } catch (e) {
      Logger.error(TAG, 'Failed to write value, cause:' + e)
    }
    await preference.flush()
  }

  static async readModel<T>(key: string) {
    if (preference == null) {
      await PreferenceUtil.getPreferences()
    }
    let value: string = ''
    try {
      value = await preference.get(key, '').then()
    } catch (e) {
      Logger.error(TAG, 'Failed to read value, cause:' + e)
    }
    if (StrUtil.isEmpty(value)) {
      return null
    } else {
      return JSON.parse(value) as T
    }
  }

  static async delete(key: string) {
    if (preference == null) {
      await PreferenceUtil.getPreferences()
    }
    try {
      await preference.delete(key).then()
    } catch (e) {
      Logger.error(TAG, 'Failed to delete, cause:' + e)
    }
  }

  static async clear() {
    if (preference == null) {
      await PreferenceUtil.getPreferences()
    }
    try {
      await preference.clear().then()
    } catch (e) {
      Logger.error(TAG, 'Failed to clear, cause:' + e)
    }
  }

  static async flush() {
    if (preference == null) {
      await PreferenceUtil.getPreferences()
    }
    try {
      await preference.flush().then()
    } catch (e) {
      Logger.error(TAG, 'Failed to flush, cause:' + e)
    }
  }

  private static async getPreferences() {
    try {
      preference = await dataPreferences.getPreferences(context, Constant.Preference_Name)
    } catch (e) {
      Logger.error(TAG, 'Failed to get preferences, cause:' + e)
    }
  }

  /* private static async deletePreferences() {
     try {
       await dataPreferences.deletePreferences(context, Constant.Preference_Name)
     } catch (e) {
     }
     preference=null
   }*/
}
HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

里外都是异步的return的位置需要调整一下:

getUserInfo(): string {
  let data = '{}'
  PreferenceUtil.readBoolean(PreferenceKey.Key_Login, false).then((isLogin: boolean) => {
    if (isLogin) {
      PreferenceUtil.readModel<UserInfo>(PreferenceKey.Key_User_Info).then((userInfo) => {
        if (userInfo != null) {
          data = JSON.stringify(userInfo)
        }
      })
    } else {
      data = '{}'
    }
    return data
  })
}
分享
微博
QQ
微信
回复
3天前
相关问题
@State 修饰的变量值改变,界面不刷新
1606浏览 • 1回复 待解决
如何监听Viewmodel里变量值的变化
1988浏览 • 1回复 待解决
如何动态查看代码运行时变量值
702浏览 • 1回复 待解决
HarmonyOS 图片颜色改变API
23浏览 • 1回复 待解决
HarmonyOS 如何改变光标位置
383浏览 • 1回复 待解决
HarmonyOS如何动态改变图标
553浏览 • 1回复 待解决