mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
feat: prefetch live room danmu (#4)
This commit is contained in:
@@ -289,6 +289,11 @@ class Api {
|
|||||||
static const String liveRoomInfoH5 =
|
static const String liveRoomInfoH5 =
|
||||||
'${HttpString.liveBaseUrl}/xlive/web-room/v1/index/getH5InfoByRoom';
|
'${HttpString.liveBaseUrl}/xlive/web-room/v1/index/getH5InfoByRoom';
|
||||||
|
|
||||||
|
// 直播间弹幕预获取
|
||||||
|
// roomid roomId
|
||||||
|
static const String liveRoomDmPrefetch =
|
||||||
|
'${HttpString.liveBaseUrl}/xlive/web-room/v1/dM/gethistory';
|
||||||
|
|
||||||
//直播间弹幕密钥获取接口
|
//直播间弹幕密钥获取接口
|
||||||
static const String liveRoomDmToken =
|
static const String liveRoomDmToken =
|
||||||
'${HttpString.liveBaseUrl}/xlive/web-room/v1/index/getDanmuInfo';
|
'${HttpString.liveBaseUrl}/xlive/web-room/v1/index/getDanmuInfo';
|
||||||
|
|||||||
@@ -111,6 +111,21 @@ class LiveHttp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Future liveRoomDanmaPrefetch({roomId}) async {
|
||||||
|
var res = await Request().get(Api.liveRoomDmPrefetch, data: {
|
||||||
|
'roomid': roomId,
|
||||||
|
});
|
||||||
|
if (res.data['code'] == 0) {
|
||||||
|
return {'status': true, 'data': res.data['data']['room']};
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
'status': false,
|
||||||
|
'data': [],
|
||||||
|
'msg': res.data['message'],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static Future liveRoomGetDanmakuToken({roomId}) async {
|
static Future liveRoomGetDanmakuToken({roomId}) async {
|
||||||
var res = await Request().get(Api.liveRoomDmToken, data: {
|
var res = await Request().get(Api.liveRoomDmToken, data: {
|
||||||
'id': roomId,
|
'id': roomId,
|
||||||
|
|||||||
@@ -120,6 +120,24 @@ class LiveRoomController extends GetxController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void liveMsg() {
|
void liveMsg() {
|
||||||
|
LiveHttp.liveRoomDanmaPrefetch(roomId: roomId).then((v) {
|
||||||
|
if (v['status']) {
|
||||||
|
for (var obj in v['data'] as List) {
|
||||||
|
messages.add({
|
||||||
|
'name': obj['user']['base']['name'],
|
||||||
|
'uid': obj['user']['uid'],
|
||||||
|
'text': obj['text'],
|
||||||
|
'emots': obj['emots'],
|
||||||
|
'uemote': obj['emoticon']['emoticon_unique'] != ""
|
||||||
|
? obj['emoticon']
|
||||||
|
: null,
|
||||||
|
});
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback(
|
||||||
|
(_) => scrollToBottom(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
LiveHttp.liveRoomGetDanmakuToken(roomId: roomId).then((v) {
|
LiveHttp.liveRoomGetDanmakuToken(roomId: roomId).then((v) {
|
||||||
if (v['status']) {
|
if (v['status']) {
|
||||||
LiveDanmakuInfo info = v['data'];
|
LiveDanmakuInfo info = v['data'];
|
||||||
@@ -134,7 +152,13 @@ class LiveRoomController extends GetxController {
|
|||||||
msgStream?.addEventListener((obj) {
|
msgStream?.addEventListener((obj) {
|
||||||
if (obj['cmd'] == 'DANMU_MSG') {
|
if (obj['cmd'] == 'DANMU_MSG') {
|
||||||
// logger.i(' 原始弹幕消息 ======> ${jsonEncode(obj)}');
|
// logger.i(' 原始弹幕消息 ======> ${jsonEncode(obj)}');
|
||||||
messages.add(obj);
|
messages.add({
|
||||||
|
'name': obj['info'][0][15]['user']['base']['name'],
|
||||||
|
'uid': obj['info'][0][15]['user']['uid'],
|
||||||
|
'text': obj['info'][1],
|
||||||
|
'emots': jsonDecode(obj['info'][0][15]['extra'])['emots'],
|
||||||
|
'uemote': obj['info'][0][13],
|
||||||
|
});
|
||||||
Map json = jsonDecode(obj['info'][0][15]['extra']);
|
Map json = jsonDecode(obj['info'][0][15]['extra']);
|
||||||
if (showDanmaku) {
|
if (showDanmaku) {
|
||||||
controller?.addItems([
|
controller?.addItems([
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ class _LiveRoomChatState extends State<LiveRoomChat> {
|
|||||||
children: [
|
children: [
|
||||||
TextSpan(
|
TextSpan(
|
||||||
text:
|
text:
|
||||||
'${widget.liveRoomController.messages[index]['info'][0][15]['user']['base']['name']}: ',
|
'${widget.liveRoomController.messages[index]['name']}: ',
|
||||||
style: const TextStyle(
|
style: const TextStyle(
|
||||||
color: Color(0xFFAAAAAA),
|
color: Color(0xFFAAAAAA),
|
||||||
fontSize: 14,
|
fontSize: 14,
|
||||||
@@ -57,9 +57,8 @@ class _LiveRoomChatState extends State<LiveRoomChat> {
|
|||||||
recognizer: TapGestureRecognizer()
|
recognizer: TapGestureRecognizer()
|
||||||
..onTap = () {
|
..onTap = () {
|
||||||
try {
|
try {
|
||||||
dynamic uid =
|
dynamic uid = widget
|
||||||
widget.liveRoomController.messages[index]
|
.liveRoomController.messages[index]['uid'];
|
||||||
['info'][0][15]['user']['uid'];
|
|
||||||
Get.toNamed(
|
Get.toNamed(
|
||||||
'/member?mid=$uid',
|
'/member?mid=$uid',
|
||||||
arguments: {
|
arguments: {
|
||||||
@@ -102,8 +101,8 @@ class _LiveRoomChatState extends State<LiveRoomChat> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TextSpan _buildMsg(obj) {
|
TextSpan _buildMsg(obj) {
|
||||||
dynamic emots = jsonDecode(obj['info'][0][15]['extra'])['emots'];
|
dynamic emots = obj['emots'];
|
||||||
dynamic uemote = obj['info'][0][13];
|
dynamic uemote = obj['uemote'];
|
||||||
List list = [
|
List list = [
|
||||||
if (emots != null) emots.keys,
|
if (emots != null) emots.keys,
|
||||||
if (uemote is Map) uemote['emoticon_unique'].replaceFirst('upower_', '')
|
if (uemote is Map) uemote['emoticon_unique'].replaceFirst('upower_', '')
|
||||||
@@ -117,7 +116,7 @@ class _LiveRoomChatState extends State<LiveRoomChat> {
|
|||||||
}).toList();
|
}).toList();
|
||||||
RegExp regExp = RegExp(list.join('|'));
|
RegExp regExp = RegExp(list.join('|'));
|
||||||
final List<InlineSpan> spanChildren = <InlineSpan>[];
|
final List<InlineSpan> spanChildren = <InlineSpan>[];
|
||||||
(obj['info'][1] as String).splitMapJoin(
|
(obj['text'] as String).splitMapJoin(
|
||||||
regExp,
|
regExp,
|
||||||
onMatch: (Match match) {
|
onMatch: (Match match) {
|
||||||
String key = match[0]!;
|
String key = match[0]!;
|
||||||
@@ -150,7 +149,7 @@ class _LiveRoomChatState extends State<LiveRoomChat> {
|
|||||||
return TextSpan(children: spanChildren);
|
return TextSpan(children: spanChildren);
|
||||||
} else {
|
} else {
|
||||||
return TextSpan(
|
return TextSpan(
|
||||||
text: obj['info'][1],
|
text: obj['text'],
|
||||||
style: const TextStyle(
|
style: const TextStyle(
|
||||||
color: Color(0xFFFFFFFF),
|
color: Color(0xFFFFFFFF),
|
||||||
fontSize: 14,
|
fontSize: 14,
|
||||||
|
|||||||
Reference in New Issue
Block a user