/* Приёмка виджет. Установка на сайт: Опции: data-api (база API), data-name (название в шапке), data-color, data-position ("left"), data-welcome. Без атрибутов — берёт настройки из кабинета (/api/widget-config/{tenant}). */ (function () { var s = document.currentScript; var TENANT = s.getAttribute('data-tenant') || 'service'; var API = s.getAttribute('data-api') || s.src.replace(/\/widget\.js.*$/, ''); var SESSION = 'w_' + (localStorage.getItem('a1_sid') || (localStorage.setItem('a1_sid', Math.random().toString(36).slice(2) + Date.now()), localStorage.getItem('a1_sid'))); function esc(t) { var d = document.createElement('div'); d.textContent = t; return d.innerHTML; } fetch(API + '/api/widget-config/' + encodeURIComponent(TENANT)) .then(function (r) { return r.ok ? r.json() : {}; }) .catch(function () { return {}; }) .then(function (cfg) { build(cfg || {}); }); function build(cfg) { var NAME = s.getAttribute('data-name') || cfg.title || 'Чат'; var COLOR = s.getAttribute('data-color') || cfg.color || '#5E6AD2'; var POS = s.getAttribute('data-position') || cfg.position || 'right'; var WELCOME = s.getAttribute('data-welcome') || cfg.welcome || ''; var AUTO_OPEN = parseInt(s.getAttribute('data-auto-open') || cfg.auto_open || 0, 10); var LABEL = s.getAttribute('data-bubble-label') || cfg.bubble_label || ''; var RADIUS = parseInt(s.getAttribute('data-radius') != null ? s.getAttribute('data-radius') : cfg.radius, 10); if (isNaN(RADIUS)) RADIUS = 15; var OFFSET = parseInt(s.getAttribute('data-offset') != null ? s.getAttribute('data-offset') : cfg.offset, 10); if (isNaN(OFFSET)) OFFSET = 20; var PH = s.getAttribute('data-placeholder') || cfg.placeholder || 'Сообщение…'; var side = POS === 'left' ? 'left:' + OFFSET + 'px;right:auto' : 'right:' + OFFSET + 'px;left:auto'; var css = document.createElement('style'); css.textContent = '#a1w{position:fixed;' + side + ';bottom:20px;z-index:99999;font-family:"Inter Tight",system-ui,sans-serif}' + '#a1w .p{width:334px;height:0;overflow:hidden;transition:height .28s;display:flex;flex-direction:column;' + 'background:#FFFFFF;border:1px solid #E5E7EB;border-radius:' + (RADIUS + 4) + 'px;box-shadow:0 20px 60px rgba(14,15,17,.18);margin-bottom:10px}' + '#a1w.open .p{height:' + Math.min(436, Math.max(280, window.innerHeight - 120)) + 'px}' + '#a1w .h{background:#0E0F11;color:#fff;padding:12px 15px;display:flex;gap:10px;align-items:center}' + '#a1w .av{width:30px;height:30px;border-radius:8px;background:' + COLOR + ';color:#fff;display:grid;' + 'place-items:center;font-weight:700;font-size:15px}' + '#a1w .n{font-weight:600;font-size:15px}' + '#a1w .o{font-size:12px;color:#9CA3AF;display:flex;gap:5px;align-items:center;margin-top:1px}' + '#a1w .d{width:6px;height:6px;border-radius:50%;background:#34D399}' + '#a1w .b{flex:1;overflow:auto;padding:13px;display:flex;flex-direction:column;gap:8px;background:#F7F8FA}' + '#a1w .m{max-width:82%;padding:9px 12px;border-radius:12px;font-size:14.5px;background:#FFFFFF;' + 'border:1px solid #E5E7EB;border-bottom-left-radius:3px;color:#0E0F11;line-height:1.55;white-space:pre-wrap}' + '#a1w .m.me{align-self:flex-end;background:#0E0F11;color:#fff;border:none;border-bottom-left-radius:12px;border-bottom-right-radius:3px}' + '#a1w .m.typing{color:#9CA3AF;font-style:italic;background:transparent;border:none;padding:3px 12px}' + '#a1w .f{padding:10px;border-top:1px solid #E5E7EB;display:flex;gap:7px;background:#FFFFFF}' + '#a1w input{flex:1;border:1px solid #D9DCE1;border-radius:8px;padding:9px 12px;font-size:14.5px;outline:none;font-family:inherit;color:#0E0F11;background:#F7F8FA}' + '#a1w input:focus{border-color:#0E0F11}' + '#a1w .send{background:#0E0F11;border:none;color:#fff;font-weight:700;border-radius:8px;' + 'padding:0 15px;cursor:pointer;font-size:16px;transition:.12s}' + '#a1w .send:hover{background:' + COLOR + '}' + '#a1w .bub{position:relative;width:54px;height:54px;border-radius:' + RADIUS + 'px;background:#0E0F11;border:none;cursor:pointer;' + 'display:grid;place-items:center;box-shadow:0 10px 26px rgba(14,15,17,.3);' + (POS === 'left' ? 'margin-left:0' : 'margin-left:auto') + ';transition:.18s}' + '#a1w .bub:hover{transform:scale(1.05);background:' + COLOR + '}' + '#a1w .bub svg{width:22px;height:22px;stroke:#fff;fill:none;stroke-width:1.8}' + '#a1w .badge{position:absolute;top:-4px;left:-4px;min-width:18px;height:18px;border-radius:9px;' + 'background:#B91C1C;color:#fff;font-size:11px;font-weight:700;display:none;' + 'align-items:center;justify-content:center;padding:0 4px;box-shadow:0 2px 8px rgba(0,0,0,.25)}' + '#a1w .wlabel{position:absolute;bottom:12px;' + (POS === 'left' ? 'left:64px' : 'right:64px') + ';background:#0E0F11;color:#fff;font-size:13px;font-weight:600;padding:8px 13px;border-radius:10px;' + 'cursor:pointer;white-space:nowrap;box-shadow:0 8px 22px rgba(14,15,17,.25);transition:.15s;display:none}' + '#a1w .wlabel:hover{background:' + COLOR + '}' + '#a1w .clip{background:none;border:none;font-size:17px;cursor:pointer;padding:0 6px;' + 'color:#9CA3AF;line-height:1}' + '#a1w .clip:hover{color:' + COLOR + '}' + '#a1w .m img{max-width:100%;border-radius:8px;display:block;margin-top:4px}'; document.head.appendChild(css); var root = document.createElement('div'); root.id = 'a1w'; root.innerHTML = '
' + esc(NAME[0] || '?') + '
' + '
' + esc(NAME) + '
онлайн · ответ ~5 сек
' + '
' + '
' + '
' + '' + '' + (LABEL ? '
' + esc(LABEL) + '
' : ''); document.body.appendChild(root); var body = root.querySelector('.b'), input = root.querySelector('input'); var badge = root.querySelector('.badge'); var wlabel = root.querySelector('.wlabel'); var lastId = 0, welcomed = false, unread = 0; function bump() { if (root.classList.contains('open')) return; unread += 1; badge.textContent = unread > 9 ? '9+' : unread; badge.style.display = 'flex'; } function openW() { root.classList.add('open'); unread = 0; badge.style.display = 'none'; if (wlabel) wlabel.style.display = 'none'; if (!welcomed && WELCOME) { welcomed = true; add(WELCOME); } } root.querySelector('.bub').onclick = function () { if (root.classList.contains('open')) { root.classList.remove('open'); return; } openW(); }; if (wlabel) { wlabel.style.display = 'block'; wlabel.onclick = openW; } if (AUTO_OPEN > 0) setTimeout(function () { if (!root.classList.contains('open')) openW(); }, AUTO_OPEN * 1000); var PHOTO_RE = /^\[фото\]\((\/uploads\/[^)]+)\)\s*/; function add(text, me) { var m = document.createElement('div'); m.className = 'm' + (me ? ' me' : ''); var pm = (text || '').match(PHOTO_RE); if (pm) { var img = document.createElement('img'); img.src = pm[1]; m.appendChild(img); text = text.slice(pm[0].length); } if (text) m.appendChild(document.createTextNode(text)); body.appendChild(m); body.scrollTop = body.scrollHeight; if (!me) bump(); return m; } function sendText(t, photo) { var typing = add('печатает…'); typing.classList.add('typing'); fetch(API + '/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ tenant_id: TENANT, session_id: SESSION, text: t, channel: 'widget', photo: photo || null }) }).then(function (r) { return r.json(); }).then(function (d) { if (d.msg_id) lastId = d.msg_id; if (d.reply) { typing.textContent = d.reply; typing.classList.remove('typing'); } else { typing.remove(); } body.scrollTop = body.scrollHeight; }).catch(function () { typing.textContent = 'Не удалось связаться. Попробуйте ещё раз.'; typing.classList.remove('typing'); }); } function send() { var t = input.value.trim(); if (!t) return; input.value = ''; add(t, true); sendText(t); } root.querySelector('.send').onclick = send; input.addEventListener('keydown', function (e) { if (e.key === 'Enter') send(); }); var fileIn = root.querySelector('.file'); root.querySelector('.clip').onclick = function () { fileIn.click(); }; fileIn.onchange = function () { var f = fileIn.files[0]; fileIn.value = ''; if (!f) return; var fd = new FormData(); fd.append('file', f); var ph = add('загружаю фото…', true); fetch(API + '/api/upload', { method: 'POST', body: fd }) .then(function (r) { return r.json(); }).then(function (d) { if (!d.url) { ph.textContent = 'фото не загрузилось'; return; } var img = document.createElement('img'); img.src = d.url; ph.textContent = ''; ph.appendChild(img); sendText('', d.url); }).catch(function () { ph.textContent = 'фото не загрузилось'; }); }; // поллинг: дожимы агента и сообщения мастера setInterval(function () { fetch(API + '/chat/pending?session_id=' + SESSION + '&after=' + lastId) .then(function (r) { return r.json(); }).then(function (d) { (d.messages || []).forEach(function (m) { if (m.id > lastId) lastId = m.id; add(m.content); }); }).catch(function () {}); }, 8000); } })();