fix: 评论区无法切换最新

This commit is contained in:
orz12
2024-08-13 01:11:39 +08:00
parent 5c9c2dd722
commit a669488612
7 changed files with 122 additions and 40 deletions

View File

@@ -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 // 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'; static const String replyList = '/x/v2/reply/main';
// 楼中楼 // 楼中楼
static const String replyReplyList = '/x/v2/reply/reply'; static const String replyReplyList = '/x/v2/reply/reply';

View File

@@ -6,17 +6,15 @@ import 'init.dart';
class ReplyHttp { class ReplyHttp {
static Future replyList({ static Future replyList({
required int oid, required int oid,
required int pageNum, required String nextOffset,
required int type, required int type,
int? ps,
int sort = 1, int sort = 1,
}) async { }) async {
var res = await Request().get(Api.replyList, data: { var res = await Request().get(Api.replyList, data: {
'oid': oid, 'oid': oid,
'pn': pageNum,
'type': type, 'type': type,
'sort': sort, 'pagination_str': '{"offset":"${nextOffset.replaceAll('"', '\\"')}"}',
'ps': ps ?? 20 'mode': sort + 2, //2:按时间排序3按热度排序
}); });
if (res.data['code'] == 0) { if (res.data['code'] == 0) {
return { return {
@@ -50,7 +48,7 @@ class ReplyHttp {
if (res.data['code'] == 0) { if (res.data['code'] == 0) {
return { return {
'status': true, 'status': true,
'data': ReplyData.fromJson(res.data['data']), 'data': ReplyReplyData.fromJson(res.data['data']),
}; };
} else { } else {
return { return {

View File

@@ -6,21 +6,21 @@ import 'upper.dart';
class ReplyData { class ReplyData {
ReplyData({ ReplyData({
this.page, this.cursor,
this.config, this.config,
this.replies, this.replies,
this.topReplies, this.topReplies,
this.upper, this.upper,
}); });
ReplyPage? page; ReplyCursor? cursor;
ReplyConfig? config; ReplyConfig? config;
late List<ReplyItemModel>? replies; late List<ReplyItemModel>? replies;
late List<ReplyItemModel>? topReplies; late List<ReplyItemModel>? topReplies;
ReplyUpper? upper; ReplyUpper? upper;
ReplyData.fromJson(Map<String, dynamic> json) { ReplyData.fromJson(Map<String, dynamic> json) {
page = ReplyPage.fromJson(json['page']); cursor = ReplyCursor.fromJson(json['cursor']);
config = ReplyConfig.fromJson(json['config']); config = ReplyConfig.fromJson(json['config']);
replies = json['replies'] != null replies = json['replies'] != null
? List<ReplyItemModel>.from(json['replies'] ? List<ReplyItemModel>.from(json['replies']
@@ -36,3 +36,36 @@ class ReplyData {
upper = ReplyUpper.fromJson(json['upper']); upper = ReplyUpper.fromJson(json['upper']);
} }
} }
class ReplyReplyData {
ReplyReplyData({
this.page,
this.config,
this.replies,
this.topReplies,
this.upper,
});
ReplyPage? page;
ReplyConfig? config;
late List<ReplyItemModel>? replies;
late List<ReplyItemModel>? topReplies;
ReplyUpper? upper;
ReplyReplyData.fromJson(Map<String, dynamic> json) {
page = ReplyPage.fromJson(json['page']);
config = ReplyConfig.fromJson(json['config']);
replies = json['replies'] != null
? List<ReplyItemModel>.from(json['replies']
.map<ReplyItemModel>(
(item) => ReplyItemModel.fromJson(item, json['upper']['mid'])))
: <ReplyItemModel>[];
topReplies = json['top_replies'] != null
? List<ReplyItemModel>.from(json['top_replies']
.map<ReplyItemModel>((item) => ReplyItemModel.fromJson(
item, json['upper']['mid'],
isTopStatus: true)))
: <ReplyItemModel>[];
upper = ReplyUpper.fromJson(json['upper']);
}
}

View File

@@ -18,3 +18,60 @@ class ReplyPage {
acount = json['acount']; acount = json['acount'];
} }
} }
class ReplyCursor {
ReplyCursor({
this.isBegin,
this.prev,
this.next,
this.isEnd,
this.mode,
this.modeText,
this.allCount,
this.supportMode,
this.name,
this.paginationReply,
this.sessionId,
});
bool? isBegin;
int? prev;
int? next;
bool? isEnd;
int? mode;
String? modeText;
int? allCount;
List<int>? supportMode;
String? name;
PaginationReply? paginationReply;
String? sessionId;
ReplyCursor.fromJson(Map<String, dynamic> json) {
isBegin = json['is_begin'];
prev = json['prev'];
next = json['next'];
isEnd = json['is_end'];
mode = json['mode'];
modeText = json['mode_text'];
allCount = json['all_count'];
supportMode = json['support_mode'].cast<int>();
name = json['name'];
paginationReply = json['pagination_reply'] != null
? PaginationReply.fromJson(json['pagination_reply'])
: null;
sessionId = json['session_id'];
}
}
class PaginationReply {
PaginationReply({
this.nextOffset,
this.prevOffset,
});
String? nextOffset;
String? prevOffset;
PaginationReply.fromJson(Map<String, dynamic> json) {
nextOffset = json['next_offset'];
prevOffset = json['prev_offset'];
}
}

View File

@@ -14,7 +14,7 @@ class DynamicDetailController extends GetxController {
int? type; int? type;
dynamic item; dynamic item;
int? floor; int? floor;
int currentPage = 0; String nextOffset = "";
bool isLoadingMore = false; bool isLoadingMore = false;
RxString noMore = ''.obs; RxString noMore = ''.obs;
RxList<ReplyItemModel> replyList = <ReplyItemModel>[].obs; RxList<ReplyItemModel> replyList = <ReplyItemModel>[].obs;
@@ -48,27 +48,27 @@ class DynamicDetailController extends GetxController {
Future queryReplyList({reqType = 'init'}) async { Future queryReplyList({reqType = 'init'}) async {
if (reqType == 'init') { if (reqType == 'init') {
currentPage = 0; nextOffset = "";
} }
isLoadingMore = true; isLoadingMore = true;
var res = await ReplyHttp.replyList( var res = await ReplyHttp.replyList(
oid: oid!, oid: oid!,
pageNum: currentPage + 1, nextOffset: nextOffset,
type: type!, type: type!,
sort: _sortType.index, sort: _sortType.index,
); );
isLoadingMore = false; isLoadingMore = false;
if (res['status']) { if (res['status']) {
List<ReplyItemModel> replies = res['data'].replies; List<ReplyItemModel> replies = res['data'].replies;
acount.value = res['data'].page.acount; acount.value = res['data'].cursor.allCount;
nextOffset = res['data'].cursor.paginationReply.nextOffset ?? "";
if (replies.isNotEmpty) { if (replies.isNotEmpty) {
currentPage++;
noMore.value = '加载中...'; noMore.value = '加载中...';
if (replies.length < 20) { if (res['data'].cursor.isEnd == true) {
noMore.value = '没有更多了'; noMore.value = '没有更多了';
} }
} else { } else {
noMore.value = currentPage == 0 ? '还没有评论' : '没有更多了'; noMore.value = nextOffset == "" ? '还没有评论' : '没有更多了';
} }
if (reqType == 'init') { if (reqType == 'init') {
// 添加置顶回复 // 添加置顶回复

View File

@@ -15,7 +15,7 @@ class HtmlRenderController extends GetxController {
RxInt oid = (-1).obs; RxInt oid = (-1).obs;
late Map response; late Map response;
int? floor; int? floor;
int currentPage = 0; String nextOffset = "";
bool isLoadingMore = false; bool isLoadingMore = false;
RxString noMore = ''.obs; RxString noMore = ''.obs;
RxList<ReplyItemModel> replyList = <ReplyItemModel>[].obs; RxList<ReplyItemModel> replyList = <ReplyItemModel>[].obs;
@@ -34,7 +34,7 @@ class HtmlRenderController extends GetxController {
dynamicType = Get.parameters['dynamicType']!; dynamicType = Get.parameters['dynamicType']!;
type = dynamicType == 'picture' ? 11 : 12; type = dynamicType == 'picture' ? 11 : 12;
int defaultReplySortIndex = int defaultReplySortIndex =
setting.get(SettingBoxKey.replySortType, defaultValue: 0) as int; setting.get(SettingBoxKey.replySortType, defaultValue: 0) as int;
if (defaultReplySortIndex == 2) { if (defaultReplySortIndex == 2) {
setting.put(SettingBoxKey.replySortType, 0); setting.put(SettingBoxKey.replySortType, 0);
defaultReplySortIndex = 0; defaultReplySortIndex = 0;
@@ -61,25 +61,25 @@ class HtmlRenderController extends GetxController {
// 请求评论 // 请求评论
Future queryReplyList({reqType = 'init'}) async { Future queryReplyList({reqType = 'init'}) async {
if (reqType == 'init') { if (reqType == 'init') {
currentPage = 0; nextOffset = "";
} }
var res = await ReplyHttp.replyList( var res = await ReplyHttp.replyList(
oid: oid.value, oid: oid.value,
pageNum: currentPage + 1, nextOffset: nextOffset,
type: type, type: type,
sort: _sortType.index, sort: _sortType.index,
); );
if (res['status']) { if (res['status']) {
List<ReplyItemModel> replies = res['data'].replies; List<ReplyItemModel> replies = res['data'].replies;
acount.value = res['data'].page.acount; acount.value = res['data'].cursor.allCount;
nextOffset = res['data'].cursor.paginationReply.nextOffset ?? "";
if (replies.isNotEmpty) { if (replies.isNotEmpty) {
currentPage++;
noMore.value = '加载中...'; noMore.value = '加载中...';
if (replies.length < 20) { if (res['data'].cursor.isEnd == true) {
noMore.value = '没有更多了'; noMore.value = '没有更多了';
} }
} else { } else {
noMore.value = currentPage == 0 ? '还没有评论' : '没有更多了'; noMore.value = nextOffset == "" ? '还没有评论' : '没有更多了';
} }
if (reqType == 'init') { if (reqType == 'init') {
// 添加置顶回复 // 添加置顶回复
@@ -115,7 +115,7 @@ class HtmlRenderController extends GetxController {
} }
sortTypeTitle.value = _sortType.titles; sortTypeTitle.value = _sortType.titles;
sortTypeLabel.value = _sortType.labels; sortTypeLabel.value = _sortType.labels;
currentPage = 0; nextOffset = "";
replyList.clear(); replyList.clear();
queryReplyList(reqType: 'init'); queryReplyList(reqType: 'init');
} }

View File

@@ -23,11 +23,9 @@ class VideoReplyController extends GetxController {
// rpid 请求楼中楼回复 // rpid 请求楼中楼回复
String? rpid; String? rpid;
RxList<ReplyItemModel> replyList = <ReplyItemModel>[].obs; RxList<ReplyItemModel> replyList = <ReplyItemModel>[].obs;
// 当前页 String nextOffset = "";
int currentPage = 0;
bool isLoadingMore = false; bool isLoadingMore = false;
RxString noMore = ''.obs; RxString noMore = ''.obs;
int ps = 20;
RxInt count = 0.obs; RxInt count = 0.obs;
// 当前回复的回复 // 当前回复的回复
ReplyItemModel? currentReplyItem; ReplyItemModel? currentReplyItem;
@@ -57,7 +55,7 @@ class VideoReplyController extends GetxController {
return; return;
} }
if (type == 'init') { if (type == 'init') {
currentPage = 0; nextOffset = '';
noMore.value = ''; noMore.value = '';
} }
if (noMore.value == '没有更多了') { if (noMore.value == '没有更多了') {
@@ -66,29 +64,25 @@ class VideoReplyController extends GetxController {
isLoadingMore = true; isLoadingMore = true;
final res = await ReplyHttp.replyList( final res = await ReplyHttp.replyList(
oid: aid!, oid: aid!,
pageNum: currentPage + 1, nextOffset: nextOffset,
ps: ps,
type: ReplyType.video.index, type: ReplyType.video.index,
sort: _sortType.index, sort: _sortType.index,
); );
isLoadingMore = false; isLoadingMore = false;
if (res['status']) { if (res['status']) {
final List<ReplyItemModel> replies = res['data'].replies; final List<ReplyItemModel> replies = res['data'].replies;
nextOffset = res['data'].cursor.paginationReply.nextOffset ?? "";
if (replies.isNotEmpty) { if (replies.isNotEmpty) {
noMore.value = '加载中...'; noMore.value = '加载中...';
/// 第一页回复数小于20 /// 第一页回复数小于20
if (currentPage == 0 && replies.length < 18) { if (res['data'].cursor.isEnd == true) {
noMore.value = '没有更多了'; noMore.value = '没有更多了';
} }
currentPage++;
if (replyList.length == res['data'].page.acount) {
noMore.value = '没有更多了';
}
} else { } else {
// 未登录状态replies可能返回null // 未登录状态replies可能返回null
noMore.value = currentPage == 0 ? '还没有评论' : '没有更多了'; noMore.value = nextOffset == "" ? '还没有评论' : '没有更多了';
} }
if (type == 'init') { if (type == 'init') {
// 添加置顶回复 // 添加置顶回复
@@ -100,7 +94,7 @@ class VideoReplyController extends GetxController {
} }
} }
replies.insertAll(0, res['data'].topReplies); replies.insertAll(0, res['data'].topReplies);
count.value = res['data'].page.count; count.value = res['data'].cursor.allCount;
replyList.value = replies; replyList.value = replies;
} else { } else {
replyList.addAll(replies); replyList.addAll(replies);
@@ -128,7 +122,7 @@ class VideoReplyController extends GetxController {
} }
sortTypeTitle.value = _sortType.titles; sortTypeTitle.value = _sortType.titles;
sortTypeLabel.value = _sortType.labels; sortTypeLabel.value = _sortType.labels;
currentPage = 0; nextOffset = "";
noMore.value = ''; noMore.value = '';
replyList.clear(); replyList.clear();
queryReplyList(type: 'init'); queryReplyList(type: 'init');