untis.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import { formatTime } from "./date.ts";
  2. import { decodeText } from "./decodeText";
  3. import { bigEmojiList } from "./emojiMap";
  4. // 处理头像
  5. export function handleAvatar(item: any) {
  6. let avatar = "";
  7. if (item.type === uni.$TIM.TYPES.CONV_C2C) {
  8. avatar =
  9. item?.userProfile?.avatar ||
  10. "https://web.sdk.qcloud.com/component/TUIKit/assets/avatar_21.png";
  11. } else {
  12. avatar =
  13. item?.groupProfile?.avatar ||
  14. "https://web.sdk.qcloud.com/component/TUIKit/assets/group_avatar.png";
  15. }
  16. return avatar;
  17. }
  18. // 处理昵称
  19. export function handleName(item: any) {
  20. let name = "";
  21. switch (item.type) {
  22. case uni.$TIM.TYPES.CONV_C2C:
  23. name = item?.userProfile.nick || item?.userProfile?.userID || "";
  24. break;
  25. case uni.$TIM.TYPES.CONV_GROUP:
  26. name = item.groupProfile.name || item?.groupProfile?.groupID || "";
  27. break;
  28. case uni.$TIM.TYPES.CONV_SYSTEM:
  29. name = "系统通知";
  30. break;
  31. }
  32. return name;
  33. }
  34. // 处理系统提示消息展示
  35. export function handleTipMessageShowContext(message: any) {
  36. const options: any = {
  37. message,
  38. text: "",
  39. };
  40. const userName = message.nick || message.payload.userIDList.join(",");
  41. switch (message.payload.operationType) {
  42. case uni.$TIM.TYPES.GRP_TIP_MBR_JOIN:
  43. options.text = `群成员:${userName} 加入群组`;
  44. break;
  45. case uni.$TIM.TYPES.GRP_TIP_MBR_QUIT:
  46. options.text = `群成员:${userName} 退出群组`;
  47. break;
  48. case uni.$TIM.TYPES.GRP_TIP_MBR_KICKED_OUT:
  49. options.text = `群成员:${userName} 被 ${message.payload.operatorID} 踢出群组`;
  50. break;
  51. case uni.$TIM.TYPES.GRP_TIP_MBR_SET_ADMIN:
  52. options.text = `群成员:${userName} 成为管理员`;
  53. break;
  54. case uni.$TIM.TYPES.GRP_TIP_MBR_CANCELED_ADMIN:
  55. options.text = `群成员:${userName} 被撤销管理员`;
  56. break;
  57. case uni.$TIM.TYPES.GRP_TIP_GRP_PROFILE_UPDATED:
  58. // options.text = `${userName} 修改群组资料`;
  59. options.text = handleTipGrpUpdated(message);
  60. break;
  61. case uni.$TIM.TYPES.GRP_TIP_MBR_PROFILE_UPDATED:
  62. for (const member of message.payload.memberList) {
  63. if (member.muteTime > 0) {
  64. // options.text = `群成员:${member.userID}被禁言${member.muteTime}秒`;
  65. options.text = `群成员:${member.userID} 被禁言`;
  66. } else {
  67. options.text = `群成员:${member.userID} 被取消禁言`;
  68. }
  69. }
  70. break;
  71. default:
  72. options.text = `[群提示消息]`;
  73. break;
  74. }
  75. return options;
  76. }
  77. function handleTipGrpUpdated(message: any) {
  78. const { payload } = message;
  79. const { newGroupProfile } = payload;
  80. const { operatorID } = payload;
  81. let text = "";
  82. const name = Object.keys(newGroupProfile)[0];
  83. switch (name) {
  84. case "ownerID":
  85. text = `${newGroupProfile[name]} 成为新的群主`;
  86. break;
  87. case "groupName":
  88. text = `${operatorID} 修改群名为 ${newGroupProfile[name]}`;
  89. break;
  90. case "notification":
  91. text = `${operatorID} 发布新公告`;
  92. break;
  93. default:
  94. break;
  95. }
  96. return text;
  97. }
  98. // 解析处理文本消息展示
  99. export function handleTextMessageShowContext(item: any) {
  100. const options: any = {
  101. text: decodeText(item.payload),
  102. };
  103. return options;
  104. }
  105. // 解析处理表情消息展示
  106. export function handleFaceMessageShowContext(item: any) {
  107. const face: any = {
  108. message: item,
  109. name: "",
  110. url: "",
  111. };
  112. const currentEmojiList = bigEmojiList.filter(
  113. (emoItem: any) => emoItem.icon === item.payload.data
  114. );
  115. if (currentEmojiList.length > 0) {
  116. face.name = currentEmojiList[0].list[item.payload.index];
  117. }
  118. // 与native 互通兼容
  119. if (item.payload.data.indexOf("@2x") > 0) {
  120. face.name = item.payload.data;
  121. } else {
  122. face.name = `${item.payload.data}@2x`;
  123. }
  124. face.url = `https://web.sdk.qcloud.com/im/assets/face-elem/${face.name}.png`;
  125. return face;
  126. }
  127. // 解析处理位置消息展示
  128. export function handleLocationMessageShowContext(item: any) {
  129. const location: any = {
  130. lon: "",
  131. lat: "",
  132. href: "",
  133. url: "",
  134. description: "",
  135. message: item,
  136. };
  137. location.lon = item.payload.longitude.toFixed(6);
  138. location.lat = item.payload.latitude.toFixed(6);
  139. location.href =
  140. "https://map.qq.com/?type=marker&isopeninfowin=1&markertype=1&" +
  141. `pointx=${location.lon}&pointy=${location.lat}&name=${item.payload.description}`;
  142. location.url =
  143. "https://apis.map.qq.com/ws/staticmap/v2/?" +
  144. `center=${location.lat},${location.lon}&zoom=10&size=300*150&maptype=roadmap&` +
  145. `markers=size:large|color:0xFFCCFF|label:k|${location.lat},${location.lon}&` +
  146. "key=UBNBZ-PTP3P-TE7DB-LHRTI-Y4YLE-VWBBD";
  147. location.description = item.payload.description;
  148. return location;
  149. }
  150. // 解析处理图片消息展示
  151. export function handleImageMessageShowContext(item: any) {
  152. return {
  153. progress: item?.status === "unSend" && item.progress,
  154. info: item.payload.imageInfoArray,
  155. message: item,
  156. };
  157. }
  158. // 解析处理视频消息展示
  159. export function handleVideoMessageShowContext(item: any) {
  160. return {
  161. progress: item?.status === "unSend" && item?.progress,
  162. url: item?.payload?.videoUrl,
  163. snapshotUrl: item?.payload?.snapshotUrl,
  164. message: item,
  165. };
  166. }
  167. // 解析处理语音消息展示
  168. export function handleAudioMessageShowContext(item: any) {
  169. return {
  170. progress: item?.status === "unSend" && item.progress,
  171. url: item.payload.url,
  172. message: item,
  173. second: item.payload.second,
  174. };
  175. }
  176. // 解析处理文件消息展示
  177. export function handleFileMessageShowContext(item: any) {
  178. let size = "";
  179. if (item.payload.fileSize >= 1024 * 1024) {
  180. size = `${(item.payload.fileSize / (1024 * 1024)).toFixed(2)} Mb`;
  181. } else if (item.payload.fileSize >= 1024) {
  182. size = `${(item.payload.fileSize / 1024).toFixed(2)} Kb`;
  183. } else {
  184. size = `${item.payload.fileSize.toFixed(2)}B`;
  185. }
  186. return {
  187. progress: item?.status === "unSend" && item.progress,
  188. url: item.payload.fileUrl,
  189. message: item,
  190. name: item.payload.fileName,
  191. size,
  192. };
  193. }
  194. // 解析处理合并消息展示
  195. export function handleMergerMessageShowContext(item: any) {
  196. return { message: item, ...item.payload };
  197. }
  198. // 解析音视频通话消息
  199. export function extractCallingInfoFromMessage(message: any) {
  200. let callingmessage: any = {};
  201. let objectData: any = {};
  202. try {
  203. callingmessage = JSON.parse(message.payload.data);
  204. } catch (error) {
  205. callingmessage = {};
  206. }
  207. if (callingmessage.businessID !== 1) {
  208. return "";
  209. }
  210. try {
  211. objectData = JSON.parse(callingmessage.data);
  212. } catch (error) {
  213. objectData = {};
  214. }
  215. switch (callingmessage.actionType) {
  216. case 1: {
  217. if (objectData.call_end >= 0 && !callingmessage.groupID) {
  218. return `通话时长 :${formatTime(objectData.call_end)}`;
  219. }
  220. if (callingmessage.groupID) {
  221. return `结束群聊`;
  222. }
  223. if (objectData.data && objectData.data.cmd === "switchToAudio") {
  224. return `切换语音通话`;
  225. }
  226. if (objectData.data && objectData.data.cmd === "switchToVideo") {
  227. return `切换视频通话`;
  228. }
  229. return `发起通话`;
  230. }
  231. case 2:
  232. return `取消通话`;
  233. case 3:
  234. if (objectData.data && objectData.data.cmd === "switchToAudio") {
  235. return `切换语音通话`;
  236. }
  237. if (objectData.data && objectData.data.cmd === "switchToVideo") {
  238. return `切换视频通话`;
  239. }
  240. return `已接听`;
  241. case 4:
  242. return `拒绝通话`;
  243. case 5:
  244. if (objectData.data && objectData.data.cmd === "switchToAudio") {
  245. return `切换语音通话`;
  246. }
  247. if (objectData.data && objectData.data.cmd === "switchToVideo") {
  248. return `切换视频通话`;
  249. }
  250. return `无应答`;
  251. default:
  252. return "";
  253. }
  254. }
  255. // 解析处理自定义消息展示
  256. export function handleCustomMessageShowContext(item: any) {
  257. return {
  258. message: item,
  259. custom:
  260. extractCallingInfoFromMessage(item) ||
  261. item?.payload?.extension ||
  262. `[自定义消息]`,
  263. };
  264. }
  265. // 解析处理系统消息
  266. export function translateGroupSystemNotice(message: any) {
  267. const groupName =
  268. message.payload.groupProfile?.name || message.payload.groupProfile?.groupID;
  269. switch (message.payload.operationType) {
  270. case 1:
  271. return `${message.payload.operatorID} 申请加入群组:${groupName}`;
  272. case 2:
  273. return `成功加入群组:${groupName}`;
  274. case 3:
  275. return `申请加入群组:${groupName} 被拒绝`;
  276. case 4:
  277. return `你被管理员 ${message.payload.operatorID} 踢出群组:${groupName}`;
  278. case 5:
  279. return `群:${groupName} 被 ${message.payload.operatorID} 解散`;
  280. case 6:
  281. return `${message.payload.operatorID} 创建群:${groupName}`;
  282. case 7:
  283. return `${message.payload.operatorID} 邀请你加群:${groupName}`;
  284. case 8:
  285. return `你退出群组:${groupName}`;
  286. case 9:
  287. return `你被${message.payload.operatorID} 设置为群:${groupName} 的管理员`;
  288. case 10:
  289. return `你被 ${message.payload.operatorID} 撤销群:${groupName} 的管理员身份`;
  290. case 12:
  291. return `${message.payload.operatorID} 邀请你加群:${groupName}`;
  292. case 13:
  293. return `${message.payload.operatorID} 同意加群 :${groupName}`;
  294. case 14:
  295. return `${message.payload.operatorID} 拒接加群 :${groupName}`;
  296. case 255:
  297. return `自定义群系统通知: ${message.payload.userDefinedField}`;
  298. }
  299. }