;!function (win, doc) { "use strict"; var config = { domain: "https://lian.zj11.net", uid: 'TEST-UID', uName: 'TEST-UNAME', avatar: 'TEST-AVATAR', language: document.getElementsByTagName('html')[0].getAttribute('lang') } , ws_ck_div , ws_chat_div , ck_style = "position:fixed;z-index:201902151030;right:20px;bottom:50px;padding:0;" + "margin:0;width:100px;height:40px;background:#1E9FFF;border-radius:100px;" + "line-height:40px;text-align:center;color:#fff;font-size:13px;cursor:pointer;" , chat_style = "position:fixed;bottom:50px;z-index:201902151030;right:20px;padding:0;margin:0;" + "overflow:hidden;background-color:transparent;box-shadow:0 0 20px 0 rgba(0, 0, 0, .15);" , hide_style = "display:none" , show_style = "display:block" , lianChat = function () { this.v = '2.0'; } , setCustomer = function (customer) { localStorage.setItem('uid', customer.uid); localStorage.setItem('uName', customer.uName); localStorage.setItem('avatar', customer.avatar); } , getCustomer = function () { return { uid: localStorage.getItem('uid'), uName: localStorage.getItem('uName'), avatar: localStorage.getItem('avatar'), referrer: localStorage.getItem('lianChat_referrer') }; } , getGuestMark = function () { return { uid: localStorage.getItem('uid'), uIP: "95.216.99.153", fromDomain: location.hostname, pageTitle: document.getElementsByTagName('title')[0].text, pageUrl: location.href, language: config.language }; } , showChat = function () { doc.getElementById("WS-SHOW-CHAT").onclick = function () { ws_ck_div.setAttribute("style", ck_style + hide_style); ws_chat_div.setAttribute("style", chat_style + show_style); // console.log('config:',config); doc.getElementById("parentIframe").contentWindow.postMessage(JSON.stringify({ cmd: 'c_info', data: getCustomer() }), '*'); doc.getElementById("parentIframe").contentWindow.postMessage(JSON.stringify({ cmd: 'open_chat', data: getGuestMark() }), 'https://lian.zj11.net'); }; } , createBox = function () { ws_ck_div = document.createElement("div"); ws_ck_div.setAttribute("style", ck_style); ws_ck_div.setAttribute("id", "WS-SHOW-CHAT"); var str_tips = "咨询客服"; if (config.language == 'en') { str_tips = 'Consulting'; } var text = document.createTextNode(str_tips); ws_ck_div.appendChild(text); doc.body.appendChild(ws_ck_div); ws_chat_div = document.createElement("div"); ws_chat_div.setAttribute("id", "lianChat"); ws_chat_div.setAttribute("style", chat_style + hide_style); doc.body.appendChild(ws_chat_div); var ws_button = doc.createElement('button'); ws_button.setAttribute('type', 'button'); ws_button.setAttribute('style', 'background: #1E9FFF;border: 0;color: white;padding: 5px 15px;margin-right: 5px;'); ws_button.setAttribute('onclick','lianChat.closeBox();'); ws_button.innerText = '关闭'; var ws_dir = document.createElement('div'); ws_dir.setAttribute('style', 'height: 30px;background-color: #FBFBFB;text-align: right;'); ws_dir.appendChild(ws_button); ws_chat_div.appendChild(ws_dir); var ws_iframe = document.createElement("iframe"); ws_iframe.scrolling = "no"; ws_iframe.setAttribute("frameborder", "0", 0); ws_iframe.setAttribute("id", "parentIframe"); ws_iframe.setAttribute("width", "400px"); var height = "420px"; if (config.language == 'en') { height = "450px"; } ws_iframe.setAttribute("height", height); var code = '5c6cbcb7d55ca'; if (code === '5c6cbcb7d55ca') { $.ajax({ type: 'post', url: config.domain + "/index/index/leaveMsgCache", data: getGuestMark(), dataType: 'json', crossDomain: true, success: function (res) { var urlKey = ((typeof res === "object") && res.hasOwnProperty('uk')) ? res.uk : ''; ws_iframe.src = config.domain + '/index/index/leaveMsg/s/5c6cbcb7d55ca/uid/' + getCustomer().uid + '/uk/' + urlKey; ws_chat_div.appendChild(ws_iframe); showChat(); }, error: function (data) { console.log(data); } }); } else { ws_iframe.src = config.domain + "/index/index/clibox/u/5c6cbcb7d55ca/t/1629601083/tk/00ad6fdd71653ab598bec11880280726"; ws_chat_div.appendChild(ws_iframe); showChat(); } } , hideChatDiv = function () { ws_ck_div.setAttribute("style", ck_style + show_style); ws_chat_div.setAttribute("style", chat_style + hide_style); }; win.addEventListener('message', function (event) { if ('hide_chat' == event.data) { hideChatDiv(); } else if ('show_chat' == event.data) { doc.getElementById("WS-SHOW-CHAT").click(); } }, false); win.DOMContentLoaded = function () { doc.getElementById("parentIframe").contentWindow.postMessage(JSON.stringify({ cmd: 'c_info', data: getCustomer() }), '*'); }; lianChat.prototype.init = function (conf) { for (var key in conf) { if (config.hasOwnProperty(key) === true) { config[key] = conf[key]; } } // TODO-ZH: 临时清除旧数据(头像地址域名变更) // localStorage.clear(); var customerInfo = getCustomer(); if (config.uid == 'TEST-UID') { if (customerInfo.uid == null) { config.uid = Number(Math.random().toString().substr(3, 4) + Date.now()).toString(36); } else { config.uid = customerInfo.uid; } } if (config.uName == 'TEST-UNAME') { config.uName = '访客' + config.uid; } if (config.avatar == 'TEST-AVATAR') { //console.log('customerInfo:',customerInfo); if (customerInfo.avatar == null) { config.avatar = 'https://lian.zj11.net/static/common/images/customer.png'; } else { config.avatar = customerInfo.avatar; } } //console.log('setCustomer config:',config); setCustomer(config); createBox(); localStorage.setItem('lianChat_referrer', document.referrer); }; // TODO-ZH: 立即咨询按钮点击后出现在按钮的旁边 /* 基础对象 */ var baseObj = { chatbox_id: '#lianChat', }; /* 基础对象 */ var baseObj = { chatbox_id: '#lianChat', }; /* 状态obj */ var state = { out_flag: null, //弹出层是否超范围 }; /* 弹出chatBox层 */ var chatScrollObj = { chatScroll: function (cla, options) { var defaultOptions = { noScroll: false, top: 0, // left: 10 }; var options = Object.assign({}, defaultOptions, options); var _top = parseInt(options.top); var bodyHeight = document.documentElement.clientHeight; var clas = cla; var button = $(clas)[0]; var first_buttonRect = button.getBoundingClientRect(); //记录下第一次按钮距离视口顶部的距离 var calc_top = 0; if (state.out_flag == -1) { calc_top = bodyHeight - 400; } else if (state.out_flag == 1) { calc_top = 0; } else { calc_top = first_buttonRect.top + _top; } $(window).on("scroll", function () { chatScrollObj.scrollChatbox(options, calc_top); }); }, betweenButton: function betweenButton(cla, position) { var defaultPosition = { top: 0, left: 10 }; var position = Object.assign({}, defaultPosition, position); var clas = cla; var _top = parseInt(position.top); var _left = parseInt(position.left); var button = $(clas)[0]; var bodyWidth = document.documentElement.clientWidth; var bodyHeight = document.documentElement.clientHeight; var buttonRect = button.getBoundingClientRect(); var scroll_top = document.documentElement.scrollTop || document.body.scrollTop; var style = {}; var buttonLeft = buttonRect.left + buttonRect.width + _left; //更新后的 左距 buttonLeft = buttonLeft + 400 > bodyWidth ? bodyWidth - 400 : buttonLeft; //右边最大值 buttonLeft = buttonLeft < 0 ? 0 : buttonLeft; //左边最小值 // 如果bodyHeight - buttonRect.top < 400 向 下 超出了时,设置最小的定位 if (bodyHeight - buttonRect.top - _top <= 400) { style = { top: bodyHeight - 400 + scroll_top + "px", left: buttonLeft + "px", }; state.out_flag = -1; //向 下 } else if (buttonRect.top + _top < 0) { //向 上 超出了 style = { top: scroll_top + "px", left: buttonLeft + "px", }; state.out_flag = 1; //向 上 } else { style = { top: buttonRect.top + _top + scroll_top + "px", left: buttonLeft + "px", }; state.out_flag = 0; //没有超出范围 } $(baseObj.chatbox_id).css(style); }, scrollChatbox: function scrollChatbox(position, calc_top) { var scroll_top = document.documentElement.scrollTop || document.body.scrollTop; var style = {}; style = { top: calc_top + scroll_top + "px", // left: (bodyWidth - first_buttonRect.left + _left) + "px", }; $(baseObj.chatbox_id).css(style); }, }; lianChat.prototype.closeBox = function () { $('#lianChat').hide(); $('#WS-SHOW-CHAT').show(); }; lianChat.prototype.btnRun = function (btnClass, options) { ws_ck_div.click(); state.out_flag = null; $(baseObj.chatbox_id).css({ "position": "absolute", "display": 'block', "width": "400px", "height": "400px", }); chatScrollObj.betweenButton(btnClass, options); chatScrollObj.chatScroll(btnClass, options); } win.lianChat = new lianChat(); }(window, document);