Files
wecom_it_smart_desk/frontend-agent/dist.bak/assets/agent-WRYR5BAs.js
T

4 lines
6.4 KiB
JavaScript
Raw Normal View History

const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["assets/index-B1u3nieA.js","assets/index-Do7r--Ys.css"])))=>i.map(i=>d[i]);
import{_ as $,v as R,r as b,x as w,a0 as O}from"./index-B1u3nieA.js";import{a as g}from"./index-ChNUjLLo.js";import{u as N}from"./conversation-C4tqOoff.js";async function q(t,r,s){return await g.post("/agents/login",{user_id:t,name:t,password:r,otp_code:s||void 0})}async function x(){return await g.get("/agents/me")}async function P(t){return await g.put("/agents/me/status",{status:t})}async function D(t){const r={};return r.status=t,await g.get("/agents",{params:r})}async function j(){return await g.post("/agents/otp-bind")}async function J(t,r){return await g.post("/agents/otp-verify",{user_id:t,otp_code:r})}async function K(){return await g.post("/agents/otp-unbind")}const L=3e4,M=3e4,V=1e3;function H(){let t=null,r=null,s=null,c=0,d=!1;function m(){const e=h();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&&y(),d=!1;const l=window.location.protocol==="https:"?"wss:":"ws:",u=window.location.host,W=`${l}//${u}/ws/${o}`;console.log(`[WebSocket] 正在连接: ${W}`),t=new WebSocket(W,[`bearer.${n}`]),t.onopen=()=>{console.log("[WebSocket] 连接成功"),c=0,I(),N().stopAllPolling()},t.onmessage=S=>{try{const C=JSON.parse(S.data);v(C)}catch(C){console.error("[WebSocket] 消息解析失败:",C)}},t.onclose=()=>{console.log("[WebSocket] 连接关闭"),_(),t=null,d||(N().startAllPolling(),T())},t.onerror=S=>{console.error("[WebSocket] 连接错误:",S)}}function y(){d=!0,s&&(clearTimeout(s),s=null),_(),t&&(t.close(),t=null),c=0,console.log("[WebSocket] 已主动断开连接")}function I(){_(),r=setInterval(()=>{t&&t.readyState===WebSocket.OPEN&&t.send(JSON.stringify({type:"ping"}))},L)}function _(){r&&(clearInterval(r),r=null)}function T(){if(d)return;const e=Math.min(V*Math.pow(2,c),M);c++,console.log(`[WebSocket] 将在 ${e/1e3} 秒后重连(第 ${c} 次)`),s&&clearTimeout(s),s=setTimeout(()=>{console.log("[WebSocket] 正在重连..."),m()},e)}function v(e){const o=N();switch(e.type){case"new_message":e.data&&(o.handleNewMessage(e.data),A());break;case"conversation_updated":e.data&&o.handleConversationUpdated(e.data);break;case"collaborator_invited":if(e.data){o.handleCollaboratorInvited(e.data);const n=h();e.data.invitee_agent_id===n.userId&&$(async()=>{const{ElNotification:l}=await import("./index-B1u3nieA.js").then(u=>u.a2);return{ElNotification:l}},__vite__mapDeps([0,1])).then(({ElNotification:l})=>{l({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"typing":if(e.data){const n=h();e.data.sender_id!==n.userId&&o.handleTypingEvent(e.data)}break;default:console.warn(`[WebSocket] 未知消息类型: ${e.type}`)}}let i=null;function A(){try{i||(i=new AudioContext);const e=i.currentTime,o=i.createOscillator(),n=i.createGain();o.type="sine",o.frequency.value=880,n.gain.setValueAtTime(.3,e),n.gain.exponentialRampToValueAtTime(.001,e+.15),o.connect(n),n.connect(i.destination),o.start(e),o.stop(e+.15);const l=i.createOscillator(),u=i.createGain();l.type="sine",l.frequency.value=660,u.gain.setValueAtTime(.3,e+.1),u.gain.exponentialRampToValueAtTime(.001,e+.3),l.connect(u),u.connect(i.destination),l.start(e+.1),l.stop(e+.3)}catch{}}let k=0;const E=3e3;function a(e){if(!t||t.readyState!==WebSocket.OPEN)return;const o=Date.now();if(o-k<E)return;k=o;const n=h();t.send(JSON.stringify({type:"typing",conversation_id:e,sender_name:n.agentName||"坐席"}))}return{connect:m,disconnect:y,sendTyping:a}}const p="agent_token",f="agent_user_id",h=R("agent",()=>{const t=b(null),r=b(localStorage.getItem(p)),s=b(localStorage.getItem(f)),c=b(!1),d=b([]),m=w(()=>!!r.value&&!!t.value),y=w(()=>{var a;return((a=t.value)==null?void 0:a.status)||"offline"}),I=w(()=>{var a;return((a=t.value)==null?void 0:a.name)||""}),_=w(()=>{var a;return((a=t.value)==null?void 0:a.user_id)||s.value||""});async function T(a,e,o){try{c.value=!0,localStorage.removeItem(p),localStorage.removeItem(f),r.value=null,s.value=null,t.value=null;const n=await q(a,e,o);if("require_otp_bind"in n&&n.require_otp_bind)return n.token&&(r.value=n.token,s.value=n.user_id,localStorage.setItem(p,n.token),localStorage.setItem(f,n.user_id)),c.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 c.value=!1,new Error("require_otp");r.value=n.token,s.value=n.user_id,localStorage.setItem(p,n.token),localStorage.setItem(f,n.user_id);const{token:l,...u}=n;t.value=u,O.push("/workspace")}catch(n){throw console.error("登录失败:",n),n instanceof Error&&n.message==="require_otp",n}finally{c.value=!1}}function v(){const{disconnect:a}=H();a(),r.value=null,s.value=null,t.value=null,localStorage.removeItem(p),localStorage.removeItem(f),O.push("/login")}async function i(){var a;try{if(!r.value)return;const e=await x();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 A(a){try{const e=await P(a);t.value=e}catch(e){console.error("更新坐席状态失败:",e)}}async function k(){try{const a=await D("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 E(){const a=localStorage.getItem(p);if(a){r.value=a,s.value=localStorage.getItem(f);try{await i()}catch{v()}}}return{agentInfo:t,token:r,agentUserId:s,logging:c,availableAgents:d,isLoggedIn:m,agentStatus:y,agentName:I,userId:_,login:T,logout:v,refreshAgentInfo:i,changeStatus:A,loadAvailableAgents:k,initAuth:E}});export{H as a,j as b,K as c,h as u,J as v};