123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- import TUIChatEngine, {
- TUIFriendService,
- TUIConversationService,
- TUIGroupService,
- TUIUserService,
- TUITranslateService,
- AddFriendParams,
- JoinGroupParams,
- } from '@tencentcloud/chat-uikit-engine';
- import { TUIGlobal } from '@tencentcloud/universal-api';
- import { Toast, TOAST_TYPE } from '../../common/Toast/index';
- export const generateAvatar = (item: any): string => {
- return (
- item?.avatar
- || item?.profile?.avatar
- || (item?.groupID && 'https://web.sdk.qcloud.com/im/assets/images/Public.svg')
- || 'https://bucket.sxdirectpurchase.com/wx/static/img/ImAvatar.png'
- );
- };
- export const generateName = (item: any): string => {
- return (
- item?.remark
- || item?.name
- || item?.profile?.nick
- || item?.nick
- || item?.groupID
- || item?.userID
- || ''
- );
- };
- export const generateContactInfoName = (item: any): string => {
- return (
- item?.name
- || item?.profile?.nick
- || item?.nick
- || item?.groupID
- || item?.userID
- || ''
- );
- };
- // Parse the basic information display content of the contactInfo module
- // Group information display: group ID group type
- // User information display: user ID personal signature
- export const generateContactInfoBasic = (
- contactInfo: any,
- ): any[] => {
- const res = [
- {
- label: contactInfo?.groupID ? '群ID' : 'ID',
- data: contactInfo?.groupID || contactInfo?.userID || '',
- },
- ];
- if (!isApplicationType(contactInfo)) {
- res.push({
- label: contactInfo?.groupID ? '群类型' : '个性签名',
- data: contactInfo?.type || contactInfo?.profile?.selfSignature || '',
- });
- }
- return res;
- };
- export const isApplicationType = (info: any) => {
- return (
- info?.type === TUIChatEngine?.TYPES?.SNS_APPLICATION_SENT_TO_ME
- || info?.type === TUIChatEngine?.TYPES?.SNS_APPLICATION_SENT_BY_ME
- );
- };
- export const isFriend = (info: any): Promise<boolean> => {
- return new Promise((resolve, reject) => {
- if (info?.groupID || !info?.userID) {
- resolve(false);
- return;
- }
- if (info?.addTime) {
- resolve(true);
- return;
- }
- TUIFriendService.checkFriend({
- userIDList: [info?.userID],
- type: TUIChatEngine.TYPES.SNS_CHECK_TYPE_BOTH,
- })
- .then((res: any) => {
- switch (res?.data?.successUserIDList[0]?.relation) {
- // No friend relationship: A does not have B in his friend list, and B does not have A in his friend list
- case TUIChatEngine.TYPES.SNS_TYPE_NO_RELATION:
- resolve(false);
- break;
- // Single-item friend: A has B in his friend list, but B does not have A in his friend list
- case TUIChatEngine.TYPES.SNS_TYPE_A_WITH_B:
- resolve(false);
- break;
- // Single-item friend: A does not have B in his friend list, but B has A in his friend list
- case TUIChatEngine.TYPES.SNS_TYPE_B_WITH_A:
- resolve(false);
- break;
- // Two-way friendship
- case TUIChatEngine.TYPES.SNS_TYPE_BOTH_WAY:
- resolve(true);
- break;
- default:
- resolve(false);
- break;
- }
- })
- .catch((error: any) => {
- console.warn('checkFriend error', error);
- reject(error);
- });
- });
- };
- // Change friend‘s remark
- export const updateFriendRemark = (userID: string, remark: string) => {
- // eslint-disable-next-line no-control-regex
- if (remark?.replace(/[^\u0000-\u00ff]/g, 'aa')?.length > 96) {
- Toast({
- message: TUITranslateService.t('TUIContact.修改备注失败: 备注长度不得超过 96 字节'),
- type: TOAST_TYPE.ERROR,
- });
- return;
- }
- TUIFriendService.updateFriend({
- userID,
- remark,
- })
- .then(() => {
- Toast({
- message: TUITranslateService.t('TUIContact.修改备注成功'),
- type: TOAST_TYPE.SUCCESS,
- });
- })
- .catch((error: any) => {
- console.warn('update friend remark failed:', error);
- Toast({
- message: TUITranslateService.t('TUIContact.修改备注失败'),
- type: TOAST_TYPE.ERROR,
- });
- });
- };
- // Delete one friend
- export const deleteFriend = (userID: string) => {
- TUIFriendService.deleteFriend({
- userIDList: [userID],
- type: TUIChatEngine.TYPES.SNS_DELETE_TYPE_BOTH,
- })
- .then((res: any) => {
- const { successUserIDList } = res.data;
- if (successUserIDList[0].userID === userID) {
- Toast({
- message: TUITranslateService.t('TUIContact.删除好友成功'),
- type: TOAST_TYPE.SUCCESS,
- });
- } else {
- Toast({
- message: TUITranslateService.t('TUIContact.删除好友失败'),
- type: TOAST_TYPE.ERROR,
- });
- }
- })
- .catch((error: any) => {
- console.warn('delete friend failed:', error);
- Toast({
- message: TUITranslateService.t('TUIContact.删除好友失败'),
- type: TOAST_TYPE.ERROR,
- });
- });
- };
- // Add friend
- export const addFriend = (params: AddFriendParams) => {
- TUIFriendService.addFriend(params)
- .then(() => {
- Toast({
- message: TUITranslateService.t('TUIContact.申请已发送'),
- type: TOAST_TYPE.SUCCESS,
- });
- })
- .catch((error: any) => {
- console.warn('delete friend failed:', error);
- Toast({
- message: TUITranslateService.t('TUIContact.申请发送失败'),
- type: TOAST_TYPE.ERROR,
- });
- });
- };
- // Enter conversation
- export const enterConversation = (item: any) => {
- const conversationID = item?.groupID
- ? `GROUP${item?.groupID}`
- : `C2C${item?.userID}`;
- TUIConversationService.switchConversation(conversationID).catch(
- (error: any) => {
- console.warn('switch conversation failed:', error);
- Toast({
- message: TUITranslateService.t('TUIContact.进入会话失败'),
- type: TOAST_TYPE.ERROR,
- });
- },
- );
- };
- // Accept friend application
- export const acceptFriendApplication = (userID: string) => {
- TUIFriendService.acceptFriendApplication({
- userID,
- type: TUIChatEngine.TYPES.SNS_APPLICATION_AGREE_AND_ADD,
- })
- .then(() => {
- Toast({
- message: TUITranslateService.t('TUIContact.添加好友成功'),
- type: TOAST_TYPE.SUCCESS,
- });
- })
- .catch((error: any) => {
- console.warn('accept friend application failed:', error);
- Toast({
- message: TUITranslateService.t('TUIContact.同意好友申请失败'),
- type: TOAST_TYPE.ERROR,
- });
- });
- };
- // Refuse friend application
- export const refuseFriendApplication = (userID: string) => {
- TUIFriendService.refuseFriendApplication(userID)
- .then(() => {
- Toast({
- message: TUITranslateService.t('TUIContact.拒绝成功'),
- type: TOAST_TYPE.SUCCESS,
- });
- })
- .catch((error: any) => {
- console.warn('accept friend application failed:', error);
- Toast({
- message: TUITranslateService.t('TUIContact.拒绝好友申请失败'),
- type: TOAST_TYPE.ERROR,
- });
- });
- };
- // Dismiss group
- export const dismissGroup = (groupID: string) => {
- TUIGroupService.dismissGroup(groupID)
- .then(() => {
- Toast({
- message: TUITranslateService.t('TUIContact.解散群聊成功'),
- type: TOAST_TYPE.SUCCESS,
- });
- TUIGlobal?.updateContactSearch && TUIGlobal?.updateContactSearch();
- })
- .catch((error: any) => {
- console.warn('dismiss group failed:', error);
- Toast({
- message: TUITranslateService.t('TUIContact.解散群聊失败'),
- type: TOAST_TYPE.ERROR,
- });
- });
- };
- // Quit group
- export const quitGroup = (groupID: string) => {
- TUIGroupService.quitGroup(groupID)
- .then(() => {
- Toast({
- message: TUITranslateService.t('TUIContact.退出群组成功'),
- type: TOAST_TYPE.SUCCESS,
- });
- })
- .catch((error: any) => {
- console.warn('quit group failed:', error);
- Toast({
- message: TUITranslateService.t('TUIContact.退出群组失败'),
- type: TOAST_TYPE.ERROR,
- });
- });
- };
- // Join group
- export const joinGroup = (groupID: string, applyMessage?: string) => {
- TUIGroupService.joinGroup({
- groupID,
- applyMessage,
- } as JoinGroupParams)
- .then((imResponse: { data: { status?: string } }) => {
- switch (imResponse?.data?.status) {
- case TUIChatEngine.TYPES.JOIN_STATUS_WAIT_APPROVAL: // Wait for administrator approval
- Toast({
- message: TUITranslateService.t('TUIContact.等待管理员同意'),
- type: TOAST_TYPE.SUCCESS,
- });
- break;
- case TUIChatEngine.TYPES.JOIN_STATUS_SUCCESS: // Join group successfully
- Toast({
- message: TUITranslateService.t('TUIContact.加群成功'),
- type: TOAST_TYPE.SUCCESS,
- });
- break;
- case TUIChatEngine.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // Already in the group
- Toast({
- message: TUITranslateService.t('TUIContact.您已是群成员'),
- type: TOAST_TYPE.SUCCESS,
- });
- break;
- default:
- break;
- }
- })
- .catch((error: any) => {
- console.warn('join group failed:', error);
- Toast({
- message: '申请入群失败',
- type: TOAST_TYPE.ERROR,
- });
- });
- };
- // Add to blacklist
- export const addToBlacklist = (userID: string, successCallBack?: () => void) => {
- TUIUserService.addToBlacklist({
- userIDList: [userID],
- })
- .then(() => {
- successCallBack && successCallBack();
- })
- .catch((error: any) => {
- console.warn('add to blacklist failed:', error);
- Toast({
- message: TUITranslateService.t('TUIContact.加入黑名单失败'),
- type: TOAST_TYPE.ERROR,
- });
- });
- };
- // Remove from Blacklist
- export const removeFromBlacklist = (
- userID: string,
- successCallBack?: () => void,
- ) => {
- TUIUserService.removeFromBlacklist({
- userIDList: [userID],
- })
- .then(() => {
- successCallBack && successCallBack();
- })
- .catch((error: any) => {
- console.warn('remove from blacklist failed:', error);
- Toast({
- message: TUITranslateService.t('TUIContact.移除黑名单失败'),
- type: TOAST_TYPE.ERROR,
- });
- });
- };
|