ysyt-agent-sdk 1.0.85 → 1.0.86
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/src/index.d.ts
CHANGED
|
@@ -6,9 +6,6 @@ export declare class ReactiveStore<T extends Record<string, any>> {
|
|
|
6
6
|
private proxy;
|
|
7
7
|
constructor(initialState: T);
|
|
8
8
|
getState(): T;
|
|
9
|
-
/**
|
|
10
|
-
* 推荐使用的统一更新方法,支持批量更新、保证只触发一次 notify
|
|
11
|
-
*/
|
|
12
9
|
setState(partial: Partial<T>, forceNotify?: boolean): void;
|
|
13
10
|
/**
|
|
14
11
|
* 订阅整个 state 的变化
|
|
@@ -911,4 +911,4 @@ const Cs=A=>(e,t)=>{void 0!==t?t.addInitializer(()=>{customElements.define(A,e)}
|
|
|
911
911
|
`:""}
|
|
912
912
|
</div>
|
|
913
913
|
</my-popover>
|
|
914
|
-
`;is(s,this.rttHTML)}}class Ks{static requestPermission(){"Notification"in window&&"default"===Notification.permission&&Notification.requestPermission()}static async requestMediaPermissions(){try{const A=await navigator.mediaDevices.enumerateDevices();if(A.some(A=>"audioinput"===A.kind&&""!==A.label))return null;return await navigator.mediaDevices.getUserMedia({audio:!0})}catch(A){return console.warn("获取音频权限失败:",A),null}}static show(A,e,t){if("Notification"in window&&"granted"===Notification.permission){const s=new Notification(A,{body:e,tag:"xy-global-notification",renotify:!0});s.onclick=()=>{window.focus(),s.close(),t?.()},setTimeout(()=>s.close(),5e3)}}static async checkMediaPermissions(){try{return(await navigator.mediaDevices.enumerateDevices()).some(A=>"audioinput"===A.kind&&""!==A.label)}catch(A){return console.warn("无法检查设备权限:",A),!1}}}const Os=console.log,Ns=(...A)=>{Ve.logBuiltinEnabled&&Os(...A)};let bs=null;class Js{constructor(A){this.url=A,this.ws=null,this.heartbeatWorker=null,this.reconnectDelay=3e3,this.manualClose=!1,this.apiClient=new ks,this.initWebSocket()}static getInstance(A){if(!bs){if(!A)throw new Error("WebSocket 尚未初始化");bs=new Js(A)}return bs}stateIdleChange(){const{state:A}=Ve.stateObject;A!==Ae.IDLE&&this.apiClient._changeState({data:{...Ve.stateBeforeChange}})}async getCustomerInfo(A){try{await this.apiClient._getInCallCrmDetail({data:{phone:A},success:A=>{const{contact_count:e,customer_name:t,phone:s,city:i,province:r}=A.data;ae.emit(se.CUSTOMER_INFO,{customer_name:t||"未知客户",phone:s,city:i||"未知",province:r||"未知",contact_count:e||0})}})}catch(A){}}putAgentState(){Ve.sessionId="";const{post_call_process_time:A}=Ve.agentInfo,e=A||0;Ve.autoStateTimer&&(clearTimeout(Ve.autoStateTimer),Ve.autoStateTimer=null),ae.emit(se.CUSTOMER_INFO,void 0),e>0?Ve.autoStateTimer=setTimeout(()=>{Ve.autoStateTimer=null,this.stateIdleChange()},e):this.stateIdleChange()}initWebSocket(){const A=be(Le,We);this.ws=new WebSocket(this.url+A),this.ws.onopen=()=>{this.sendMessage({type:0}),this.startHeartbeat()},this.ws.onmessage=A=>{const e=JSON.parse(A.data),{type:t,data:s,code:i,msg:r}=e;if(0!==t&&Ns("%cWebSocket 收到消息:","color: blue; font-weight: bold;",e),t===ie.OUT_CALL&&(0!==i?(he.reset(),ae.emit(se.OUT_FAILED,r),ae.emit(re.FAIL_NOTIFICATION,r),setTimeout(()=>{ae.emit(se.CUSTOMER_INFO,void 0)},500),this.stateIdleChange()):0===i&&ae.emit(se.OUT_SUCCESS)),t===ie.AGENT_STATE){Ve.stateObject.state!==Ae.IDLE&&Ve.stateObject.state!==Ae.BUSY||(Ve.stateBeforeChange=Ve.stateObject);const{state:A,state_name:e}=s;Ve.stateObject={state:A,state_name:e},"振铃中"===e&&he.updateDisplayText(oe.AGENT_RINGING),ae.emit(se.AGENT_STATE,{state:A,state_name:e})}if(t===ie.OUT_CALL_INCOMING_CALL&&(ae.emit(se.OUT_INCOMING_CALL),Ve.sessionId=s.session_id,he.updateActionConfigs(hs)),t===ie.OUT_CALL_END&&(he.updateDisplayText(""),1===s.device_type&&s.agent_no===Ve.agentInfo.agent_no?(this.putAgentState(),he.reset(),ae.emit(se.OUT_HANGUP,s),he.updateActionConfigs(Vs)):2===s.device_type&&s.agent_no===Ve.agentInfo.agent_no?(he.reset(),ae.emit(se.OUR_SIDE_CONSULT_HANGUP,s),this.putAgentState(),he.updateActionConfigs(Vs)):2===s.device_type&&s.agent_no!==Ve.agentInfo.agent_no?(he.updateDisplayText(oe.CALLING),ae.emit(se.OTHER_SIDE_CONSULT_HANGUP,s),he.updateActionConfigs(ls)):3===s.device_type&&s.agent_no===Ve.agentInfo.agent_no?(this.putAgentState(),he.reset(),ae.emit(se.THREE_WAY_OUR_SIDE_HANGUP),he.updateActionConfigs(Vs)):4===s.device_type&&s.agent_no===Ve.agentInfo.agent_no&&(this.putAgentState(),he.reset(),ae.emit(se.TRANSFER_END),he.updateActionConfigs(Vs))),t===ie.OUT_CALL_RINGING&&1===s.type&&(ae.emit(se.OUT_RINGING,s),he.updateDisplayText(oe.RINGING)),t===ie.OUT_CALL_ANSWER&&(ae.emit(se.OUT_OFF_ANSWER,s),he.updateOutCallIsAnswer(!0),he.updateDisplayText(oe.CALLING),he.updateActionConfigs(ls)),t===ie.CONSULT_RINGING&&(ae.emit(se.CONSULT_RINGING),he.updateDisplayText(oe.AGENT_RINGING),he.updateActionConfigs([])),t===ie.CONSULT_ANSWER&&(ae.emit(se.CONSULT_OFF_HOOK,s),he.updateConsultIsAnswer(!0),he.updateDisplayText(oe.CONSULTING),he.updateActionConfigs(gs)),t===ie.CONSULT_FAIL&&(ae.emit(se.CONSULT_FAILED,{custom_reason:s.custom_reason}),he.updateDisplayText(oe.CALLING),he.updateActionConfigs(ls)),t===ie.CONSULT_CALL_IN&&(ae.emit(se.CONSULT_INCOMING,s),he.updateIsCalling(!0),he.updateDirection(ee.CONSULT),he.updateDisplayText(oe.CONSULT_CALL_IN),he.updateActionConfigs(us),Ve.enableBrowserAlert&&Ks.show("📞 咨询来电",`坐席 [${s.agent_name}] 正在咨询...`,()=>{ae.emit(re.NOTIFICATION_CLICK,{agent_mane:s.agent_name})})),t===ie.CONSULT_CALL_IN_SUCCESS&&(Ve.sessionId=s.session_id,ae.emit(se.CONSULT_CALL_IN_SUCCESS),he.updateConsultIsAnswer(!0),he.updateDisplayText(oe.CALLING),he.updateActionConfigs(qs)),t===ie.CONSULT_RETURN_CALL_SUCCESS&&(he.updateConsultIsAnswer(!1),ae.emit(se.CONSULT_RETURN_CALL_SUCCESS),he.updateDisplayText(oe.CALLING),ae.emit(se.OTHER_SIDE_CONSULT_HANGUP,s),he.updateActionConfigs(ls)),t===ie.MUTE&&(0===i?(he.updateIsMuted(!0),ae.emit(se.MUTE_SUCCESS),he.updateDisplayText(oe.MUTING)):(ae.emit(se.MUTE_FAILED,r),ae.emit(re.FAIL_NOTIFICATION,r))),t===ie.UNMUTE&&(0===i?(he.updateIsMuted(!1),ae.emit(se.UNMUTE_SUCCESS),he.updateDisplayText(oe.CALLING)):(ae.emit(se.UNMUTE_FAILED,r),ae.emit(re.FAIL_NOTIFICATION,r))),t===ie.CALL_HOLD&&(0===i?(he.updateIsHold(!0),he.updateDisplayText(oe.HOLDING),ae.emit(se.HOLD_SUCCESS)):(ae.emit(se.HOLD_FAILED,r),ae.emit(re.FAIL_NOTIFICATION,r))),t===ie.CALL_UNHOLD&&(0===i?(he.updateDisplayText(oe.CALLING),he.updateIsHold(!1),ae.emit(se.UNHOLD_SUCCESS)):(ae.emit(se.UNHOLD_FAILED,r),ae.emit(re.FAIL_NOTIFICATION,r))),t===ie.CONSULT_TRANSFER&&(0===i?ae.emit(se.CONSULT_TRANSFER_SUCCESS):(ae.emit(se.CONSULT_TRANSFER_FAILED,r),ae.emit(re.FAIL_NOTIFICATION,r))),t===ie.CONSULT_TRANSFER_FAILED&&ae.emit(se.CONSULT_TRANSFER_FAILED,s.status),t===ie.CONSULT_TRANSFER_OFF_HOOK&&(he.updateDirection(ee.INCOMING),he.updateDisplayText(oe.CALLING),ae.emit(se.CONSULT_TRANSFER_OFF_HOOK),he.updateActionConfigs(ps),he.updateIncomingIsAnswer(!0),he.updateConsultIsAnswer(!1)),t===ie.INCOMING_CALL&&(Ve.sessionId=s.session_id,he.updateIsCalling(!0),he.updateDirection(ee.INCOMING),he.updateDisplayText(oe.INCOMING_CALL),ae.emit(se.INCOMING_CALL,{customer_phone:s.customer_phone}),he.updateActionConfigs(us),this.getCustomerInfo(s.customer_phone),Ve.enableBrowserAlert&&Ks.show("📞 客户来电",`客户 [${s.customer_phone}] 正在呼入...`,()=>{ae.emit(re.NOTIFICATION_CLICK,{customer_phone:s.customer_phone})})),t===ie.INCOMING_CALL_OFF_HOOK&&(he.updateIncomingIsAnswer(!0),ae.emit(se.INCOMING_CALL_OFF_HOOK),he.updateActionConfigs(ps),he.updateDisplayText(oe.CALLING)),t===ie.INCOMING_CALL_END&&(he.updateDisplayText(""),1===s.device_type&&s.agent_no===Ve.agentInfo.agent_no?(this.putAgentState(),he.reset(),ae.emit(se.INCOMING_CALL_END),he.updateActionConfigs(Vs)):2===s.device_type&&s.agent_no===Ve.agentInfo.agent_no?(he.reset(),ae.emit(se.OUR_SIDE_CONSULT_HANGUP,s),this.putAgentState(),he.updateActionConfigs(Vs)):2===s.device_type&&s.agent_no!==Ve.agentInfo.agent_no?(he.updateDisplayText(oe.CALLING),ae.emit(se.OTHER_SIDE_CONSULT_HANGUP,s),he.updateActionConfigs(ls)):3===s.device_type&&s.agent_no===Ve.agentInfo.agent_no?(this.putAgentState(),he.reset(),ae.emit(se.THREE_WAY_OUR_SIDE_HANGUP),he.updateActionConfigs(Vs)):4===s.device_type&&s.agent_no===Ve.agentInfo.agent_no&&(this.putAgentState(),he.reset(),ae.emit(se.TRANSFER_END),he.updateActionConfigs(Vs))),t===ie.SATISFACTION_EVALUATION&&(0===i?ae.emit(se.SATISFACTION_EVALUATION_SUCCESS):(ae.emit(se.SATISFACTION_EVALUATION_FAILED,r),ae.emit(re.FAIL_NOTIFICATION,r))),t===ie.CONSULT_THREE_WAY&&(0!==i?(ae.emit(se.CONSULT_THREE_WAY_FAILED,r),ae.emit(re.FAIL_NOTIFICATION,r)):0===i&&he.updateIsMeeting(!0)),t===ie.CONSULT_THREE_WAY_RESULT&&("success"===s.status?(he.updateDirection(ee.MEETING),he.updateDisplayText(oe.MEETING),he.updateActionConfigs(ds),ae.emit(se.CONSULT_THREE_WAY_SUCCESS)):(ae.emit(se.CONSULT_THREE_WAY_FAILED,{conf_name:s.conf_name}),he.updateIsMeeting(!1))),t===ie.TRANSFER_INCOMING_CALL&&(s.agent_no===Ve.agentInfo.agent_no?(he.updateDisplayText(oe.TRANSFERRING),ae.emit(se.TRANSFER_PROGRESS),he.updateActionConfigs([]),he.updateTransferIsAnswer(!0)):(ae.emit(se.TRANSFER_INCOMING_CALL,s),he.updateIsCalling(!0),he.updateDirection(ee.TRANSFER),he.updateDisplayText(oe.TRANSFER_INCOMING_CALL),he.updateActionConfigs(us),Ve.enableBrowserAlert&&Ks.show("📞 转接来电",`坐席 [${s.agent_no}] 正在转接...`,()=>{ae.emit(re.NOTIFICATION_CLICK,{agent_mane:s.agent_no})}))),t===ie.TRANSFER_OFF_HOOK&&(Ve.sessionId=s.session_id,ae.emit(se.TRANSFER_OFF_HOOK),he.updateTransferIsAnswer(!0),he.updateDisplayText(oe.CALLING),he.updateActionConfigs(ps)),t===ie.TRANSFER_FAILED&&"failed"===s.status){ae.emit(se.TRANSFER_FAILED,"未知原因");const A=he.get("direction");A===ee.OUTGOING?he.updateActionConfigs(ls):A===ee.INCOMING&&he.updateActionConfigs(ps)}t===ie.KICK_OFFLINE&&ae.emit(re.KICK_OFF)},this.ws.onclose=()=>{this.stopHeartbeat(),this.manualClose||setTimeout(()=>this.initWebSocket(),this.reconnectDelay)},this.ws.onerror=()=>{this.ws?.close()}}startHeartbeat(){const A=new Blob(["\n let timer;\n self.onmessage = function (e) {\n if (e.data === 'start') {\n timer = setInterval(() => {\n self.postMessage('heartbeat');\n }, 14000);\n } else if (e.data === 'stop') {\n clearInterval(timer);\n }\n };\n "],{type:"application/javascript"}),e=URL.createObjectURL(A);this.heartbeatWorker=new Worker(e),this.heartbeatWorker.onmessage=()=>{this.sendMessage({type:0})},this.heartbeatWorker.postMessage("start")}stopHeartbeat(){this.heartbeatWorker&&(this.heartbeatWorker.postMessage("stop"),this.heartbeatWorker.terminate(),this.heartbeatWorker=null)}sendMessage(A){this.ws?.readyState===WebSocket.OPEN?(100===A.type&&this.getCustomerInfo(A.data.customer_phone),this.ws.send(JSON.stringify(A)),0!==A.type&&Ns("%cWebSocket 发送消息:","color: green; font-weight: bold;",A)):console.warn("WebSocket 未连接,消息未发送")}close(){this.manualClose=!0,this.stopHeartbeat(),this.ws?.close()}static __internalClose(){bs&&(bs.close(),bs=null)}static __internalSend(A){if(!bs)throw new Error("WebSocket 尚未初始化");bs.sendMessage(A)}}class Hs{constructor(A){this.sipClient=null,this.sipClient=A}roleMute(){const A=he.get("isCalling"),e=Ve.sessionId;if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));if(!e)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}))}holdCall(){this.roleMute();const A=he.get("isHold"),{sessionId:e}=Ve;if(!e)return;if(!he.get("actionConfigs").includes("hold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿保持"}));if(A)throw new Error(JSON.stringify({code:1,msg:"当前已处于保持状态"}));Js.__internalSend({type:ie.CALL_HOLD,data:{session_id:e}})}unholdCall(){this.roleMute();const{isHold:A}=he.getState(),{sessionId:e}=Ve;if(!he.get("actionConfigs").includes("unhold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取消保持"}));if(e){if(!A)throw new Error(JSON.stringify({code:1,msg:"当前未处于保持状态"}));Js.__internalSend({type:ie.CALL_UNHOLD,data:{session_id:e}})}}sendDTMF(A){if(1!==he.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!he.get("actionConfigs").includes("dtmf"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿发送按键"}));const{isCalling:e}=he.getState();if(!e)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));this.sipClient.sendDTMF(A)}cancelCall(){if(!he.get("isCalling"))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));const A=Ve.sessionId;Js.__internalSend({type:ie.AGENT_HANGUP,data:{session_id:A}})}async consultCall(A){this.roleMute();const{isCalling:e,outCallIsAnswer:t,consultIsAnswer:s,incomingIsAnswer:i}=he.getState(),r=Ve.sessionId;if(!he.get("actionConfigs").includes("consult"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用咨询"}));if(s)throw new Error(JSON.stringify({code:1,msg:"当前正在咨询"}));if(!(e||t||i))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));if(!r)throw new Error(JSON.stringify({code:1,msg:"sessionId不存在"}));Js.__internalSend({type:ie.CONSULT_REQUEST,data:{session_id:r,consultation_agent_no:A}})}async answerCall(){if(1!==he.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));await this.sipClient.answerCall()}async makeCall(A){const e=he.get("isCalling"),t=he.get("selectOutNumber")||"";if(e)throw new Error(JSON.stringify({code:1,msg:"正在呼叫中,请勿重复点击"}));he.updateIsCalling(!0),he.updateDirection(ee.OUTGOING),Js.__internalSend({type:ie.OUT_CALL,data:{customer_phone:A,caller:t}})}async hangup(){if(!he.get("isCalling"))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));const A=Ve.sessionId;Js.__internalSend({type:ie.AGENT_HANGUP,data:{session_id:A}})}mute(){this.roleMute();const A=he.get("actionConfigs"),e=he.get("isMuted"),t=Ve.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前未处于静音状态"}));if(!A.includes("mute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));Js.__internalSend({type:ie.MUTE,data:{session_id:t}})}unmute(){this.roleMute();const A=he.get("actionConfigs"),e=he.get("isMuted"),t=Ve.sessionId;if(!e)throw new Error(JSON.stringify({code:1,msg:"当前未处于未静音状态"}));if(!A.includes("unmute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));Js.__internalSend({type:ie.UNMUTE,data:{session_id:t}})}consultTransfer(){this.roleMute();const A=he.get("consultIsAnswer"),e=Ve.sessionId;if(!he.get("actionConfigs").includes("consult_transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用咨询转移"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Js.__internalSend({type:ie.CONSULT_TRANSFER,data:{session_id:e}})}satisfactionEvaluation(){this.roleMute();const A=Ve.sessionId;if(!he.get("actionConfigs").includes("satisfaction"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用满意度评价"}));Js.__internalSend({type:ie.SATISFACTION_EVALUATION,data:{session_id:A}})}startConference(){this.roleMute();if(!he.get("actionConfigs").includes("conference"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用三方会议"}));const A=Ve.sessionId;if(!he.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Js.__internalSend({type:ie.CONSULT_THREE_WAY,data:{session_id:A}})}consultRetrieve(){this.roleMute();if(!he.get("actionConfigs").includes("consult_retrieve"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取回通话"}));const A=Ve.sessionId;if(!he.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Js.__internalSend({type:ie.CONSULT_RETURN_CALL,data:{session_id:A}})}async transferAgent(A){this.roleMute();if(!he.get("actionConfigs").includes("transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿转接"}));const{transferIsAnswer:e}=he.getState(),t=Ve.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));Js.__internalSend({type:ie.TRANSFER,data:{session_id:t,transfer_type:0,transfer_dest:A}})}async transferOutline(A){this.roleMute();if(!he.get("actionConfigs").includes("transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿转接"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"号码不能为空"}));if(!/^\d+$/.test(A))throw new Error(JSON.stringify({code:1,msg:"号码格式错误"}));const{transferIsAnswer:e}=he.getState(),t=Ve.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));Js.__internalSend({type:ie.TRANSFER,data:{session_id:t,transfer_type:2,transfer_dest:A}})}}class js{static{this._instance=null}#A=null;#e=new ks;#t;#s=null;#i=!1;#r;#o=!1;constructor(){if(!js._creatingInstance)throw new Error("请使用 YSYTAgentSdk.getInstance() 获取实例");clearInterval(this.#s),this.#s=setInterval(async()=>{const A=await this.#n();_e.updateRttObject(A)},2e3),Ks.requestMediaPermissions(),this.#r=he.subscribeKey("answerDevice",A=>{1===A?this.initWebRtc():this.#A=null}),this.on(re.KICK_OFF,()=>{this.destroy()})}static get instance(){return this._instance}static async destroyInstance(){this._instance&&(await this._instance.destroy(),this._instance=null)}static getInstance(A){if(!this._instance){if(!A)throw new Error("首次调用必须传入配置参数");const{url:e,appKey:t,appSecret:s,agentNo:i}=A;je=e||"https://aicc.yescloudy.com",Le=t,We=s,Pe=i,js._creatingInstance=!0,this._instance=new js,delete js._creatingInstance,window.addEventListener("beforeunload",()=>{this._instance?.destroy()})}return this._instance}getInit(){return this.#o}async initWebRtc(){if(this.#A)return;const A=Ve.agentInfo.sip_account_list.find(A=>1===A.device_type);if(!A)throw new Error(JSON.stringify({code:1,msg:"未找到 sip_account_list"}));{const e=A.sip_register_addr;this.#A=new $e({server:e.split(":")[0],user:A.sip_account,password:A.sip_password,webSocket:`wss://${e}`,callTimeout:3e4},this.#a),await this.#A.start()}}async init({actionNodeParams:A,enableBrowserAlert:e,logBuiltinEnabled:t}){try{const{viewHtmlElement:s,rttHTML:i}=A||{};Ve.logBuiltinEnabled=t||!1,await this.#e._agentLogin(Pe);const{agent_no:r,current_answer_device:o,current_state:n,out_display_number:a,out_display_select_switch:V}=Ve.agentInfo;n===Ae.IDLE?(he.updateActionConfigs(Vs),Ve.stateObject={state:n,state_name:"空闲"}):n===Ae.BUSY&&(he.updateActionConfigs(cs),Ve.stateObject={state:n,state_name:"忙碌"}),1===V?he.updateSelectOutNumber(a):he.updateSelectOutNumber("");let c="";const h=new URL(je).host;c=je.includes("https")?`wss://${h}`:`ws://${h}`,Js.getInstance(`${c}/ccs-ws/api/v1/ws/call/${r}?Authorization=`),he.updateAnswerDevice(o),1===o&&await this.initWebRtc(),this.#t=new Hs(this.#A),A&&s&&(this.#i=!0,new xs({container:s,rttHTML:i,statusParams:{state:Ae.IDLE,statusName:"空闲"}},this,this.#t)),Ve.enableBrowserAlert=e,e&&Ks.requestPermission(),ae.emit(re.AGENT_INIT_STATUS,!0),this.#o=!0}catch(A){this.#i&&Zs("初始化失败"),ae.emit(re.AGENT_INIT_STATUS,!1),this.#o=!1}}#a=A=>{const{type:e,data:t}=A;ae.emit(e,t)};on(A,e){return ae.on(A,e),this}off(A,e){ae.off(A,e)}offAll(){ae.clearAllListeners()}async destroyRtc(){await(this.#A?.destroy())}async destroy(){js._instance&&(await this.destroyRtc(),clearInterval(this.#s),Js.__internalClose(),this.offAll(),this.#r?.(),this.#A=null,this.#i=!1,js._instance=null,he.reset(),he.updateSelectOutNumber(""))}async#n(){return this.#A?this.#A.getNetworkStats():{}}get call_api(){const A=A=>(...e)=>{if(!this.#i)return A.apply(this,e);console.warn("当前未使用个人开发模式")};return{answerCall:A(()=>this.#t.answerCall()),makeCall:A=>this.#t.makeCall(A),cancelCall:A(()=>this.#t.cancelCall()),hangup:A(()=>this.#t.hangup()),sendDTMF:A(A=>this.#t.sendDTMF(A)),holdCall:A(()=>this.#t.holdCall()),unholdCall:A(()=>this.#t.unholdCall()),consultCall:A(async A=>{try{await this.#t.consultCall(A)}catch(A){throw console.warn(A),A}}),consultRetrieve:A(()=>this.#t.consultRetrieve()),consultTransfer:A(()=>this.#t.consultTransfer()),mute:A(()=>this.#t.mute()),unmute:A(()=>this.#t.unmute()),satisfactionEvaluation:A(()=>this.#t.satisfactionEvaluation()),startConference:A(()=>this.#t.startConference()),transferAgent:A(A=>this.#t.transferAgent(A)),transferOutline:A(A=>this.#t.transferOutline(A))}}get agent_api(){const A=A=>(...e)=>{if(!this.#i)return A.apply(this,e);console.warn("当前未使用个人开发模式")};return{changeDevice:A(A=>this.#e.changeDevice(A)),changeState:A(A=>this.#e.changeState(A)),getIdleAgentList:A(A=>this.#e.getIdleAgentList(A)),getAgentState:A(A=>this.#e.getAgentState(A)),setAgentExplicit:A(A=>this.#e.setAgentExplicit(A)),checkAgentLogin:A=>this.#e.checkAgentLogin(A)}}getAgentData(){const{agent_no:A,agent_name:e,answer_devices:t,out_display_data:s,out_display_select_switch:i,current_answer_device:r,current_state:o}=Ve.agentInfo,{stateObject:n}=Ve,a={1:"软电话",2:"手机模式",3:"SIP话机"};return{agent_no:A,agent_name:e,init_state:o,default_device:r,out_display_data:s,out_display_select_switch:i,answer_devices:(t||"").split(",").map(A=>({value:Number(A),label:a[Number(A)]})),stateObject:n}}}import.meta.hot&&import.meta.hot.dispose(()=>{js.instance&&js.destroyInstance()});export{Ae as AgentStatus,se as CallSipCallEvent,ee as DirectionEnum,oe as DisplayTextEnum,re as EventName,te as WebRtcCallSipEventEnum,ie as WebsocketTypeEnum,js as default};
|
|
914
|
+
`;is(s,this.rttHTML)}}class Ks{static requestPermission(){"Notification"in window&&"default"===Notification.permission&&Notification.requestPermission()}static async requestMediaPermissions(){try{const A=await navigator.mediaDevices.enumerateDevices();if(A.some(A=>"audioinput"===A.kind&&""!==A.label))return null;return await navigator.mediaDevices.getUserMedia({audio:!0})}catch(A){return console.warn("获取音频权限失败:",A),null}}static show(A,e,t){if("Notification"in window&&"granted"===Notification.permission){const s=new Notification(A,{body:e,tag:"xy-global-notification",renotify:!0});s.onclick=()=>{window.focus(),s.close(),t?.()},setTimeout(()=>s.close(),5e3)}}static async checkMediaPermissions(){try{return(await navigator.mediaDevices.enumerateDevices()).some(A=>"audioinput"===A.kind&&""!==A.label)}catch(A){return console.warn("无法检查设备权限:",A),!1}}}const Os=console.log,Ns=(...A)=>{Ve.logBuiltinEnabled&&Os(...A)};let bs=null;class Js{constructor(A){this.url=A,this.ws=null,this.heartbeatWorker=null,this.reconnectDelay=3e3,this.manualClose=!1,this.apiClient=new ks,this.initWebSocket()}static getInstance(A){if(!bs){if(!A)throw new Error("WebSocket 尚未初始化");bs=new Js(A)}return bs}stateIdleChange(){const{state:A}=Ve.stateObject;A!==Ae.IDLE&&this.apiClient._changeState({data:{...Ve.stateBeforeChange}})}async getCustomerInfo(A){try{await this.apiClient._getInCallCrmDetail({data:{phone:A},success:A=>{const{contact_count:e,customer_name:t,phone:s,city:i,province:r}=A.data;ae.emit(se.CUSTOMER_INFO,{customer_name:t||"未知客户",phone:s,city:i||"未知",province:r||"未知",contact_count:e||0})}})}catch(A){}}putAgentState(){Ve.sessionId="";const{post_call_process_time:A}=Ve.agentInfo,e=A||0;Ve.autoStateTimer&&(clearTimeout(Ve.autoStateTimer),Ve.autoStateTimer=null),ae.emit(se.CUSTOMER_INFO,void 0),e>0?Ve.autoStateTimer=setTimeout(()=>{Ve.autoStateTimer=null,this.stateIdleChange()},e):this.stateIdleChange()}initWebSocket(){const A=be(Le,We);this.ws=new WebSocket(this.url+A),this.ws.onopen=()=>{this.sendMessage({type:0}),this.startHeartbeat()},this.ws.onmessage=A=>{const e=JSON.parse(A.data),{type:t,data:s,code:i,msg:r}=e;if(0!==t&&Ns("%cWebSocket 收到消息:","color: blue; font-weight: bold;",e),t===ie.OUT_CALL&&(0!==i?(he.reset(),ae.emit(se.OUT_FAILED,r),ae.emit(re.FAIL_NOTIFICATION,r),setTimeout(()=>{ae.emit(se.CUSTOMER_INFO,void 0)},500),this.stateIdleChange()):0===i&&ae.emit(se.OUT_SUCCESS)),t===ie.AGENT_STATE){Ve.stateObject.state!==Ae.IDLE&&Ve.stateObject.state!==Ae.BUSY||(Ve.stateBeforeChange=Ve.stateObject);const{state:A,state_name:e}=s;Ve.stateObject={state:A,state_name:e},"振铃中"===e&&he.updateDisplayText(oe.AGENT_RINGING),ae.emit(se.AGENT_STATE,{state:A,state_name:e})}if(t===ie.OUT_CALL_INCOMING_CALL&&(ae.emit(se.OUT_INCOMING_CALL),Ve.sessionId=s.session_id,he.updateActionConfigs(hs)),t===ie.OUT_CALL_END&&(he.updateDisplayText(""),1===s.device_type&&s.agent_no===Ve.agentInfo.agent_no?(this.putAgentState(),he.reset(),ae.emit(se.OUT_HANGUP,s),he.updateActionConfigs(Vs)):2===s.device_type&&s.agent_no===Ve.agentInfo.agent_no?(he.reset(),ae.emit(se.OUR_SIDE_CONSULT_HANGUP,s),this.putAgentState(),he.updateActionConfigs(Vs)):2===s.device_type&&s.agent_no!==Ve.agentInfo.agent_no?(he.updateDisplayText(oe.CALLING),ae.emit(se.OTHER_SIDE_CONSULT_HANGUP,s),he.updateActionConfigs(ls)):3===s.device_type&&s.agent_no===Ve.agentInfo.agent_no?(this.putAgentState(),he.reset(),ae.emit(se.THREE_WAY_OUR_SIDE_HANGUP),he.updateActionConfigs(Vs)):4===s.device_type&&s.agent_no===Ve.agentInfo.agent_no&&(this.putAgentState(),he.reset(),ae.emit(se.TRANSFER_END),he.updateActionConfigs(Vs))),t===ie.OUT_CALL_RINGING&&1===s.type&&(ae.emit(se.OUT_RINGING,s),he.updateDisplayText(oe.RINGING)),t===ie.OUT_CALL_ANSWER&&(ae.emit(se.OUT_OFF_ANSWER,s),he.updateOutCallIsAnswer(!0),he.updateDisplayText(oe.CALLING),he.updateActionConfigs(ls)),t===ie.CONSULT_RINGING&&(ae.emit(se.CONSULT_RINGING),he.updateDisplayText(oe.AGENT_RINGING),he.updateActionConfigs([])),t===ie.CONSULT_ANSWER&&(ae.emit(se.CONSULT_OFF_HOOK,s),he.updateConsultIsAnswer(!0),he.updateDisplayText(oe.CONSULTING),he.updateActionConfigs(gs)),t===ie.CONSULT_FAIL&&(ae.emit(se.CONSULT_FAILED,{custom_reason:s.custom_reason}),he.updateDisplayText(oe.CALLING),he.updateActionConfigs(ls)),t===ie.CONSULT_CALL_IN&&(ae.emit(se.CONSULT_INCOMING,s),he.updateIsCalling(!0),he.updateDirection(ee.CONSULT),he.updateDisplayText(oe.CONSULT_CALL_IN),he.updateActionConfigs(us),Ve.enableBrowserAlert&&Ks.show("📞 咨询来电",`坐席 [${s.agent_name}] 正在咨询...`,()=>{ae.emit(re.NOTIFICATION_CLICK,{agent_mane:s.agent_name})})),t===ie.CONSULT_CALL_IN_SUCCESS&&(Ve.sessionId=s.session_id,ae.emit(se.CONSULT_CALL_IN_SUCCESS),he.updateConsultIsAnswer(!0),he.updateDisplayText(oe.CALLING),he.updateActionConfigs(qs)),t===ie.CONSULT_RETURN_CALL_SUCCESS&&(he.updateConsultIsAnswer(!1),ae.emit(se.CONSULT_RETURN_CALL_SUCCESS),he.updateDisplayText(oe.CALLING),ae.emit(se.OTHER_SIDE_CONSULT_HANGUP,s),he.updateActionConfigs(ls)),t===ie.MUTE&&(0===i?(he.updateIsMuted(!0),ae.emit(se.MUTE_SUCCESS),he.updateDisplayText(oe.MUTING)):(ae.emit(se.MUTE_FAILED,r),ae.emit(re.FAIL_NOTIFICATION,r))),t===ie.UNMUTE&&(0===i?(he.updateIsMuted(!1),ae.emit(se.UNMUTE_SUCCESS),he.updateDisplayText(oe.CALLING)):(ae.emit(se.UNMUTE_FAILED,r),ae.emit(re.FAIL_NOTIFICATION,r))),t===ie.CALL_HOLD&&(0===i?(he.updateIsHold(!0),he.updateDisplayText(oe.HOLDING),ae.emit(se.HOLD_SUCCESS)):(ae.emit(se.HOLD_FAILED,r),ae.emit(re.FAIL_NOTIFICATION,r))),t===ie.CALL_UNHOLD&&(0===i?(he.updateDisplayText(oe.CALLING),he.updateIsHold(!1),ae.emit(se.UNHOLD_SUCCESS)):(ae.emit(se.UNHOLD_FAILED,r),ae.emit(re.FAIL_NOTIFICATION,r))),t===ie.CONSULT_TRANSFER&&(0===i?ae.emit(se.CONSULT_TRANSFER_SUCCESS):(ae.emit(se.CONSULT_TRANSFER_FAILED,r),ae.emit(re.FAIL_NOTIFICATION,r))),t===ie.CONSULT_TRANSFER_FAILED&&ae.emit(se.CONSULT_TRANSFER_FAILED,s.status),t===ie.CONSULT_TRANSFER_OFF_HOOK&&(he.updateDirection(ee.INCOMING),he.updateDisplayText(oe.CALLING),ae.emit(se.CONSULT_TRANSFER_OFF_HOOK),he.updateActionConfigs(ps),he.updateIncomingIsAnswer(!0),he.updateConsultIsAnswer(!1)),t===ie.INCOMING_CALL&&(Ve.sessionId=s.session_id,he.updateIsCalling(!0),he.updateDirection(ee.INCOMING),he.updateDisplayText(oe.INCOMING_CALL),ae.emit(se.INCOMING_CALL,{customer_phone:s.customer_phone}),he.updateActionConfigs(us),this.getCustomerInfo(s.customer_phone),Ve.enableBrowserAlert&&Ks.show("📞 客户来电",`客户 [${s.customer_phone}] 正在呼入...`,()=>{ae.emit(re.NOTIFICATION_CLICK,{customer_phone:s.customer_phone})})),t===ie.INCOMING_CALL_OFF_HOOK&&(he.updateIncomingIsAnswer(!0),ae.emit(se.INCOMING_CALL_OFF_HOOK),he.updateActionConfigs(ps),he.updateDisplayText(oe.CALLING)),t===ie.INCOMING_CALL_END&&(he.updateDisplayText(""),1===s.device_type&&s.agent_no===Ve.agentInfo.agent_no?(this.putAgentState(),he.reset(),ae.emit(se.INCOMING_CALL_END),he.updateActionConfigs(Vs)):2===s.device_type&&s.agent_no===Ve.agentInfo.agent_no?(he.reset(),ae.emit(se.OUR_SIDE_CONSULT_HANGUP,s),this.putAgentState(),he.updateActionConfigs(Vs)):2===s.device_type&&s.agent_no!==Ve.agentInfo.agent_no?(he.updateDisplayText(oe.CALLING),ae.emit(se.OTHER_SIDE_CONSULT_HANGUP,s),he.updateActionConfigs(ls)):3===s.device_type&&s.agent_no===Ve.agentInfo.agent_no?(this.putAgentState(),he.reset(),ae.emit(se.THREE_WAY_OUR_SIDE_HANGUP),he.updateActionConfigs(Vs)):4===s.device_type&&s.agent_no===Ve.agentInfo.agent_no&&(this.putAgentState(),he.reset(),ae.emit(se.TRANSFER_END),he.updateActionConfigs(Vs))),t===ie.SATISFACTION_EVALUATION&&(0===i?ae.emit(se.SATISFACTION_EVALUATION_SUCCESS):(ae.emit(se.SATISFACTION_EVALUATION_FAILED,r),ae.emit(re.FAIL_NOTIFICATION,r))),t===ie.CONSULT_THREE_WAY&&(0!==i?(ae.emit(se.CONSULT_THREE_WAY_FAILED,r),ae.emit(re.FAIL_NOTIFICATION,r)):0===i&&he.updateIsMeeting(!0)),t===ie.CONSULT_THREE_WAY_RESULT&&("success"===s.status?(he.updateDirection(ee.MEETING),he.updateDisplayText(oe.MEETING),he.updateActionConfigs(ds),ae.emit(se.CONSULT_THREE_WAY_SUCCESS)):(ae.emit(se.CONSULT_THREE_WAY_FAILED,{conf_name:s.conf_name}),he.updateIsMeeting(!1))),t===ie.TRANSFER_INCOMING_CALL&&(s.agent_no===Ve.agentInfo.agent_no?(he.updateDisplayText(oe.TRANSFERRING),ae.emit(se.TRANSFER_PROGRESS),he.updateActionConfigs([]),he.updateTransferIsAnswer(!0)):(ae.emit(se.TRANSFER_INCOMING_CALL,s),he.updateIsCalling(!0),he.updateDirection(ee.TRANSFER),he.updateDisplayText(oe.TRANSFER_INCOMING_CALL),he.updateActionConfigs(us),Ve.enableBrowserAlert&&Ks.show("📞 转接来电",`坐席 [${s.agent_no}] 正在转接...`,()=>{ae.emit(re.NOTIFICATION_CLICK,{agent_mane:s.agent_no})}))),t===ie.TRANSFER_OFF_HOOK&&(Ve.sessionId=s.session_id,ae.emit(se.TRANSFER_OFF_HOOK),he.updateTransferIsAnswer(!0),he.updateDisplayText(oe.CALLING),he.updateActionConfigs(ps)),t===ie.TRANSFER_FAILED&&"failed"===s.status){ae.emit(se.TRANSFER_FAILED,"未知原因");const A=he.get("direction");A===ee.OUTGOING?he.updateActionConfigs(ls):A===ee.INCOMING&&he.updateActionConfigs(ps)}t===ie.KICK_OFFLINE&&ae.emit(re.KICK_OFF)},this.ws.onclose=()=>{this.stopHeartbeat(),this.manualClose||setTimeout(()=>this.initWebSocket(),this.reconnectDelay)},this.ws.onerror=()=>{this.ws?.close()}}startHeartbeat(){const A=new Blob(["\n let timer;\n self.onmessage = function (e) {\n if (e.data === 'start') {\n timer = setInterval(() => {\n self.postMessage('heartbeat');\n }, 14000);\n } else if (e.data === 'stop') {\n clearInterval(timer);\n }\n };\n "],{type:"application/javascript"}),e=URL.createObjectURL(A);this.heartbeatWorker=new Worker(e),this.heartbeatWorker.onmessage=()=>{this.sendMessage({type:0})},this.heartbeatWorker.postMessage("start")}stopHeartbeat(){this.heartbeatWorker&&(this.heartbeatWorker.postMessage("stop"),this.heartbeatWorker.terminate(),this.heartbeatWorker=null)}sendMessage(A){this.ws?.readyState===WebSocket.OPEN?(100===A.type&&this.getCustomerInfo(A.data.customer_phone),this.ws.send(JSON.stringify(A)),0!==A.type&&Ns("%cWebSocket 发送消息:","color: green; font-weight: bold;",A)):console.warn("WebSocket 未连接,消息未发送")}close(){this.manualClose=!0,this.stopHeartbeat(),this.ws?.close()}static __internalClose(){bs&&(bs.close(),bs=null)}static __internalSend(A){if(!bs)throw new Error("WebSocket 尚未初始化");bs.sendMessage(A)}}class Hs{constructor(A){this.sipClient=null,this.sipClient=A}roleMute(){const A=he.get("isCalling"),e=Ve.sessionId;if(!A)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));if(!e)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}))}holdCall(){this.roleMute();const A=he.get("isHold"),{sessionId:e}=Ve;if(!e)return;if(!he.get("actionConfigs").includes("hold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿保持"}));if(A)throw new Error(JSON.stringify({code:1,msg:"当前已处于保持状态"}));Js.__internalSend({type:ie.CALL_HOLD,data:{session_id:e}})}unholdCall(){this.roleMute();const{isHold:A}=he.getState(),{sessionId:e}=Ve;if(!he.get("actionConfigs").includes("unhold"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取消保持"}));if(e){if(!A)throw new Error(JSON.stringify({code:1,msg:"当前未处于保持状态"}));Js.__internalSend({type:ie.CALL_UNHOLD,data:{session_id:e}})}}sendDTMF(A){if(1!==he.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));if(!he.get("actionConfigs").includes("dtmf"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿发送按键"}));const{isCalling:e}=he.getState();if(!e)throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));this.sipClient.sendDTMF(A)}cancelCall(){if(!he.get("isCalling"))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));const A=Ve.sessionId;Js.__internalSend({type:ie.AGENT_HANGUP,data:{session_id:A}})}async consultCall(A){this.roleMute();const{isCalling:e,outCallIsAnswer:t,consultIsAnswer:s,incomingIsAnswer:i}=he.getState(),r=Ve.sessionId;if(!he.get("actionConfigs").includes("consult"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用咨询"}));if(s)throw new Error(JSON.stringify({code:1,msg:"当前正在咨询"}));if(!(e||t||i))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));if(!r)throw new Error(JSON.stringify({code:1,msg:"sessionId不存在"}));Js.__internalSend({type:ie.CONSULT_REQUEST,data:{session_id:r,consultation_agent_no:A}})}async answerCall(){if(1!==he.get("answerDevice"))throw new Error(JSON.stringify({code:1,msg:"当前设备不支持此功能"}));await this.sipClient.answerCall()}async makeCall(A){const e=he.get("isCalling"),t=he.get("selectOutNumber")||"";if(e)throw new Error(JSON.stringify({code:1,msg:"正在呼叫中,请勿重复点击"}));he.updateIsCalling(!0),he.updateDirection(ee.OUTGOING),Js.__internalSend({type:ie.OUT_CALL,data:{customer_phone:A,caller:t}})}async hangup(){if(!he.get("isCalling"))throw new Error(JSON.stringify({code:1,msg:"当前没有通话"}));const A=Ve.sessionId;Js.__internalSend({type:ie.AGENT_HANGUP,data:{session_id:A}})}mute(){this.roleMute();const A=he.get("actionConfigs"),e=he.get("isMuted"),t=Ve.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前未处于静音状态"}));if(!A.includes("mute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));Js.__internalSend({type:ie.MUTE,data:{session_id:t}})}unmute(){this.roleMute();const A=he.get("actionConfigs"),e=he.get("isMuted"),t=Ve.sessionId;if(!e)throw new Error(JSON.stringify({code:1,msg:"当前未处于未静音状态"}));if(!A.includes("unmute"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用静音"}));Js.__internalSend({type:ie.UNMUTE,data:{session_id:t}})}consultTransfer(){this.roleMute();const A=he.get("consultIsAnswer"),e=Ve.sessionId;if(!he.get("actionConfigs").includes("consult_transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用咨询转移"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Js.__internalSend({type:ie.CONSULT_TRANSFER,data:{session_id:e}})}satisfactionEvaluation(){this.roleMute();const A=Ve.sessionId;if(!he.get("actionConfigs").includes("satisfaction"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用满意度评价"}));Js.__internalSend({type:ie.SATISFACTION_EVALUATION,data:{session_id:A}})}startConference(){this.roleMute();if(!he.get("actionConfigs").includes("conference"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿调用三方会议"}));const A=Ve.sessionId;if(!he.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Js.__internalSend({type:ie.CONSULT_THREE_WAY,data:{session_id:A}})}consultRetrieve(){this.roleMute();if(!he.get("actionConfigs").includes("consult_retrieve"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿取回通话"}));const A=Ve.sessionId;if(!he.get("consultIsAnswer"))throw new Error(JSON.stringify({code:1,msg:"当前未处于咨询中"}));Js.__internalSend({type:ie.CONSULT_RETURN_CALL,data:{session_id:A}})}async transferAgent(A){this.roleMute();if(!he.get("actionConfigs").includes("transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿转接"}));const{transferIsAnswer:e}=he.getState(),t=Ve.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));Js.__internalSend({type:ie.TRANSFER,data:{session_id:t,transfer_type:0,transfer_dest:A}})}async transferOutline(A){this.roleMute();if(!he.get("actionConfigs").includes("transfer"))throw new Error(JSON.stringify({code:1,msg:"当前状态请勿转接"}));if(!A)throw new Error(JSON.stringify({code:1,msg:"号码不能为空"}));if(!/^\d+$/.test(A))throw new Error(JSON.stringify({code:1,msg:"号码格式错误"}));const{transferIsAnswer:e}=he.getState(),t=Ve.sessionId;if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));if(e)throw new Error(JSON.stringify({code:1,msg:"当前正在转接"}));Js.__internalSend({type:ie.TRANSFER,data:{session_id:t,transfer_type:2,transfer_dest:A}})}}class js{static{this._instance=null}#A=null;#e=new ks;#t;#s=null;#i=!1;#r;#o=!1;constructor(){if(!js._creatingInstance)throw new Error("请使用 YSYTAgentSdk.getInstance() 获取实例");clearInterval(this.#s),this.#s=setInterval(async()=>{const A=await this.#n();_e.updateRttObject(A)},2e3),Ks.requestMediaPermissions(),this.#r=he.subscribeKey("answerDevice",A=>{1===A?this.initWebRtc():this.#A=null}),this.on(re.KICK_OFF,()=>{this.destroy()})}static get instance(){return this._instance}static async destroyInstance(){this._instance&&(await this._instance.destroy(),this._instance=null)}static getInstance(A){if(!this._instance){if(!A)throw new Error("首次调用必须传入配置参数");const{url:e,appKey:t,appSecret:s,agentNo:i}=A;je=e||"https://aicc.yescloudy.com",Le=t,We=s,Pe=i,js._creatingInstance=!0,this._instance=new js,delete js._creatingInstance,window.addEventListener("beforeunload",()=>{this._instance?.destroy()})}return this._instance}getInit(){return this.#o}async initWebRtc(){if(this.#A)return;const A=Ve.agentInfo.sip_account_list.find(A=>1===A.device_type);if(!A)throw new Error(JSON.stringify({code:1,msg:"未找到 sip_account_list"}));{const e=A.sip_register_addr;this.#A=new $e({server:e.split(":")[0],user:A.sip_account,password:A.sip_password,webSocket:`wss://${e}`,callTimeout:3e4},this.#a),await this.#A.start()}}async init({actionNodeParams:A,enableBrowserAlert:e,logBuiltinEnabled:t}){try{const{viewHtmlElement:s,rttHTML:i}=A||{};Ve.logBuiltinEnabled=t||!1,await this.#e._agentLogin(Pe);const{agent_no:r,current_answer_device:o,current_state:n,out_display_number:a,out_display_select_switch:V}=Ve.agentInfo;n===Ae.IDLE?(he.updateActionConfigs(Vs),Ve.stateObject={state:n,state_name:"空闲"}):n===Ae.BUSY&&(he.updateActionConfigs(cs),Ve.stateObject={state:n,state_name:"忙碌"}),1===V?he.updateSelectOutNumber(a):he.updateSelectOutNumber("");let c="";const h=new URL(je).host;c=je.includes("https")?`wss://${h}`:`ws://${h}`,Js.getInstance(`${c}/ccs-ws/api/v1/ws/call/${r}?Authorization=`),he.updateAnswerDevice(o),1===o&&await this.initWebRtc(),this.#t=new Hs(this.#A),A&&s&&(this.#i=!0,new xs({container:s,rttHTML:i,statusParams:{state:Ae.IDLE,statusName:"空闲"}},this,this.#t)),Ve.enableBrowserAlert=e,e&&Ks.requestPermission(),ae.emit(re.AGENT_INIT_STATUS,!0),this.#o=!0}catch(A){this.#i&&Zs("初始化失败"),ae.emit(re.AGENT_INIT_STATUS,!1),this.#o=!1}}#a=A=>{const{type:e,data:t}=A;ae.emit(e,t)};on(A,e){return ae.on(A,e),this}off(A,e){ae.off(A,e)}offAll(){ae.clearAllListeners()}async destroyRtc(){await(this.#A?.destroy())}async destroy(){js._instance&&(await this.destroyRtc(),clearInterval(this.#s),Js.__internalClose(),this.offAll(),this.#r?.(),this.#A=null,this.#i=!1,js._instance=null,he.reset(),he.updateSelectOutNumber(""))}async#n(){return this.#A?this.#A.getNetworkStats():{}}get call_api(){const A=A=>(...e)=>{if(!this.#i)return A.apply(this,e);console.warn("当前未使用个人开发模式")};return{answerCall:A(()=>this.#t.answerCall()),makeCall:A=>this.#t.makeCall(A),cancelCall:A(()=>this.#t.cancelCall()),hangup:A(()=>this.#t.hangup()),sendDTMF:A(A=>this.#t.sendDTMF(A)),holdCall:A(()=>this.#t.holdCall()),unholdCall:A(()=>this.#t.unholdCall()),consultCall:A(async A=>{try{await this.#t.consultCall(A)}catch(A){throw console.warn(A),A}}),consultRetrieve:A(()=>this.#t.consultRetrieve()),consultTransfer:A(()=>this.#t.consultTransfer()),mute:A(()=>this.#t.mute()),unmute:A(()=>this.#t.unmute()),satisfactionEvaluation:A(()=>this.#t.satisfactionEvaluation()),startConference:A(()=>this.#t.startConference()),transferAgent:A(A=>this.#t.transferAgent(A)),transferOutline:A(A=>this.#t.transferOutline(A))}}get agent_api(){const A=A=>(...e)=>{if(!this.#i)return A.apply(this,e);console.warn("当前未使用个人开发模式")};return{changeDevice:A(A=>this.#e.changeDevice(A)),changeState:A(A=>this.#e.changeState(A)),getIdleAgentList:A(A=>this.#e.getIdleAgentList(A)),getAgentState:A(A=>this.#e.getAgentState(A)),setAgentExplicit:A(A=>this.#e.setAgentExplicit(A)),checkAgentLogin:A=>this.#e.checkAgentLogin(A)}}getAgentData(){const{agent_no:A,agent_name:e,answer_devices:t,out_display_data:s,out_display_select_switch:i,current_answer_device:r,current_state:o}=Ve.agentInfo,{stateObject:n}=Ve,a={1:"软电话",2:"手机模式",3:"SIP话机"};return{agent_no:A,agent_name:e,init_state:o,default_device:r,out_display_data:s,out_display_select_switch:i,answer_devices:(t||"").split(",").map(A=>({value:Number(A),label:a[Number(A)]})),stateObject:n}}}import.meta.hot&&import.meta.hot.dispose(()=>{window.location.reload()});export{Ae as AgentStatus,se as CallSipCallEvent,ee as DirectionEnum,oe as DisplayTextEnum,re as EventName,te as WebRtcCallSipEventEnum,ie as WebsocketTypeEnum,js as default};
|