Skip to content

Commit

Permalink
fix: 评论区无法切换最新
Browse files Browse the repository at this point in the history
  • Loading branch information
orz12 committed Aug 12, 2024
1 parent 5c9c2dd commit a669488
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 40 deletions.
2 changes: 1 addition & 1 deletion lib/http/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
static const String replyList = '/x/v2/reply/main';

// 楼中楼
static const String replyReplyList = '/x/v2/reply/reply';
Expand Down
10 changes: 4 additions & 6 deletions lib/http/reply.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import 'init.dart';
class ReplyHttp {
static Future replyList({
required int oid,
required int pageNum,
required String nextOffset,
required int type,
int? ps,
int sort = 1,
}) async {
var res = await Request().get(Api.replyList, data: {
'oid': oid,
'pn': pageNum,
'type': type,
'sort': sort,
'ps': ps ?? 20
'pagination_str': '{"offset":"${nextOffset.replaceAll('"', '\\"')}"}',
'mode': sort + 2, //2:按时间排序;3:按热度排序
});
if (res.data['code'] == 0) {
return {
Expand Down Expand Up @@ -50,7 +48,7 @@ class ReplyHttp {
if (res.data['code'] == 0) {
return {
'status': true,
'data': ReplyData.fromJson(res.data['data']),
'data': ReplyReplyData.fromJson(res.data['data']),
};
} else {
return {
Expand Down
39 changes: 36 additions & 3 deletions lib/models/video/reply/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import 'upper.dart';

class ReplyData {
ReplyData({
this.page,
this.cursor,
this.config,
this.replies,
this.topReplies,
this.upper,
});

ReplyPage? page;
ReplyCursor? cursor;
ReplyConfig? config;
late List<ReplyItemModel>? replies;
late List<ReplyItemModel>? topReplies;
ReplyUpper? upper;

ReplyData.fromJson(Map<String, dynamic> json) {
page = ReplyPage.fromJson(json['page']);
cursor = ReplyCursor.fromJson(json['cursor']);
config = ReplyConfig.fromJson(json['config']);
replies = json['replies'] != null
? List<ReplyItemModel>.from(json['replies']
Expand All @@ -36,3 +36,36 @@ class ReplyData {
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']);
}
}
57 changes: 57 additions & 0 deletions lib/models/video/reply/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,60 @@ class ReplyPage {
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'];
}
}
14 changes: 7 additions & 7 deletions lib/pages/dynamics/detail/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DynamicDetailController extends GetxController {
int? type;
dynamic item;
int? floor;
int currentPage = 0;
String nextOffset = "";
bool isLoadingMore = false;
RxString noMore = ''.obs;
RxList<ReplyItemModel> replyList = <ReplyItemModel>[].obs;
Expand Down Expand Up @@ -48,27 +48,27 @@ class DynamicDetailController extends GetxController {

Future queryReplyList({reqType = 'init'}) async {
if (reqType == 'init') {
currentPage = 0;
nextOffset = "";
}
isLoadingMore = true;
var res = await ReplyHttp.replyList(
oid: oid!,
pageNum: currentPage + 1,
nextOffset: nextOffset,
type: type!,
sort: _sortType.index,
);
isLoadingMore = false;
if (res['status']) {
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) {
currentPage++;
noMore.value = '加载中...';
if (replies.length < 20) {
if (res['data'].cursor.isEnd == true) {
noMore.value = '没有更多了';
}
} else {
noMore.value = currentPage == 0 ? '还没有评论' : '没有更多了';
noMore.value = nextOffset == "" ? '还没有评论' : '没有更多了';
}
if (reqType == 'init') {
// 添加置顶回复
Expand Down
18 changes: 9 additions & 9 deletions lib/pages/html/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class HtmlRenderController extends GetxController {
RxInt oid = (-1).obs;
late Map response;
int? floor;
int currentPage = 0;
String nextOffset = "";
bool isLoadingMore = false;
RxString noMore = ''.obs;
RxList<ReplyItemModel> replyList = <ReplyItemModel>[].obs;
Expand All @@ -34,7 +34,7 @@ class HtmlRenderController extends GetxController {
dynamicType = Get.parameters['dynamicType']!;
type = dynamicType == 'picture' ? 11 : 12;
int defaultReplySortIndex =
setting.get(SettingBoxKey.replySortType, defaultValue: 0) as int;
setting.get(SettingBoxKey.replySortType, defaultValue: 0) as int;
if (defaultReplySortIndex == 2) {
setting.put(SettingBoxKey.replySortType, 0);
defaultReplySortIndex = 0;
Expand All @@ -61,25 +61,25 @@ class HtmlRenderController extends GetxController {
// 请求评论
Future queryReplyList({reqType = 'init'}) async {
if (reqType == 'init') {
currentPage = 0;
nextOffset = "";
}
var res = await ReplyHttp.replyList(
oid: oid.value,
pageNum: currentPage + 1,
nextOffset: nextOffset,
type: type,
sort: _sortType.index,
);
if (res['status']) {
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) {
currentPage++;
noMore.value = '加载中...';
if (replies.length < 20) {
if (res['data'].cursor.isEnd == true) {
noMore.value = '没有更多了';
}
} else {
noMore.value = currentPage == 0 ? '还没有评论' : '没有更多了';
noMore.value = nextOffset == "" ? '还没有评论' : '没有更多了';
}
if (reqType == 'init') {
// 添加置顶回复
Expand Down Expand Up @@ -115,7 +115,7 @@ class HtmlRenderController extends GetxController {
}
sortTypeTitle.value = _sortType.titles;
sortTypeLabel.value = _sortType.labels;
currentPage = 0;
nextOffset = "";
replyList.clear();
queryReplyList(reqType: 'init');
}
Expand Down
22 changes: 8 additions & 14 deletions lib/pages/video/detail/reply/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ class VideoReplyController extends GetxController {
// rpid 请求楼中楼回复
String? rpid;
RxList<ReplyItemModel> replyList = <ReplyItemModel>[].obs;
// 当前页
int currentPage = 0;
String nextOffset = "";
bool isLoadingMore = false;
RxString noMore = ''.obs;
int ps = 20;
RxInt count = 0.obs;
// 当前回复的回复
ReplyItemModel? currentReplyItem;
Expand Down Expand Up @@ -57,7 +55,7 @@ class VideoReplyController extends GetxController {
return;
}
if (type == 'init') {
currentPage = 0;
nextOffset = '';
noMore.value = '';
}
if (noMore.value == '没有更多了') {
Expand All @@ -66,29 +64,25 @@ class VideoReplyController extends GetxController {
isLoadingMore = true;
final res = await ReplyHttp.replyList(
oid: aid!,
pageNum: currentPage + 1,
ps: ps,
nextOffset: nextOffset,
type: ReplyType.video.index,
sort: _sortType.index,
);
isLoadingMore = false;
if (res['status']) {
final List<ReplyItemModel> replies = res['data'].replies;
nextOffset = res['data'].cursor.paginationReply.nextOffset ?? "";
if (replies.isNotEmpty) {
noMore.value = '加载中...';

/// 第一页回复数小于20
if (currentPage == 0 && replies.length < 18) {
if (res['data'].cursor.isEnd == true) {
noMore.value = '没有更多了';
}
currentPage++;

if (replyList.length == res['data'].page.acount) {
noMore.value = '没有更多了';
}
} else {
// 未登录状态replies可能返回null
noMore.value = currentPage == 0 ? '还没有评论' : '没有更多了';
noMore.value = nextOffset == "" ? '还没有评论' : '没有更多了';
}
if (type == 'init') {
// 添加置顶回复
Expand All @@ -100,7 +94,7 @@ class VideoReplyController extends GetxController {
}
}
replies.insertAll(0, res['data'].topReplies);
count.value = res['data'].page.count;
count.value = res['data'].cursor.allCount;
replyList.value = replies;
} else {
replyList.addAll(replies);
Expand Down Expand Up @@ -128,7 +122,7 @@ class VideoReplyController extends GetxController {
}
sortTypeTitle.value = _sortType.titles;
sortTypeLabel.value = _sortType.labels;
currentPage = 0;
nextOffset = "";
noMore.value = '';
replyList.clear();
queryReplyList(type: 'init');
Expand Down

0 comments on commit a669488

Please sign in to comment.