Files
2026-03-04 15:32:30 +08:00

2 lines
11 KiB
JavaScript

import e from"@tencentcloud/lite-chat/basic";const t="undefined"!=typeof wx&&"function"==typeof wx.getSystemInfoSync&&("mac"===wx.getSystemInfoSync().platform||"windows"===wx.getSystemInfoSync().platform),n="undefined"!=typeof wx&&"function"==typeof wx.getSystemInfoSync&&Boolean(wx.getSystemInfoSync().fontSizeSetting)||t,s="undefined"!=typeof qq&&"function"==typeof qq.getSystemInfoSync&&Boolean(qq.getSystemInfoSync().fontSizeSetting),i="undefined"!=typeof tt&&"function"==typeof tt.getSystemInfoSync&&Boolean(tt.getSystemInfoSync().fontSizeSetting),o="undefined"!=typeof swan&&"function"==typeof swan.getSystemInfoSync&&Boolean(swan.getSystemInfoSync().fontSizeSetting),E="undefined"!=typeof my&&"function"==typeof my.getSystemInfoSync&&Boolean(my.getSystemInfoSync().fontSizeSetting),r="undefined"!=typeof jd&&"function"==typeof jd.getSystemInfoSync,a="undefined"!=typeof uni&&"undefined"==typeof window,S=n||s||i||o||E||r||a,c="undefined"!=typeof uni,g=("undefined"!=typeof uni||"undefined"!=typeof window)&&!S,T=s?qq:i?tt:o?swan:E?my:n?wx:a?uni:r?jd:g?window:{};class I{constructor(){this.serviceMap=new Map}registerService(e,t){this.serviceMap.has(e)||(console.log(`TUIServiceManager.registerService serviceName:${e}`),this.serviceMap.set(e,t))}unregisterService(e){this.serviceMap.has(e)&&(console.log(`TUIServiceManager.unregisterService serviceName:${e}`),this.serviceMap.delete(e))}getService(e){return this.serviceMap.get(e)}callService(e){const{serviceName:t,method:n,params:s,callback:i}=e,o=this.getService(t);if(console.log(`TUIServiceManager.callService serviceName:${t}, service:`,o),o)return o.onCall(n,s,i)}}class h{constructor(){this.extensionMap=new Map}registerExtension(e,t){if(console.log(`TUIExtensionManager.registerExtension extensionID:${e}`),!this.extensionMap.has(e)){const t=[];this.extensionMap.set(e,t)}const n=this.extensionMap.get(e);-1===n.indexOf(t)&&n.push(t)}unregisterExtension(e,t){if(console.log(`TUIExtensionManager.unregisterExtension extensionID:${e}`),this.extensionMap.has(e)){const n=this.extensionMap.get(e),s=n.indexOf(t);s>-1&&n.splice(s,1)}}getExtensionList(e,t){const n=t?`params:${JSON.stringify(t)}`:"";console.log(`TUIExtensionManager.getExtensionList extensionID:${e} ${n}`);let s=[],i=[];if(this.extensionMap.has(e)){s=this.extensionMap.get(e);for(let n=0;n<s.length;n++){const o=s[n].onGetExtension(e,t);i=i.concat(o)}}return i}}const u=["vue2","vue3","rn"],_={TUILogin:{EVENT:{LOGIN_STATE_CHANGED:"loginStateChanged"},EVENT_SUB_KEY:{USER_LOGIN_SUCCESS:"userLoginSuccess",USER_LOGOUT_SUCCESS:"userLogoutSuccess",USER_KICKED_OFFLINE:"userKickedOffline",USER_SIG_EXPIRED:"userSigExpired"}},TUIConversation:{SERVICE:{NAME:"TUIConversationService",METHOD:{CREATE_CONVERSATION:"createConversation",HIDE_CONVERSATION_HEADER:"hideConversationHeader"}},EXTENSION:{CONV_POP_MENU:{EXT_ID:"conversationPopMenu"},CONV_GROUP:{EXT_ID:"conversationGroup"}}},TUIChat:{SERVICE:{NAME:"TUIChatService",METHOD:{UPDATE_MESSAGE_LIST:"updateMessageList",CLOSE_MESSAGE_POP_MENU:"closeMessagePopMenu",SEND_CUSTOM_MESSAGE:"sendCustomMessage",SEND_TEXT_MESSAGE:"sendTextMessage",SET_CHAT_TYPE:"setChatType",UPDATE_MESSAGE_INFO:"updateMessageInfo"}},EXTENSION:{INPUT_MORE:{EXT_ID:"inputToolBarMore"},MSG_POP_MENU:{EXT_ID:"messagePopMenu"},CHAT_HEADER:{EXT_ID:"chatHeader"}},TYPE:{C2C:"C2C",GROUP:"GROUP",CUSTOMER_SERVICE:"customerService",ROOM:"room"},EVENT:{CHAT_STATE_CHANGED:"chatStateChanged",CHAT_TYPE_CHANGED:"chatTypeChanged"},EVENT_SUB_KEY:{CHAT_OPENED:"chatOpened",CHANGE_SUCCESS:"changeSuccess"},FEATURE:{DownloadFile:"DownloadFile",CopyMessage:"CopyMessage",DeleteMessage:"DeleteMessage",RevokeMessage:"RevokeMessage",QuoteMessage:"QuoteMessage",ForwardMessage:"ForwardMessage",TranslateMessage:"TranslateMessage",VoiceToText:"VoiceToText",MultiSelection:"MultiSelection",EmojiReaction:"EmojiReaction",InputEmoji:"InputEmoji",InputStickers:"InputStickers",InputImage:"InputImage",InputVoice:"InputVoice",InputVideo:"InputVideo",InputFile:"InputFile",InputEvaluation:"InputEvaluation",InputQuickReplies:"InputQuickReplies",InputMention:"InputMention",MessageSearch:"MessageSearch",ReadStatus:"ReadStatus"}},TUIGroup:{SERVICE:{NAME:"TUIGroupService",METHOD:{CREATE_GROUP:"createGroup",OPEN_GROUP_MANAGEMENT:"openGroupManagement",SELECT_GROUP_MEMBER:"selectGroupMember"}}},TUIContact:{SERVICE:{NAME:"TUIContactService",METHOD:{SELECT_FRIEND:"selectFriend"}},EXTENSION:{CONTACT_LIST:{EXT_ID:"contactList"}}},TUISearch:{SERVICE:{NAME:"TUISearchService",METHOD:{SEARCH_GROUP:"searchGroup",SEARCH_USER:"searchUser",SEARCH_FRIEND:"searchFriend",SEARCH_GROUP_MEMBER:"searchGroupMember"}},EXTENSION:{SEARCH_MORE:{EXT_ID:"searchMore"}}},TUICalling:{SERVICE:{NAME:"TUICallingService",METHOD:{START_CALL:"startCall"}}},TUIRoom:{SERVICE:{NAME:"TUIRoomService"}},TUIEmojiPlugin:{SERVICE:{NAME:"TUIEmojiPluginService",METHOD:{CLOSE_EMOJI_DETAIL:"closeEmojiDetail"}}},TUITranslate:{EVENT:{LANGUAGE_CHANGED:"languageChanged"},EVENT_SUB_KEY:{CHANGE_SUCCESS:"changeSuccess"}},TUITranslatePlugin:{SERVICE:{NAME:"TUITranslatePluginService"}},TUIVoiceToTextPlugin:{SERVICE:{NAME:"TUIVoiceToTextPluginService"}},TUICustomerServicePlugin:{SERVICE:{NAME:"TUICustomerServicePlugin",METHOD:{ACTIVE_CONVERSATION:"activeConversation",FILTER_MESSAGE:"filterMessage"}},EXTENSION:{}},TUIPushPlugin:{SERVICE:{NAME:"TUIPushPluginService"}},TUIChatbotPlugin:{SERVICE:{NAME:"TUIChatbotPluginService"}},TUITheme:{EVENT:{THEME_CHANGED:"themeChanged"},EVENT_SUB_KEY:{CHANGE_SUCCESS:"changeSuccess"}}},C={[_.TUITranslatePlugin.SERVICE.NAME]:Math.pow(2,38),[_.TUIVoiceToTextPlugin.SERVICE.NAME]:Math.pow(2,39),[_.TUICustomerServicePlugin.SERVICE.NAME]:Math.pow(2,40),[_.TUIPushPlugin.SERVICE.NAME]:Math.pow(2,41),[_.TUIChatbotPlugin.SERVICE.NAME]:Math.pow(2,42),[_.TUIEmojiPlugin.SERVICE.NAME]:Math.pow(2,48)};class l{constructor(e){this.loginStatusPromise=new Map,this.SDKAppID=0,this.userID="",this.userSig="",this.TUICore=e,this.isOnChatEvent=!1}static getInstance(e){return T.__$RTC_TUICore__&&T.__$RTC_TUILogin__?T.__$RTC_TUILogin__:(!l.instance&&e&&(l.instance=new l(e)),T.__$RTC_TUILogin__=l.instance,l.instance)}login(t){const{SDKAppID:n,userID:s,userSig:i,scene:o="",framework:E}=t,r=Object.assign(Object.assign({},t),{scene:o||this.getStatScene(E)});return this.chat=e.create(r),this.EVENT=e.EVENT,this.TYPES=e.TYPES,this.SDKAppID=n,this.userID=s,this.userSig=i,this.offChatEvent(),this.onChatEvent(),this._addSDKExtensions(),this.loginChat(t)}logout(){return this.chat.logout().then(e=>(this.userID="",this.userSig="",this.TUICore.notifyEvent(_.TUILogin.EVENT.LOGIN_STATE_CHANGED,_.TUILogin.EVENT_SUB_KEY.USER_LOGOUT_SUCCESS),Promise.resolve(e)))}destroy(){return this.chat.destroy().then(e=>(this.userID="",this.userSig="",this.TUICore.notifyEvent(_.TUILogin.EVENT.LOGIN_STATE_CHANGED,_.TUILogin.EVENT_SUB_KEY.USER_LOGOUT_SUCCESS),Promise.resolve(e)))}setLogLevel(e){return this.chat.setLogLevel(e)}getContext(){return{chat:this.chat,SDKAppID:this.SDKAppID,userID:this.userID,userSig:this.userSig}}reportPlugin(){var e;for(const t in C)this.TUICore.getService(t)&&(null===(e=this.chat.callExperimentalAPI("isFeatureEnabledForStat",C[t]))||void 0===e||e.catch(()=>{}))}loginChat(e){const{userID:t,userSig:n}=e;return new Promise((e,s)=>{this.chat.login({userID:t,userSig:n}).then(t=>{t.data.repeatLogin&&this.chat.isReady()&&e(t),this.reportPlugin(),this.loginStatusPromise.set("login",{resolve:e,reject:s,imResponse:t})}).catch(e=>{s(e)})})}onChatEvent(){this.chat.on(this.EVENT.SDK_READY,this.onSDKReady,this),this.chat.on(this.EVENT.SDK_NOT_READY,this.onSDKNotReady,this),this.chat.on(this.EVENT.KICKED_OUT,this.onUserKicked,this),this.isOnChatEvent=!0}offChatEvent(){this.isOnChatEvent&&(this.chat.off(this.EVENT.SDK_READY,this.onSDKReady,this),this.chat.off(this.EVENT.SDK_NOT_READY,this.onSDKNotReady,this),this.chat.off(this.EVENT.KICKED_OUT,this.onUserKicked,this),this.isOnChatEvent=!1)}onSDKReady(){if(this.loginStatusPromise.has("login")){this.TUICore.notifyEvent(_.TUILogin.EVENT.LOGIN_STATE_CHANGED,_.TUILogin.EVENT_SUB_KEY.USER_LOGIN_SUCCESS);const e=setTimeout(()=>{const t=this.loginStatusPromise.get("login");t.resolve(t.imResponse),clearTimeout(e),this.loginStatusPromise.delete("login")},500)}}onSDKNotReady(){if(this.loginStatusPromise.has("login")){this.loginStatusPromise.get("login").reject(new Error("sdk not ready"))}this.loginStatusPromise.delete("login")}onUserKicked(e){e.data.type!==this.TYPES.KICKED_OUT_USERSIG_EXPIRED?this.TUICore.notifyEvent(_.TUILogin.EVENT.LOGIN_STATE_CHANGED,_.TUILogin.EVENT_SUB_KEY.USER_KICKED_OFFLINE):this.TUICore.notifyEvent(_.TUILogin.EVENT.LOGIN_STATE_CHANGED,_.TUILogin.EVENT_SUB_KEY.USER_SIG_EXPIRED)}_addSDKExtensions(){Array.isArray(T.__$RTC_CHAT__)&&T.__$RTC_CHAT__.length>0&&this.chat.use(T.__$RTC_CHAT__)}getStatScene(e){return e&&u.indexOf(e)>-1?"rn"===e?"38":c?"vue2"===e?"31":"32":"vue2"===e?"29":"30":e}}class U{constructor(){this.eventMap=new Map}registerEvent(e,t,n){const s=this.getKey(e,t);if(console.log(`TUIEventManager.registerEvent eventName:${e} subKey:${t}`),!this.eventMap.has(s)){const e=[];this.eventMap.set(s,e)}const i=this.eventMap.get(s);-1===i.indexOf(n)&&(i.push(n),this.renotify(e,t,n))}unregisterEvent(e,t,n){console.log(`TUIEventManager.unregisterEvent eventName:${e} subKey:${t}`);const s=this.getKey(e,t);if(this.eventMap.has(s)){const e=this.eventMap.get(s),t=e.indexOf(n);t>-1&&e.splice(t,1)}}notifyEvent(e,t,n){const s=this.getKey(e,t),i=n?`params:${JSON.stringify(n)}`:"";if(console.log(`TUIEventManager.notifyEvent eventName:${e} subKey:${t} ${i}`),this.eventMap.has(s)){this.eventMap.get(s).forEach(s=>{s.onNotifyEvent(e,t,n)})}}getKey(e,t){let n=e;return t&&(n=`${e}-${n}`),n}renotify(e,t,n){if(e===_.TUILogin.EVENT.LOGIN_STATE_CHANGED&&t===_.TUILogin.EVENT_SUB_KEY.USER_LOGIN_SUCCESS){const{chat:e}=l.getInstance().getContext();e&&e.isReady()&&(n.onNotifyEvent(_.TUILogin.EVENT.LOGIN_STATE_CHANGED,_.TUILogin.EVENT_SUB_KEY.USER_LOGIN_SUCCESS),console.log("TUIEventManager.renotify success."))}}}class M{constructor(){this.serviceManager=new I,this.extensionManager=new h,this.eventManager=new U}static getInstance(){return T.__$RTC_TUICore__&&T.__$RTC_TUILogin__?T.__$RTC_TUICore__:(M.instance||(console.log("TUICore.getInstance ok."),M.instance=new M),T.__$RTC_TUICore__=M.instance,M.instance)}registerService(e,t){return this.serviceManager.registerService(e,t)}unregisterService(e){return this.serviceManager.unregisterService(e)}getService(e){return this.serviceManager.getService(e)}callService(e){return this.serviceManager.callService(e)}registerExtension(e,t){return this.extensionManager.registerExtension(e,t)}unregisterExtension(e,t){return this.extensionManager.unregisterExtension(e,t)}getExtensionList(e,t){return this.extensionManager.getExtensionList(e,t)}registerEvent(e,t,n){return this.eventManager.registerEvent(e,t,n)}unregisterEvent(e,t,n){return this.eventManager.unregisterEvent(e,t,n)}notifyEvent(e,t,n){return this.eventManager.notifyEvent(e,t,n)}}console.log("TUICore-Lite.VERSION:1.0.0");const N=M.getInstance(),v=l.getInstance(N);export{_ as TUIConstants,N as TUICore,v as TUILogin,N as default};