From 1c08edb113d2b6446285a6dcc571d7cf96c26206 Mon Sep 17 00:00:00 2001 From: guozhigq Date: Mon, 7 Aug 2023 21:50:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20AV/BV=E5=8F=B7=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E8=B7=B3=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pages/searchPanel/controller.dart | 23 +++++++++++++++++++++++ lib/utils/id_utils.dart | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/lib/pages/searchPanel/controller.dart b/lib/pages/searchPanel/controller.dart index 640ae246..b8e4a166 100644 --- a/lib/pages/searchPanel/controller.dart +++ b/lib/pages/searchPanel/controller.dart @@ -2,6 +2,8 @@ import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:pilipala/http/search.dart'; import 'package:pilipala/models/common/search_type.dart'; +import 'package:pilipala/utils/id_utils.dart'; +import 'package:pilipala/utils/utils.dart'; class SearchPanelController extends GetxController { SearchPanelController({this.keyword, this.searchType}); @@ -21,6 +23,7 @@ class SearchPanelController extends GetxController { } else if (type == 'onRefresh') { resultList.value = result['data'].list; } + onPushDetail(keyword, resultList); } return result; } @@ -40,4 +43,24 @@ class SearchPanelController extends GetxController { duration: const Duration(milliseconds: 500), curve: Curves.easeInOut); } } + + void onPushDetail(keyword, resultList) async { + // 匹配输入内容,如果是AV、BV号且有结果 直接跳转详情页 + Map matchRes = IdUtils.matchAvorBv(input: keyword); + List matchKeys = matchRes.keys.toList(); + if (matchKeys.isNotEmpty && searchType == SearchType.video) { + String bvid = resultList.first.bvid; + int aid = resultList.first.aid; + String heroTag = Utils.makeHeroTag(bvid); + + int cid = await SearchHttp.ab2c(aid: aid, bvid: bvid); + if (matchKeys.first == 'BV' && matchRes[matchKeys.first] == bvid || + matchKeys.first == 'AV' && matchRes[matchKeys.first] == aid) { + Get.toNamed( + '/video?bvid=$bvid&cid=$cid', + arguments: {'videoItem': resultList.first, 'heroTag': heroTag}, + ); + } + } + } } diff --git a/lib/utils/id_utils.dart b/lib/utils/id_utils.dart index 227e58a7..8e2e6d70 100644 --- a/lib/utils/id_utils.dart +++ b/lib/utils/id_utils.dart @@ -46,4 +46,28 @@ class IdUtils { } return (r - ADD) ^ XOR; } + + // 匹配 + static Map matchAvorBv({String? input}) { + Map result = {}; + if (input == null || input == '') { + return result; + } + RegExp bvRegex = RegExp(r'BV[0-9A-Za-z]{10}', caseSensitive: false); + RegExp avRegex = RegExp(r'AV\d+', caseSensitive: false); + + Iterable bvMatches = bvRegex.allMatches(input); + Iterable avMatches = avRegex.allMatches(input); + + List bvs = bvMatches.map((match) => match.group(0)!).toList(); + List avs = avMatches.map((match) => match.group(0)!).toList(); + + if (bvs.isNotEmpty) { + result['BV'] = bvs[0].substring(0, 2).toUpperCase() + bvs[0].substring(2); + } + if (avs.isNotEmpty) { + result['AV'] = avs[0].substring(2); + } + return result; + } }