diff --git a/lib/http/api.dart b/lib/http/api.dart index 1614f70c8..56c64c7fc 100644 --- a/lib/http/api.dart +++ b/lib/http/api.dart @@ -475,11 +475,11 @@ class Api { // 获取未读私信数 // https://api.vc.bilibili.com/session_svr/v1/session_svr/single_unread - // static const String msgUnread = - // '${HttpString.tUrl}/session_svr/v1/session_svr/single_unread'; + static const String msgUnread = + '${HttpString.tUrl}/session_svr/v1/session_svr/single_unread'; // 获取消息中心未读信息 - // static const String msgFeedUnread = '/x/msgfeed/unread'; + static const String msgFeedUnread = '/x/msgfeed/unread'; //https://api.bilibili.com/x/msgfeed/reply?platform=web&build=0&mobi_app=web static const String msgFeedReply = '/x/msgfeed/reply'; //https://api.bilibili.com/x/msgfeed/at?platform=web&build=0&mobi_app=web diff --git a/lib/http/msg.dart b/lib/http/msg.dart index eede587d9..2035d6c16 100644 --- a/lib/http/msg.dart +++ b/lib/http/msg.dart @@ -9,6 +9,8 @@ import 'package:PiliPlus/models_new/msg/msg_like/data.dart'; import 'package:PiliPlus/models_new/msg/msg_reply/data.dart'; import 'package:PiliPlus/models_new/msg/msg_sys/data.dart'; import 'package:PiliPlus/models_new/msg/session_ss/data.dart'; +import 'package:PiliPlus/models_new/msgfeed_unread/data.dart'; +import 'package:PiliPlus/models_new/single_unread/data.dart'; import 'package:PiliPlus/models_new/upload_bfs/data.dart'; import 'package:PiliPlus/utils/storage.dart'; import 'package:PiliPlus/utils/wbi_sign.dart'; @@ -556,4 +558,43 @@ class MsgHttp { return Error(res.data['message']); } } + + static Future msgUnread() async { + var res = await Request().get( + Api.msgUnread, + queryParameters: { + 'build': 0, + 'mobi_app': 'web', + 'unread_type': 0, + 'web_location': 333.1365, + }, + ); + if (res.data['code'] == 0) { + return { + 'status': true, + 'data': SingleUnreadData.fromJson(res.data['data']) + }; + } else { + return {'status': false, 'data': res.data['message']}; + } + } + + static Future msgFeedUnread() async { + var res = await Request().get( + Api.msgFeedUnread, + queryParameters: { + 'build': 0, + 'mobi_app': 'web', + 'web_location': 333.1365, + }, + ); + if (res.data['code'] == 0) { + return { + 'status': true, + 'data': MsgFeedUnreadData.fromJson(res.data['data']) + }; + } else { + return {'status': false, 'data': res.data['message']}; + } + } } diff --git a/lib/models_new/msgfeed_unread/data.dart b/lib/models_new/msgfeed_unread/data.dart new file mode 100644 index 000000000..ac0536801 --- /dev/null +++ b/lib/models_new/msgfeed_unread/data.dart @@ -0,0 +1,42 @@ +class MsgFeedUnreadData { + int at; + int coin; + int danmu; + int favorite; + int like; + int recvLike; + int recvReply; + int reply; + int sysMsg; + int sysMsgStyle; + int up; + + MsgFeedUnreadData({ + required this.at, + required this.coin, + required this.danmu, + required this.favorite, + required this.like, + required this.recvLike, + required this.recvReply, + required this.reply, + required this.sysMsg, + required this.sysMsgStyle, + required this.up, + }); + + factory MsgFeedUnreadData.fromJson(Map json) => + MsgFeedUnreadData( + at: json['at'] ?? 0, + coin: json['coin'] ?? 0, + danmu: json['danmu'] ?? 0, + favorite: json['favorite'] ?? 0, + like: json['like'] ?? 0, + recvLike: json['recv_like'] ?? 0, + recvReply: json['recv_reply'] ?? 0, + reply: json['reply'] ?? 0, + sysMsg: json['sys_msg'] ?? 0, + sysMsgStyle: json['sys_msg_style'] ?? 0, + up: json['up'] ?? 0, + ); +} diff --git a/lib/models_new/single_unread/data.dart b/lib/models_new/single_unread/data.dart new file mode 100644 index 000000000..21680e6bd --- /dev/null +++ b/lib/models_new/single_unread/data.dart @@ -0,0 +1,33 @@ +class SingleUnreadData { + int unfollowUnread; + int followUnread; + int unfollowPushMsg; + int dustbinPushMsg; + int dustbinUnread; + int bizMsgUnfollowUnread; + int bizMsgFollowUnread; + int customUnread; + + SingleUnreadData({ + required this.unfollowUnread, + required this.followUnread, + required this.unfollowPushMsg, + required this.dustbinPushMsg, + required this.dustbinUnread, + required this.bizMsgUnfollowUnread, + required this.bizMsgFollowUnread, + required this.customUnread, + }); + + factory SingleUnreadData.fromJson(Map json) => + SingleUnreadData( + unfollowUnread: json['unfollow_unread'] ?? 0, + followUnread: json['follow_unread'] ?? 0, + unfollowPushMsg: json['unfollow_push_msg'] ?? 0, + dustbinPushMsg: json['dustbin_push_msg'] ?? 0, + dustbinUnread: json['dustbin_unread'] ?? 0, + bizMsgUnfollowUnread: json['biz_msg_unfollow_unread'] ?? 0, + bizMsgFollowUnread: json['biz_msg_follow_unread'] ?? 0, + customUnread: json['custom_unread'] ?? 0, + ); +} diff --git a/lib/pages/main/controller.dart b/lib/pages/main/controller.dart index 29ae13120..99fe0dbf2 100644 --- a/lib/pages/main/controller.dart +++ b/lib/pages/main/controller.dart @@ -2,10 +2,12 @@ import 'dart:async'; import 'dart:math' show max; import 'package:PiliPlus/grpc/dyn.dart'; -import 'package:PiliPlus/grpc/im.dart'; +import 'package:PiliPlus/http/msg.dart'; import 'package:PiliPlus/models/common/dynamic/dynamic_badge_mode.dart'; import 'package:PiliPlus/models/common/msg/msg_unread_type.dart'; import 'package:PiliPlus/models/common/nav_bar_config.dart'; +import 'package:PiliPlus/models_new/msgfeed_unread/data.dart'; +import 'package:PiliPlus/models_new/single_unread/data.dart'; import 'package:PiliPlus/utils/storage.dart'; import 'package:PiliPlus/utils/utils.dart'; import 'package:flutter/material.dart'; @@ -83,45 +85,65 @@ class MainController extends GetxController } } - Future queryUnreadMsg() async { - if (!isLogin.value || homeIndex == -1 || msgUnReadTypes.isEmpty) { - msgUnReadCount.value = ''; - return; + Future _msgUnread() async { + if (msgUnReadTypes.contains(MsgUnReadType.pm)) { + var res = await MsgHttp.msgUnread(); + if (res['status']) { + SingleUnreadData data = res['data']; + return data.followUnread + + data.unfollowUnread + + data.bizMsgFollowUnread + + data.bizMsgUnfollowUnread + + data.unfollowPushMsg + + data.customUnread; + } } + return 0; + } + Future _msgFeedUnread() async { int count = 0; - final res = await ImGrpc.getTotalUnread(); - if (res.isSuccess) { - final data = res.data; - if (msgUnReadTypes.length == MsgUnReadType.values.length) { - count = data.hasTotalUnread() ? data.totalUnread : 0; - } else { - final msgUnread = data.msgFeedUnread.unread; - for (final item in msgUnReadTypes) { + var remainTypes = Set.from(msgUnReadTypes) + ..remove(MsgUnReadType.pm); + if (remainTypes.isNotEmpty) { + var res = await MsgHttp.msgFeedUnread(); + if (res['status']) { + MsgFeedUnreadData data = res['data']; + for (var item in remainTypes) { switch (item) { case MsgUnReadType.pm: - final pmUnread = data.sessionSingleUnread; - count += (pmUnread.followUnread + - pmUnread.unfollowUnread + - pmUnread.dustbinUnread) - .toInt(); break; case MsgUnReadType.reply: - count += msgUnread['reply']?.toInt() ?? 0; + count += data.reply; break; case MsgUnReadType.at: - count += msgUnread['at']?.toInt() ?? 0; + count += data.at; break; case MsgUnReadType.like: - count += msgUnread['like']?.toInt() ?? 0; + count += data.like; break; case MsgUnReadType.sysMsg: - count += msgUnread['sys_msg']?.toInt() ?? 0; + count += data.sysMsg; break; } } } } + return count; + } + + Future queryUnreadMsg([bool isChangeType = false]) async { + if (!isLogin.value || + homeIndex == -1 || + msgUnReadTypes.isEmpty || + msgBadgeMode == DynamicBadgeMode.hidden) { + msgUnReadCount.value = ''; + return; + } + + var res = await Future.wait([_msgUnread(), _msgFeedUnread()]); + + int count = res.fold(0, (prev, e) => prev + e); final countStr = count == 0 ? '' @@ -129,7 +151,9 @@ class MainController extends GetxController ? '99+' : count.toString(); if (msgUnReadCount.value == countStr) { - msgUnReadCount.refresh(); + if (isChangeType) { + msgUnReadCount.refresh(); + } } else { msgUnReadCount.value = countStr; } diff --git a/lib/pages/setting/widgets/model.dart b/lib/pages/setting/widgets/model.dart index e8b4fe1fe..fb99d86d1 100644 --- a/lib/pages/setting/widgets/model.dart +++ b/lib/pages/setting/widgets/model.dart @@ -373,7 +373,7 @@ List get styleSettings => [ MainController mainController = Get.put(MainController()) ..msgBadgeMode = DynamicBadgeMode.values[result.index]; if (mainController.msgBadgeMode != DynamicBadgeMode.hidden) { - mainController.queryUnreadMsg(); + mainController.queryUnreadMsg(true); } else { mainController.msgUnReadCount.value = ''; }