111
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import 'dart:convert';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// 本地存储服务
|
||||
/// 本地存儲服務
|
||||
class LocalStorage {
|
||||
LocalStorage._();
|
||||
|
||||
@@ -15,7 +15,7 @@ class LocalStorage {
|
||||
_prefs = await SharedPreferences.getInstance();
|
||||
}
|
||||
|
||||
/// 获取实例
|
||||
/// 獲取實例
|
||||
static SharedPreferences get prefs {
|
||||
if (_prefs == null) {
|
||||
throw Exception('LocalStorage not initialized. Call init() first.');
|
||||
@@ -30,7 +30,7 @@ class LocalStorage {
|
||||
await prefs.setString(_tokenKey, token);
|
||||
}
|
||||
|
||||
/// 获取 Token
|
||||
/// 獲取 Token
|
||||
static String? getToken() {
|
||||
return prefs.getString(_tokenKey);
|
||||
}
|
||||
@@ -40,17 +40,17 @@ class LocalStorage {
|
||||
await prefs.remove(_tokenKey);
|
||||
}
|
||||
|
||||
/// 是否已登录
|
||||
/// 是否已登錄
|
||||
static bool get isLoggedIn => getToken() != null && getToken()!.isNotEmpty;
|
||||
|
||||
// ==================== 用户信息管理 ====================
|
||||
// ==================== 用戶信息管理 ====================
|
||||
|
||||
/// 保存用户信息
|
||||
/// 保存用戶信息
|
||||
static Future<void> saveUserInfo(Map<String, dynamic> userInfo) async {
|
||||
await prefs.setString(_userInfoKey, jsonEncode(userInfo));
|
||||
}
|
||||
|
||||
/// 获取用户信息
|
||||
/// 獲取用戶信息
|
||||
static Map<String, dynamic>? getUserInfo() {
|
||||
final str = prefs.getString(_userInfoKey);
|
||||
if (str == null) return null;
|
||||
@@ -61,7 +61,7 @@ class LocalStorage {
|
||||
}
|
||||
}
|
||||
|
||||
/// 移除用户信息
|
||||
/// 移除用戶信息
|
||||
static Future<void> removeUserInfo() async {
|
||||
await prefs.remove(_userInfoKey);
|
||||
}
|
||||
@@ -73,47 +73,47 @@ class LocalStorage {
|
||||
await prefs.setString(key, value);
|
||||
}
|
||||
|
||||
/// 获取字符串
|
||||
/// 獲取字符串
|
||||
static String? getString(String key) {
|
||||
return prefs.getString(key);
|
||||
}
|
||||
|
||||
/// 保存布尔值
|
||||
/// 保存布爾值
|
||||
static Future<void> setBool(String key, bool value) async {
|
||||
await prefs.setBool(key, value);
|
||||
}
|
||||
|
||||
/// 获取布尔值
|
||||
/// 獲取布爾值
|
||||
static bool? getBool(String key) {
|
||||
return prefs.getBool(key);
|
||||
}
|
||||
|
||||
/// 清除所有数据
|
||||
/// 清除所有數據
|
||||
static Future<void> clearAll() async {
|
||||
await prefs.clear();
|
||||
}
|
||||
|
||||
/// 清除用户数据(退出登录时调用)
|
||||
/// 清除用戶數據(退出登錄時調用)
|
||||
static Future<void> clearUserData() async {
|
||||
await removeToken();
|
||||
await removeUserInfo();
|
||||
}
|
||||
|
||||
// ==================== 引导页状态 ====================
|
||||
// ==================== 引導頁狀態 ====================
|
||||
|
||||
static const String _onboardingKey = 'onboarding_completed';
|
||||
|
||||
/// 检查是否已完成引导页
|
||||
/// 檢查是否已完成引導頁
|
||||
static bool get isOnboardingCompleted {
|
||||
return getBool(_onboardingKey) ?? false;
|
||||
}
|
||||
|
||||
/// 标记引导页已完成
|
||||
/// 標記引導頁已完成
|
||||
static Future<void> setOnboardingCompleted() async {
|
||||
await setBool(_onboardingKey, true);
|
||||
}
|
||||
|
||||
/// 重置引导页状态(用于测试)
|
||||
/// 重置引導頁狀態(用於測試)
|
||||
static Future<void> resetOnboarding() async {
|
||||
await prefs.remove(_onboardingKey);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user