- Flutter 3.x 项目初始化(pubspec.yaml + 目录结构) - 核心依赖:dio/go_router/provider/shared_preferences/hive/geolocator - 统一登录页 + Token管理(TokenService + AuthInterceptor) - 底部Tab三合一导航(供水/巡检/营收) - 供水管理Tab:监测数据列表(MonitorListPage) - 巡检Tab:任务列表+状态筛选+进度展示(PatrolTaskListPage) - 营收Tab:抄表页面+账单列表(MeterReadingPage/BillListPage) - 消息推送服务(PushService) - GPS定位服务(LocationService) - 拍照/相册服务(CameraService) - 离线缓存服务(CacheService) - Android/iOS打包配置+权限声明 - GoRouter路由+Auth认证守卫 - Provider状态管理
123 lines
3.4 KiB
Dart
123 lines
3.4 KiB
Dart
import 'dart:convert';
|
||
|
||
/// 离线缓存服务
|
||
/// 基于 Hive 提供本地数据存储能力
|
||
class CacheService {
|
||
static CacheService? _instance;
|
||
final Map<String, dynamic> _memoryCache = {};
|
||
bool _isInitialized = false;
|
||
|
||
CacheService._internal();
|
||
|
||
factory CacheService() {
|
||
_instance ??= CacheService._internal();
|
||
return _instance!;
|
||
}
|
||
|
||
/// 初始化缓存(打开 Hive Box)
|
||
Future<void> initialize() async {
|
||
if (_isInitialized) return;
|
||
|
||
try {
|
||
// TODO: 初始化 Hive
|
||
// await Hive.initFlutter();
|
||
// await Hive.openBox(AppConstants.cacheBoxName);
|
||
// await Hive.openBox(AppConstants.userBoxName);
|
||
_isInitialized = true;
|
||
print('[CacheService] 缓存服务已初始化(模拟)');
|
||
} catch (e) {
|
||
print('[CacheService] 初始化失败: $e');
|
||
}
|
||
}
|
||
|
||
/// 写入缓存
|
||
Future<void> put(String key, dynamic value, {String box = 'default'}) async {
|
||
try {
|
||
// TODO: 使用 Hive 持久化
|
||
// final cacheBox = Hive.box(AppConstants.cacheBoxName);
|
||
// await cacheBox.put(key, value);
|
||
_memoryCache['$box:$key'] = value;
|
||
} catch (e) {
|
||
print('[CacheService] 写入缓存失败: $e');
|
||
}
|
||
}
|
||
|
||
/// 读取缓存
|
||
Future<T?> get<T>(String key, {String box = 'default'}) async {
|
||
try {
|
||
// TODO: 使用 Hive 读取
|
||
// final cacheBox = Hive.box(AppConstants.cacheBoxName);
|
||
// return cacheBox.get(key) as T?;
|
||
return _memoryCache['$box:$key'] as T?;
|
||
} catch (e) {
|
||
print('[CacheService] 读取缓存失败: $e');
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// 删除缓存
|
||
Future<void> delete(String key, {String box = 'default'}) async {
|
||
try {
|
||
_memoryCache.remove('$box:$key');
|
||
} catch (e) {
|
||
print('[CacheService] 删除缓存失败: $e');
|
||
}
|
||
}
|
||
|
||
/// 清空指定 Box 的所有缓存
|
||
Future<void> clearBox({String box = 'default'}) async {
|
||
try {
|
||
_memoryCache.removeWhere((key, _) => key.startsWith('$box:'));
|
||
} catch (e) {
|
||
print('[CacheService] 清空缓存失败: $e');
|
||
}
|
||
}
|
||
|
||
/// 清空所有缓存
|
||
Future<void> clearAll() async {
|
||
try {
|
||
_memoryCache.clear();
|
||
} catch (e) {
|
||
print('[CacheService] 清空所有缓存失败: $e');
|
||
}
|
||
}
|
||
|
||
/// 缓存 JSON 对象
|
||
Future<void> cacheJson(String key, Map<String, dynamic> data, {String box = 'default'}) async {
|
||
await put(key, json.encode(data), box: box);
|
||
}
|
||
|
||
/// 读取缓存的 JSON 对象
|
||
Future<Map<String, dynamic>?> getCachedJson(String key, {String box = 'default'}) async {
|
||
final data = await get<String>(key, box: box);
|
||
if (data == null) return null;
|
||
try {
|
||
return json.decode(data) as Map<String, dynamic>;
|
||
} catch (e) {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// 缓存列表数据
|
||
Future<void> cacheList(String key, List<Map<String, dynamic>> items, {String box = 'default'}) async {
|
||
await put(key, json.encode(items), box: box);
|
||
}
|
||
|
||
/// 读取缓存的列表数据
|
||
Future<List<Map<String, dynamic>>?> getCachedList(String key, {String box = 'default'}) async {
|
||
final data = await get<String>(key, box: box);
|
||
if (data == null) return null;
|
||
try {
|
||
final decoded = json.decode(data) as List;
|
||
return decoded.cast<Map<String, dynamic>>();
|
||
} catch (e) {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// 检查是否有缓存
|
||
Future<bool> has(String key, {String box = 'default'}) async {
|
||
return _memoryCache.containsKey('$box:$key');
|
||
}
|
||
}
|