import 'dart:typed_data'; import 'dart:convert'; import 'package:chat/data/UserData.dart'; import 'package:chat/data/constants.dart'; import 'package:chat/generated/i18n.dart'; import 'package:chat/proto/chat.pb.dart'; import 'package:chat/proto/chat.pbenum.dart'; import 'package:chat/proto/transhousekeeper.pb.dart'; import 'package:chat/utils/date_utils.dart'; import 'package:chat/utils/file_cache_mgr.dart'; import 'package:chat/utils/sql_util.dart'; import 'package:chat/data/group_data_mgr.dart'; import 'package:chat/utils/upload_util.dart'; class MsgState { static const int None = 0; //默认 static const int UnUpload = 1; //未上传 static const int Uploading = 2; //上传中 static const int UploadFailed = 3; //上传失败 static const int Uploaded = 4; //已上传 static const int Sending = 5; //发送中 static const int SendingFailed = 6; //发送失败, static const int SendingSuccess = 7; //发送成功 static const int Downloading = 9; //下载中 static const int DownloadFailed = 10; //下载失败 static const int DownloadSuccess = 11; //下载成功 } class MsgModel { int transTag; //翻译类型标记 int channelType; //群聊还是会话 int from; int friendId; int msgType; List msgContent; int time; //消息的时间 int sessionId; //会话id int readState; //0代表已显示 String extraFile; //下载文件的识别码 int extraInfo; //表示语音的长度信息 List translateContent; //腾讯翻译 List enTranslateContent; //google翻译 String localFile; //文件的本地路径 int state; //发送状态0默认,1代表未上传 2代表上传中,3代表已上传,4代表发送中,5发送失败,6发送成功 int soundListened; //语音是否听过0没有,1有 List refMsgContent; //引用的内容 List altUsers; //@的用户 MsgModel(this.from, this.friendId, this.msgType, this.msgContent, this.time, this.sessionId, {this.extraFile, this.extraInfo, this.transTag = 0, this.translateContent, this.enTranslateContent, this.channelType = 0, int readState = 0, this.localFile, this.state = MsgState.None, this.soundListened = 0, this.refMsgContent, this.altUsers}); copy() { return MsgModel(this.from, this.friendId, this.msgType, this.msgContent, this.time, this.sessionId, extraFile: this.extraFile, transTag: this.transTag, translateContent: this.translateContent, enTranslateContent: this.enTranslateContent, extraInfo: this.extraInfo, readState: this.readState ?? 0, localFile: this.localFile, state: this.state, soundListened: this.soundListened, refMsgContent: this.refMsgContent, altUsers: this.altUsers); } //从本地数据库数据初始化消息体 MsgModel.fromJson(Map json, [bool isGroup = false]) { transTag = json['transTag']; from = json['fromId']; friendId = json['friendId']; msgType = json['msgType']; msgContent = json['msgContent'] ?? Uint8List(0); time = json['time']; sessionId = json['sessionId']; readState = json['readState'] ?? 0; if (json['extraFile'] != '') { extraFile = json['extraFile']; } extraInfo = json['extraInfo'] ?? 0; if (json['translateContent'] != null) { translateContent = json['translateContent']; } if (json['enTranslateContent'] != null) { enTranslateContent = json['enTranslateContent']; } if (json['localFile'] != '') { localFile = FileCacheMgr.replacePath(json['localFile']); } soundListened = json['soundListened']; refMsgContent = json['refMsgContent']; altUsers = json['altUsers']; //群聊还是私聊 if (isGroup) { channelType = 1; } else { channelType = 0; } state = json['msgState']; } updateSoundListened() { soundListened = 1; SqlUtil().updateSoundListened(this); } //人工付费翻译收费 getNeedMoney() { int needMoney = 0; if (msgType == ChatType.TextChatType.value) { int length = utf8.decode(msgContent).length; needMoney = length ~/ 50 + 1; //每50个字耗费1H币 } else if (msgType == ChatType.ShortVoiceChatType.value) { //没20秒扣除1Hb needMoney = extraInfo != null ? extraInfo ~/ 20000 + 1 : 0; } return needMoney; } //获得翻译的结果数组 getTransTextList() { List textList = []; if (translateContent != null && translateContent.length > 0) { if (transTag == 5 || transTag == 6) { print('人工翻译失败'); } else { try { var transTxt = utf8.decode(translateContent); if (transTxt.contains('[ ')) { transTxt = transTxt.replaceAll('[ ', '['); } if (transTxt.contains(' ]')) { transTxt = transTxt.replaceAll(' ]', ']'); } print('翻译结果:$transTxt'); if (altUsers != null && altUsers.length > 0) { var alterStr = ''; for (var i = 0; i < altUsers.length; i++) { var name = GroupInfoMgr().getGroupFdName(sessionId, altUsers[i]); alterStr = '@$name $alterStr'; } transTxt = '$alterStr $transTxt'; } textList.add(transTxt); } catch (e) { var origin = utf8.decode(msgContent); print('译文异常,原文 $origin 发送人 $from 发送时间$time'); } } } //人工翻译之后就不需要这个字段了 if (enTranslateContent != null && enTranslateContent.length > 0 && (transTag != 4 && transTag != 10)) { var enTransTxt = utf8.decode(enTranslateContent); if (enTransTxt.contains('[ ')) { enTransTxt = enTransTxt.replaceAll('[ ', '['); } if (enTransTxt.contains(' ]')) { enTransTxt = enTransTxt.replaceAll(' ]', ']'); } if (altUsers != null && altUsers.length > 0) { var alterStr = ''; for (var i = 0; i < altUsers.length; i++) { var name = GroupInfoMgr().getGroupFdName(sessionId, altUsers[i]); alterStr = '@$name $alterStr'; } enTransTxt = '$alterStr $enTransTxt'; } textList.add(enTransTxt); } if (msgType == ChatType.TextChatType.value) { if (msgContent != null && msgContent.length > 0) { var text = utf8.decode(msgContent); textList.add(text); } } return textList; } getFileUrl() { var fullUrl = UploadUtil().getFullUrl(extraFile, sessionId, channelType); if (extraFile.contains('http://')) { fullUrl = extraFile; } return fullUrl; } //发送给翻译人员的记录 shortRecordForTranslator() { String desc = ''; switch (ChatType.valueOf(msgType)) { case ChatType.TextChatType: desc = utf8.decode(msgContent); break; case ChatType.EmoticonType: desc = '[${I18n.of(Constants.getCurrentContext()).emoji}]'; break; case ChatType.ImageChatType: case ChatType.ShortVideoChatType: case ChatType.ShortVoiceChatType: case ChatType.FileChatType: desc = getFileUrl(); break; case ChatType.PlaceChatType: desc = '[${I18n.of(Constants.getCurrentContext()).locate}]'; break; case ChatType.GiftChatType: GiftChat giftChat = GiftChat.fromBuffer(msgContent); if (giftChat.tuId == UserData().basicInfo.userId) { desc = I18n.of(Constants.getCurrentContext()).you_get; } else { desc = I18n.of(Constants.getCurrentContext()).you_give; } break; case ChatType.RedWalletChatType: if (from == 0) { //服务器通知消息 RedWallet wallet = RedWallet.fromBuffer(msgContent); if (wallet.state == RedWalletState.Received) { var myId = UserData().basicInfo.userId; if (wallet.suId == myId) { desc = I18n.of(Constants.getCurrentContext()) .get_money .replaceFirst('/s1', '好友'); } else { desc = I18n.of(Constants.getCurrentContext()) .you_get_money .replaceFirst('/s1', '好友'); } } else if (wallet.state == RedWalletState.Expire) { desc = I18n.of(Constants.getCurrentContext()).money_over; } else { print('WWWWW${wallet.state}'); } } else { desc = '[${I18n.of(Constants.getCurrentContext()).red_money}]'; } break; case ChatType.GroupChatNoticeType: desc = '群聊通知消息'; break; default: } return { 'SendTime': DateUtils() .getFormartData(timeSamp: time, format: 'yyyy:MM:dd hh:mm:ss'), 'Content': desc, 'UserId': from }; } }