Hibok
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

200 líneas
6.2 KiB

  1. import 'dart:typed_data';
  2. import 'dart:convert';
  3. import 'package:chat/proto/chat.pbenum.dart';
  4. import 'package:chat/utils/file_cache_mgr.dart';
  5. import 'package:chat/utils/sql_util.dart';
  6. import 'package:chat/data/group_data_mgr.dart';
  7. class MsgState {
  8. static const int None = 0; //默认
  9. static const int UnUpload = 1; //未上传
  10. static const int Uploading = 2; //上传中
  11. static const int UploadFailed = 3; //上传失败
  12. static const int Uploaded = 4; //已上传
  13. static const int Sending = 5; //发送中
  14. static const int SendingFailed = 6; //发送失败,
  15. static const int SendingSuccess = 7; //发送成功
  16. static const int Downloading = 9; //下载中
  17. static const int DownloadFailed = 10; //下载失败
  18. static const int DownloadSuccess = 11; //下载成功
  19. }
  20. class MsgModel {
  21. int transTag; //翻译类型标记
  22. int channelType; //群聊还是会话
  23. int from;
  24. int friendId;
  25. int msgType;
  26. List<int> msgContent;
  27. int time; //消息的时间
  28. int sessionId; //会话id
  29. int readState; //0代表已显示
  30. String extraFile; //下载文件的识别码
  31. int extraInfo; //表示语音的长度信息
  32. List<int> translateContent; //腾讯翻译
  33. List<int> enTranslateContent; //google翻译
  34. String localFile; //文件的本地路径
  35. int state; //发送状态0默认,1代表未上传 2代表上传中,3代表已上传,4代表发送中,5发送失败,6发送成功
  36. int soundListened; //语音是否听过0没有,1有
  37. List<int> refMsgContent; //引用的内容
  38. List<int> altUsers; //@的用户
  39. MsgModel(this.from, this.friendId, this.msgType, this.msgContent, this.time,
  40. this.sessionId,
  41. {this.extraFile,
  42. this.extraInfo,
  43. this.transTag = 0,
  44. this.translateContent,
  45. this.enTranslateContent,
  46. this.channelType = 0,
  47. int readState = 0,
  48. this.localFile,
  49. this.state = MsgState.None,
  50. this.soundListened = 0,
  51. this.refMsgContent,
  52. this.altUsers});
  53. copy() {
  54. return MsgModel(this.from, this.friendId, this.msgType, this.msgContent,
  55. this.time, this.sessionId,
  56. extraFile: this.extraFile,
  57. transTag: this.transTag,
  58. translateContent: this.translateContent,
  59. enTranslateContent: this.enTranslateContent,
  60. extraInfo: this.extraInfo,
  61. readState: this.readState ?? 0,
  62. localFile: this.localFile,
  63. state: this.state,
  64. soundListened: this.soundListened,
  65. refMsgContent: this.refMsgContent,
  66. altUsers: this.altUsers);
  67. }
  68. //从本地数据库数据初始化消息体
  69. MsgModel.fromJson(Map<String, dynamic> json, [bool isGroup = false]) {
  70. transTag = json['transTag'];
  71. from = json['fromId'];
  72. friendId = json['friendId'];
  73. msgType = json['msgType'];
  74. msgContent = json['msgContent'] ?? Uint8List(0);
  75. time = json['time'];
  76. sessionId = json['sessionId'];
  77. readState = json['readState'] ?? 0;
  78. if (json['extraFile'] != '') {
  79. extraFile = json['extraFile'];
  80. }
  81. extraInfo = json['extraInfo'] ?? 0;
  82. if (json['translateContent'] != null) {
  83. translateContent = json['translateContent'];
  84. }
  85. if (json['enTranslateContent'] != null) {
  86. enTranslateContent = json['enTranslateContent'];
  87. }
  88. if (json['localFile'] != '') {
  89. localFile = FileCacheMgr.replacePath(json['localFile']);
  90. }
  91. soundListened = json['soundListened'];
  92. refMsgContent = json['refMsgContent'];
  93. altUsers = json['altUsers'];
  94. //群聊还是私聊
  95. if (isGroup) {
  96. channelType = 1;
  97. } else {
  98. channelType = 0;
  99. }
  100. state = json['msgState'];
  101. }
  102. updateSoundListened() {
  103. soundListened = 1;
  104. SqlUtil().updateSoundListened(this);
  105. }
  106. //人工付费翻译收费
  107. getNeedMoney() {
  108. int needMoney = 0;
  109. if (msgType == ChatType.TextChatType.value) {
  110. int length = utf8.decode(msgContent).length;
  111. needMoney = length ~/ 50 + 1; //每50个字耗费1H币
  112. } else if (msgType == ChatType.ShortVoiceChatType.value) {
  113. //没20秒扣除1Hb
  114. needMoney = extraInfo != null ? extraInfo ~/ 20000 + 1 : 0;
  115. }
  116. return needMoney;
  117. }
  118. //获得翻译的结果数组
  119. getTransTextList() {
  120. List<String> textList = [];
  121. if (translateContent != null && translateContent.length > 0) {
  122. try {
  123. var transTxt = utf8.decode(translateContent);
  124. if (transTxt.contains('[ ')) {
  125. transTxt = transTxt.replaceAll('[ ', '[');
  126. }
  127. if (transTxt.contains(' ]')) {
  128. transTxt = transTxt.replaceAll(' ]', ']');
  129. }
  130. print('翻译结果:$transTxt');
  131. if (altUsers != null && altUsers.length > 0) {
  132. var alterStr = '';
  133. for (var i = 0; i < altUsers.length; i++) {
  134. var name = GroupInfoMgr().getGroupFdName(sessionId, altUsers[i]);
  135. alterStr = '@$name $alterStr';
  136. }
  137. transTxt = '$alterStr $transTxt';
  138. }
  139. textList.add(transTxt);
  140. } catch (e) {
  141. var origin = utf8.decode(msgContent);
  142. print('译文异常,原文 $origin 发送人 $from 发送时间$time');
  143. }
  144. }
  145. //人工翻译之后就不需要这个字段了
  146. if (enTranslateContent != null &&
  147. enTranslateContent.length > 0 &&
  148. (transTag != 4 && transTag != 10)) {
  149. var enTransTxt = utf8.decode(enTranslateContent);
  150. if (enTransTxt.contains('[ ')) {
  151. enTransTxt = enTransTxt.replaceAll('[ ', '[');
  152. }
  153. if (enTransTxt.contains(' ]')) {
  154. enTransTxt = enTransTxt.replaceAll(' ]', ']');
  155. }
  156. if (altUsers != null && altUsers.length > 0) {
  157. var alterStr = '';
  158. for (var i = 0; i < altUsers.length; i++) {
  159. var name = GroupInfoMgr().getGroupFdName(sessionId, altUsers[i]);
  160. alterStr = '@$name $alterStr';
  161. }
  162. enTransTxt = '$alterStr $enTransTxt';
  163. }
  164. textList.add(enTransTxt);
  165. }
  166. if (msgType == ChatType.TextChatType.value) {
  167. if (msgContent != null && msgContent.length > 0) {
  168. var text = utf8.decode(msgContent);
  169. textList.add(text);
  170. }
  171. }
  172. return textList;
  173. }
  174. }