UNPKG

@tencentcloud/ai-desk-customer-wechat

Version:

AI Desk customer UIKit for wechat mini program

3 lines (2 loc) 5.85 kB
"use strict";var U=Object.defineProperty;var L=(u,e,t)=>e in u?U(u,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):u[e]=t;var r=(u,e,t)=>(L(u,typeof e!="symbol"?e+"":e,t),t);const l=require("./utils/index.js"),i=require("./@aidesk/uikit-engine/index.js"),s=require("./utils/logger.js"),D=require("./components/common/Toast/index-uniapp.js"),C=require("./adapter-vue-uniapp.js"),g=require("./utils/utils.js"),S=require("./utils/state.js"),f=require("./components/common/Toast/type.js"),T="1.6.8",a=class a{constructor(){r(this,"isLoggedIn");r(this,"customerServiceIDList");r(this,"currentCustomerServiceID");r(this,"loggedInUserID");r(this,"myProfile");r(this,"bindOnActionConversation");r(this,"paramsForActiveAgain");r(this,"_scene");r(this,"clientCustomData");this.customerServiceIDList=["@customer_service_account"],this.currentCustomerServiceID=this.customerServiceIDList[0],this.isLoggedIn=!1,this.loggedInUserID="",this.myProfile={},this.bindOnActionConversation=this.onActiveConversation.bind(this),this._scene="",this.clientCustomData=""}static getInstance(){return a.instance||(a.instance=new a),a.instance}async loginCustomerUIKit(e,t,c){return l.clearChatStorage(e,t),i.Vt.login({SDKAppID:e,userID:t,userSig:c,useUploadPlugin:!0,scene:this._scene}).then(()=>{s.Log.i(`login success. userID:${t}`),this.isLoggedIn=!0,this.loggedInUserID=t,i.Kt.watch(i.a.CUSTOM,{activeConversation:this.bindOnActionConversation}),this.switchConversation(`C2C${this.currentCustomerServiceID}`),g.switchReadStatus(S.state.get("showReadStatus")),i.Vt.chat.callExperimentalAPI("isFeatureEnabledForStat",Math.pow(2,42))}).catch(n=>{D.Toast({message:i.jt.t("TUIChat.登录失败"),type:f.TOAST_TYPE.ERROR,duration:3e4}),s.Log.l(n)})}async init(e,t,c){if(s.Log.l(`TUICustomerServer.init vue:${C.vueVersion} version:${T} SDKAppID:${e} userID:${t} isLoggedIn:${this.isLoggedIn} loggedInUserID:${this.loggedInUserID} currentCustomerServiceID:${this.currentCustomerServiceID}`),t){const n=g.validateUserID(t);if(n>0){setTimeout(()=>{let o="";n===1?o=i.jt.t("AIDesk.userID 只能包含可打印 ASCII 字符"):n===2?o=i.jt.t("AIDesk.userID 不能包含'administrator'"):n===3&&(o=i.jt.t("AIDesk.userID 长度不能超过45字节")),D.Toast({message:o,type:f.TOAST_TYPE.ERROR,duration:3e4})},500);return}}if(this.isLoggedIn){if(this.loggedInUserID===t){await this.switchConversation(`C2C${this.currentCustomerServiceID}`);return}return this.unInit().finally(()=>{this.isLoggedIn=!1,this.loginCustomerUIKit(e,t,c)})}else return this.loginCustomerUIKit(e,t,c)}async initWithProfile(e){const{SDKAppID:t,userID:c,userSig:n,nickName:o,avatar:m,customerServiceID:I,scene:v,clientCustomData:d}=e;return s.Log.l(`TUICustomerServer.initWithProfile version:${T} scene:${v}`),o&&(this.myProfile.nick=o),m&&(this.myProfile.avatar=m),I&&(this.currentCustomerServiceID=I,this.customerServiceIDList.includes(this.currentCustomerServiceID)||this.customerServiceIDList.push(this.currentCustomerServiceID)),v?this._scene=v:C.vueVersion===2?this._scene="aidesk-customer-uni-vue2":C.vueVersion===3&&(this._scene="aidesk-customer-uni-vue3"),d?this.clientCustomData=d:this.clientCustomData="",this.init(t,c,n)}async unInit(){s.Log.l("TUICustomerServer.unInit"),i.Kt.unwatch(i.a.CUSTOM,{activeConversation:this.bindOnActionConversation}),this.isLoggedIn=!1;let e=S.state.get("preloadedEmojiMap");return e&&e.clear(),i.Vt.logout()}sendTextMessage(e,t){return i.Yt.sendTextMessage(e,t)}sendCustomMessage(e,t){return i.Yt.sendCustomMessage(e,t)}changeLanguage(e){return i.jt.changeLanguage(e).then(()=>{s.Log.i(`language changed to ${e}`)})}getLoggedInUserID(){return this.loggedInUserID}isCustomerConversation(e){const t=e&&e.slice(3)||"";return this.customerServiceIDList.indexOf(t)>-1}isCustomerServicePluginMessage(e){return!e||!this.isCustomerConversation(e.conversationID)||l.isThinkingMessage(e)?!1:l.isCustomerServiceMessage(e)||l.isMessageInvisible(e)}onGetExtension(e){if(e===TUIConstants.TUIContact.EXTENSION.CONTACT_LIST.EXT_ID)return[{weight:0,icon:"",text:"智能客服",data:{name:"customer",accountList:this.customerServiceIDList}}]}setCustomerServiceIDList(e){!Array.isArray(e)||e.length===0||(this.customerServiceIDList=e,this.currentCustomerServiceID=e[0],s.Log.i("TUICustomerServer.setCustomerServiceIDList",this.customerServiceIDList))}transferToTaskFlow(e){g.transferToTaskFlow(this.currentCustomerServiceID,e.taskFlowID,e.description)}transferToHuman(e){g.transferToHuman(this.currentCustomerServiceID,e.groupID,e.specificMemberList,e.description)}clearConversationUnreadCount(e){s.Log.l(`TUICustomerServer.clearConversationUnreadCount conversationID:${e}`),e?i.Ht.setMessageRead(e):i.Ht.setMessageRead("C2C@customer_service_account")}onActiveConversation(e){e&&(s.Log.l("TUICustomerServer.onActiveConversation options:",e),this.isCustomerConversation(e.conversationID)&&(Object.keys(this.myProfile).length>0?(s.Log.l(`TUICustomerServer.onActiveConversation updateMyProfile:${JSON.stringify(this.myProfile)}`),i.Bt.updateMyProfile({...this.myProfile}).finally(()=>{this.activeServiceFlow(e)})):this.activeServiceFlow(e)))}async switchConversation(e){(await i.Ht.switchConversation(e)).code===-100002&&this.activeServiceFlow(this.paramsForActiveAgain)}activeServiceFlow(e){s.Log.l("TUICustomerServer.activeServiceFlow params:",e,`clientCustomData:${this.clientCustomData}`),this.paramsForActiveAgain={...e},i.Yt.sendCustomMessage({to:e.conversationID.slice(3),conversationType:i.Vt.TYPES.CONV_C2C,payload:{data:JSON.stringify({src:"7",customerServicePlugin:0,triggeredContent:{language:e.robotLang||"",clientCustomData:this.clientCustomData}})}},{onlineUserOnly:!0}),s.Log.w("TUICustomerServer.activeServiceFlow src 7 sent")}};r(a,"instance");let h=a;exports.TUICustomerServer=h; //# sourceMappingURL=../../.sourcemap/mp-weixin/ai-desk-customer-uniapp/server.js.map