(() => { if (window.SanxiaYoulunChatLoaded) return; window.SanxiaYoulunChatLoaded = true; const cfg = window.SanxiaYoulunChatConfig || window.DJJChatConfig || {}; const apiBase = (cfg.apiBase || '').replace(/\/$/, ''); const site = cfg.site || 'sanxia'; const deviceKey = 'sanxiayoulun_deviceid_' + site; const sessionKey = 'sanxiayoulun_sessionid_' + site; let deviceid = localStorage.getItem(deviceKey) || ('dev_' + Math.random().toString(16).slice(2)); localStorage.setItem(deviceKey, deviceid); let sessionid = localStorage.getItem(sessionKey) || ''; const css = ` #sanxiayoulun-chat-btn{position:fixed;right:22px;bottom:22px;z-index:999999;background:#111827;color:white;border-radius:999px;padding:14px 18px;box-shadow:0 10px 30px rgba(0,0,0,.25);cursor:pointer;font-family:Arial,"Microsoft YaHei",sans-serif;font-weight:700} #sanxiayoulun-chat-panel{position:fixed;right:22px;bottom:82px;width:360px;max-width:calc(100vw - 32px);height:520px;max-height:calc(100vh - 120px);z-index:999999;background:white;border-radius:18px;box-shadow:0 18px 60px rgba(0,0,0,.28);display:none;overflow:hidden;font-family:Arial,"Microsoft YaHei",sans-serif} #sanxiayoulun-head{background:#111827;color:white;padding:16px;font-weight:700}#sanxiayoulun-close{float:right;cursor:pointer}#sanxiayoulun-body{height:360px;overflow:auto;background:#f8fafc;padding:12px}.sanxiayoulun-msg{padding:10px 12px;border-radius:12px;margin:8px 0;line-height:1.45;font-size:14px}.sanxiayoulun-ai{background:#eef2ff;color:#111827}.sanxiayoulun-user{background:#111827;color:white;margin-left:45px}#sanxiayoulun-input{display:flex;gap:8px;padding:12px;border-top:1px solid #eee}#sanxiayoulun-text{flex:1;border:1px solid #d1d5db;border-radius:10px;padding:10px;font-size:14px}#sanxiayoulun-send{border:0;background:#111827;color:white;border-radius:10px;padding:0 14px;cursor:pointer}`; const style = document.createElement('style'); style.textContent = css; document.head.appendChild(style); const panel = document.createElement('div'); panel.id = 'sanxiayoulun-chat-panel'; panel.innerHTML = `
三峡游轮客服 ×
咨询船期、价格、房型
`; const btn = document.createElement('div'); btn.id = 'sanxiayoulun-chat-btn'; btn.textContent = '三峡游轮咨询'; document.body.appendChild(panel); document.body.appendChild(btn); const body = panel.querySelector('#sanxiayoulun-body'); const input = panel.querySelector('#sanxiayoulun-text'); const send = panel.querySelector('#sanxiayoulun-send'); function add(role, text){const el=document.createElement('div');el.className='sanxiayoulun-msg '+(role==='user'?'sanxiayoulun-user':'sanxiayoulun-ai');el.textContent=text;body.appendChild(el);body.scrollTop=body.scrollHeight;} async function start(){ if(sessionid) return sessionid; const r = await fetch(apiBase + '/api/chat/start', {method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({site,deviceid})}); const data = await r.json(); sessionid = data.sessionid; localStorage.setItem(sessionKey, sessionid); return sessionid; } btn.onclick=async()=>{panel.style.display='block'; if(!body.dataset.greeted){add('ai','我是三峡游轮客服。您几月出发?几个人?'); body.dataset.greeted='1'; try{await start();}catch(e){}}}; panel.querySelector('#sanxiayoulun-close').onclick=()=>panel.style.display='none'; async function submit(){const message=input.value.trim(); if(!message) return; input.value=''; add('user',message); send.disabled=true; try{await start(); const r=await fetch(apiBase+'/api/chat/send',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({sessionid,deviceid,message})}); const data=await r.json(); add('ai',data.reply || '收到,请继续补充一下出发时间和人数。');}catch(e){add('ai','网络暂时异常,请稍后再试,或直接留下手机号/微信。')} finally{send.disabled=false; input.focus();}} send.onclick=submit; input.addEventListener('keydown',e=>{if(e.key==='Enter') submit()}); })();