liveChat.js 2.5 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; // 你的LiveChat商户ID(必须保留)
  18. // #endif
  19. (function (n, t, c) {
  20. function i(n) {
  21. return e._h ? e._h.apply(null, n) : e._q.push(n);
  22. }
  23. var e = {
  24. _q: [],
  25. _h: null,
  26. _v: "2.0",
  27. on: function () {
  28. i(["on", c.call(arguments)]);
  29. },
  30. once: function () {
  31. i(["once", c.call(arguments)]);
  32. },
  33. off: function () {
  34. i(["off", c.call(arguments)]);
  35. },
  36. get: function () {
  37. if (!e._h)
  38. throw new Error("[LiveChatWidget] You can't use getters before load.");
  39. return i(["get", c.call(arguments)]);
  40. },
  41. call: function () {
  42. i(["call", c.call(arguments)]);
  43. },
  44. init: function () {
  45. var n = t.createElement("script");
  46. n.async = !0;
  47. n.type = "text/javascript";
  48. n.src = "https://cdn.livechatinc.com/tracking.js";
  49. t.head.appendChild(n);
  50. },
  51. };
  52. // #ifdef H5
  53. !n.__lc.asyncInit && e.init();
  54. n.LiveChatWidget = n.LiveChatWidget || e;
  55. n.LiveChatWidget.call('hide');
  56. LiveChatWidget.on('visibility_changed', (data) => {
  57. switch (data.visibility) {
  58. case 'minimized':
  59. n.LiveChatWidget.call('hide');
  60. break
  61. }
  62. })
  63. // #endif
  64. })(window, document, [].slice);
  65. this.initialized = true;
  66. }
  67. showChat() {
  68. // #ifdef H5
  69. if (window.LiveChatWidget) {
  70. window.LiveChatWidget.call('maximize');
  71. }
  72. // #endif
  73. }
  74. setLanguage(lang) {
  75. // #ifdef H5
  76. if (window.LiveChatWidget) {
  77. window.LiveChatWidget.call('set_language', lang === 'cn' ? 'zh_CN' : 'en');
  78. }
  79. // #endif
  80. }
  81. adjustPosition(footerHeight) {
  82. // #ifdef H5
  83. if (window.innerWidth > 768) {
  84. const launcher = document.getElementById('chat-widget-launcher');
  85. const widget = document.getElementById('chat-widget-container');
  86. if (launcher) launcher.style.bottom = `${footerHeight}px`;
  87. if (widget) widget.style.bottom = `${footerHeight}px`;
  88. }
  89. // #endif
  90. }
  91. }
  92. export default LiveChatService.getInstance();