4 lines
7.8 KiB
JavaScript
4 lines
7.8 KiB
JavaScript
const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["assets/index-CnIbBo4X.js","assets/index-Bv-hKmn9.css"])))=>i.map(i=>d[i]);
|
||
import{_ as w,v as R,r as h,x as I,a2 as W}from"./index-CnIbBo4X.js";import{a as p}from"./index-ra8jRslb.js";import{u as $}from"./conversation-BQKKYvcD.js";async function P(t,s,i){return await p.post("/agents/login",{user_id:t,name:t,password:s,otp_code:i||void 0})}async function D(){return await p.get("/agents/me")}async function q(t){return await p.put("/agents/me/status",{status:t})}async function L(t){const s={};return s.status=t,await p.get("/agents",{params:s})}async function G(){return await p.post("/agents/otp-bind")}async function J(t,s){return await p.post("/agents/otp-verify",{user_id:t,otp_code:s})}async function K(){return await p.post("/agents/otp-unbind")}const V=3e4,x=3e4,M=1e3;function j(){let t=null,s=null,i=null,l=0,d=!1;function y(){const e=E();let o=e.userId,n=e.token;if(o||(o=localStorage.getItem("agent_user_id")||""),n||(n=localStorage.getItem("agent_token")||""),console.log("[WebSocket] 连接参数:",{userId:o,token:n?n.substring(0,20)+"...":null,localStorage_agent_token:localStorage.getItem("agent_token"),localStorage_agent_user_id:localStorage.getItem("agent_user_id")}),!o){console.warn("[WebSocket] 未登录,跳过连接 - agentId为空");return}t&&b(),d=!1;const r=window.location.protocol==="https:"?"wss:":"ws:",c=window.location.host,k=`${r}//${c}/ws/${o}`;console.log(`[WebSocket] 正在连接: ${k}`),t=new WebSocket(k,[`bearer.${n}`]),t.onopen=()=>{console.log("[WebSocket] 连接成功"),l=0,T(),$().stopAllPolling()},t.onmessage=S=>{try{const O=JSON.parse(S.data);v(O)}catch(O){console.error("[WebSocket] 消息解析失败:",O)}},t.onclose=()=>{console.log("[WebSocket] 连接关闭"),f(),t=null,d||($().startAllPolling(),A())},t.onerror=S=>{console.error("[WebSocket] 连接错误:",S)}}function b(){d=!0,i&&(clearTimeout(i),i=null),f(),t&&(t.close(),t=null),l=0,console.log("[WebSocket] 已主动断开连接")}function T(){f(),s=setInterval(()=>{t&&t.readyState===WebSocket.OPEN&&t.send(JSON.stringify({type:"ping"}))},V)}function f(){s&&(clearInterval(s),s=null)}function A(){if(d)return;const e=Math.min(M*Math.pow(2,l),x);l++,console.log(`[WebSocket] 将在 ${e/1e3} 秒后重连(第 ${l} 次)`),i&&clearTimeout(i),i=setTimeout(()=>{console.log("[WebSocket] 正在重连..."),y()},e)}function v(e){var n;const o=$();switch(e.type){case"new_message":e.data&&(o.handleNewMessage(e.data),C());break;case"conversation_updated":e.data&&o.handleConversationUpdated(e.data);break;case"collaborator_invited":if(e.data){o.handleCollaboratorInvited(e.data);const r=E();e.data.invitee_agent_id===r.userId&&w(async()=>{const{ElNotification:c}=await import("./index-CnIbBo4X.js").then(k=>k.a8);return{ElNotification:c}},__vite__mapDeps([0,1])).then(({ElNotification:c})=>{c({title:"🔔 摇人邀请",message:`坐席 ${e.data.inviter_agent_id} 邀请你协助处理会话「${e.data.employee_name||"未知"}」
|
||
${e.data.last_message_summary||""}`,type:"info",duration:0,position:"top-right",onClick:()=>{o.selectConversation(e.data.conversation_id)}})})}break;case"collaborator_joined":case"collaborator_left":o.handleCollaboratorChanged();break;case"participant_invited":o.handleCollaboratorChanged();break;case"participant_joined":o.handleCollaboratorChanged();break;case"participant_removed":case"participant_left":o.handleCollaboratorChanged();break;case"pong":break;case"pending_close_confirmed":e.data&&(o.handlePendingCloseConfirmed(e.data),w(async()=>{const{ElNotification:r}=await import("./index-CnIbBo4X.js").then(c=>c.a8);return{ElNotification:r}},__vite__mapDeps([0,1])).then(({ElNotification:r})=>{r({title:"✅ 员工已确认结单",message:`会话「${e.data.employee_name||""}」已确认解决`,type:"success",duration:5e3,position:"top-right"})}));break;case"pending_close_rejected":e.data&&(o.handlePendingCloseRejected(e.data),w(async()=>{const{ElNotification:r}=await import("./index-CnIbBo4X.js").then(c=>c.a8);return{ElNotification:r}},__vite__mapDeps([0,1])).then(({ElNotification:r})=>{r({title:"🔄 员工拒绝结单",message:`会话「${e.data.employee_name||""}」员工要求继续处理${e.data.reason?":"+e.data.reason:""}`,type:"warning",duration:0,position:"top-right"})}));break;case"conversation_resolved":e.data&&(o.handleConversationUpdated(e.data),w(async()=>{const{ElNotification:r}=await import("./index-CnIbBo4X.js").then(c=>c.a8);return{ElNotification:r}},__vite__mapDeps([0,1])).then(({ElNotification:r})=>{r({title:"📌 会话已关闭",message:`会话「${e.data.employee_name||""}」已关闭${e.data.resolved_method?"("+e.data.resolved_method+")":""}`,type:"info",duration:5e3,position:"top-right"})}));break;case"typing":if(e.data){const r=E();e.data.sender_id!==r.userId&&o.handleTypingEvent(e.data)}break;case"ai_thinking":(n=e.data)!=null&&n.conversation_id&&o.handleAiThinking(e.data);break;default:console.warn(`[WebSocket] 未知消息类型: ${e.type}`)}}let u=null;function C(){try{u||(u=new AudioContext);const e=u.currentTime,o=u.createOscillator(),n=u.createGain();o.type="sine",o.frequency.value=880,n.gain.setValueAtTime(.3,e),n.gain.exponentialRampToValueAtTime(.001,e+.15),o.connect(n),n.connect(u.destination),o.start(e),o.stop(e+.15);const r=u.createOscillator(),c=u.createGain();r.type="sine",r.frequency.value=660,c.gain.setValueAtTime(.3,e+.1),c.gain.exponentialRampToValueAtTime(.001,e+.3),r.connect(c),c.connect(u.destination),r.start(e+.1),r.stop(e+.3)}catch{}}let m=0;const N=3e3;function a(e){if(!t||t.readyState!==WebSocket.OPEN)return;const o=Date.now();if(o-m<N)return;m=o;const n=E();t.send(JSON.stringify({type:"typing",conversation_id:e,sender_name:n.agentName||"坐席"}))}return{connect:y,disconnect:b,sendTyping:a}}const _="agent_token",g="agent_user_id",E=R("agent",()=>{const t=h(null),s=h(localStorage.getItem(_)),i=h(localStorage.getItem(g)),l=h(!1),d=h([]),y=I(()=>!!s.value&&!!t.value),b=I(()=>{var a;return((a=t.value)==null?void 0:a.status)||"offline"}),T=I(()=>{var a;return((a=t.value)==null?void 0:a.name)||""}),f=I(()=>{var a;return((a=t.value)==null?void 0:a.user_id)||i.value||""});async function A(a,e,o){try{l.value=!0,localStorage.removeItem(_),localStorage.removeItem(g),s.value=null,i.value=null,t.value=null;const n=await P(a,e,o);if("require_otp_bind"in n&&n.require_otp_bind)return n.token&&(s.value=n.token,i.value=n.user_id,localStorage.setItem(_,n.token),localStorage.setItem(g,n.user_id)),l.value=!1,{require_otp_bind:!0,user_id:n.user_id,name:n.name,role:n.role};if("require_otp"in n&&n.require_otp)throw l.value=!1,new Error("require_otp");s.value=n.token,i.value=n.user_id,localStorage.setItem(_,n.token),localStorage.setItem(g,n.user_id);const{token:r,...c}=n;t.value=c,W.push("/workspace")}catch(n){throw console.error("登录失败:",n),n instanceof Error&&n.message==="require_otp",n}finally{l.value=!1}}function v(){const{disconnect:a}=j();a(),s.value=null,i.value=null,t.value=null,localStorage.removeItem(_),localStorage.removeItem(g),W.push("/login")}async function u(){var a;try{if(!s.value)return;const e=await D();t.value=e}catch(e){console.error("获取坐席信息失败:",e),e&&typeof e=="object"&&"response"in e&&((a=e.response)==null?void 0:a.status)===401&&v()}}async function C(a){try{const e=await q(a);t.value=e}catch(e){console.error("更新坐席状态失败:",e)}}async function m(){try{const a=await L("online");d.value=a.items.filter(e=>{var o;return e.user_id!==((o=t.value)==null?void 0:o.user_id)})}catch(a){console.error("获取坐席列表失败:",a)}}async function N(){const a=localStorage.getItem(_);if(a){s.value=a,i.value=localStorage.getItem(g);try{await u()}catch{v()}}}return{agentInfo:t,token:s,agentUserId:i,logging:l,availableAgents:d,isLoggedIn:y,agentStatus:b,agentName:T,userId:f,login:A,logout:v,refreshAgentInfo:u,changeStatus:C,loadAvailableAgents:m,initAuth:N}});export{j as a,G as b,K as c,E as u,J as v};
|