50 lines
1.4 KiB
Dart
50 lines
1.4 KiB
Dart
import '../core/network/api_client.dart';
|
||
import '../core/network/endpoints.dart';
|
||
import '../models/video_item.dart';
|
||
|
||
/// 搜索服务(《03-接口文档》5.10/5.11)
|
||
class SearchService {
|
||
SearchService._();
|
||
|
||
/// 视频搜索:sort=hot(默认)| latest;命中敏感词服务端返回空结果
|
||
static Future<SearchPage> search(
|
||
String keyword, {
|
||
String sort = 'hot',
|
||
int page = 1,
|
||
int pageSize = 20,
|
||
}) async {
|
||
final data = await ApiClient.get<Map<String, dynamic>>(
|
||
Endpoints.search,
|
||
queryParameters: {
|
||
'keyword': keyword,
|
||
'sort': sort,
|
||
'page': page,
|
||
'page_size': pageSize,
|
||
},
|
||
);
|
||
return SearchPage.fromJson(data);
|
||
}
|
||
|
||
/// 热搜榜 Top10(Redis ZSet search:hot)
|
||
static Future<List<String>> hotWords() async {
|
||
final data = await ApiClient.get<Map<String, dynamic>>(Endpoints.hotSearch);
|
||
return ((data['words'] as List?) ?? const []).map((e) => e.toString()).toList();
|
||
}
|
||
}
|
||
|
||
/// 搜索结果分页(普通 page/page_size 分页)
|
||
class SearchPage {
|
||
SearchPage({required this.total, required this.items});
|
||
|
||
final int total;
|
||
final List<VideoItem> items;
|
||
|
||
factory SearchPage.fromJson(Map<String, dynamic> json) => SearchPage(
|
||
total: (json['total'] as num?)?.toInt() ?? 0,
|
||
items: ((json['list'] as List?) ?? const [])
|
||
.whereType<Map<String, dynamic>>()
|
||
.map(VideoItem.fromJson)
|
||
.toList(),
|
||
);
|
||
}
|