You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
4.9 KiB
157 lines
4.9 KiB
1 week ago
|
|
||
|
import 'dart:convert';
|
||
|
|
||
|
import 'package:geolocator/geolocator.dart';
|
||
|
import 'package:huixiang/data/user_entity.dart';
|
||
|
import 'package:huixiang/data/user_info.dart';
|
||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||
|
|
||
|
class SharedInstance {
|
||
|
|
||
|
factory SharedInstance() => SharedInstance._internal();
|
||
|
// instance的getter方法,singletonManager.instance获取对象
|
||
|
static SharedInstance get instance => _getInstance();
|
||
|
|
||
|
// 静态变量_instance,存储唯一对象
|
||
|
static SharedInstance? _instance;
|
||
|
|
||
|
// 获取对象
|
||
|
static SharedInstance _getInstance() {
|
||
|
_instance ??= SharedInstance._internal();
|
||
|
return _instance!;
|
||
|
}
|
||
|
|
||
|
SharedInstance._internal();
|
||
|
|
||
|
SharedPreferences? _storage;
|
||
|
|
||
|
Future<void> init(SharedPreferences sharedPreferences) async {
|
||
|
_storage = sharedPreferences;
|
||
|
}
|
||
|
|
||
|
void saveUser(UserInfo user) {
|
||
|
_storage?.setString('user', jsonEncode(user));
|
||
|
}
|
||
|
|
||
|
void saveUserInfo(UserEntity userEntity) {
|
||
|
_storage?.setString('userJson', jsonEncode(userEntity.toJson()));
|
||
|
_storage?.setString('token', "${userEntity.token}");
|
||
|
_storage?.setString('userId', "${userEntity.userId}");
|
||
|
_storage?.setString('nick', "${userEntity.name}");
|
||
|
_storage?.setString('mobile', "${userEntity.mobile}");
|
||
|
}
|
||
|
|
||
|
void showPrivacyPolicy() {
|
||
|
_storage?.setBool('isShowPrivacyPolicy', true);
|
||
|
}
|
||
|
|
||
|
void savePushData({String? pushData}) {
|
||
|
_storage?.setString('pushData', pushData ?? "");
|
||
|
}
|
||
|
|
||
|
void saveLatLng(Position? latLng) {
|
||
|
_storage?.setString("latitude", "${latLng?.latitude}");
|
||
|
_storage?.setString("longitude", "${latLng?.longitude}");
|
||
|
}
|
||
|
|
||
|
void setProvince(province) {
|
||
|
_storage?.setString('province', province);
|
||
|
}
|
||
|
|
||
|
void setCity(city) {
|
||
|
_storage?.setString('city', city);
|
||
|
}
|
||
|
|
||
|
void setFirst() {
|
||
|
_storage?.setBool('isFirst', false);
|
||
|
}
|
||
|
|
||
|
void setDistrict(district) {
|
||
|
_storage?.setString('district', district);
|
||
|
}
|
||
|
|
||
|
void saveMini(minToken, tenant, storeId) {
|
||
|
_storage?.setString('minToken', minToken);
|
||
|
_storage?.setString('tenant', tenant);
|
||
|
_storage?.setString('storeId', storeId);
|
||
|
}
|
||
|
|
||
|
void saveSearchHistory(List<String> history) {
|
||
|
_storage?.setStringList('historySearch', history);
|
||
|
}
|
||
|
|
||
|
void saveMiniAppId(List<String> miniAppids) {
|
||
|
_storage?.setStringList('miniAppids', miniAppids);
|
||
|
}
|
||
|
|
||
|
void saveAppLastVersion(String appLastVersion) {
|
||
|
_storage?.setString('appLastVersion', appLastVersion);
|
||
|
}
|
||
|
|
||
|
String? getSize(String imageLocalPath) {
|
||
|
return _storage?.getString(imageLocalPath);
|
||
|
}
|
||
|
|
||
|
void setSize(String imageLocalPath, String sizeMap) {
|
||
|
_storage?.setString(imageLocalPath, sizeMap);
|
||
|
}
|
||
|
|
||
|
void saveToday(int today) {
|
||
|
_storage?.setInt('today', today);
|
||
|
}
|
||
|
|
||
|
void saveActivityPosCode(String activityPosCode) {
|
||
|
_storage?.setString('ActivityPosCode', activityPosCode);
|
||
|
}
|
||
|
|
||
|
void saveLanguage(String longitude) {
|
||
|
_storage?.setString('longitude', longitude);
|
||
|
}
|
||
|
|
||
|
void saveMiniMember(String minMember) {
|
||
|
_storage?.setString('minMember', minMember);
|
||
|
}
|
||
|
|
||
|
void logout() {
|
||
|
_storage?.setString("token", "");
|
||
|
_storage?.setString("user", "");
|
||
|
_storage?.setString("userJson", "");
|
||
|
_storage?.setString("userId", "");
|
||
|
_storage?.setString("mobile", "");
|
||
|
_storage?.setString("nick", "");
|
||
|
_storage?.setString("account", "");
|
||
|
_storage?.setString("password", "");
|
||
|
}
|
||
|
|
||
|
String? get appLastVersion => _storage?.getString('appLastVersion');
|
||
|
int? get today => _storage?.getInt('today');
|
||
|
String get ActivityPosCode => _storage?.getString('ActivityPosCode') ?? "";
|
||
|
String get latitude => _storage?.getString('latitude') ?? "";
|
||
|
List<String> get searchHistory => _storage?.getStringList('historySearch') ?? [];
|
||
|
List<String> get miniAppids => _storage?.getStringList('miniAppids') ?? [];
|
||
|
String get minMember => _storage?.getString('minMember') ?? "";
|
||
|
String get mobile => _storage?.getString('mobile') ?? "";
|
||
|
String get province => _storage?.getString('province') ?? "";
|
||
|
String get city => _storage?.getString('city') ?? "";
|
||
|
String get district => _storage?.getString('district') ?? "";
|
||
|
String get miniToken => _storage?.getString('miniToken') ?? "";
|
||
|
String get storeId => _storage?.getString('storeId') ?? "";
|
||
|
String get tenant => _storage?.getString('tenant') ?? "";
|
||
|
String get userJson => _storage?.getString('user') ?? "";
|
||
|
String get longitude => _storage?.getString('longitude') ?? "";
|
||
|
String get userId => _storage?.getString('userId') ?? "";
|
||
|
String get pushData => _storage?.getString('pushData') ?? "";
|
||
|
String get token => _storage?.getString('token') ?? "";
|
||
|
String get language => _storage?.getString('language') ?? "";
|
||
|
bool get isShowPrivacyPolicy => _storage?.getBool('isShowPrivacyPolicy') ?? false;
|
||
|
bool get isFirst => _storage?.getBool('isFirst') ?? true;
|
||
|
|
||
|
int? getShopColor(String shopLogo) {
|
||
|
return _storage?.getInt(shopLogo);
|
||
|
}
|
||
|
|
||
|
void saveShopColor(String shopLogo, int value) {
|
||
|
_storage?.setInt(shopLogo, value);
|
||
|
}
|
||
|
|
||
|
}
|