message-facade.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import {
  2. emojiMap,
  3. emojiUrl
  4. } from './emojiMap';
  5. /** 传入message.element(群系统消息SystemMessage,群提示消息GroupTip除外)
  6. * content = {
  7. * type: 'TIMTextElem',
  8. * content: {
  9. * text: 'AAA[龇牙]AAA[龇牙]AAA[龇牙AAA]'
  10. * }
  11. *}
  12. **/
  13. // 群提示消息的含义 (opType)
  14. const GROUP_TIP_TYPE = {
  15. MEMBER_JOIN: 1,
  16. MEMBER_QUIT: 2,
  17. MEMBER_KICKED_OUT: 3,
  18. MEMBER_SET_ADMIN: 4,
  19. // 被设置为管理员
  20. MEMBER_CANCELED_ADMIN: 5,
  21. // 被取消管理员
  22. GROUP_INFO_MODIFIED: 6,
  23. // 修改群资料,转让群组为该类型,msgBody.msgGroupNewInfo.ownerAccount表示新群主的ID
  24. MEMBER_INFO_MODIFIED: 7 // 修改群成员信息
  25. }; // 解析小程序text, 表情信息也是[嘻嘻]文本
  26. export function parseText(message) {
  27. const renderDom = [];
  28. let temp = message.payload.text;
  29. let left = -1;
  30. let right = -1;
  31. while (temp !== '') {
  32. left = temp.indexOf('[');
  33. right = temp.indexOf(']');
  34. switch (left) {
  35. case 0:
  36. if (right === -1) {
  37. renderDom.push({
  38. name: 'span',
  39. text: temp
  40. });
  41. temp = '';
  42. } else {
  43. const _emoji = temp.slice(0, right + 1);
  44. if (emojiMap[_emoji]) {
  45. renderDom.push({
  46. name: 'img',
  47. src: emojiUrl + emojiMap[_emoji]
  48. });
  49. temp = temp.substring(right + 1);
  50. } else {
  51. renderDom.push({
  52. name: 'span',
  53. text: '['
  54. });
  55. temp = temp.slice(1);
  56. }
  57. }
  58. break;
  59. case -1:
  60. renderDom.push({
  61. name: 'span',
  62. text: temp
  63. });
  64. temp = '';
  65. break;
  66. default:
  67. renderDom.push({
  68. name: 'span',
  69. text: temp.slice(0, left)
  70. });
  71. temp = temp.substring(left);
  72. break;
  73. }
  74. }
  75. return renderDom;
  76. } // 解析群系统消息
  77. export function parseGroupSystemNotice(message) {
  78. const {
  79. payload
  80. } = message;
  81. const groupName = payload.groupProfile.name || payload.groupProfile.groupID;
  82. const {
  83. groupID
  84. } = payload.groupProfile;
  85. let text;
  86. switch (payload.operationType) {
  87. case 1:
  88. text = `${payload.operatorID} 申请加入群组:${groupName}(群ID:${groupID})`;
  89. break;
  90. case 2:
  91. text = `成功加入群组:${groupName} (群ID:${groupID})`;
  92. break;
  93. case 3:
  94. text = `申请加入群组:${groupName} (群ID:${groupID})被拒绝`;
  95. break;
  96. case 4:
  97. text = `被管理员${payload.operatorID}踢出群组:${groupName}(群ID:${groupID})`;
  98. break;
  99. case 5:
  100. text = `群:${groupName} (群ID:${groupID})已被${payload.operatorID}解散`;
  101. break;
  102. case 6:
  103. text = `我(用户ID:${payload.operatorID})成功创建群聊:${groupName}(群ID:${groupID})`;
  104. break;
  105. case 7:
  106. text = `用户ID:${payload.operatorID}邀请你加群:${groupName}(群ID:${groupID})`;
  107. break;
  108. case 8:
  109. text = `你退出群组:${groupName}(群ID:${groupID})`;
  110. break;
  111. case 9:
  112. text = `你被${payload.operatorID}设置为群:${groupName}(群ID:${groupID})的管理员`;
  113. break;
  114. case 10:
  115. text = `你被${payload.operatorID}撤销群:${groupName} (群ID:${groupID})的管理员身份`;
  116. break;
  117. case 255:
  118. text = `自定义群系统通知: ${payload.userDefinedField}`;
  119. break;
  120. }
  121. return text;
  122. } // 解析群提示消息
  123. export function parseGroupTip(message) {
  124. const {
  125. payload
  126. } = message;
  127. const userName = message.nick || payload.userIDList.join(',');
  128. let tip;
  129. let user;
  130. switch (payload.operationType) {
  131. case GROUP_TIP_TYPE.MEMBER_JOIN:
  132. tip = `${userName} 加入群聊`;
  133. break;
  134. case GROUP_TIP_TYPE.MEMBER_QUIT:
  135. tip = `群成员退群:${userName}`;
  136. break;
  137. case GROUP_TIP_TYPE.MEMBER_KICKED_OUT:
  138. tip = `群成员被踢:${userName}`;
  139. break;
  140. case GROUP_TIP_TYPE.MEMBER_SET_ADMIN:
  141. tip = `${payload.operatorID}将 ${userName}设置为管理员`;
  142. break;
  143. case GROUP_TIP_TYPE.MEMBER_CANCELED_ADMIN:
  144. tip = `${payload.operatorID}将 ${userName}取消作为管理员`;
  145. break;
  146. case GROUP_TIP_TYPE.GROUP_INFO_MODIFIED:
  147. tip = '群资料修改';
  148. break;
  149. case GROUP_TIP_TYPE.MEMBER_INFO_MODIFIED:
  150. for (const member of payload.memberList) {
  151. if (member.muteTime > 0) {
  152. tip = `群成员:${member.userID}被禁言${member.muteTime}秒`;
  153. } else {
  154. tip = `群成员:${member.userID}被取消禁言`;
  155. }
  156. }
  157. break;
  158. case 256:
  159. user = message.nick || message.from;
  160. if (payload.text === '无应答') {
  161. user = payload.userIDList.join(',');
  162. }
  163. tip = payload.text === '结束群聊' ? '结束群聊' : `"${user}" ${payload.text}`;
  164. break;
  165. }
  166. return [{
  167. name: 'groupTip',
  168. text: tip
  169. }];
  170. } // 解析图片消息
  171. export function parseImage(message) {
  172. const renderDom = [{
  173. name: 'image',
  174. // 这里默认渲染的是 1080P 的图片
  175. src: message.payload.imageInfoArray[0].url
  176. }];
  177. return renderDom;
  178. } // 解析视频消息
  179. export function parseVideo(message) {
  180. const renderDom = {
  181. name: 'video',
  182. src: message.payload.videoUrl
  183. };
  184. return renderDom;
  185. } // 解析语音消息
  186. export function parseAudio(message) {
  187. const renderDom = {
  188. name: 'audio',
  189. src: message.payload.url,
  190. second: message.payload.second === 0 ? 1 : message.payload.second
  191. };
  192. return renderDom;
  193. }