Hibok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

287 rivejä
9.0 KiB

  1. import 'dart:typed_data';
  2. import 'dart:convert';
  3. import 'package:chat/data/UserData.dart';
  4. import 'package:chat/data/constants.dart';
  5. import 'package:chat/generated/i18n.dart';
  6. import 'package:chat/proto/chat.pb.dart';
  7. import 'package:chat/proto/chat.pbenum.dart';
  8. import 'package:chat/proto/transhousekeeper.pb.dart';
  9. import 'package:chat/utils/date_utils.dart';
  10. import 'package:chat/utils/file_cache_mgr.dart';
  11. import 'package:chat/utils/sql_util.dart';
  12. import 'package:chat/data/group_data_mgr.dart';
  13. import 'package:chat/utils/upload_util.dart';
  14. class MsgState {
  15. static const int None = 0; //默认
  16. static const int UnUpload = 1; //未上传
  17. static const int Uploading = 2; //上传中
  18. static const int UploadFailed = 3; //上传失败
  19. static const int Uploaded = 4; //已上传
  20. static const int Sending = 5; //发送中
  21. static const int SendingFailed = 6; //发送失败,
  22. static const int SendingSuccess = 7; //发送成功
  23. static const int Downloading = 9; //下载中
  24. static const int DownloadFailed = 10; //下载失败
  25. static const int DownloadSuccess = 11; //下载成功
  26. }
  27. class MsgModel {
  28. int transTag; //翻译类型标记
  29. int channelType; //群聊还是会话
  30. int from;
  31. int friendId;
  32. int msgType;
  33. List<int> msgContent;
  34. int time; //消息的时间
  35. int sessionId; //会话id
  36. int readState; //0代表已显示
  37. String extraFile; //下载文件的识别码
  38. int extraInfo; //表示语音的长度信息
  39. List<int> translateContent; //腾讯翻译
  40. List<int> enTranslateContent; //google翻译
  41. String localFile; //文件的本地路径
  42. int state; //发送状态0默认,1代表未上传 2代表上传中,3代表已上传,4代表发送中,5发送失败,6发送成功
  43. int soundListened; //语音是否听过0没有,1有
  44. List<int> refMsgContent; //引用的内容
  45. List<int> altUsers; //@的用户
  46. MsgModel(this.from, this.friendId, this.msgType, this.msgContent, this.time,
  47. this.sessionId,
  48. {this.extraFile,
  49. this.extraInfo,
  50. this.transTag = 0,
  51. this.translateContent,
  52. this.enTranslateContent,
  53. this.channelType = 0,
  54. int readState = 0,
  55. this.localFile,
  56. this.state = MsgState.None,
  57. this.soundListened = 0,
  58. this.refMsgContent,
  59. this.altUsers});
  60. copy() {
  61. return MsgModel(this.from, this.friendId, this.msgType, this.msgContent,
  62. this.time, this.sessionId,
  63. extraFile: this.extraFile,
  64. transTag: this.transTag,
  65. translateContent: this.translateContent,
  66. enTranslateContent: this.enTranslateContent,
  67. extraInfo: this.extraInfo,
  68. readState: this.readState ?? 0,
  69. localFile: this.localFile,
  70. state: this.state,
  71. soundListened: this.soundListened,
  72. refMsgContent: this.refMsgContent,
  73. altUsers: this.altUsers);
  74. }
  75. //从本地数据库数据初始化消息体
  76. MsgModel.fromJson(Map<String, dynamic> json, [bool isGroup = false]) {
  77. transTag = json['transTag'];
  78. from = json['fromId'];
  79. friendId = json['friendId'];
  80. msgType = json['msgType'];
  81. msgContent = json['msgContent'] ?? Uint8List(0);
  82. time = json['time'];
  83. sessionId = json['sessionId'];
  84. readState = json['readState'] ?? 0;
  85. if (json['extraFile'] != '') {
  86. extraFile = json['extraFile'];
  87. }
  88. extraInfo = json['extraInfo'] ?? 0;
  89. if (json['translateContent'] != null) {
  90. translateContent = json['translateContent'];
  91. }
  92. if (json['enTranslateContent'] != null) {
  93. enTranslateContent = json['enTranslateContent'];
  94. }
  95. if (json['localFile'] != '') {
  96. localFile = FileCacheMgr.replacePath(json['localFile']);
  97. }
  98. soundListened = json['soundListened'];
  99. refMsgContent = json['refMsgContent'];
  100. altUsers = json['altUsers'];
  101. //群聊还是私聊
  102. if (isGroup) {
  103. channelType = 1;
  104. } else {
  105. channelType = 0;
  106. }
  107. state = json['msgState'];
  108. }
  109. updateSoundListened() {
  110. soundListened = 1;
  111. SqlUtil().updateSoundListened(this);
  112. }
  113. //人工付费翻译收费
  114. getNeedMoney() {
  115. int needMoney = 0;
  116. if (msgType == ChatType.TextChatType.value) {
  117. int length = utf8.decode(msgContent).length;
  118. needMoney = length ~/ 50 + 1; //每50个字耗费1H币
  119. } else if (msgType == ChatType.ShortVoiceChatType.value) {
  120. //没20秒扣除1Hb
  121. needMoney = extraInfo != null ? extraInfo ~/ 20000 + 1 : 0;
  122. }
  123. return needMoney;
  124. }
  125. //获得翻译的结果数组
  126. getTransTextList() {
  127. List<String> textList = [];
  128. if (translateContent != null && translateContent.length > 0) {
  129. try {
  130. var transTxt = utf8.decode(translateContent);
  131. if (transTxt.contains('[ ')) {
  132. transTxt = transTxt.replaceAll('[ ', '[');
  133. }
  134. if (transTxt.contains(' ]')) {
  135. transTxt = transTxt.replaceAll(' ]', ']');
  136. }
  137. print('翻译结果:$transTxt');
  138. if (altUsers != null && altUsers.length > 0) {
  139. var alterStr = '';
  140. for (var i = 0; i < altUsers.length; i++) {
  141. var name = GroupInfoMgr().getGroupFdName(sessionId, altUsers[i]);
  142. alterStr = '@$name $alterStr';
  143. }
  144. transTxt = '$alterStr $transTxt';
  145. }
  146. textList.add(transTxt);
  147. } catch (e) {
  148. var origin = utf8.decode(msgContent);
  149. print('译文异常,原文 $origin 发送人 $from 发送时间$time');
  150. }
  151. }
  152. //人工翻译之后就不需要这个字段了
  153. if (enTranslateContent != null &&
  154. enTranslateContent.length > 0 &&
  155. (transTag != 4 && transTag != 10)) {
  156. var enTransTxt = utf8.decode(enTranslateContent);
  157. if (enTransTxt.contains('[ ')) {
  158. enTransTxt = enTransTxt.replaceAll('[ ', '[');
  159. }
  160. if (enTransTxt.contains(' ]')) {
  161. enTransTxt = enTransTxt.replaceAll(' ]', ']');
  162. }
  163. if (altUsers != null && altUsers.length > 0) {
  164. var alterStr = '';
  165. for (var i = 0; i < altUsers.length; i++) {
  166. var name = GroupInfoMgr().getGroupFdName(sessionId, altUsers[i]);
  167. alterStr = '@$name $alterStr';
  168. }
  169. enTransTxt = '$alterStr $enTransTxt';
  170. }
  171. textList.add(enTransTxt);
  172. }
  173. if (msgType == ChatType.TextChatType.value) {
  174. if (msgContent != null && msgContent.length > 0) {
  175. var text = utf8.decode(msgContent);
  176. textList.add(text);
  177. }
  178. }
  179. return textList.length == 0 ? [''] : textList;
  180. }
  181. getFileUrl() {
  182. var fullUrl = UploadUtil().getFullUrl(extraFile, sessionId, channelType);
  183. if (extraFile.contains('http://')) {
  184. fullUrl = extraFile;
  185. }
  186. return fullUrl;
  187. }
  188. //发送给翻译人员的记录
  189. shortRecordForTranslator() {
  190. String desc = '';
  191. switch (ChatType.valueOf(msgType)) {
  192. case ChatType.TextChatType:
  193. desc = utf8.decode(msgContent);
  194. break;
  195. case ChatType.EmoticonType:
  196. desc = '[${I18n.of(Constants.getCurrentContext()).emoji}]';
  197. break;
  198. case ChatType.ImageChatType:
  199. case ChatType.ShortVideoChatType:
  200. case ChatType.ShortVoiceChatType:
  201. case ChatType.FileChatType:
  202. desc = getFileUrl();
  203. break;
  204. case ChatType.PlaceChatType:
  205. desc = '[${I18n.of(Constants.getCurrentContext()).locate}]';
  206. break;
  207. case ChatType.GiftChatType:
  208. GiftChat giftChat = GiftChat.fromBuffer(msgContent);
  209. if (giftChat.tuId == UserData().basicInfo.userId) {
  210. desc = I18n.of(Constants.getCurrentContext()).you_get;
  211. } else {
  212. desc = I18n.of(Constants.getCurrentContext()).you_give;
  213. }
  214. break;
  215. case ChatType.RedWalletChatType:
  216. if (from == 0) {
  217. //服务器通知消息
  218. RedWallet wallet = RedWallet.fromBuffer(msgContent);
  219. if (wallet.state == RedWalletState.Received) {
  220. var myId = UserData().basicInfo.userId;
  221. if (wallet.suId == myId) {
  222. desc = I18n.of(Constants.getCurrentContext())
  223. .get_money
  224. .replaceFirst('/s1', '好友');
  225. } else {
  226. desc = I18n.of(Constants.getCurrentContext())
  227. .you_get_money
  228. .replaceFirst('/s1', '好友');
  229. }
  230. } else if (wallet.state == RedWalletState.Expire) {
  231. desc = I18n.of(Constants.getCurrentContext()).money_over;
  232. } else {
  233. print('WWWWW${wallet.state}');
  234. }
  235. } else {
  236. desc = '[${I18n.of(Constants.getCurrentContext()).red_money}]';
  237. }
  238. break;
  239. case ChatType.GroupChatNoticeType:
  240. desc = '群聊通知消息';
  241. break;
  242. default:
  243. }
  244. return {
  245. 'SendTime' :
  246. DateUtils().getFormartData(timeSamp: time, format: 'yyyy:MM:dd hh:mm:ss');
  247. 'Content' : desc,
  248. 'UserId':from
  249. };
  250. }
  251. }