Hibok
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

293 строки
9.1 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. if (transTag == 5 || transTag == 6) {
  130. print('人工翻译失败');
  131. } else {
  132. try {
  133. var transTxt = utf8.decode(translateContent);
  134. if (transTxt.contains('[ ')) {
  135. transTxt = transTxt.replaceAll('[ ', '[');
  136. }
  137. if (transTxt.contains(' ]')) {
  138. transTxt = transTxt.replaceAll(' ]', ']');
  139. }
  140. print('翻译结果:$transTxt');
  141. if (altUsers != null && altUsers.length > 0) {
  142. var alterStr = '';
  143. for (var i = 0; i < altUsers.length; i++) {
  144. var name = GroupInfoMgr().getGroupFdName(sessionId, altUsers[i]);
  145. alterStr = '@$name $alterStr';
  146. }
  147. transTxt = '$alterStr $transTxt';
  148. }
  149. textList.add(transTxt);
  150. } catch (e) {
  151. var origin = utf8.decode(msgContent);
  152. print('译文异常,原文 $origin 发送人 $from 发送时间$time');
  153. }
  154. }
  155. }
  156. //人工翻译之后就不需要这个字段了
  157. if (enTranslateContent != null &&
  158. enTranslateContent.length > 0 &&
  159. (transTag != 4 && transTag != 10)) {
  160. var enTransTxt = utf8.decode(enTranslateContent);
  161. if (enTransTxt.contains('[ ')) {
  162. enTransTxt = enTransTxt.replaceAll('[ ', '[');
  163. }
  164. if (enTransTxt.contains(' ]')) {
  165. enTransTxt = enTransTxt.replaceAll(' ]', ']');
  166. }
  167. if (altUsers != null && altUsers.length > 0) {
  168. var alterStr = '';
  169. for (var i = 0; i < altUsers.length; i++) {
  170. var name = GroupInfoMgr().getGroupFdName(sessionId, altUsers[i]);
  171. alterStr = '@$name $alterStr';
  172. }
  173. enTransTxt = '$alterStr $enTransTxt';
  174. }
  175. textList.add(enTransTxt);
  176. }
  177. if (msgType == ChatType.TextChatType.value) {
  178. if (msgContent != null && msgContent.length > 0) {
  179. var text = utf8.decode(msgContent);
  180. textList.add(text);
  181. }
  182. }
  183. return textList;
  184. }
  185. getFileUrl() {
  186. var fullUrl = UploadUtil().getFullUrl(extraFile, sessionId, channelType);
  187. if (extraFile.contains('http://')) {
  188. fullUrl = extraFile;
  189. }
  190. return fullUrl;
  191. }
  192. //发送给翻译人员的记录
  193. shortRecordForTranslator() {
  194. String desc = '';
  195. switch (ChatType.valueOf(msgType)) {
  196. case ChatType.TextChatType:
  197. desc = utf8.decode(msgContent);
  198. break;
  199. case ChatType.EmoticonType:
  200. desc = '[${I18n.of(Constants.getCurrentContext()).emoji}]';
  201. break;
  202. case ChatType.ImageChatType:
  203. case ChatType.ShortVideoChatType:
  204. case ChatType.ShortVoiceChatType:
  205. case ChatType.FileChatType:
  206. desc = getFileUrl();
  207. break;
  208. case ChatType.PlaceChatType:
  209. desc = '[${I18n.of(Constants.getCurrentContext()).locate}]';
  210. break;
  211. case ChatType.GiftChatType:
  212. GiftChat giftChat = GiftChat.fromBuffer(msgContent);
  213. if (giftChat.tuId == UserData().basicInfo.userId) {
  214. desc = I18n.of(Constants.getCurrentContext()).you_get;
  215. } else {
  216. desc = I18n.of(Constants.getCurrentContext()).you_give;
  217. }
  218. break;
  219. case ChatType.RedWalletChatType:
  220. if (from == 0) {
  221. //服务器通知消息
  222. RedWallet wallet = RedWallet.fromBuffer(msgContent);
  223. if (wallet.state == RedWalletState.Received) {
  224. var myId = UserData().basicInfo.userId;
  225. if (wallet.suId == myId) {
  226. desc = I18n.of(Constants.getCurrentContext())
  227. .get_money
  228. .replaceFirst('/s1', '好友');
  229. } else {
  230. desc = I18n.of(Constants.getCurrentContext())
  231. .you_get_money
  232. .replaceFirst('/s1', '好友');
  233. }
  234. } else if (wallet.state == RedWalletState.Expire) {
  235. desc = I18n.of(Constants.getCurrentContext()).money_over;
  236. } else {
  237. print('WWWWW${wallet.state}');
  238. }
  239. } else {
  240. desc = '[${I18n.of(Constants.getCurrentContext()).red_money}]';
  241. }
  242. break;
  243. case ChatType.GroupChatNoticeType:
  244. desc = '群聊通知消息';
  245. break;
  246. default:
  247. }
  248. return {
  249. 'SendTime': DateUtils()
  250. .getFormartData(timeSamp: time, format: 'yyyy-MM-dd hh:mm:ss'),
  251. 'Content': base64Encode(utf8.encode(desc)),
  252. 'UserId': from,
  253. 'ChatType':msgType
  254. };
  255. }
  256. }