liveChat.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. export class LiveChatService {
  2. static instance = null;
  3. initialized = false;
  4. constructor() {
  5. this.initLiveChat();
  6. }
  7. static getInstance() {
  8. if (!LiveChatService.instance) {
  9. LiveChatService.instance = new LiveChatService();
  10. }
  11. return LiveChatService.instance;
  12. }
  13. initLiveChat() {
  14. if (this.initialized) return;
  15. // #ifdef H5
  16. window.__lc = window.__lc || {};
  17. window.__lc.license = 18945964;
  18. (function (n, t, c) {
  19. function i(n) {
  20. return e._h ? e._h.apply(null, n) : e._q.push(n);
  21. }
  22. var e = {
  23. _q: [],
  24. _h: null,
  25. _v: "2.0",
  26. on: function () {
  27. i(["on", c.call(arguments)]);
  28. },
  29. once: function () {
  30. i(["once", c.call(arguments)]);
  31. },
  32. off: function () {
  33. i(["off", c.call(arguments)]);
  34. },
  35. get: function () {
  36. if (!e._h)
  37. throw new Error("[LiveChatWidget] You can't use getters before load.");
  38. return i(["get", c.call(arguments)]);
  39. },
  40. call: function () {
  41. i(["call", c.call(arguments)]);
  42. },
  43. init: function () {
  44. var n = t.createElement("script");
  45. n.async = !0;
  46. n.type = "text/javascript";
  47. n.src = "https://cdn.livechatinc.com/tracking.js";
  48. t.head.appendChild(n);
  49. },
  50. };
  51. !n.__lc.asyncInit && e.init();
  52. n.LiveChatWidget = n.LiveChatWidget || e;
  53. })(window, document, [].slice);
  54. // #endif
  55. this.initialized = true;
  56. }
  57. setLanguage(lang) {
  58. // #ifdef H5
  59. if (window.LiveChatWidget) {
  60. if (lang === 'cn') {
  61. window.LiveChatWidget.call('set_language', 'zh_CN');
  62. } else {
  63. window.LiveChatWidget.call('set_language', 'en');
  64. }
  65. }
  66. // #endif
  67. }
  68. adjustPosition(footerHeight) {
  69. // #ifdef H5
  70. if (window.innerWidth > 768) {
  71. const launcher = document.getElementById('chat-widget-launcher');
  72. const widget = document.getElementById('chat-widget-container');
  73. if (launcher) {
  74. launcher.style.bottom = `${footerHeight}px`;
  75. }
  76. if (widget) {
  77. widget.style.bottom = `${footerHeight}px`;
  78. }
  79. }
  80. // #endif
  81. }
  82. }
  83. export default LiveChatService.getInstance();