226 lines
7.1 KiB
Plaintext
226 lines
7.1 KiB
Plaintext
/*
|
|
* Copyright (c) 2022 NetEase, Inc. All rights reserved.
|
|
* Use of this source code is governed by a MIT license that can be
|
|
* found in the LICENSE file.
|
|
*
|
|
*/
|
|
|
|
import { CoreKitClient } from '@nimkit/corekit';
|
|
import {
|
|
IM_SDK_VERSION,
|
|
NIM,
|
|
NIMInterface,
|
|
V2NIMConnectStatus,
|
|
V2NIMDataSyncState,
|
|
V2NIMDataSyncType,
|
|
V2NIMEnableServiceType,
|
|
V2NIMError,
|
|
V2NIMKickedOfflineDetail,
|
|
V2NIMLoginStatus,
|
|
V2NIMMessage,
|
|
V2NIMMessageRevokeNotification,
|
|
V2NIMP2PMessageMuteMode
|
|
} from '@nimsdk/base';
|
|
import { ContactRepo } from '../../../Index';
|
|
import { KitLogger } from './logger/AppLogger';
|
|
import { LoggerKitImpl } from './logger/LoggerKitImpl';
|
|
import { ChatRepo } from './repo/ChatRepo';
|
|
import { saveLocalRevokeMessageFormOther } from './utils/MessageUtils';
|
|
import { router } from '@kit.ArkUI';
|
|
import { HMRouterMgr } from '@hadss/hmrouter';
|
|
import { NotificationUtil } from '@itcast/basic';
|
|
import { BusinessError } from '@kit.BasicServicesKit';
|
|
import { NotificationBasicOptions } from '@itcast/basic/src/main/ets/pushnotification/NotificationOptions';
|
|
|
|
export const currentConversationChanged: string = 'CurrentConversationChanged'
|
|
|
|
export class ChatKitClient {
|
|
declare static nim: NIMInterface
|
|
static haveSyncedConversation: boolean = false
|
|
//是否主动离开群
|
|
static selfLeaveTeam = false
|
|
static hasInitListener = false
|
|
static currentConversationId: string = ''
|
|
static networkAvailable: boolean = true
|
|
static logger: KitLogger | undefined = undefined
|
|
|
|
static init(nimSdk: NIMInterface, appKey: string, disableLog?: boolean) {
|
|
ChatKitClient.nim = nimSdk
|
|
if (disableLog !== true) {
|
|
if (nimSdk instanceof NIM) {
|
|
let logger = new LoggerKitImpl(nimSdk.context.cacheDir)
|
|
ChatKitClient.logger = new KitLogger(logger)
|
|
}
|
|
}
|
|
ChatKitClient.haveSyncedConversation = false
|
|
ChatKitClient.initListener()
|
|
CoreKitClient.init({
|
|
appKey: appKey,
|
|
imVersion: IM_SDK_VERSION,
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 长连接是否断开
|
|
* 可用于判断网络是否断开
|
|
*/
|
|
static connectBroken() {
|
|
if(ChatKitClient.nim!=null&&ChatKitClient.nim!=undefined&&ChatKitClient.nim.loginService!=null&&ChatKitClient.nim.loginService!=undefined)
|
|
{
|
|
return ChatKitClient.nim.loginService.getConnectStatus() !== V2NIMConnectStatus.V2NIM_CONNECT_STATUS_CONNECTED
|
|
}
|
|
else
|
|
return false
|
|
|
|
}
|
|
|
|
/**
|
|
* 设置当前会话id
|
|
* @param conversationId 当前会话id
|
|
*/
|
|
static setCurrentConversationId(conversationId: string) {
|
|
ChatKitClient.currentConversationId = conversationId
|
|
getContext().eventHub.emit(currentConversationChanged, conversationId)
|
|
}
|
|
|
|
/**
|
|
* 清除当前会话id
|
|
*/
|
|
static clearCurrentConversationId() {
|
|
ChatKitClient.currentConversationId = ''
|
|
getContext().eventHub.emit(currentConversationChanged, '')
|
|
}
|
|
|
|
/**
|
|
* 获取当前会话id
|
|
* @returns 当前会话id
|
|
*/
|
|
static getCurrentConversationId(): string {
|
|
return ChatKitClient.currentConversationId
|
|
}
|
|
|
|
/**
|
|
* 是否登录
|
|
* @returns
|
|
*/
|
|
static hasLogin(): boolean {
|
|
return ChatKitClient.nim != null && ChatKitClient.nim.loginService.getLoginUser() != null
|
|
}
|
|
|
|
static getLoginUserId(): string {
|
|
return ChatKitClient.nim.loginService.getLoginUser()
|
|
}
|
|
|
|
/**
|
|
* IM 主数据是否同步完成
|
|
* @returns
|
|
*/
|
|
static isMainDataSynced(): boolean {
|
|
let dataSync = ChatKitClient.nim.loginService.getDataSync()
|
|
if (dataSync != null) {
|
|
for (const item of dataSync) {
|
|
if (item.type === V2NIMDataSyncType.V2NIM_DATA_SYNC_TYPE_MAIN &&
|
|
item.state === V2NIMDataSyncState.V2NIM_DATA_SYNC_STATE_COMPLETED) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
static isLocalConversation(): boolean {
|
|
return ChatKitClient.nim.isServiceEnable(V2NIMEnableServiceType.LOCAL_CONVERSATION)
|
|
}
|
|
|
|
/**
|
|
* 等待登录后执行
|
|
*/
|
|
static runAfterLoggedIn(fn: Function) {
|
|
if (ChatKitClient.nim.loginService?.getLoginStatus() === V2NIMLoginStatus.V2NIM_LOGIN_STATUS_LOGINED) {
|
|
fn()
|
|
} else {
|
|
const onLoginStatusFunc = (status: V2NIMLoginStatus) => {
|
|
if (status === V2NIMLoginStatus.V2NIM_LOGIN_STATUS_LOGINED) {
|
|
fn()
|
|
ChatKitClient.nim.loginService.off('onLoginStatus', onLoginStatusFunc)
|
|
}
|
|
}
|
|
ChatKitClient.nim.loginService.on('onLoginStatus', onLoginStatusFunc)
|
|
}
|
|
}
|
|
|
|
/*
|
|
* 销毁 清理监听
|
|
*/
|
|
static onDestroy() {
|
|
ChatKitClient.hasInitListener = false
|
|
ChatKitClient.haveSyncedConversation = false
|
|
ChatKitClient.selfLeaveTeam = false
|
|
ChatKitClient.currentConversationId = ''
|
|
ChatRepo.offRevokeMessage(ChatKitClient.onRevokeFun)
|
|
ChatKitClient.nim.conversationService?.off('onSyncFinished', ChatKitClient.onSyncFinishedFun)
|
|
ChatKitClient.nim.loginService.off('onDataSync', ChatKitClient.dataSyncFun)
|
|
ChatKitClient.nim.loginService.off('onKickedOffline', (detail: V2NIMKickedOfflineDetail) => {
|
|
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 初始化监听
|
|
*/
|
|
private static initListener() {
|
|
if (ChatKitClient.hasInitListener) {
|
|
return
|
|
}
|
|
console.info("netease ChatKitClient initListener ");
|
|
ChatKitClient.nim.loginService.on("onKickedOffline", (detail: V2NIMKickedOfflineDetail) => {
|
|
// const detail = ChatKitClient.nim.loginService.getKickedOfflineDetail()
|
|
console.log('Response onKickedOffline'+detail.clientType+' 22 '+detail.customClientType)
|
|
|
|
HMRouterMgr.removeAll()
|
|
router.replaceUrl({
|
|
url: 'pages/LoginPage/LoginPage', // 目标url
|
|
},router.RouterMode.Single)
|
|
router.clear()
|
|
})
|
|
// 数据同步监听
|
|
ChatKitClient.nim.conversationService?.on('onSyncFinished', ChatKitClient.onSyncFinishedFun)
|
|
ChatKitClient.nim.loginService.on('onDataSync', ChatKitClient.dataSyncFun)
|
|
// 消息撤回监听,消息撤回后,会收到通知
|
|
ChatRepo.onRevokeMessage(ChatKitClient.onRevokeFun)
|
|
ChatKitClient.hasInitListener = true
|
|
|
|
// ChatKitClient.nim.messageService?.on("onReceiveMessages", (messages: V2NIMMessage[]) => {
|
|
// for (const message of messages) {
|
|
// let basicOptions: NotificationBasicOptions = {
|
|
// id: Number(message.messageServerId),
|
|
// title: String(message['fromNick']),
|
|
// text: String(message.text),
|
|
// }
|
|
// NotificationUtil.publishBasic(basicOptions).then((id) => {
|
|
//
|
|
// }).catch((err: BusinessError) => {
|
|
// console.error('推送展示失败:', err)
|
|
// });
|
|
// }
|
|
// })
|
|
|
|
}
|
|
|
|
private static onSyncFinishedFun = () => {
|
|
console.debug(`Performance Test onSyncFinishedFun`)
|
|
ChatKitClient.haveSyncedConversation = true
|
|
}
|
|
private static dataSyncFun = (type: V2NIMDataSyncType, state: V2NIMDataSyncState, error?: V2NIMError) => {
|
|
if (state === V2NIMDataSyncState.V2NIM_DATA_SYNC_STATE_COMPLETED) {
|
|
ContactRepo.getFriendList()
|
|
}
|
|
}
|
|
private static onRevokeFun = (messages: V2NIMMessageRevokeNotification[]) => {
|
|
messages.forEach((msg, index, messages) => {
|
|
if (msg.messageRefer.conversationId !== ChatKitClient.currentConversationId) {
|
|
saveLocalRevokeMessageFormOther(msg.messageRefer.conversationId, msg, false)
|
|
}
|
|
})
|
|
}
|
|
} |