feat: 私信页面表情面板

This commit is contained in:
guozhigq
2024-02-25 22:48:02 +08:00
parent b2a4c54565
commit e2489ef0e3
7 changed files with 377 additions and 119 deletions

View File

@ -496,4 +496,7 @@ class Api {
/// 已读标记 /// 已读标记
static const String ackSessionMsg = static const String ackSessionMsg =
'${HttpString.tUrl}/session_svr/v1/session_svr/update_ack'; '${HttpString.tUrl}/session_svr/v1/session_svr/update_ack';
/// 发送私信
static const String sendMsg = '${HttpString.tUrl}/web_im/v1/web_im/send_msg';
} }

View File

@ -1,3 +1,4 @@
import 'dart:math';
import '../models/msg/account.dart'; import '../models/msg/account.dart';
import '../models/msg/session.dart'; import '../models/msg/session.dart';
import '../utils/wbi_sign.dart'; import '../utils/wbi_sign.dart';
@ -22,10 +23,18 @@ class MsgHttp {
Map signParams = await WbiSign().makSign(params); Map signParams = await WbiSign().makSign(params);
var res = await Request().get(Api.sessionList, data: signParams); var res = await Request().get(Api.sessionList, data: signParams);
if (res.data['code'] == 0) { if (res.data['code'] == 0) {
return { try {
'status': true, return {
'data': SessionDataModel.fromJson(res.data['data']), 'status': true,
}; 'data': SessionDataModel.fromJson(res.data['data']),
};
} catch (err) {
return {
'status': false,
'date': [],
'msg': err.toString(),
};
}
} else { } else {
return { return {
'status': false, 'status': false,
@ -42,12 +51,16 @@ class MsgHttp {
'mobi_app': 'web', 'mobi_app': 'web',
}); });
if (res.data['code'] == 0) { if (res.data['code'] == 0) {
return { try {
'status': true, return {
'data': res.data['data'] 'status': true,
.map<AccountListModel>((e) => AccountListModel.fromJson(e)) 'data': res.data['data']
.toList(), .map<AccountListModel>((e) => AccountListModel.fromJson(e))
}; .toList(),
};
} catch (err) {
print('err🔟: $err');
}
} else { } else {
return { return {
'status': false, 'status': false,
@ -118,4 +131,93 @@ class MsgHttp {
}; };
} }
} }
// 发送私信
static Future sendMsg({
int? senderUid,
int? receiverId,
int? receiverType,
int? msgType,
dynamic content,
}) async {
String csrf = await Request.getCsrf();
Map<String, dynamic> params = await WbiSign().makSign({
'msg[sender_uid]': senderUid,
'msg[receiver_id]': receiverId,
'msg[receiver_type]': receiverType ?? 1,
'msg[msg_type]': msgType ?? 1,
'msg[msg_status]': 0,
'msg[dev_id]': getDevId(),
'msg[timestamp]': DateTime.now().millisecondsSinceEpoch ~/ 1000,
'msg[new_face_version]': 0,
'msg[content]': content,
'from_firework': 0,
'build': 0,
'mobi_app': 'web',
'csrf_token': csrf,
'csrf': csrf,
});
var res =
await Request().post(Api.sendMsg, queryParameters: <String, dynamic>{
...params,
'csrf_token': csrf,
'csrf': csrf,
}, data: {
'w_sender_uid': params['msg[sender_uid]'],
'w_receiver_id': params['msg[receiver_id]'],
'w_dev_id': params['msg[dev_id]'],
'w_rid': params['w_rid'],
'wts': params['wts'],
'csrf_token': csrf,
'csrf': csrf,
});
if (res.data['code'] == 0) {
return {
'status': true,
'data': res.data['data'],
};
} else {
return {
'status': false,
'date': [],
'msg': "message: ${res.data['message']},"
" msg: ${res.data['msg']},"
" code: ${res.data['code']}",
};
}
}
static String getDevId() {
final List<String> b = [
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F'
];
final List<String> s = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".split('');
for (int i = 0; i < s.length; i++) {
if ('-' == s[i] || '4' == s[i]) {
continue;
}
final int randomInt = Random().nextInt(16);
if ('x' == s[i]) {
s[i] = b[randomInt];
} else {
s[i] = b[3 & randomInt | 8];
}
}
return s.join();
}
} }

