问题:cc中的localstorage仅能保存字符,用typescript开发时不便于各场景的应用
扩展内容如下:
1.支付默认值
2.增加string bool number强类型
3.当set时,写入localstorage,当get时,取缓存里里的数据
export class LocalStorageValue {
//声音音量
private static _soundVolume: number = null;
public static get SoundVolume() {
if (this._soundVolume == null)
this._soundVolume = LocalStorage.GetNumber('SoundVolume', 1)
return this._soundVolume;
}
public static set SoundVolume(value) {
this._soundVolume = value
LocalStorage.SetNumber('SoundVolume', this._soundVolume)
}
//音乐音量
private static _musicVolume: number = null;
public static get MusicVolume() {
if (this._musicVolume == null)
this._musicVolume = LocalStorage.GetNumber('MusicVolume', 1)
return this._musicVolume;
}
public static set MusicVolume(value) {
this._musicVolume = value
LocalStorage.SetNumber('MusicVolume', this._musicVolume)
}
//手机登录账号
private static _phoneLoginAccount: string = null;
public static get PhoneLoginAccount() {
if (this._phoneLoginAccount == null)
this._phoneLoginAccount = LocalStorage.GetString('PhoneLoginAccount', '')
return this._phoneLoginAccount;
}
public static set PhoneLoginAccount(value) {
this._phoneLoginAccount = value
LocalStorage.SetString('PhoneLoginAccount', this._phoneLoginAccount)
}
//手机登录密码
private static _phoneLoginPassword: string = null;
public static get PhoneLoginPassword() {
if (this._phoneLoginPassword == null)
this._phoneLoginPassword = LocalStorage.GetString('PhoneLoginPassword', '')
return this._phoneLoginPassword;
}
public static set PhoneLoginPassword(value) {
this._phoneLoginPassword = value
LocalStorage.SetString('PhoneLoginPassword', this._phoneLoginPassword)
}
//游客登录账号
private static _guestLoginAccount: string = null;
public static get GuestLoginAccount() {
if (this._guestLoginAccount == null)
this._guestLoginAccount = LocalStorage.GetString('GuestLoginAccount', 'guest' + RandomNumber(1, 10000))
return this._guestLoginAccount;
}
public static set GuestLoginAccount(value) {
this._guestLoginAccount = value
LocalStorage.SetString('GuestLoginAccount', this._guestLoginAccount)
}
//是否自动登录
private static _IsAutoLogin: boolean = null;
public static get IsAutoLogin() {
if (this._IsAutoLogin == null)
this._IsAutoLogin = LocalStorage.GetBool('IsAutoLogin', false)
return this._IsAutoLogin;
}
public static set IsAutoLogin(value) {
this._IsAutoLogin = value
LocalStorage.SetBool('IsAutoLogin', this._IsAutoLogin)
}
}
class LocalStorage {
public static SetString(_key: string, _value: string) {
cc.sys.localStorage.setItem(_key, _value)
}
public static GetString(_key: string, _default?: string) {
var str = cc.sys.localStorage.getItem(_key)
if (str != null) {
return str;
}
if (str == null && _default != null) {
return _default;
}
return null;
}
public static SetBool(_key: string, _bool: boolean) {
if (_bool) {
cc.sys.localStorage.setItem(_key, 'true')
}
else {
cc.sys.localStorage.setItem(_key, 'false')
}
}
public static GetBool(_key: string, _default?: boolean): boolean {
var str = cc.sys.localStorage.getItem(_key)
if (str == 'true') {
return true;
}
if (str == 'false') {
return false;
}
if (IsNullOrEmpty(str) && _default != null) {
return _default;
}
if (IsNullOrEmpty(str) && _default == null) {
Log.Orange(Log.Key.System, 'LocalStorage get bool is null ,defult is null, return false!')
return false;
}
}
public static SetNumber(_key: string, _value: number) {
if (_value != null) {
cc.sys.localStorage.setItem(_key, _value)
}
}
public static GetNumber(_key: string, _default?: number): number {
var _value = cc.sys.localStorage.getItem(_key)
if (_value == null && _default != null) {
Log.Orange(Log.Key.System, 'LocalStorage get Number is null or empty ,return defult false!')
return _default;
}
if (_value == null && _default == null) {
Log.Orange(Log.Key.System, 'LocalStorage get Number is null ,defult is null, return -1!')
return -1;
}
let v = Number(_value);
return isNaN(v) ? _default : v;
}
}