Files
weiyu/visitor/web/pc/js/custom.min.js
jack ning 8a84e6b738 update
2024-04-05 15:53:09 +08:00

1 line
21 KiB
JavaScript

"use strict"; function _classCallCheck(e, t) { if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function") } var _createClass = function () { function e(e, t) { for (var o = 0; o < t.length; o++) { var n = t[o]; n.enumerable = n.enumerable || !1, n.configurable = !0, "value" in n && (n.writable = !0), Object.defineProperty(e, n.key, n) } } return function (t, o, n) { return o && e(t.prototype, o), n && e(t, n), t } }(), ByteDesk = function () { function e() { _classCallCheck(this, e), this.config = void 0, this.roundButton = void 0, this.rectangleButton = void 0, this.chatWindow = void 0, this.chatWindowDOM = void 0, this.rectangleButtonMobile = void 0, this.chatWindowMobile = void 0, this.chatWindowDomMobile = void 0, this.chatWindowMobileMask = void 0, this.unreadCount = 0, this.messageTips = void 0, this.firstStart = !0, this.firstOpen = !0, this.isChatWindowShown = !1, this.chatUrl = "", this.chatMaxUrl = "", this.chatUrlPreload = "", this.websiteUrl = "", this.websiteTitle = "", this.refererUrl = "", this.isProduction = !0, this.h5BaseUrl = "https://h5.kefux.com/chat/h5/index.html", this.h5BaseUrlEn = "https://h5.kefux.com/chat/h5/indexen.html", this.pcBaseUrl = "https://pc.kefux.com/chat/pc/index.html", this.pcBaseUrlEn = "https://pc.kefux.com/chat/pc/indexen.html", this.httpHost = "https://pcapi.bytedesk.com", this.type = "", this.client = "web_pc" } return _createClass(e, [{ key: "render", value: function () { this.addCss(), this.createDOMElements(), this.showIconButton(), this.firstOpen = !0 } }, { key: "addCss", value: function () { var e = document.createElement("link"); e.setAttribute("rel", "stylesheet"), e.setAttribute("href", this.isProduction ? "https://cdn.kefux.com/chat/pc/css/custom.min.css" : "http://127.0.0.1:8887/chat/pc/css/custom.css"), document.head.appendChild(e) } }, { key: "createDOMElements", value: function () { var e = this; this.chatWindowMobileMask = document.createElement("div"), this.chatWindowMobileMask.classList.add("bytedesk_black_overlay"), this.chatWindowMobileMask.setAttribute("id", "bytedesk_black_overlay"), document.body.appendChild(this.chatWindowMobileMask), document.getElementById("bytedesk_black_overlay").addEventListener("click", function () { document.getElementById("bytedesk_black_overlay").style.display = "none", e.showIconButton() }), this.rectangleButton = document.createElement("div"), this.rectangleButton.innerHTML = '<button type="button" id="bytedesk_rectangle_button" class="bytedesk_rectangle_button bytedesk_' + this.config.position + "_" + this.config.margin + " bytedesk_bottom_" + this.config.bottomMargin + ' animation" style="color: ' + this.config.iconButton.color + "; background-color: " + this.config.iconButton.background + ';">\n <svg fill="currentColor" style="margin-right: 7px; margin-top: 2px; vertical-align: text-top;" height="16px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024">\n <path d="M506.496 939.136c85.76 0 168.64-22.464 240.512-65.024l187.712 54.656-43.136-193.792c41.28-64.384 62.912-137.344 62.912-212.032 0-229.44-201.024-416.128-448-416.128s-448 186.688-448 416.128C58.496 752.512 259.456 939.136 506.496 939.136L506.496 939.136 506.496 939.136zM748.672 831.168l-9.536 5.952c-68.672 43.008-149.184 65.664-232.64 65.664-228.224 0-413.888-170.368-413.888-379.84s185.664-379.84 413.888-379.84c228.224 0 413.888 170.368 413.888 379.84 0 70.528-21.632 139.712-62.592 200.064l-6.4 9.344 30.656 137.536L748.672 831.168 748.672 831.168 748.672 831.168zM739.904 819.904" p-id="6481"></path>\n <path d="M602.368 524.224c0 26.88 21.376 48.704 47.744 48.704s47.744-21.824 47.744-48.704c0-26.88-21.376-48.704-47.744-48.704S602.368 497.344 602.368 524.224L602.368 524.224 602.368 524.224zM602.368 524.224" p-id="6482"></path>\n <path d="M436.608 524.224c0 26.88 21.376 48.704 47.744 48.704 26.432 0 47.744-21.824 47.744-48.704 0-26.88-21.312-48.704-47.744-48.704C457.984 475.584 436.608 497.344 436.608 524.224L436.608 524.224 436.608 524.224zM436.608 524.224" p-id="6483"></path>\n <path d="M270.848 524.224c0 26.88 21.376 48.704 47.744 48.704 26.368 0 47.744-21.824 47.744-48.704 0-26.88-21.376-48.704-47.744-48.704C292.224 475.584 270.848 497.344 270.848 524.224L270.848 524.224 270.848 524.224zM270.848 524.224" p-id="6484"></path>\n </svg>' + this.config.iconButton.text + "</button>", this.rectangleButton.style.display = "none", document.body.appendChild(this.rectangleButton), this.roundButton = document.createElement("div"), this.roundButton.setAttribute("id", "bytedesk_round_button"), this.roundButton.classList.add("bytedesk_" + this.config.position + "_" + this.config.margin), this.roundButton.classList.add("bytedesk_bottom_" + this.config.bottomMargin), this.roundButton.setAttribute("style", "color: " + this.config.iconButton.color + "; background-color: " + this.config.iconButton.background + ";"), this.roundButton.innerHTML = '<img src="https://cdn.bytedesk.com/assets/img/icon/chat.png" class="bytedesk_round_button_icon"/><span id="bytedesk_unread_count" class="bytedesk_unread_count">1</span>', this.roundButton.style.display = "none", document.body.appendChild(this.roundButton), this.chatWindow = document.createElement("div"), this.chatWindow.setAttribute("id", "bytedesk_chat_window"), this.chatWindow.classList.add("bytedesk_chat_window_" + this.config.column), this.chatWindow.classList.add("bytedesk_" + this.config.positionWindow), this.chatWindow.setAttribute("style", "background-color: " + this.config.background), this.chatWindow.innerHTML = '<iframe name="bytedesk" id="bytedesk_iframe" loading="lazy" width="100%" height="100%" src="" frameborder="0"></iframe>\n <iframe name="bytedesk_preload" id="bytedesk_iframe_preload" loading="lazy" width="0%" height="0%" src="" frameborder="0"></iframe>', document.body.appendChild(this.chatWindow), this.chatWindowDOM = document.getElementById("bytedesk_chat_window"), this.rectangleButtonMobile = document.createElement("div"), this.rectangleButtonMobile.setAttribute("id", "bytedesk_rectangle_button_mobile"), this.rectangleButtonMobile.setAttribute("style", "color: " + this.config.iconButton.color + "; background-color: " + this.config.iconButton.background + ";"), this.rectangleButtonMobile.innerHTML = '<div style="margin-top: 14px;"><img src="https://cdn.bytedesk.com/assets/img/icon/chat.png" class="bytedesk_rectangle_button_mobile_icon"/>在线客服<span id="bytedesk_rectangle_button_mobile_unread_count" class="bytedesk_unread_count">1</span></div>', this.rectangleButtonMobile.style.display = "none", document.body.appendChild(this.rectangleButtonMobile), this.chatWindowMobile = document.createElement("div"), this.chatWindowMobile.setAttribute("id", "bytedesk_chat_window_mobile"), this.chatWindowMobile.classList.add("bytedesk_chat_window_mobile"), this.chatWindowMobile.setAttribute("style", "background-color: " + this.config.background + ";"), this.chatWindowMobile.innerHTML = '<iframe name="bytedesk_mobile" id="bytedesk_iframe_mobile" loading="lazy" width="100%" height="100%" src="" frameborder="0"></iframe>\n <iframe name="bytedesk_preload_mobile" id="bytedesk_iframe_preload_mobile" loading="lazy" width="0%" height="0%" src="" frameborder="0"></iframe>', document.body.appendChild(this.chatWindowMobile), this.chatWindowDomMobile = document.getElementById("bytedesk_chat_window_mobile"), null != document.getElementById("bytedesk_rectangle_button") && document.getElementById("bytedesk_rectangle_button").addEventListener("click", function () { return e.openChatWindow() }), null != document.getElementById("bytedesk_round_button") && document.getElementById("bytedesk_round_button").addEventListener("click", function () { return e.openChatWindow() }), null != document.getElementById("bytedesk_rectangle_button_mobile") && document.getElementById("bytedesk_rectangle_button_mobile").addEventListener("click", function () { return e.openChatWindow() }) } }, { key: "removeDOMElements", value: function () { null != document.getElementById("bytedesk_rectangle_button") && document.getElementById("bytedesk_rectangle_button").remove(), null != document.getElementById("bytedesk_round_button") && document.getElementById("bytedesk_round_button").remove(), null != document.getElementById("bytedesk_chat_window") && document.getElementById("bytedesk_chat_window").remove(), null != document.getElementById("bytedesk_rectangle_button_mobile") && document.getElementById("bytedesk_rectangle_button_mobile").remove() } }, { key: "showIconButton", value: function () { this.isMobile() ? "rectangle" === this.config.shape ? this.rectangleButtonMobile.style.display = "block" : this.roundButton.style.display = "block" : "rectangle" === this.config.shape ? this.rectangleButton.style.display = "block" : this.roundButton.style.display = "block", this.chatWindowDOM.classList.remove("bytedesk_show"), this.chatWindowDomMobile.classList.remove("bytedesk_show"), this.isChatWindowShown = !1, document.getElementById("bytedesk_black_overlay").style.display = "none" } }, { key: "openChatWindow", value: function () { this.printLog("openChatWindow:", window.frames), this.rectangleButton.style.display = "none", this.roundButton.style.display = "none", this.rectangleButtonMobile.style.display = "none", this.isMobile() ? (console.log("isMobile display mask"), document.getElementById("bytedesk_black_overlay").style.display = "block", this.chatWindowDomMobile.classList.add("bytedesk_show")) : (console.log("is not isMobile"), this.chatWindowDOM.classList.add("bytedesk_show")), this.firstOpen && (this.isMobile() ? (console.log("bytedesk_iframe_mobile", this.chatUrl), window.frames.bytedesk_iframe_mobile.src = this.chatUrl, window.frames.bytedesk_iframe_preload_mobile.src = "") : (console.log("bytedesk_iframe", this.chatUrl), window.frames.bytedesk_iframe.src = this.chatUrl, window.frames.bytedesk_iframe_preload.src = ""), this.firstOpen = !1), this.isChatWindowShown = !0, this.unreadCount = 0, document.getElementById("bytedesk_unread_count").style.display = "none", document.getElementById("bytedesk_unread_count").innerHTML = this.unreadCount, document.getElementById("bytedesk_rectangle_button_mobile_unread_count").style.display = "none", document.getElementById("bytedesk_rectangle_button_mobile_unread_count").innerHTML = this.unreadCount } }, { key: "isMobile", value: function () { return function () { return navigator.userAgent.match(/Android/i) }() || function () { return navigator.userAgent.match(/BlackBerry/i) }() || function () { return navigator.userAgent.match(/iPhone|iPad|iPod/i) }() || function () { return navigator.userAgent.match(/Opera Mini/i) }() || function () { return navigator.userAgent.match(/IEMobile/i) }() } }, { key: "start", value: function (e, t) { t || (t = {}), this.config = { shape: t.shape || "rectangle", column: t.column || "one", history: t.history || "0", closable: t.history || "1", autoPop: void 0 !== t.autoPop && t.autoPop, v2robot: void 0 === t.v2robot ? "0" : t.v2robot, selfuser: t.selfuser || "0", username: t.username || "", nickname: t.nickname || "", avatar: t.avatar || "", goods: t.goods || "0", goods_id: t.goods_id || "0", goods_title: t.goods_title || "", goods_content: t.goods_content || "", goods_price: t.goods_price || "0", goods_url: t.goods_url || "", goods_imageUrl: t.goods_imageUrl || "", goods_categoryCode: t.goods_categoryCode || "", postscript: t.postscript || "", position: t.position || "left", margin: t.margin || "15", bottomMargin: t.bottomMargin || "15", positionWindow: t.positionWindow || "right", background: t.bannerBackground || "#fff", color: t.bannerColor || "#1d2e38", iconButton: { color: t.buttonColor || "#1d2e38", background: t.buttonBackground || "#fff", text: t.buttonText || ("cn" === e ? "在线客服" : "Live Chat") } }, this.websiteUrl = window.location.href, this.websiteTitle = document.title, this.refererUrl = document.referrer, this.type = t.type, this.isMobile() ? (this.chatUrl = ("cn" === e ? this.h5BaseUrl : this.h5BaseUrlEn) + "?sub=" + t.subDomain + "&uid=" + t.adminUid + "&wid=" + t.workGroupWid + "&type=" + t.type + "&aid=" + t.agentUid + "&history=" + this.config.history + "&lang=" + e + "&color=" + encodeURIComponent(this.config.color) + "&background=" + encodeURIComponent(this.config.background) + "&websiteurl=" + encodeURIComponent(this.websiteUrl) + "&websitetitle=" + encodeURIComponent(this.websiteTitle) + "&refererurl=" + encodeURIComponent(this.refererUrl) + "&v2robot=" + this.config.v2robot + ("1" === this.config.selfuser ? "&selfuser=" + this.config.selfuser + "&username=" + encodeURIComponent(this.config.username) + "&nickname=" + encodeURIComponent(this.config.nickname) + "&avatar=" + encodeURIComponent(this.config.avatar) : "") + ("1" === this.config.goods ? "&goods=" + this.config.goods + "&goods_id=" + this.config.goods_id + "&goods_title=" + encodeURIComponent(this.config.goods_title) + "&goods_content=" + encodeURIComponent(this.config.goods_content) + "&goods_price=" + encodeURIComponent(this.config.goods_price) + "&goods_url=" + encodeURIComponent(this.config.goods_url) + "&goods_imageUrl=" + encodeURIComponent(this.config.goods_imageUrl) + "&goods_categoryCode=" + this.config.goods_categoryCode : "") + ("" !== this.config.postscript ? "&postscript=" + encodeURIComponent(this.config.postscript) : "") + "&isembed=1&closable=1&preload=0&p", this.chatUrlPreload = ("cn" === e ? this.h5BaseUrl : this.h5BaseUrlEn) + "?sub=" + t.subDomain + "&uid=" + t.adminUid + "&wid=" + t.workGroupWid + "&type=" + t.type + "&aid=" + t.agentUid + "&history=" + this.config.history + "&lang=" + e + "&color=" + encodeURIComponent(this.config.color) + "&background=" + encodeURIComponent(this.config.background) + "&websiteurl=" + encodeURIComponent(this.websiteUrl) + "&websitetitle=" + encodeURIComponent(this.websiteTitle) + "&refererurl=" + encodeURIComponent(this.refererUrl) + "&v2robot=" + this.config.v2robot + ("1" === this.config.selfuser ? "&selfuser=" + this.config.selfuser + "&username=" + encodeURIComponent(this.config.username) + "&nickname=" + encodeURIComponent(this.config.nickname) + "&avatar=" + encodeURIComponent(this.config.avatar) : "") + ("1" === this.config.goods ? "&goods=" + this.config.goods + "&goods_id=" + this.config.goods_id + "&goods_title=" + encodeURIComponent(this.config.goods_title) + "&goods_content=" + encodeURIComponent(this.config.goods_content) + "&goods_price=" + encodeURIComponent(this.config.goods_price) + "&goods_url=" + encodeURIComponent(this.config.goods_url) + "&goods_imageUrl=" + encodeURIComponent(this.config.goods_imageUrl) + "&goods_categoryCode=" + this.config.goods_categoryCode : "") + ("" !== this.config.postscript ? "&postscript=" + encodeURIComponent(this.config.postscript) : "") + "&isembed=1&closable=1&preload=1&p") : (this.chatUrl = ("cn" === e ? this.pcBaseUrl : this.pcBaseUrlEn) + "?sub=" + t.subDomain + "&uid=" + t.adminUid + "&wid=" + t.workGroupWid + "&type=" + t.type + "&aid=" + t.agentUid + "&history=" + this.config.history + "&lang=" + e + "&color=" + encodeURIComponent(this.config.color) + "&background=" + encodeURIComponent(this.config.background) + "&column=" + ("two" === t.column ? "2" : "1") + "&websiteurl=" + encodeURIComponent(this.websiteUrl) + "&websitetitle=" + encodeURIComponent(this.websiteTitle) + "&refererurl=" + encodeURIComponent(this.refererUrl) + "&v2robot=" + this.config.v2robot + ("1" === this.config.selfuser ? "&selfuser=" + this.config.selfuser + "&username=" + encodeURIComponent(this.config.username) + "&nickname=" + encodeURIComponent(this.config.nickname) + "&avatar=" + encodeURIComponent(this.config.avatar) : "") + ("1" === this.config.goods ? "&goods=" + this.config.goods + "&goods_id=" + this.config.goods_id + "&goods_title=" + encodeURIComponent(this.config.goods_title) + "&goods_content=" + encodeURIComponent(this.config.goods_content) + "&goods_price=" + encodeURIComponent(this.config.goods_price) + "&goods_url=" + encodeURIComponent(this.config.goods_url) + "&goods_imageUrl=" + encodeURIComponent(this.config.goods_imageUrl) + "&goods_categoryCode=" + this.config.goods_categoryCode : "") + ("" !== this.config.postscript ? "&postscript=" + encodeURIComponent(this.config.postscript) : "") + "&closable=1&preload=0&p", this.chatMaxUrl = ("cn" === e ? this.pcBaseUrl : this.pcBaseUrlEn) + "?sub=" + t.subDomain + "&uid=" + t.adminUid + "&wid=" + t.workGroupWid + "&type=" + t.type + "&aid=" + t.agentUid + "&history=" + this.config.history + "&lang=" + e + "&color=" + encodeURIComponent(this.config.color) + "&background=" + encodeURIComponent(this.config.background) + "&column=2&websiteurl=" + encodeURIComponent(this.websiteUrl) + "&websitetitle=" + encodeURIComponent(this.websiteTitle) + "&refererurl=" + encodeURIComponent(this.refererUrl) + "&v2robot=" + this.config.v2robot + ("1" === this.config.selfuser ? "&selfuser=" + this.config.selfuser + "&username=" + encodeURIComponent(this.config.username) + "&nickname=" + encodeURIComponent(this.config.nickname) + "&avatar=" + encodeURIComponent(this.config.avatar) : "") + ("1" === this.config.goods ? "&goods=" + this.config.goods + "&goods_id=" + this.config.goods_id + "&goods_title=" + encodeURIComponent(this.config.goods_title) + "&goods_content=" + encodeURIComponent(this.config.goods_content) + "&goods_price=" + encodeURIComponent(this.config.goods_price) + "&goods_url=" + encodeURIComponent(this.config.goods_url) + "&goods_imageUrl=" + encodeURIComponent(this.config.goods_imageUrl) + "&goods_categoryCode=" + this.config.goods_categoryCode : "") + ("" !== this.config.postscript ? "&postscript=" + encodeURIComponent(this.config.postscript) : "") + "&closable=0&preload=0&p", this.chatUrlPreload = ("cn" === e ? this.pcBaseUrl : this.pcBaseUrlEn) + "?sub=" + t.subDomain + "&uid=" + t.adminUid + "&wid=" + t.workGroupWid + "&type=" + t.type + "&aid=" + t.agentUid + "&history=" + this.config.history + "&lang=" + e + "&color=" + encodeURIComponent(this.config.color) + "&background=" + encodeURIComponent(this.config.background) + "&column=" + ("two" === t.column ? "2" : "1") + "&websiteurl=" + encodeURIComponent(this.websiteUrl) + "&websitetitle=" + encodeURIComponent(this.websiteTitle) + "&refererurl=" + encodeURIComponent(this.refererUrl) + "&v2robot=" + this.config.v2robot + ("1" === this.config.selfuser ? "&selfuser=" + this.config.selfuser + "&username=" + encodeURIComponent(this.config.username) + "&nickname=" + encodeURIComponent(this.config.nickname) + "&avatar=" + encodeURIComponent(this.config.avatar) : "") + ("1" === this.config.goods ? "&goods=" + this.config.goods + "&goods_id=" + this.config.goods_id + "&goods_title=" + encodeURIComponent(this.config.goods_title) + "&goods_content=" + encodeURIComponent(this.config.goods_content) + "&goods_price=" + encodeURIComponent(this.config.goods_price) + "&goods_url=" + encodeURIComponent(this.config.goods_url) + "&goods_imageUrl=" + encodeURIComponent(this.config.goods_imageUrl) + "&goods_categoryCode=" + this.config.goods_categoryCode : "") + ("" !== this.config.postscript ? "&postscript=" + encodeURIComponent(this.config.postscript) : "") + "&closable=1&preload=1&p"); var o = this; o.render(); var n = 0, i = 0, s = 0, d = 0, c = !1; o.firstStart && (o.firstStart = !1, window.addEventListener("message", function (e) { if ("bytedesk-close" === e.data) o.showIconButton(); else if ("bytedesk-minus" === e.data) o.showIconButton(); else if ("bytedesk-max" === e.data) window.open(o.chatMaxUrl); else if ("bytedesk-popup" === e.data) o.openChatWindow(); else if ("bytedesk-mousedown" === e.data.msg) s = o.chatWindowDOM.offsetLeft, d = o.chatWindowDOM.offsetTop, n = e.data.mouseX + s, i = e.data.mouseY + d, c = !0; else if ("bytedesk-mousemove" === e.data.msg) { var t = e.data.mouseX + s, a = e.data.mouseY + d; if (c) { var r = s + t - n, l = d + a - i, h = window.screen.width, g = window.screen.height, u = o.chatWindowDOM.offsetWidth, m = o.chatWindowDOM.offsetHeight, b = h - u, p = g - m; r = Math.min(Math.max(0, r), b), l = Math.min(Math.max(0, l), p), o.chatWindowDOM.style.left = r + "px", o.chatWindowDOM.style.top = l + "px" } } else "bytedesk-mouseup" === e.data ? c = !1 : "bytedesk-message" === e.data.msg ? o.isChatWindowShown || (o.unreadCount += 1, o.printLog("unreadCount: " + o.unreadCount), document.getElementById("bytedesk_unread_count").style = "", document.getElementById("bytedesk_unread_count").innerHTML = o.unreadCount, document.getElementById("bytedesk_rectangle_button_mobile_unread_count").style = "", document.getElementById("bytedesk_rectangle_button_mobile_unread_count").innerHTML = o.unreadCount) : "bytedesk-status" === e.data.msg && o.printLog("bytedesk-status type:" + e.data.type + " uuid:" + e.data.uuid + " status:" + e.data.status) }, !1)), this.config.autoPop && this.openChatWindow(), 0 === o.unreadCount && (document.getElementById("bytedesk_unread_count").style.display = "none", document.getElementById("bytedesk_rectangle_button_mobile_unread_count").style.display = "none"), this.isMobile() ? window.frames.bytedesk_iframe_mobile.src = this.chatUrl : window.frames.bytedesk_iframe_preload.src = this.chatUrlPreload } }, { key: "startWithoutIcon", value: function () { this.openChatWindow() } }, { key: "hideIcon", value: function () { this.rectangleButton.style.display = "none", this.roundButton.style.display = "none" } }, { key: "showIcon", value: function () { this.roundButton.style.display = "block" } }, { key: "printLog", value: function (e) { console.log(e) } }]), e }(), byteDesk = new ByteDesk;