View File

@ -8,7 +8,7 @@ class SessionDataModel {
this.hasMore, this.hasMore,
}); });
List? sessionList; List<SessionList>? sessionList;
int? hasMore; int? hasMore;
SessionDataModel.fromJson(Map<String, dynamic> json) { SessionDataModel.fromJson(Map<String, dynamic> json) {
@ -121,35 +121,37 @@ class LastMsg {
this.msgKey, this.msgKey,
this.msgStatus, this.msgStatus,
this.notifyCode, this.notifyCode,
this.newFaceVersion, // this.newFaceVersion,
}); });
int? senderIid; int? senderIid;
int? receiverType; int? receiverType;
int? receiverId; int? receiverId;
int? msgType; int? msgType;
Map? content; dynamic content;
int? msgSeqno; int? msgSeqno;
int? timestamp; int? timestamp;
String? atUids; String? atUids;
int? msgKey; int? msgKey;
int? msgStatus; int? msgStatus;
String? notifyCode; String? notifyCode;
int? newFaceVersion; // int? newFaceVersion;
LastMsg.fromJson(Map<String, dynamic> json) { LastMsg.fromJson(Map<String, dynamic> json) {
senderIid = json['sender_uid']; senderIid = json['sender_uid'];
receiverType = json['receiver_type']; receiverType = json['receiver_type'];
receiverId = json['receiver_id']; receiverId = json['receiver_id'];
msgType = json['msg_type']; msgType = json['msg_type'];
content = jsonDecode(json['content']); content = json['content'] != null && json['content'] != ''
? jsonDecode(json['content'])
: '';
msgSeqno = json['msg_seqno']; msgSeqno = json['msg_seqno'];
timestamp = json['timestamp']; timestamp = json['timestamp'];
atUids = json['at_uids']; atUids = json['at_uids'];
msgKey = json['msg_key']; msgKey = json['msg_key'];
msgStatus = json['msg_status']; msgStatus = json['msg_status'];
notifyCode = json['notify_code']; notifyCode = json['notify_code'];
newFaceVersion = json['new_face_version']; // newFaceVersion = json['new_face_version'];
} }
} }
@ -214,7 +216,9 @@ class MessageItem {
receiverId = json['receiver_id']; receiverId = json['receiver_id'];
// 1 文本 2 图片 18 系统提示 10 系统通知 5 撤回的消息 // 1 文本 2 图片 18 系统提示 10 系统通知 5 撤回的消息
msgType = json['msg_type']; msgType = json['msg_type'];
content = jsonDecode(json['content']); content = json['content'] != null && json['content'] != ''
? jsonDecode(json['content'])
: '';
msgSeqno = json['msg_seqno']; msgSeqno = json['msg_seqno'];
timestamp = json['timestamp']; timestamp = json['timestamp'];
atUids = json['at_uids']; atUids = json['at_uids'];

View File

@ -108,8 +108,8 @@ class _WhisperPageState extends State<WhisperPage> {
future: _futureBuilderFuture, future: _futureBuilderFuture,
builder: (context, snapshot) { builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) { if (snapshot.connectionState == ConnectionState.done) {
Map data = snapshot.data as Map; Map? data = snapshot.data;
if (data['status']) { if (data != null && data['status']) {
RxList sessionList = _whisperController.sessionList; RxList sessionList = _whisperController.sessionList;
return Obx( return Obx(
() => sessionList.isEmpty () => sessionList.isEmpty
@ -162,20 +162,26 @@ class _WhisperPageState extends State<WhisperPage> {
title: Text( title: Text(
sessionList[i].accountInfo.name), sessionList[i].accountInfo.name),
subtitle: Text( subtitle: Text(
sessionList[i] sessionList[i].lastMsg.content !=
.lastMsg null &&
.content['text'] ?? sessionList[i]
sessionList[i] .lastMsg
.lastMsg .content !=
.content['content'] ?? ''
sessionList[i] ? (sessionList[i]
.lastMsg
.content['title'] ??
sessionList[i]
.lastMsg .lastMsg
.content[ .content['text'] ??
'reply_content'] ?? sessionList[i]
'', .lastMsg
.content['content'] ??
sessionList[i]
.lastMsg
.content['title'] ??
sessionList[i]
.lastMsg
.content[
'reply_content'])
: '不支持的消息类型',
maxLines: 1, maxLines: 1,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
style: Theme.of(context) style: Theme.of(context)
@ -212,7 +218,9 @@ class _WhisperPageState extends State<WhisperPage> {
); );
} else { } else {
// 请求错误 // 请求错误
return const SizedBox(); return Center(
child: Text(data?['msg'] ?? '请求异常'),
);
} }
} else { } else {
// 骨架屏 // 骨架屏

View File

@ -1,7 +1,11 @@
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:hive/hive.dart';
import 'package:pilipala/http/msg.dart'; import 'package:pilipala/http/msg.dart';
import 'package:pilipala/models/msg/session.dart'; import 'package:pilipala/models/msg/session.dart';
import '../../utils/feed_back.dart';
import '../../utils/storage.dart';
class WhisperDetailController extends GetxController { class WhisperDetailController extends GetxController {
late int talkerId; late int talkerId;
@ -11,6 +15,8 @@ class WhisperDetailController extends GetxController {
RxList<MessageItem> messageList = <MessageItem>[].obs; RxList<MessageItem> messageList = <MessageItem>[].obs;
//表情转换图片规则 //表情转换图片规则
List<dynamic>? eInfos; List<dynamic>? eInfos;
final TextEditingController replyContentController = TextEditingController();
Box userInfoCache = GStrorage.userInfo;
@override @override
void onInit() { void onInit() {
@ -42,14 +48,34 @@ class WhisperDetailController extends GetxController {
if (messageList.isEmpty) { if (messageList.isEmpty) {
return; return;
} }
var res = await MsgHttp.ackSessionMsg( await MsgHttp.ackSessionMsg(
talkerId: talkerId, talkerId: talkerId,
ackSeqno: messageList.last.msgSeqno, ackSeqno: messageList.last.msgSeqno,
); );
if (res['status']) { }
SmartDialog.showToast("已读成功");
Future sendMsg() async {
feedBack();
String message = replyContentController.text;
final userInfo = userInfoCache.get('userInfoCache');
if (userInfo == null) {
SmartDialog.showToast('请先登录');
return;
}
if (message == '') {
SmartDialog.showToast('请输入内容');
return;
}
var result = await MsgHttp.sendMsg(
senderUid: userInfo.mid,
receiverId: int.parse(mid),
content: {'content': message},
msgType: 1,
);
if (result['status']) {
SmartDialog.showToast('发送成功');
} else { } else {
SmartDialog.showToast(res['msg']); SmartDialog.showToast(result['msg']);
} }
} }
} }

View File

@ -1,9 +1,12 @@
import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:hive/hive.dart';
import 'package:pilipala/common/widgets/network_img_layer.dart'; import 'package:pilipala/common/widgets/network_img_layer.dart';
import 'package:pilipala/pages/emote/index.dart';
import 'package:pilipala/pages/whisper_detail/controller.dart'; import 'package:pilipala/pages/whisper_detail/controller.dart';
import 'package:pilipala/utils/feed_back.dart'; import 'package:pilipala/utils/feed_back.dart';
import '../../utils/storage.dart';
import 'widget/chat_item.dart'; import 'widget/chat_item.dart';
class WhisperDetailPage extends StatefulWidget { class WhisperDetailPage extends StatefulWidget {
@ -13,15 +16,63 @@ class WhisperDetailPage extends StatefulWidget {
State<WhisperDetailPage> createState() => _WhisperDetailPageState(); State<WhisperDetailPage> createState() => _WhisperDetailPageState();
} }
class _WhisperDetailPageState extends State<WhisperDetailPage> { class _WhisperDetailPageState extends State<WhisperDetailPage>
with WidgetsBindingObserver {
final WhisperDetailController _whisperDetailController = final WhisperDetailController _whisperDetailController =
Get.put(WhisperDetailController()); Get.put(WhisperDetailController());
late Future _futureBuilderFuture; late Future _futureBuilderFuture;
late TextEditingController _replyContentController;
final FocusNode replyContentFocusNode = FocusNode();
final _debouncer = Debouncer(milliseconds: 200); // 设置延迟时间
late double emoteHeight = 0.0;
double keyboardHeight = 0.0; // 键盘高度
String toolbarType = 'input';
Box userInfoCache = GStrorage.userInfo;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
WidgetsBinding.instance.addObserver(this);
_futureBuilderFuture = _whisperDetailController.querySessionMsg(); _futureBuilderFuture = _whisperDetailController.querySessionMsg();
_replyContentController = _whisperDetailController.replyContentController;
_focuslistener();
}
_focuslistener() {
replyContentFocusNode.addListener(() {
if (replyContentFocusNode.hasFocus) {
setState(() {
toolbarType = 'input';
});
}
});
}
@override
void didChangeMetrics() {
super.didChangeMetrics();
WidgetsBinding.instance.addPostFrameCallback((_) {
// 键盘高度
final viewInsets = EdgeInsets.fromViewPadding(
View.of(context).viewInsets, View.of(context).devicePixelRatio);
_debouncer.run(() {
if (mounted) {
if (keyboardHeight == 0) {
setState(() {
emoteHeight = keyboardHeight =
keyboardHeight == 0.0 ? viewInsets.bottom : keyboardHeight;
});
}
}
});
});
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
replyContentFocusNode.removeListener(() {});
super.dispose();
} }
@override @override
@ -89,55 +140,63 @@ class _WhisperDetailPageState extends State<WhisperDetailPage> {
), ),
), ),
), ),
body: FutureBuilder( body: GestureDetector(
future: _futureBuilderFuture, onTap: () {
builder: (BuildContext context, snapshot) { FocusScope.of(context).unfocus();
if (snapshot.connectionState == ConnectionState.done) { setState(() {
if (snapshot.data == null) { keyboardHeight = 0;
return const SizedBox(); });
}
final Map data = snapshot.data as Map;
if (data['status']) {
List messageList = _whisperDetailController.messageList;
return Obx(
() => messageList.isEmpty
? const SizedBox()
: ListView.builder(
itemCount: messageList.length,
shrinkWrap: true,
reverse: true,
itemBuilder: (_, int i) {
if (i == 0) {
return Column(
children: [
ChatItem(
item: messageList[i],
e_infos: _whisperDetailController.eInfos),
const SizedBox(height: 12),
],
);
} else {
return ChatItem(
item: messageList[i],
e_infos: _whisperDetailController.eInfos);
}
},
),
);
} else {
// 请求错误
return const SizedBox();
}
} else {
// 骨架屏
return const SizedBox();
}
}, },
child: FutureBuilder(
future: _futureBuilderFuture,
builder: (BuildContext context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.data == null) {
return const SizedBox();
}
final Map data = snapshot.data as Map;
if (data['status']) {
List messageList = _whisperDetailController.messageList;
return Obx(
() => messageList.isEmpty
? const SizedBox()
: ListView.builder(
itemCount: messageList.length,
shrinkWrap: true,
reverse: true,
itemBuilder: (_, int i) {
if (i == 0) {
return Column(
children: [
ChatItem(
item: messageList[i],
e_infos: _whisperDetailController.eInfos),
const SizedBox(height: 12),
],
);
} else {
return ChatItem(
item: messageList[i],
e_infos: _whisperDetailController.eInfos);
}
},
),
);
} else {
// 请求错误
return const SizedBox();
}
} else {
// 骨架屏
return const SizedBox();
}
},
),
), ),
// resizeToAvoidBottomInset: true, // resizeToAvoidBottomInset: true,
bottomNavigationBar: Container( bottomNavigationBar: Container(
width: double.infinity, width: double.infinity,
height: MediaQuery.of(context).padding.bottom + 70, height: MediaQuery.of(context).padding.bottom + 70 + keyboardHeight,
padding: EdgeInsets.only( padding: EdgeInsets.only(
left: 8, left: 8,
right: 12, right: 12,
@ -152,48 +211,102 @@ class _WhisperDetailPageState extends State<WhisperDetailPage> {
), ),
), ),
), ),
child: Row( child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
// IconButton( Row(
// onPressed: () {}, mainAxisAlignment: MainAxisAlignment.center,
// icon: Icon( crossAxisAlignment: CrossAxisAlignment.start,
// Icons.add_circle_outline, children: [
// color: Theme.of(context).colorScheme.outline, // IconButton(
// ), // onPressed: () {},
// ), // icon: Icon(
IconButton( // Icons.add_circle_outline,
onPressed: () {}, // color: Theme.of(context).colorScheme.outline,
icon: Icon( // ),
Icons.emoji_emotions_outlined, // ),
color: Theme.of(context).colorScheme.outline, IconButton(
), onPressed: () {
), // if (toolbarType == 'input') {
Expanded( // setState(() {
child: Container( // toolbarType = 'emote';
height: 45, // });
decoration: BoxDecoration( // }
color: // FocusScope.of(context).unfocus();
Theme.of(context).colorScheme.primary.withOpacity(0.08), },
borderRadius: BorderRadius.circular(40.0), icon: Icon(
), Icons.emoji_emotions_outlined,
child: TextField( color: Theme.of(context).colorScheme.outline,
readOnly: true,
style: Theme.of(context).textTheme.titleMedium,
decoration: const InputDecoration(
border: InputBorder.none, // 移除默认边框
hintText: '开发中 ...', // 提示文本
contentPadding: EdgeInsets.symmetric(
horizontal: 16.0, vertical: 12.0), // 内边距
), ),
), ),
Expanded(
child: Container(
height: 45,
decoration: BoxDecoration(
color: Theme.of(context)
.colorScheme
.primary
.withOpacity(0.08),
borderRadius: BorderRadius.circular(40.0),
),
child: TextField(
readOnly: true,
style: Theme.of(context).textTheme.titleMedium,
controller: _replyContentController,
autofocus: false,
focusNode: replyContentFocusNode,
decoration: const InputDecoration(
border: InputBorder.none, // 移除默认边框
hintText: '开发中 ...', // 提示文本
contentPadding: EdgeInsets.symmetric(
horizontal: 16.0, vertical: 12.0), // 内边距
),
),
),
),
IconButton(
// onPressed: _whisperDetailController.sendMsg,
onPressed: null,
icon: Icon(
Icons.send,
color: Theme.of(context).colorScheme.outline,
),
),
// const SizedBox(width: 16),
],
),
AnimatedSize(
curve: Curves.easeInOut,
duration: const Duration(milliseconds: 300),
child: SizedBox(
width: double.infinity,
height: toolbarType == 'input' ? keyboardHeight : emoteHeight,
child: EmotePanel(
onChoose: (package, emote) => {},
),
), ),
), ),
const SizedBox(width: 16),
], ],
), ),
), ),
); );
} }
} }
typedef DebounceCallback = void Function();
class Debouncer {
DebounceCallback? callback;
final int? milliseconds;
Timer? _timer;
Debouncer({this.milliseconds});
run(DebounceCallback callback) {
if (_timer != null) {
_timer!.cancel();
}
_timer = Timer(Duration(milliseconds: milliseconds!), () {
callback();
});
}
}

View File

@ -204,7 +204,7 @@ class ChatItem extends StatelessWidget {
final int cid = await SearchHttp.ab2c(bvid: bvid); final int cid = await SearchHttp.ab2c(bvid: bvid);
final String heroTag = Utils.makeHeroTag(bvid); final String heroTag = Utils.makeHeroTag(bvid);
SmartDialog.dismiss<dynamic>().then( SmartDialog.dismiss<dynamic>().then(
(e) => Get.toNamed<dynamic>('/video?bvid=$bvid&cid=$cid', (e) => Get.toNamed<dynamic>('/video?bvid=$bvid&cid=$cid',
arguments: <String, String?>{ arguments: <String, String?>{
'pic': content['thumb'], 'pic': content['thumb'],
'heroTag': heroTag, 'heroTag': heroTag,
@ -352,7 +352,9 @@ class ChatItem extends StatelessWidget {
)); ));
default: default:
return Text( return Text(
content['content'] ?? content.toString(), content != null && content != ''
? (content['content'] ?? content.toString())
: '不支持的消息类型',
style: TextStyle( style: TextStyle(
letterSpacing: 0.6, letterSpacing: 0.6,
height: 1.5, height: 1.5,