diff --git a/lib/http/api.dart b/lib/http/api.dart index 1cddff09..428204ac 100644 --- a/lib/http/api.dart +++ b/lib/http/api.dart @@ -283,6 +283,8 @@ class Api { static const String liveRoomInfo = '${HttpString.liveBaseUrl}/xlive/web-room/v2/index/getRoomPlayInfo'; + static const String sendLiveMsg = '${HttpString.liveBaseUrl}/msg/send'; + // 直播间详情 H5 static const String liveRoomInfoH5 = '${HttpString.liveBaseUrl}/xlive/web-room/v1/index/getH5InfoByRoom'; diff --git a/lib/http/live.dart b/lib/http/live.dart index 9d00f3d1..f513cfd3 100644 --- a/lib/http/live.dart +++ b/lib/http/live.dart @@ -1,5 +1,7 @@ +import 'package:PiliPalaX/common/constants.dart'; import 'package:PiliPalaX/http/loading_state.dart'; import 'package:PiliPalaX/models/live/danmu_info.dart'; +import 'package:dio/dio.dart'; import '../models/live/item.dart'; import '../models/live/room_info.dart'; import '../models/live/room_info_h5.dart'; @@ -25,6 +27,49 @@ class LiveHttp { } } + static Future sendLiveMsg({ + roomId, + msg, + }) async { + dynamic csrf = await Request.getCsrf(); + var res = await Request().post( + Api.sendLiveMsg, + data: { + 'bubble': 0, + 'msg': msg, + 'color': 16777215, + 'mode': 1, + 'room_type': 0, + 'jumpfrom': 71000, + 'reply_mid': 0, + 'reply_attr': 0, + 'replay_dmid': '', + 'statistics': Constants.statistics, + 'reply_type': 0, + 'reply_uname': '', + 'fontsize': 25, + 'rnd': DateTime.now().millisecondsSinceEpoch ~/ 1000, + 'roomid': roomId, + 'csrf': csrf, + 'csrf_token': csrf, + }, + options: Options( + contentType: Headers.formUrlEncodedContentType, + ), + ); + if (res.data['code'] == 0) { + return { + 'status': true, + 'data': res.data['data'], + }; + } else { + return { + 'status': false, + 'msg': res.data['message'], + }; + } + } + static Future liveRoomInfo({roomId, qn}) async { var res = await Request().get(Api.liveRoomInfo, data: { 'room_id': roomId, diff --git a/lib/pages/live_room/view.dart b/lib/pages/live_room/view.dart index ae4791bf..398b7b25 100644 --- a/lib/pages/live_room/view.dart +++ b/lib/pages/live_room/view.dart @@ -1,8 +1,10 @@ import 'dart:io'; +import 'package:PiliPalaX/http/live.dart'; import 'package:PiliPalaX/pages/live_room/widgets/chat.dart'; import 'package:floating/floating.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:get/get.dart'; import 'package:PiliPalaX/common/widgets/network_img_layer.dart'; import 'package:PiliPalaX/plugin/pl_player/index.dart'; @@ -28,6 +30,9 @@ class _LiveRoomPageState extends State { bool isPlay = true; Floating? floating; + late final _isLogin = GStorage.userInfo.get('userInfoCache') != null; + late final _ctr = TextEditingController(); + void playCallBack() { plPlayerController?.play(); } @@ -54,6 +59,7 @@ class _LiveRoomPageState extends State { PlPlayerController.setPlayCallBack(null); floating?.dispose(); plPlayerController?.dispose(); + _ctr.dispose(); super.dispose(); } @@ -232,8 +238,61 @@ class _LiveRoomPageState extends State { ), ), Expanded( - child: LiveRoomChat( - roomId: int.parse(Get.parameters['roomid']!))) + child: LiveRoomChat( + roomId: int.parse(Get.parameters['roomid']!), + ), + ), + Container( + padding: EdgeInsets.only( + left: 16, + top: 10, + right: 16, + bottom: 25 + MediaQuery.of(context).padding.bottom, + ), + decoration: BoxDecoration( + borderRadius: BorderRadius.only( + topLeft: Radius.circular(20), + topRight: Radius.circular(20), + ), + border: Border( + top: BorderSide(color: Color(0x1AFFFFFF)), + ), + color: Color(0x1AFFFFFF), + ), + child: Row( + children: [ + Expanded( + child: TextField( + controller: _ctr, + textInputAction: TextInputAction.send, + onSubmitted: (value) { + if (value.isNotEmpty) { + _onSendMsg(value); + } + }, + decoration: InputDecoration( + border: InputBorder.none, + hintText: '发送弹幕', + hintStyle: TextStyle( + color: Theme.of(context).colorScheme.outline, + ), + ), + ), + ), + IconButton( + onPressed: () { + if (_ctr.text.isNotEmpty) { + _onSendMsg(_ctr.text); + } + }, + icon: Icon( + Icons.send, + color: Colors.white, + ), + ), + ], + ), + ), ], ), ], @@ -249,4 +308,19 @@ class _LiveRoomPageState extends State { return childWhenDisabled; } } + + void _onSendMsg(msg) async { + if (!_isLogin) { + SmartDialog.showToast('未登录'); + return; + } + dynamic res = await LiveHttp.sendLiveMsg( + roomId: _liveRoomController.roomId, msg: msg); + if (res['status']) { + _ctr.clear(); + SmartDialog.showToast('发送成功'); + } else { + SmartDialog.showToast(res['msg']); + } + } } diff --git a/lib/pages/live_room/widgets/chat.dart b/lib/pages/live_room/widgets/chat.dart index 397fc2d1..e1575cd4 100644 --- a/lib/pages/live_room/widgets/chat.dart +++ b/lib/pages/live_room/widgets/chat.dart @@ -27,7 +27,7 @@ class _LiveRoomChatState extends State { padding: EdgeInsets.only( left: 10, right: 10, - bottom: MediaQuery.of(context).padding.bottom, + bottom: 5, ), child: SingleChildScrollView( child: Column(