imCommon.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import {
  2. CustomType,
  3. GroupSystemMessageTypes,
  4. AddFriendQrCodePrefix,
  5. AddGroupQrCodePrefix,
  6. } from "@/pages_im/constant";
  7. import {
  8. subUserOnlineStatus
  9. } from "../api/imApi";
  10. import {
  11. GroupAtType,
  12. MessageType,
  13. SessionType,
  14. } from "../constant/imConstants";
  15. import {
  16. initDayjs,
  17. formatMessageTime,
  18. formatHyperlink,
  19. parseLink,
  20. conversationSort,
  21. parseAt,
  22. sec2Time,
  23. parseMessageByType,
  24. formatConversionTime,
  25. caculateTimeago,
  26. secFormat,
  27. bytesToSize,
  28. tipMessaggeFormat,
  29. } from "./imUtils";
  30. export {
  31. formatMessageTime,
  32. formatHyperlink,
  33. parseLink,
  34. conversationSort,
  35. parseAt,
  36. sec2Time,
  37. parseMessageByType,
  38. formatConversionTime,
  39. caculateTimeago,
  40. secFormat,
  41. bytesToSize,
  42. tipMessaggeFormat,
  43. };
  44. const nomalTypes = [
  45. GroupAtType.AtMe,
  46. GroupAtType.AtAll,
  47. GroupAtType.AtAllAtMe,
  48. GroupAtType.AtGroupNotice,
  49. ];
  50. import IMSDK from "openim-uniapp-polyfill";
  51. import store from "@/store";
  52. export const getDesignatedUserOnlineState = (userID, targetID) => {
  53. return new Promise(async (resolve, reject) => {
  54. let status = 0;
  55. try {
  56. const data = await subUserOnlineStatus(userID, targetID);
  57. status = data.statusList[0].status;
  58. } catch (e) {
  59. reject(e);
  60. }
  61. // const onlineStr = switchOnline(
  62. // statusObj.status,
  63. // statusObj.detailPlatformStatus
  64. // );
  65. const onlineStr = status ? "在线" : "离线";
  66. resolve(onlineStr);
  67. });
  68. };
  69. export const markConversationAsRead = (conversation, fromChating = false) => {
  70. //const _IMSDK = getIMSDK();
  71. if (conversation.unreadCount !== 0) {
  72. IMSDK.asyncApi(
  73. IMSDK.IMMethods.MarkConversationMessageAsRead,
  74. IMSDK.uuid(),
  75. conversation.conversationID,
  76. );
  77. }
  78. const isNomalAtType = nomalTypes.includes(conversation.groupAtType);
  79. if (isNomalAtType) {
  80. console.log('ResetConversationGroupAtType', conversation.conversationID)
  81. IMSDK.asyncApi(
  82. IMSDK.IMMethods.ResetConversationGroupAtType,
  83. IMSDK.uuid(),
  84. conversation.conversationID,
  85. );
  86. }
  87. };
  88. export const prepareConversationState = (conversation, back2Tab = false, refreshChat = true) => {
  89. markConversationAsRead(conversation);
  90. if (conversation.conversationType === SessionType.WorkingGroup) {
  91. store.dispatch("conversation/getCurrentGroup", conversation.groupID);
  92. store.dispatch("conversation/getCurrentMemberInGroup", conversation.groupID);
  93. }
  94. // store.dispatch("message/resetMessageState");
  95. store.commit("conversation/SET_CURRENT_CONVERSATION", conversation);
  96. let url = `/pages_im/pages/conversation/chating1/index?back2Tab=${back2Tab}`;
  97. if (conversation.conversationType === SessionType.Notification) {
  98. url = "/pages_im/pages/conversation/notifyMessageList/index";
  99. }
  100. uni.navigateTo({
  101. url
  102. });
  103. };
  104. export const navigateToDesignatedConversation = (sourceID, sessionType, back2Tab = false, refreshChat = true) => {
  105. console.log("qxj navigateToDesignatedConversation back2Tab", back2Tab);
  106. return new Promise(async (resolve, reject) => {
  107. try {
  108. const _IMSDK = IMSDK;
  109. const {
  110. data
  111. } = await _IMSDK.asyncApi(
  112. _IMSDK.IMMethods.GetOneConversation,
  113. _IMSDK.uuid(), {
  114. sessionType,
  115. sourceID
  116. }
  117. );
  118. prepareConversationState(data, back2Tab);
  119. resolve(data);
  120. } catch (e) {
  121. reject(e);
  122. }
  123. });
  124. };
  125. export const setConversation = (conversationID, exStr) => {
  126. return new Promise(async (resolve, reject) => {
  127. try {
  128. //const _IMSDK = getIMSDK();
  129. const _IMSDK = IMSDK;
  130. const {
  131. data
  132. } = await _IMSDK.asyncApi(
  133. _IMSDK.IMMethods.SetConversation,
  134. _IMSDK.uuid(), {
  135. conversationID: conversationID,
  136. isPinned: false,
  137. ex: exStr
  138. }
  139. );
  140. console.log("qxj data:" + JSON.stringify(data));
  141. resolve();
  142. } catch (e) {
  143. reject(e);
  144. }
  145. });
  146. };
  147. export const scanQrCodeResult = (result) => {
  148. if (result?.includes(AddFriendQrCodePrefix)) {
  149. const userID = result.replace(AddFriendQrCodePrefix, "");
  150. uni.navigateTo({
  151. url: `/pages_im/common/userCard/index?sourceID=${userID}&isScan=true`,
  152. });
  153. } else if (result?.includes(AddGroupQrCodePrefix)) {
  154. const groupID = result.replace(AddGroupQrCodePrefix, "");
  155. uni.navigateTo({
  156. url: `/pages/common/groupCard/index?sourceID=${groupID}&isScan=true`,
  157. });
  158. } else {
  159. uni.$u.toast("未识别到有效内容");
  160. }
  161. };
  162. export const callingModule = uni.requireNativePlugin("OUICalling");
  163. export const meetingModule = uni.requireNativePlugin("OUIMeeting");
  164. export const offlinePushInfo = {
  165. title: "河山恒康",
  166. desc: "您有一条新消息",
  167. ex: "",
  168. iOSPushSound: "",
  169. iOSBadgeCount: true,
  170. };
  171. export const getOfflinePushInfo = (data) => {
  172. }
  173. export const getConversationContent = (message, currentUserID) => {
  174. let myID = currentUserID;
  175. if (!myID) {
  176. try {
  177. const currentStore = getStore();
  178. myID = currentStore.getters.storeCurrentUserID;
  179. } catch (e) {
  180. // console.warn("Store not available in getConversationContent");
  181. }
  182. }
  183. if (
  184. !message.groupID ||
  185. GroupSystemMessageTypes.includes(message.contentType) ||
  186. message.sendID === myID ||
  187. message.contentType === MessageType.GroupAnnouncementUpdated
  188. ) {
  189. return parseMessageByType(message, myID);
  190. }
  191. return `${message.senderNickname}:${parseMessageByType(message, myID)}`;
  192. };
  193. export const meetingErrorHandle = ({
  194. errCode,
  195. errMsg
  196. }) => {
  197. let message = ""
  198. if (errCode === 1004) {
  199. message = "会议已结束或会议不存在!"
  200. } else if (errCode === 200001) {
  201. message = "您已在其他会议中!"
  202. }
  203. uni.$u.toast(message || errMsg);
  204. }