From 32f916c92b069b695985acb0612c3ecffa6ab0e6 Mon Sep 17 00:00:00 2001 From: bggRGjQaUbCoE Date: Wed, 18 Sep 2024 10:13:47 +0800 Subject: [PATCH] opt: reply request --- lib/http/api.dart | 2 +- lib/http/reply.dart | 37 ++++++++++++++------ lib/models/video/reply/data.dart | 31 ++++++++-------- lib/pages/common/reply_controller.dart | 10 ++++-- lib/pages/dynamics/detail/controller.dart | 2 ++ lib/pages/html/controller.dart | 2 ++ lib/pages/video/detail/reply/controller.dart | 2 ++ 7 files changed, 57 insertions(+), 29 deletions(-) diff --git a/lib/http/api.dart b/lib/http/api.dart index ff524e2f..71b5e386 100644 --- a/lib/http/api.dart +++ b/lib/http/api.dart @@ -129,7 +129,7 @@ class Api { // 评论列表 // https://api.bilibili.com/x/v2/reply/main?csrf=6e22efc1a47225ea25f901f922b5cfdd&mode=3&oid=254175381&pagination_str=%7B%22offset%22:%22%22%7D&plat=1&seek_rpid=0&type=11 - static const String replyList = '/x/v2/reply/main'; + static const String replyList = '/x/v2/reply'; // 楼中楼 static const String replyReplyList = '/x/v2/reply/reply'; diff --git a/lib/http/reply.dart b/lib/http/reply.dart index dfa5976a..99ee4fc8 100644 --- a/lib/http/reply.dart +++ b/lib/http/reply.dart @@ -12,25 +12,40 @@ import 'init.dart'; class ReplyHttp { static Future replyList({ + required bool isLogin, required int oid, required String nextOffset, required int type, + required int page, int sort = 1, }) async { - Options? options = GStorage.userInfo.get('userInfoCache') == null + Options? options = !isLogin ? Options( headers: {HttpHeaders.cookieHeader: "buvid3= ; b_nut= ; sid= "}) : null; - var res = await Request().get( - '${HttpString.apiBaseUrl}${Api.replyList}', - data: { - 'oid': oid, - 'type': type, - 'pagination_str': '{"offset":"${nextOffset.replaceAll('"', '\\"')}"}', - 'mode': sort + 2, //2:按时间排序;3:按热度排序 - }, - options: options, - ); + var res = !isLogin + ? await Request().get( + '${HttpString.apiBaseUrl}${Api.replyList}/main', + data: { + 'oid': oid, + 'type': type, + 'pagination_str': + '{"offset":"${nextOffset.replaceAll('"', '\\"')}"}', + 'mode': sort + 2, //2:按时间排序;3:按热度排序 + }, + options: options, + ) + : await Request().get( + '${HttpString.apiBaseUrl}${Api.replyList}', + data: { + 'oid': oid, + 'type': type, + 'sort': sort, + 'pn': page, + 'ps': 20, + }, + options: options, + ); if (res.data['code'] == 0) { return LoadingState.success(ReplyData.fromJson(res.data['data'])); } else { diff --git a/lib/models/video/reply/data.dart b/lib/models/video/reply/data.dart index 15735c53..3c4b0bbb 100644 --- a/lib/models/video/reply/data.dart +++ b/lib/models/video/reply/data.dart @@ -6,6 +6,7 @@ import 'upper.dart'; class ReplyData { ReplyData({ + this.page, this.cursor, this.config, this.replies, @@ -13,6 +14,7 @@ class ReplyData { this.upper, }); + ReplyPage? page; ReplyCursor? cursor; ReplyConfig? config; late List? replies; @@ -20,20 +22,21 @@ class ReplyData { ReplyUpper? upper; ReplyData.fromJson(Map json) { - cursor = ReplyCursor.fromJson(json['cursor']); - config = ReplyConfig.fromJson(json['config']); + page = json['page'] == null ? null : ReplyPage.fromJson(json['page']); + cursor = + json['cursor'] == null ? null : ReplyCursor.fromJson(json['cursor']); + config = + json['config'] == null ? null : ReplyConfig.fromJson(json['config']); replies = json['replies'] != null - ? List.from(json['replies'] - .map( - (item) => ReplyItemModel.fromJson(item, json['upper']['mid']))) + ? List.from(json['replies'].map( + (item) => ReplyItemModel.fromJson(item, json['upper']['mid']))) : []; topReplies = json['top_replies'] != null - ? List.from(json['top_replies'] - .map((item) => ReplyItemModel.fromJson( - item, json['upper']['mid'], + ? List.from(json['top_replies'].map( + (item) => ReplyItemModel.fromJson(item, json['upper']['mid'], isTopStatus: true))) : []; - upper = ReplyUpper.fromJson(json['upper']); + upper = json['upper'] == null ? null : ReplyUpper.fromJson(json['upper']); } } @@ -58,15 +61,13 @@ class ReplyReplyData { page = ReplyPage.fromJson(json['page']); config = ReplyConfig.fromJson(json['config']); replies = json['replies'] != null - ? List.from(json['replies'] - .map( + ? List.from(json['replies'].map( (item) => ReplyItemModel.fromJson(item, json['upper']['mid']))) : []; topReplies = json['top_replies'] != null - ? List.from(json['top_replies'] - .map((item) => ReplyItemModel.fromJson( - item, json['upper']['mid'], - isTopStatus: true))) + ? List.from(json['top_replies'].map( + (item) => ReplyItemModel.fromJson(item, json['upper']['mid'], + isTopStatus: true))) : []; upper = ReplyUpper.fromJson(json['upper']); root = ReplyItemModel.fromJson(json['root'], json['upper']['mid']); diff --git a/lib/pages/common/reply_controller.dart b/lib/pages/common/reply_controller.dart index 7e08c156..169caab6 100644 --- a/lib/pages/common/reply_controller.dart +++ b/lib/pages/common/reply_controller.dart @@ -25,6 +25,8 @@ abstract class ReplyController extends CommonController { late final savedReplies = {}; + bool isLogin = GStorage.userInfo.get('userInfoCache') != null; + @override void onInit() { super.onInit(); @@ -55,7 +57,9 @@ abstract class ReplyController extends CommonController { @override bool customHandleResponse(Success response) { List replies = response.response.replies; - nextOffset = response.response.cursor.paginationReply.nextOffset ?? ""; + if (!isLogin) { + nextOffset = response.response.cursor.paginationReply.nextOffset ?? ""; + } if (replies.isNotEmpty) { noMore.value = '加载中...'; @@ -78,7 +82,9 @@ abstract class ReplyController extends CommonController { } } replies.insertAll(0, response.response.topReplies); - count.value = response.response.cursor.allCount ?? 0; + count.value = !isLogin + ? response.response.cursor.allCount + : response.response.page.count ?? 0; } else { replies.insertAll( 0, diff --git a/lib/pages/dynamics/detail/controller.dart b/lib/pages/dynamics/detail/controller.dart index de0d805c..ad81a28a 100644 --- a/lib/pages/dynamics/detail/controller.dart +++ b/lib/pages/dynamics/detail/controller.dart @@ -34,9 +34,11 @@ class DynamicDetailController extends ReplyController { @override Future customGetData() => ReplyHttp.replyList( + isLogin: isLogin, oid: oid!, nextOffset: nextOffset, type: type!, sort: sortType.index, + page: currentPage, ); } diff --git a/lib/pages/html/controller.dart b/lib/pages/html/controller.dart index a876eb41..29f55278 100644 --- a/lib/pages/html/controller.dart +++ b/lib/pages/html/controller.dart @@ -42,9 +42,11 @@ class HtmlRenderController extends ReplyController { @override Future customGetData() => ReplyHttp.replyList( + isLogin: isLogin, oid: oid.value, nextOffset: nextOffset, type: type, sort: sortType.index, + page: currentPage, ); } diff --git a/lib/pages/video/detail/reply/controller.dart b/lib/pages/video/detail/reply/controller.dart index f815d85b..1f68782d 100644 --- a/lib/pages/video/detail/reply/controller.dart +++ b/lib/pages/video/detail/reply/controller.dart @@ -18,9 +18,11 @@ class VideoReplyController extends ReplyController { @override Future customGetData() => ReplyHttp.replyList( + isLogin: isLogin, oid: aid!, nextOffset: nextOffset, type: ReplyType.video.index, sort: sortType.index, + page: currentPage, ); }