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 searchCount = '/x/web-interface/wbi/search/all/v2';
}

View File

@ -1,5 +1,7 @@
import 'dart:convert';
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/common/search_type.dart';
import '../models/search/hot.dart';
@ -179,4 +181,26 @@ class SearchHttp {
'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:pilipala/http/search.dart';
import 'package:pilipala/models/common/search_type.dart';
class SearchResultController extends GetxController {
String? keyword;
int tabIndex = 0;
RxList searchTabs = [].obs;
@override
void onInit() {
@ -10,5 +13,21 @@ class SearchResultController extends GetxController {
if (Get.parameters.keys.isNotEmpty) {
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>
with TickerProviderStateMixin {
late SearchResultController? _searchResultController;
late SearchResultController _searchResultController;
late TabController? _tabController;
@override
@ -25,7 +25,7 @@ class _SearchResultPageState extends State<SearchResultPage>
_tabController = TabController(
vsync: this,
length: SearchType.values.length,
initialIndex: _searchResultController!.tabIndex,
initialIndex: _searchResultController.tabIndex,
);
}
@ -46,7 +46,7 @@ class _SearchResultPageState extends State<SearchResultPage>
child: SizedBox(
width: double.infinity,
child: Text(
'${_searchResultController!.keyword}',
'${_searchResultController.keyword}',
style: Theme.of(context).textTheme.titleMedium,
),
),
@ -64,10 +64,12 @@ class _SearchResultPageState extends State<SearchResultPage>
splashColor: Colors.transparent, // 点击时的水波纹颜色设置为透明
highlightColor: Colors.transparent, // 点击时的背景高亮颜色设置为透明
),
child: TabBar(
child: Obx(
() => (TabBar(
controller: _tabController,
tabs: [
for (var i in SearchType.values) Tab(text: i.label),
for (var i in _searchResultController.searchTabs)
Tab(text: "${i['label']} ${i['count'] ?? ''}")
],
isScrollable: true,
indicatorWeight: 0,
@ -78,21 +80,23 @@ class _SearchResultPageState extends State<SearchResultPage>
borderRadius: const BorderRadius.all(Radius.circular(20)),
),
indicatorSize: TabBarIndicatorSize.tab,
labelColor: Theme.of(context).colorScheme.onSecondaryContainer,
labelColor:
Theme.of(context).colorScheme.onSecondaryContainer,
labelStyle: const TextStyle(fontSize: 13),
dividerColor: Colors.transparent,
unselectedLabelColor: Theme.of(context).colorScheme.outline,
tabAlignment: TabAlignment.start,
onTap: (index) {
if (index == _searchResultController!.tabIndex) {
if (index == _searchResultController.tabIndex) {
Get.find<SearchPanelController>(
tag: SearchType.values[index].type +
_searchResultController!.keyword!)
_searchResultController.keyword!)
.animateToTop();
}
_searchResultController!.tabIndex = index;
_searchResultController.tabIndex = index;
},
)),
),
),
),
@ -102,7 +106,7 @@ class _SearchResultPageState extends State<SearchResultPage>
children: [
for (var i in SearchType.values) ...{
SearchPanel(
keyword: _searchResultController!.keyword,
keyword: _searchResultController.keyword,
searchType: i,
tag: DateTime.now().millisecondsSinceEpoch.toString(),
)