You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-fly/static/js/gofly-front.js

336 lines
11 KiB

4 years ago
var GOFLY={
GOFLY_URL:"https://gofly.sopans.com",
GOFLY_KEFU_ID:"",
GOFLY_BTN_TEXT:"Chat with me",
4 years ago
GOFLY_LANG:"en",
GOFLY_EXTRA: {},
GOFLY_AUTO_OPEN:true,
3 years ago
GOFLY_AUTO_SHOW:false,
GOFLY_WITHOUT_BTN:false,
4 years ago
};
GOFLY.launchButtonFlag=false;
GOFLY.titleTimer=0;
GOFLY.titleNum=0;
4 years ago
GOFLY.noticeTimer=null;
4 years ago
GOFLY.originTitle=document.title;
GOFLY.chatPageTitle="GOFLY";
GOFLY.kefuName="";
GOFLY.kefuAvator="";
4 years ago
GOFLY.init=function(config){
4 years ago
var _this=this;
4 years ago
if(typeof config=="undefined"){
return;
}
4 years ago
4 years ago
if (typeof config.GOFLY_URL!="undefined"){
this.GOFLY_URL=config.GOFLY_URL.replace(/([\w\W]+)\/$/,"$1");
4 years ago
}
4 years ago
this.dynamicLoadCss(this.GOFLY_URL+"/static/css/gofly-front.css?v=1");
4 years ago
if (typeof config.GOFLY_KEFU_ID!="undefined"){
this.GOFLY_KEFU_ID=config.GOFLY_KEFU_ID;
}
if (typeof config.GOFLY_BTN_TEXT!="undefined"){
this.GOFLY_BTN_TEXT=config.GOFLY_BTN_TEXT;
}
if (typeof config.GOFLY_EXTRA!="undefined"){
this.GOFLY_EXTRA=config.GOFLY_EXTRA;
}
if (typeof config.GOFLY_AUTO_OPEN!="undefined"){
this.GOFLY_AUTO_OPEN=config.GOFLY_AUTO_OPEN;
}
3 years ago
if (typeof config.GOFLY_AUTO_SHOW!="undefined"){
this.GOFLY_AUTO_SHOW=config.GOFLY_AUTO_SHOW;
}
if (typeof config.GOFLY_WITHOUT_BTN!="undefined"){
this.GOFLY_WITHOUT_BTN=config.GOFLY_WITHOUT_BTN;
}
var refer=document.referrer?document.referrer:"无";
this.GOFLY_EXTRA.refer=refer;
this.GOFLY_EXTRA.host=document.location.href;
4 years ago
this.GOFLY_EXTRA=JSON.stringify(_this.GOFLY_EXTRA);
4 years ago
this.dynamicLoadJs(this.GOFLY_URL+"/static/js/functions.js?v=1",function(){
if (typeof config.GOFLY_LANG!="undefined"){
4 years ago
_this.GOFLY_LANG=config.GOFLY_LANG;
4 years ago
}else{
4 years ago
_this.GOFLY_LANG=checkLang();
4 years ago
}
4 years ago
_this.GOFLY_EXTRA=utf8ToB64(_this.GOFLY_EXTRA);
4 years ago
});
4 years ago
if (typeof $!="function"){
this.dynamicLoadJs("https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js",function () {
_this.dynamicLoadJs("https://cdn.bootcdn.net/ajax/libs/layer/3.1.1/layer.min.js",function () {
3 years ago
_this.jsCallBack();
4 years ago
});
});
4 years ago
}else{
this.dynamicLoadJs("https://cdn.bootcdn.net/ajax/libs/layer/3.1.1/layer.min.js",function () {
3 years ago
_this.jsCallBack();
4 years ago
});
}
3 years ago
_this.addEventlisten();
}
GOFLY.jsCallBack=function(){
this.showKefuBtn();
this.addClickEvent();
this.getNotice();
}
GOFLY.showKefuBtn=function(){
var _this=this;
if(_this.GOFLY_WITHOUT_BTN){
return;
}
var html="<div class='launchButtonBox'>" +
'<div id="launchButton" class="launchButton">' +
'<div id="launchIcon" class="launchIcon animateUpDown">1</div> ' +
'<div class="launchButtonText">'+_this.GOFLY_BTN_TEXT+'</div></div>' +
'<div id="launchButtonNotice" class="launchButtonNotice"></div>' +
'</div>';
$('body').append(html);
}
GOFLY.addClickEvent=function(){
var _this=this;
$(".launchButton").on("click",function() {
_this.showKefu();
$("#launchIcon").text(0).hide();
});
4 years ago
3 years ago
$("body").on("click","#launchNoticeClose",function() {
$("#launchButtonNotice").hide();
});
$("body").click(function () {
clearTimeout(_this.titleTimer);
document.title = _this.originTitle;
});
}
GOFLY.addEventlisten=function(){
var _this=this;
4 years ago
window.addEventListener('message',function(e){
var msg=e.data;
if(msg.type=="message"){
clearInterval(_this.noticeTimer);
var width=$(window).width();
if(width>768){
_this.flashTitle();//标题闪烁
}
if (this.launchButtonFlag) return;
var welcomeHtml="<div class='flyUser'><img class='flyAvatar' src='"+_this.GOFLY_URL+_this.kefuAvator+"'/> <span class='flyUsername'>"+_this.kefuName+"</span>" +
"<span id='launchNoticeClose' class='flyClose'>×</span>" +
"</div>";
welcomeHtml+="<div id='launchNoticeContent'>"+replaceContent(msg.data.content,_this.GOFLY_URL)+"</div>";
$("#launchButtonNotice").html(welcomeHtml).show();
4 years ago
var news=$("#launchIcon").text();
$("#launchIcon").text(++news).show();
4 years ago
}
if(msg.type=="focus"){
clearTimeout(_this.titleTimer);
_this.titleTimer=0;
document.title = _this.originTitle;
}
});
4 years ago
window.onfocus = function () {
clearTimeout(_this.titleTimer);
_this.titleTimer=0;
4 years ago
document.title = _this.originTitle;
};
}
GOFLY.dynamicLoadCss=function(url){
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.type='text/css';
link.rel = 'stylesheet';
link.href = url;
head.appendChild(link);
}
GOFLY.dynamicLoadJs=function(url, callback){
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
if(typeof(callback)=='function'){
script.onload = script.onreadystatechange = function () {
if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete"){
callback();
script.onload = script.onreadystatechange = null;
}
};
}
head.appendChild(script);
}
4 years ago
GOFLY.getNotice=function(){
var _this=this;
3 years ago
if(!_this.GOFLY_AUTO_OPEN){
return;
}
$.get(this.GOFLY_URL+"/notice?kefu_id="+this.GOFLY_KEFU_ID,function(res) {
if(res.result.status=='offline'){
_this.chatPageTitle="<div class='launchPointer offline'></div>";
}else{
_this.chatPageTitle="<div class='launchPointer'></div>";
4 years ago
setTimeout(function(){
var userInfo="<img style='margin-top: 8px;' class='flyAvatar' src='"+_this.GOFLY_URL+res.result.avatar+"'/> <span class='flyUsername'>"+res.result.username+"</span>"
$('.launchButtonText').html(userInfo);
},3000);
}
_this.kefuAvator=res.result.avatar;
_this.kefuName=res.result.username;
_this.chatPageTitle+="<img src='"+_this.GOFLY_URL+res.result.avatar+"' class='flyAvatar'>"+res.result.username;
4 years ago
if(_this.GOFLY_AUTO_OPEN&&_this.isIE()<=0){
_this.showKefu();
$(".launchButtonBox").show();
_this.launchButtonFlag=false;
}
4 years ago
if (res.result.welcome != null) {
var msg = res.result.welcome;
4 years ago
var len=msg.length;
var i=0;
if(len>0){
4 years ago
_this.noticeTimer=setInterval(function(){
if(i>=len||typeof msg[i]=="undefined"||msg[i]==null){
clearInterval(_this.noticeTimer);
return;
}
var content = msg[i];
if(typeof content.content =="undefined"){
return;
}
var welcomeHtml="<div class='flyUser'><img class='flyAvatar' src='"+_this.GOFLY_URL+res.result.avatar+"'/> <span class='flyUsername'>"+res.result.username+"</span>" +
"<span id='launchNoticeClose' class='flyClose'>×</span>" +
"</div>";
welcomeHtml+="<div id='launchNoticeContent'>"+replaceContent(content.content,_this.GOFLY_URL)+"</div>";
4 years ago
var obj=$("#launchButtonNotice");
3 years ago
if(obj[0]){
obj[0].innerHTML=welcomeHtml;
obj.show();
}
4 years ago
i++;
4 years ago
$("#launchIcon").text(i).show();
4 years ago
},4000);
4 years ago
}
}
});
}
4 years ago
GOFLY.isIE=function(){
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
if(isIE) {
var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
reIE.test(userAgent);
var fIEVersion = parseFloat(RegExp["$1"]);
if(fIEVersion == 7) {
return 7;
} else if(fIEVersion == 8) {
return 8;
} else if(fIEVersion == 9) {
return 9;
} else if(fIEVersion == 10) {
return 10;
} else {
return 6;//IE版本<=7
}
} else if(isEdge) {
return 'edge';//edge
} else if(isIE11) {
return 11; //IE11
}else{
return -1;//不是ie浏览器
}
}
3 years ago
GOFLY.showPanel=function (){
var width=$(window).width();
this.GOFLY_AUTO_SHOW=true;
if(this.isIE()>0){
this.windowOpen();
return;
}
if(width<768){
this.layerOpen("100%","100%");
return;
}
this.layerOpen("400px","530px");
this.launchButtonFlag=true;
}
4 years ago
GOFLY.showKefu=function (){
if (this.launchButtonFlag) return;
var width=$(window).width();
if(this.isIE()>0){
4 years ago
this.windowOpen();
return;
}
if(width<768){
this.layerOpen("100%","100%");
return;
}
this.layerOpen("400px","530px");
4 years ago
this.launchButtonFlag=true;
4 years ago
$(".launchButtonBox").hide();
4 years ago
}
GOFLY.layerOpen=function (width,height){
4 years ago
if (this.launchButtonFlag) return;
var layBox=$("#layui-layer19911116");
if(layBox.css("display")=="none"){
layBox.show();
return;
}
4 years ago
var _this=this;
layer.index="19911115";
layer.open({
type: 2,
title: this.chatPageTitle,
closeBtn: 1, //不显示关闭按钮
4 years ago
shade: 0,
area: [width, height],
offset: 'rb', //右下角弹出
anim: 2,
content: [this.GOFLY_URL+'/chatIndex?kefu_id='+this.GOFLY_KEFU_ID+'&lang='+this.GOFLY_LANG+'&refer='+window.document.title+'&extra='+this.GOFLY_EXTRA , 'yes'], //iframe的urlno代表不显示滚动条
4 years ago
success:function(){
var layBox=$("#layui-layer19911116");
3 years ago
if(_this.GOFLY_AUTO_SHOW&&layBox.css("display")=="none"){
_this.launchButtonFlag=true;
layBox.show();
4 years ago
}
},
end: function(){
4 years ago
_this.launchButtonFlag=false;
$(".launchButtonBox").show();
},
cancel: function(index, layero){
$("#layui-layer19911116").hide();
_this.launchButtonFlag=false;
$(".launchButtonBox").show();
return false;
}
});
}
4 years ago
GOFLY.windowOpen=function (){
window.open(this.GOFLY_URL+'/chatIndex?kefu_id='+this.GOFLY_KEFU_ID+'&lang='+this.GOFLY_LANG+'&refer='+window.document.title+'&extra='+this.GOFLY_EXTRA);
}
4 years ago
GOFLY.flashTitle=function () {
if(this.titleTimer!=0){
return;
}
this.titleTimer = setInterval("GOFLY.flashTitleFunc()", 500);
}
GOFLY.flashTitleFunc=function(){
4 years ago
this.titleNum++;
if (this.titleNum >=3) {
this.titleNum = 1;
}
4 years ago
if (this.titleNum == 1) {
document.title = '【】' + this.originTitle;
}
4 years ago
if (this.titleNum == 2) {
document.title = '【new message】' + this.originTitle;
}
}
4 years ago