| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- export class LiveChatService {
- static instance = null;
- initialized = false;
- constructor() {
- this.initLiveChat();
- }
- static getInstance() {
- if (!LiveChatService.instance) {
- LiveChatService.instance = new LiveChatService();
- }
- return LiveChatService.instance;
- }
- initLiveChat() {
- if (this.initialized) return;
- // #ifdef H5
- window.__lc = window.__lc || {};
- window.__lc.license = 18945964; // 你的LiveChat商户ID(必须保留)
- // #endif
- (function (n, t, c) {
- function i(n) {
- return e._h ? e._h.apply(null, n) : e._q.push(n);
- }
- var e = {
- _q: [],
- _h: null,
- _v: "2.0",
- on: function () {
- i(["on", c.call(arguments)]);
- },
- once: function () {
- i(["once", c.call(arguments)]);
- },
- off: function () {
- i(["off", c.call(arguments)]);
- },
- get: function () {
- if (!e._h)
- throw new Error("[LiveChatWidget] You can't use getters before load.");
- return i(["get", c.call(arguments)]);
- },
- call: function () {
- i(["call", c.call(arguments)]);
- },
- init: function () {
- var n = t.createElement("script");
- n.async = !0;
- n.type = "text/javascript";
- n.src = "https://cdn.livechatinc.com/tracking.js";
- t.head.appendChild(n);
- },
- };
- // #ifdef H5
- !n.__lc.asyncInit && e.init();
- n.LiveChatWidget = n.LiveChatWidget || e;
- n.LiveChatWidget.call('hide');
- LiveChatWidget.on('visibility_changed', (data) => {
- switch (data.visibility) {
- case 'minimized':
- n.LiveChatWidget.call('hide');
- break
- }
- })
- // #endif
- })(window, document, [].slice);
- this.initialized = true;
- }
- showChat() {
- // #ifdef H5
- if (window.LiveChatWidget) {
- window.LiveChatWidget.call('maximize');
- }
- // #endif
- }
- setLanguage(lang) {
- // #ifdef H5
- if (window.LiveChatWidget) {
- window.LiveChatWidget.call('set_language', lang === 'cn' ? 'zh_CN' : 'en');
- }
- // #endif
- }
- adjustPosition(footerHeight) {
- // #ifdef H5
- if (window.innerWidth > 768) {
- const launcher = document.getElementById('chat-widget-launcher');
- const widget = document.getElementById('chat-widget-container');
- if (launcher) launcher.style.bottom = `${footerHeight}px`;
- if (widget) widget.style.bottom = `${footerHeight}px`;
- }
- // #endif
- }
- }
- export default LiveChatService.getInstance();
|