index.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import TUIChatEngine, {
  2. TUIFriendService,
  3. TUIConversationService,
  4. TUIGroupService,
  5. TUIUserService,
  6. TUITranslateService,
  7. AddFriendParams,
  8. JoinGroupParams,
  9. } from '@tencentcloud/chat-uikit-engine';
  10. import { TUIGlobal } from '@tencentcloud/universal-api';
  11. import { Toast, TOAST_TYPE } from '../../common/Toast/index';
  12. export const generateAvatar = (item: any): string => {
  13. return (
  14. item?.avatar
  15. || item?.profile?.avatar
  16. || (item?.groupID && 'https://web.sdk.qcloud.com/im/assets/images/Public.svg')
  17. || 'https://web.sdk.qcloud.com/component/TUIKit/assets/avatar_21.png'
  18. );
  19. };
  20. export const generateName = (item: any): string => {
  21. return (
  22. item?.remark
  23. || item?.name
  24. || item?.profile?.nick
  25. || item?.nick
  26. || item?.groupID
  27. || item?.userID
  28. || ''
  29. );
  30. };
  31. export const generateContactInfoName = (item: any): string => {
  32. return (
  33. item?.name
  34. || item?.profile?.nick
  35. || item?.nick
  36. || item?.groupID
  37. || item?.userID
  38. || ''
  39. );
  40. };
  41. // Parse the basic information display content of the contactInfo module
  42. // Group information display: group ID group type
  43. // User information display: user ID personal signature
  44. export const generateContactInfoBasic = (
  45. contactInfo: any,
  46. ): any[] => {
  47. const res = [
  48. {
  49. label: contactInfo?.groupID ? '群ID' : 'ID',
  50. data: contactInfo?.groupID || contactInfo?.userID || '',
  51. },
  52. ];
  53. if (!isApplicationType(contactInfo)) {
  54. res.push({
  55. label: contactInfo?.groupID ? '群类型' : '个性签名',
  56. data: contactInfo?.type || contactInfo?.profile?.selfSignature || '',
  57. });
  58. }
  59. return res;
  60. };
  61. export const isApplicationType = (info: any) => {
  62. return (
  63. info?.type === TUIChatEngine?.TYPES?.SNS_APPLICATION_SENT_TO_ME
  64. || info?.type === TUIChatEngine?.TYPES?.SNS_APPLICATION_SENT_BY_ME
  65. );
  66. };
  67. export const isFriend = (info: any): Promise<boolean> => {
  68. return new Promise((resolve, reject) => {
  69. if (info?.groupID || !info?.userID) {
  70. resolve(false);
  71. return;
  72. }
  73. if (info?.addTime) {
  74. resolve(true);
  75. return;
  76. }
  77. TUIFriendService.checkFriend({
  78. userIDList: [info?.userID],
  79. type: TUIChatEngine.TYPES.SNS_CHECK_TYPE_BOTH,
  80. })
  81. .then((res: any) => {
  82. switch (res?.data?.successUserIDList[0]?.relation) {
  83. // No friend relationship: A does not have B in his friend list, and B does not have A in his friend list
  84. case TUIChatEngine.TYPES.SNS_TYPE_NO_RELATION:
  85. resolve(false);
  86. break;
  87. // Single-item friend: A has B in his friend list, but B does not have A in his friend list
  88. case TUIChatEngine.TYPES.SNS_TYPE_A_WITH_B:
  89. resolve(false);
  90. break;
  91. // Single-item friend: A does not have B in his friend list, but B has A in his friend list
  92. case TUIChatEngine.TYPES.SNS_TYPE_B_WITH_A:
  93. resolve(false);
  94. break;
  95. // Two-way friendship
  96. case TUIChatEngine.TYPES.SNS_TYPE_BOTH_WAY:
  97. resolve(true);
  98. break;
  99. default:
  100. resolve(false);
  101. break;
  102. }
  103. })
  104. .catch((error: any) => {
  105. console.warn('checkFriend error', error);
  106. reject(error);
  107. });
  108. });
  109. };
  110. // Change friend‘s remark
  111. export const updateFriendRemark = (userID: string, remark: string) => {
  112. // eslint-disable-next-line no-control-regex
  113. if (remark?.replace(/[^\u0000-\u00ff]/g, 'aa')?.length > 96) {
  114. Toast({
  115. message: TUITranslateService.t('TUIContact.修改备注失败: 备注长度不得超过 96 字节'),
  116. type: TOAST_TYPE.ERROR,
  117. });
  118. return;
  119. }
  120. TUIFriendService.updateFriend({
  121. userID,
  122. remark,
  123. })
  124. .then(() => {
  125. Toast({
  126. message: TUITranslateService.t('TUIContact.修改备注成功'),
  127. type: TOAST_TYPE.SUCCESS,
  128. });
  129. })
  130. .catch((error: any) => {
  131. console.warn('update friend remark failed:', error);
  132. Toast({
  133. message: TUITranslateService.t('TUIContact.修改备注失败'),
  134. type: TOAST_TYPE.ERROR,
  135. });
  136. });
  137. };
  138. // Delete one friend
  139. export const deleteFriend = (userID: string) => {
  140. TUIFriendService.deleteFriend({
  141. userIDList: [userID],
  142. type: TUIChatEngine.TYPES.SNS_DELETE_TYPE_BOTH,
  143. })
  144. .then((res: any) => {
  145. const { successUserIDList } = res.data;
  146. if (successUserIDList[0].userID === userID) {
  147. Toast({
  148. message: TUITranslateService.t('TUIContact.删除好友成功'),
  149. type: TOAST_TYPE.SUCCESS,
  150. });
  151. } else {
  152. Toast({
  153. message: TUITranslateService.t('TUIContact.删除好友失败'),
  154. type: TOAST_TYPE.ERROR,
  155. });
  156. }
  157. })
  158. .catch((error: any) => {
  159. console.warn('delete friend failed:', error);
  160. Toast({
  161. message: TUITranslateService.t('TUIContact.删除好友失败'),
  162. type: TOAST_TYPE.ERROR,
  163. });
  164. });
  165. };
  166. // Add friend
  167. export const addFriend = (params: AddFriendParams) => {
  168. TUIFriendService.addFriend(params)
  169. .then(() => {
  170. Toast({
  171. message: TUITranslateService.t('TUIContact.申请已发送'),
  172. type: TOAST_TYPE.SUCCESS,
  173. });
  174. })
  175. .catch((error: any) => {
  176. console.warn('delete friend failed:', error);
  177. Toast({
  178. message: TUITranslateService.t('TUIContact.申请发送失败'),
  179. type: TOAST_TYPE.ERROR,
  180. });
  181. });
  182. };
  183. // Enter conversation
  184. export const enterConversation = (item: any) => {
  185. const conversationID = item?.groupID
  186. ? `GROUP${item?.groupID}`
  187. : `C2C${item?.userID}`;
  188. TUIConversationService.switchConversation(conversationID).catch(
  189. (error: any) => {
  190. console.warn('switch conversation failed:', error);
  191. Toast({
  192. message: TUITranslateService.t('TUIContact.进入会话失败'),
  193. type: TOAST_TYPE.ERROR,
  194. });
  195. },
  196. );
  197. };
  198. // Accept friend application
  199. export const acceptFriendApplication = (userID: string) => {
  200. TUIFriendService.acceptFriendApplication({
  201. userID,
  202. type: TUIChatEngine.TYPES.SNS_APPLICATION_AGREE_AND_ADD,
  203. })
  204. .then(() => {
  205. Toast({
  206. message: TUITranslateService.t('TUIContact.添加好友成功'),
  207. type: TOAST_TYPE.SUCCESS,
  208. });
  209. })
  210. .catch((error: any) => {
  211. console.warn('accept friend application failed:', error);
  212. Toast({
  213. message: TUITranslateService.t('TUIContact.同意好友申请失败'),
  214. type: TOAST_TYPE.ERROR,
  215. });
  216. });
  217. };
  218. // Refuse friend application
  219. export const refuseFriendApplication = (userID: string) => {
  220. TUIFriendService.refuseFriendApplication(userID)
  221. .then(() => {
  222. Toast({
  223. message: TUITranslateService.t('TUIContact.拒绝成功'),
  224. type: TOAST_TYPE.SUCCESS,
  225. });
  226. })
  227. .catch((error: any) => {
  228. console.warn('accept friend application failed:', error);
  229. Toast({
  230. message: TUITranslateService.t('TUIContact.拒绝好友申请失败'),
  231. type: TOAST_TYPE.ERROR,
  232. });
  233. });
  234. };
  235. // Dismiss group
  236. export const dismissGroup = (groupID: string) => {
  237. TUIGroupService.dismissGroup(groupID)
  238. .then(() => {
  239. Toast({
  240. message: TUITranslateService.t('TUIContact.解散群聊成功'),
  241. type: TOAST_TYPE.SUCCESS,
  242. });
  243. TUIGlobal?.updateContactSearch && TUIGlobal?.updateContactSearch();
  244. })
  245. .catch((error: any) => {
  246. console.warn('dismiss group failed:', error);
  247. Toast({
  248. message: TUITranslateService.t('TUIContact.解散群聊失败'),
  249. type: TOAST_TYPE.ERROR,
  250. });
  251. });
  252. };
  253. // Quit group
  254. export const quitGroup = (groupID: string) => {
  255. TUIGroupService.quitGroup(groupID)
  256. .then(() => {
  257. Toast({
  258. message: TUITranslateService.t('TUIContact.退出群组成功'),
  259. type: TOAST_TYPE.SUCCESS,
  260. });
  261. })
  262. .catch((error: any) => {
  263. console.warn('quit group failed:', error);
  264. Toast({
  265. message: TUITranslateService.t('TUIContact.退出群组失败'),
  266. type: TOAST_TYPE.ERROR,
  267. });
  268. });
  269. };
  270. // Join group
  271. export const joinGroup = (groupID: string, applyMessage?: string) => {
  272. TUIGroupService.joinGroup({
  273. groupID,
  274. applyMessage,
  275. } as JoinGroupParams)
  276. .then((imResponse: { data: { status?: string } }) => {
  277. switch (imResponse?.data?.status) {
  278. case TUIChatEngine.TYPES.JOIN_STATUS_WAIT_APPROVAL: // Wait for administrator approval
  279. Toast({
  280. message: TUITranslateService.t('TUIContact.等待管理员同意'),
  281. type: TOAST_TYPE.SUCCESS,
  282. });
  283. break;
  284. case TUIChatEngine.TYPES.JOIN_STATUS_SUCCESS: // Join group successfully
  285. Toast({
  286. message: TUITranslateService.t('TUIContact.加群成功'),
  287. type: TOAST_TYPE.SUCCESS,
  288. });
  289. break;
  290. case TUIChatEngine.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // Already in the group
  291. Toast({
  292. message: TUITranslateService.t('TUIContact.您已是群成员'),
  293. type: TOAST_TYPE.SUCCESS,
  294. });
  295. break;
  296. default:
  297. break;
  298. }
  299. })
  300. .catch((error: any) => {
  301. console.warn('join group failed:', error);
  302. Toast({
  303. message: '申请入群失败',
  304. type: TOAST_TYPE.ERROR,
  305. });
  306. });
  307. };
  308. // Add to blacklist
  309. export const addToBlacklist = (userID: string, successCallBack?: () => void) => {
  310. TUIUserService.addToBlacklist({
  311. userIDList: [userID],
  312. })
  313. .then(() => {
  314. successCallBack && successCallBack();
  315. })
  316. .catch((error: any) => {
  317. console.warn('add to blacklist failed:', error);
  318. Toast({
  319. message: TUITranslateService.t('TUIContact.加入黑名单失败'),
  320. type: TOAST_TYPE.ERROR,
  321. });
  322. });
  323. };
  324. // Remove from Blacklist
  325. export const removeFromBlacklist = (
  326. userID: string,
  327. successCallBack?: () => void,
  328. ) => {
  329. TUIUserService.removeFromBlacklist({
  330. userIDList: [userID],
  331. })
  332. .then(() => {
  333. successCallBack && successCallBack();
  334. })
  335. .catch((error: any) => {
  336. console.warn('remove from blacklist failed:', error);
  337. Toast({
  338. message: TUITranslateService.t('TUIContact.移除黑名单失败'),
  339. type: TOAST_TYPE.ERROR,
  340. });
  341. });
  342. };