(function(){
'use strict';
var path = location.pathname;
var inMerchant = /^\/merchant(?:\/|$)/.test(path)
|| path.indexOf('/merchant-h5') === 0
|| path.indexOf('/package/merchant/') === 0
|| path.indexOf('/package/merchant-h5/') === 0;
if (!inMerchant) return;
if (/\/login(?:\.html)?(?:\/|$)/.test(path)) return;
if (window.top !== window) return;
if (document.getElementById('kf-ball')) return;
var WORK_URL = "https://shop.yuqk.cc/plugin/Kefu/Api/index";
var NOTICE_URL = "https://shop.yuqk.cc/plugin/Kefu/Api/notice";
var POLL_MS = 10000;
var LS_BASE = 'kf_dash_';
var baseline = parseInt(localStorage.getItem(LS_BASE + 'newid') || '0', 10) || 0;
var firstFrame = true;
var lastUnread = 0;
var soundOn = true;
var blinkTimer = null;
var docTitle = document.title;
var ac = null;
var lastNoreplyBeep = 0;
// ===== 悬浮球 =====
var style = document.createElement('style');
style.textContent =
'#kf-ball{position:fixed;z-index:2147483000;right:14px;bottom:96px;width:52px;height:52px;border-radius:17px;'
+ 'background:linear-gradient(135deg,#f5933c,#f45853);box-shadow:0 8px 22px rgba(244,88,83,.38);'
+ 'cursor:pointer;user-select:none;-webkit-user-select:none;touch-action:none;display:none;'
+ 'transition:transform .15s;}'
+ '#kf-ball:active{transform:scale(.92);}'
+ '#kf-ball svg{position:absolute;left:50%;top:50%;transform:translate(-50%,-50%);width:27px;height:27px;}'
+ '#kf-ball .kf-bd{position:absolute;top:-6px;right:-6px;min-width:20px;height:20px;padding:0 6px;'
+ 'border-radius:11px;background:#ff3b30;color:#fff;font-size:11.5px;font-weight:700;line-height:20px;'
+ 'text-align:center;border:2px solid #fff;box-sizing:border-box;}'
+ '@keyframes kfShake{0%,100%{transform:rotate(0)}20%{transform:rotate(-9deg)}40%{transform:rotate(8deg)}'
+ '60%{transform:rotate(-6deg)}80%{transform:rotate(4deg)}}'
+ '#kf-ball.kf-shake{animation:kfShake .7s ease;}'
+ '@keyframes kfPulse{0%{box-shadow:0 0 0 0 rgba(255,125,0,.55)}70%{box-shadow:0 0 0 16px rgba(255,125,0,0)}100%{box-shadow:0 0 0 0 rgba(255,125,0,0)}}'
+ '#kf-ball.kf-noreply{animation:kfPulse 1.8s infinite;background:linear-gradient(135deg,#ff9a4d,#ff7d00);box-shadow:0 8px 22px rgba(255,125,0,.42);}';
document.head.appendChild(style);
var ball = document.createElement('div');
ball.id = 'kf-ball';
ball.innerHTML =
''
+ '';
ball.onclick = function(){ window.open(WORK_URL, '_blank'); };
document.body.appendChild(ball);
function ensureAudio(){
if (!ac) { try { ac = new (window.AudioContext || window.webkitAudioContext)(); } catch(e){} }
if (ac && ac.state === 'suspended') { ac.resume(); }
if ('speechSynthesis' in window) { try { window.speechSynthesis.resume(); } catch(e){} }
}
document.addEventListener('touchstart', ensureAudio, { passive: true });
document.addEventListener('click', ensureAudio, { passive: true });
if ('speechSynthesis' in window) {
try {
if (!window.speechSynthesis.getVoices().length) {
window.speechSynthesis.onvoiceschanged = function(){ window.speechSynthesis.getVoices(); };
}
} catch(e){}
}
// 通用通知音:门铃双音(G5→C6),dur 0.25s,vol 0.5
function beep(times,opts){
if (!soundOn) return;
ensureAudio(); if (!ac) return;
opts=opts||{};
var freqs=opts.freqs||[784,1047], gap=opts.gap!=null?opts.gap:230, dur=opts.dur||0.25, vol=opts.vol!=null?opts.vol:0.5, type=opts.type||'sine';
for (var t=0;t 0;
var hasNoreply = parseInt(d.noreply,10) > 0;
if (hasUnread || hasNoreply) {
ball.style.display = 'block';
bd.style.display = 'block';
if (hasUnread) {
bd.textContent = unread > 99 ? '99+' : String(unread);
bd.style.background = ''; bd.style.color = '';
ball.classList.remove('kf-noreply');
} else {
// 只有已读未回复没有新未读:角标换橙色 + 悬浮球加提醒色环
bd.textContent = d.noreply > 9 ? '9+' : String(d.noreply);
bd.style.background = '#ff7d00'; bd.style.color = '#fff';
ball.classList.add('kf-noreply');
}
} else {
ball.style.display = 'none';
ball.classList.remove('kf-noreply');
}
// 新咨询:取待接待会话最大 id 作为基线,出现更大 id 的新会话即播报(每会话仅一次)
var maxId = baseline;
waiting.forEach(function(w){ if (w.id > maxId) maxId = w.id; });
if (firstFrame) {
firstFrame = false;
} else {
var fresh = waiting.filter(function(w){ return w.id > baseline; });
if (fresh.length) {
speakNew(); ball.classList.remove('kf-shake');
void ball.offsetWidth; ball.classList.add('kf-shake');
if (document.hidden || !document.hasFocus()) startBlink('【新买家咨询】' + (fresh[0].name||'该买家'));
} else if (unread > lastUnread) {
beep(1);
if (document.hidden || !document.hasFocus()) startBlink('【' + unread + '条未读】在线客服');
} else if (parseInt(d.noreply,10) > 0 && Date.now() - lastNoreplyBeep >= 30000) {
lastNoreplyBeep = Date.now();
beep(2);
if (document.hidden || !document.hasFocus()) startBlink('【有买家等待回复】在线客服');
}
}
if (maxId > baseline) baseline = maxId;
localStorage.setItem(LS_BASE + 'newid', String(baseline));
lastUnread = unread;
})
.catch(function(){})
.finally(function(){ setTimeout(poll, POLL_MS); });
}
poll();
})();