feat: 搜索计数

This commit is contained in:
guozhigq
2024-05-05 17:53:54 +08:00
parent 5934015104
commit e65389040f
5 changed files with 91 additions and 32 deletions

View File

@ -520,4 +520,7 @@ class Api {
/// 删除收藏夹 /// 删除收藏夹
static const String delFavFolder = '/x/v3/fav/folder/del'; static const String delFavFolder = '/x/v3/fav/folder/del';
/// 搜索结果计数
static const String searchCount = '/x/web-interface/wbi/search/all/v2';
} }

View File

@ -1,5 +1,7 @@
import 'dart:convert'; import 'dart:convert';
import 'package:hive/hive.dart'; import 'package:hive/hive.dart';
import 'package:pilipala/models/search/all.dart';
import 'package:pilipala/utils/wbi_sign.dart';
import '../models/bangumi/info.dart'; import '../models/bangumi/info.dart';
import '../models/common/search_type.dart'; import '../models/common/search_type.dart';
import '../models/search/hot.dart'; import '../models/search/hot.dart';
@ -179,4 +181,26 @@ class SearchHttp {
'pic': res.data['data'].first['first_frame'], 'pic': res.data['data'].first['first_frame'],
}; };
} }
static Future<Map<String, dynamic>> searchCount(
{required String keyword}) async {
Map<String, dynamic> data = {
'keyword': keyword,
'web_location': 333.999,
};
Map params = await WbiSign().makSign(data);
final dynamic res = await Request().get(Api.searchCount, data: params);
if (res.data['code'] == 0) {
return {
'status': true,
'data': SearchAllModel.fromJson(res.data['data']),
};
} else {
return {
'status': false,
'data': [],
'msg': '请求错误 🙅',
};
}
}
} }

View File

@ -0,0 +1,9 @@
class SearchAllModel {
SearchAllModel({this.topTList});
Map? topTList;
SearchAllModel.fromJson(Map<String, dynamic> json) {
topTList = json['top_tlist'];
}
}

View File

@ -1,8 +1,11 @@
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:pilipala/http/search.dart';
import 'package:pilipala/models/common/search_type.dart';
class SearchResultController extends GetxController { class SearchResultController extends GetxController {
String? keyword; String? keyword;
int tabIndex = 0; int tabIndex = 0;
RxList searchTabs = [].obs;
@override @override
void onInit() { void onInit() {
@ -10,5 +13,21 @@ class SearchResultController extends GetxController {
if (Get.parameters.keys.isNotEmpty) { if (Get.parameters.keys.isNotEmpty) {
keyword = Get.parameters['keyword']; keyword = Get.parameters['keyword'];
} }
searchTabs.value = SearchType.values
.map((type) => {'label': type.label, 'id': type.type})
.toList();
querySearchCount();
}
Future querySearchCount() async {
var result = await SearchHttp.searchCount(keyword: keyword!);
if (result['status']) {
for (var i in searchTabs) {
final count = result['data'].topTList[i['id']];
i['count'] = count > 99 ? '99+' : count.toString();
}
searchTabs.refresh();
}
return result;
} }
} }

View File

@ -13,7 +13,7 @@ class SearchResultPage extends StatefulWidget {
class _SearchResultPageState extends State<SearchResultPage> class _SearchResultPageState extends State<SearchResultPage>
with TickerProviderStateMixin { with TickerProviderStateMixin {
late SearchResultController? _searchResultController; late SearchResultController _searchResultController;
late TabController? _tabController; late TabController? _tabController;
@override @override
@ -25,7 +25,7 @@ class _SearchResultPageState extends State<SearchResultPage>
_tabController = TabController( _tabController = TabController(
vsync: this, vsync: this,
length: SearchType.values.length, length: SearchType.values.length,
initialIndex: _searchResultController!.tabIndex, initialIndex: _searchResultController.tabIndex,
); );
} }
@ -46,7 +46,7 @@ class _SearchResultPageState extends State<SearchResultPage>
child: SizedBox( child: SizedBox(
width: double.infinity, width: double.infinity,
child: Text( child: Text(
'${_searchResultController!.keyword}', '${_searchResultController.keyword}',
style: Theme.of(context).textTheme.titleMedium, style: Theme.of(context).textTheme.titleMedium,
), ),
), ),
@ -64,35 +64,39 @@ class _SearchResultPageState extends State<SearchResultPage>
splashColor: Colors.transparent, // 点击时的水波纹颜色设置为透明 splashColor: Colors.transparent, // 点击时的水波纹颜色设置为透明
highlightColor: Colors.transparent, // 点击时的背景高亮颜色设置为透明 highlightColor: Colors.transparent, // 点击时的背景高亮颜色设置为透明
), ),
child: TabBar( child: Obx(
controller: _tabController, () => (TabBar(
tabs: [ controller: _tabController,
for (var i in SearchType.values) Tab(text: i.label), tabs: [
], for (var i in _searchResultController.searchTabs)
isScrollable: true, Tab(text: "${i['label']} ${i['count'] ?? ''}")
indicatorWeight: 0, ],
indicatorPadding: isScrollable: true,
const EdgeInsets.symmetric(horizontal: 3, vertical: 8), indicatorWeight: 0,
indicator: BoxDecoration( indicatorPadding:
color: Theme.of(context).colorScheme.secondaryContainer, const EdgeInsets.symmetric(horizontal: 3, vertical: 8),
borderRadius: const BorderRadius.all(Radius.circular(20)), indicator: BoxDecoration(
), color: Theme.of(context).colorScheme.secondaryContainer,
indicatorSize: TabBarIndicatorSize.tab, borderRadius: const BorderRadius.all(Radius.circular(20)),
labelColor: Theme.of(context).colorScheme.onSecondaryContainer, ),
labelStyle: const TextStyle(fontSize: 13), indicatorSize: TabBarIndicatorSize.tab,
dividerColor: Colors.transparent, labelColor:
unselectedLabelColor: Theme.of(context).colorScheme.outline, Theme.of(context).colorScheme.onSecondaryContainer,
tabAlignment: TabAlignment.start, labelStyle: const TextStyle(fontSize: 13),
onTap: (index) { dividerColor: Colors.transparent,
if (index == _searchResultController!.tabIndex) { unselectedLabelColor: Theme.of(context).colorScheme.outline,
Get.find<SearchPanelController>( tabAlignment: TabAlignment.start,
tag: SearchType.values[index].type + onTap: (index) {
_searchResultController!.keyword!) if (index == _searchResultController.tabIndex) {
.animateToTop(); Get.find<SearchPanelController>(
} tag: SearchType.values[index].type +
_searchResultController.keyword!)
.animateToTop();
}
_searchResultController!.tabIndex = index; _searchResultController.tabIndex = index;
}, },
)),
), ),
), ),
), ),
@ -102,7 +106,7 @@ class _SearchResultPageState extends State<SearchResultPage>
children: [ children: [
for (var i in SearchType.values) ...{ for (var i in SearchType.values) ...{
SearchPanel( SearchPanel(
keyword: _searchResultController!.keyword, keyword: _searchResultController.keyword,
searchType: i, searchType: i,
tag: DateTime.now().millisecondsSinceEpoch.toString(), tag: DateTime.now().millisecondsSinceEpoch.toString(),
) )