import 'package:dio/dio.dart'; import '../models/theme_group.dart'; import '../models/theme_template.dart'; import 'api_client.dart'; /// 主题模板服务:对接 web 后台 /theme/themetemplate/* 与 /theme/page/* 接口。 class ThemeService { ThemeService({Dio? dio}) : _dio = dio ?? ApiClient.instance.dio; final Dio _dio; /// 获取全部启用的主题模板。 /// /// GET /theme/themetemplate/all?status=ENABLED Future> getThemeTemplateAll() async { final response = await _dio.get( '/theme/themetemplate/all', queryParameters: const {'status': 'ENABLED'}, ); final list = _extractList(response.data); return list .whereType>() .map(ThemeTemplate.fromJson) .where((t) => t.id.isNotEmpty && t.isEnabled) .toList(); } /// 查询门店下各设备分组的主题预览列表(界面配置)。 /// /// GET /theme/page/org/{orgId}/photos Future> getStoreThemeGroups(String orgId) async { final response = await _dio.get( '/theme/page/org/$orgId/photos', ); final data = response.data; if (data is List) { return data .whereType>() .map(ThemeGroup.fromJson) .where((g) => g.groupId > 0) .toList(); } if (data is Map) { final code = data['code']; final isSuccess = code == null || code == 200 || code == 0; if (!isSuccess) return []; final list = data['data'] ?? data['list']; if (list is List) { return list .whereType>() .map(ThemeGroup.fromJson) .where((g) => g.groupId > 0) .toList(); } } return []; } /// 兼容列表响应的三种形态: /// `{list: [...]}` / `{data: [...]}` / 直接数组。 static List _extractList(dynamic data) { if (data is List) return data; if (data is Map) { final list = data['list']; if (list is List) return list; final innerData = data['data']; if (innerData is List) return innerData; } return const []; } }