优化前端js

pull/23/head
taoshihan1991 5 years ago
parent 91bca7bf7a
commit 77ce6ef3a6

@ -79,5 +79,5 @@
var KEFU_ID='{{.KEFU_ID}}'; var KEFU_ID='{{.KEFU_ID}}';
var REFER='{{.Refer}}'; var REFER='{{.Refer}}';
</script> </script>
<script src="/static/js/chat-page.js?v=0.4.0"></script> <script src="/static/js/chat-page.js?v=0.5.0"></script>
</html> </html>

@ -19,12 +19,14 @@
<!--对接客服代码--> <!--对接客服代码-->
<script src="/static/js/gofly-front.js"></script>
<script> <script>
var GOFLY_URL=""; GOFLY.init({
var GOFLY_KEFU_ID="kefu2"; GOFLY_URL:"https://gofly.sopans.com",
var GOFLY_BTN_TEXT="{{.OnlineChat}}"; GOFLY_KEFU_ID: "kefu2",
GOFLY_BTN_TEXT: "{{.OnlineChat}}"
})
</script> </script>
<script src="/static/js/gofly-front.js"></script>
<!--//对接客服代码--> <!--//对接客服代码-->
<!--自动弹代码--> <!--自动弹代码-->

@ -433,7 +433,6 @@ new Vue({
alertSound:function(){ alertSound:function(){
var b = document.getElementById("chatMessageAudio"); var b = document.getElementById("chatMessageAudio");
if (b.canPlayType('audio/ogg; codecs="vorbis"')) { if (b.canPlayType('audio/ogg; codecs="vorbis"')) {
alert(1);
b.type= 'audio/mpeg'; b.type= 'audio/mpeg';
b.src= '/static/images/alert2.ogg'; b.src= '/static/images/alert2.ogg';
var p = b.play(); var p = b.play();
@ -445,7 +444,6 @@ new Vue({
sendSound:function(){ sendSound:function(){
var b = document.getElementById("chatMessageSendAudio"); var b = document.getElementById("chatMessageSendAudio");
if (b.canPlayType('audio/ogg; codecs="vorbis"')) { if (b.canPlayType('audio/ogg; codecs="vorbis"')) {
alert(2);
b.type= 'audio/mpeg'; b.type= 'audio/mpeg';
b.src= '/static/images/sent.ogg'; b.src= '/static/images/sent.ogg';
var p = b.play(); var p = b.play();

@ -1,60 +1,82 @@
var launchButtonFlag=false; var GOFLY={
var titleTimer,titleNum=0; GOFLY_URL:"https://gofly.sopans.com",
var originTitle = document.title; GOFLY_KEFU_ID:"",
if (typeof GOFLY_URL=="undefined"){ GOFLY_BTN_TEXT:"Chat with me",
var GOFLY_URL="https://gofly.sopans.com"; };
} GOFLY.launchButtonFlag=false;
if (typeof GOFLY_KEFU_ID=="undefined"){ GOFLY.titleTimer=0;
var GOFLY_KEFU_ID=""; GOFLY.titleNum=0;
} GOFLY.originTitle=document.title;
if (typeof GOFLY_BTN_TEXT=="undefined"){ GOFLY.init=function(config){
var GOFLY_BTN_TEXT="Chat with me"; if(typeof config=="undefined"){
} return;
dynamicLoadCss(GOFLY_URL+"/static/css/gofly-front.css"); }
if (typeof $!="function"){ if (typeof config.GOFLY_URL!="undefined"){
dynamicLoadJs("https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js",function () { this.GOFLY_URL=config.GOFLY_URL;
dynamicLoadJs("https://cdn.bootcdn.net/ajax/libs/layer/3.1.1/layer.min.js",function () { }
clickBtn(); 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;
}
this.dynamicLoadCss(this.GOFLY_URL+"/static/css/gofly-front.css");
if (typeof $!="function"){
var _this=this;
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 () {
_this.clickBtn();
});
}); });
}else{
this.dynamicLoadJs("https://cdn.bootcdn.net/ajax/libs/layer/3.1.1/layer.min.js",function () {
_this.clickBtn();
});
}
window.addEventListener('message',function(e){
var msg=e.data;
if(msg.type=="message"){
_this.flashTitle();//标题闪烁
}
}); });
}else{ window.onfocus = function () {
dynamicLoadJs("https://cdn.bootcdn.net/ajax/libs/layer/3.1.1/layer.min.js",function () { clearTimeout(this.titleTimer);
clickBtn(); 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);
} }
function clickBtn(){ GOFLY.clickBtn=function (){
$('body').append('<div id="launchButton" class="launchButton animateUpDown"><div class="launchButtonText">'+GOFLY_BTN_TEXT+'</div></div>'); var _this=this;
$('body').append('<div id="launchButton" class="launchButton animateUpDown"><div class="launchButtonText">'+_this.GOFLY_BTN_TEXT+'</div></div>');
$("#launchButton").on("click",function() { $("#launchButton").on("click",function() {
if (launchButtonFlag) return; _this.showKefu();
var width=$(window).width();
if(width<768 || isIE()>0){
window.open(GOFLY_URL+'/chatIndex?kefu_id='+GOFLY_KEFU_ID+'&refer='+window.document.title);
return;
}
layer.open({
type: 2,
title: GOFLY_BTN_TEXT,
closeBtn: 1, //不显示关闭按钮
shade: 0,
area: ['520px', '530px'],
offset: 'rb', //右下角弹出
anim: 2,
content: [GOFLY_URL+'/chatIndex?kefu_id='+GOFLY_KEFU_ID+'&refer='+window.document.title, 'yes'], //iframe的urlno代表不显示滚动条
end: function(){
launchButtonFlag=false;
$(".launchButton").show();
}
});
launchButtonFlag=true;
$(".launchButton").hide();
});
$("body").click(function () {
clearTimeout(titleTimer);
document.title = originTitle;
}); });
} }
function isIE() { GOFLY.isIE=function(){
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器 var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器 var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
@ -82,80 +104,54 @@ function isIE() {
return -1;//不是ie浏览器 return -1;//不是ie浏览器
} }
} }
function showKefu(){ GOFLY.showKefu=function (){
if (launchButtonFlag) return; if (this.launchButtonFlag) return;
var width=$(window).width(); var width=$(window).width();
if(width<768 || isIE()>0){ if(width<768 || this.isIE()>0){
window.open(GOFLY_URL+'/chatIndex?kefu_id='+GOFLY_KEFU_ID+'&refer='+window.document.title); this.windowOpen();
return; return;
} }
this.layerOpen();
this.launchButtonFlag=true;
$(".launchButton").hide();
$("body").click(function () {
clearTimeout(this.titleTimer);
document.title = this.originTitle;
});
}
GOFLY.layerOpen=function (){
if (this.launchButtonFlag) return;
layer.open({ layer.open({
type: 2, type: 2,
title: GOFLY_BTN_TEXT, title: this.GOFLY_BTN_TEXT,
closeBtn: 1, //不显示关闭按钮 closeBtn: 1, //不显示关闭按钮
shade: [0], shade: 0,
area: ['520px', '530px'], area: ['520px', '530px'],
offset: 'rb', //右下角弹出 offset: 'rb', //右下角弹出
anim: 2, anim: 2,
content: [GOFLY_URL+'/chatIndex?kefu_id='+GOFLY_KEFU_ID+'&refer='+window.document.title, 'yes'], //iframe的urlno代表不显示滚动条 content: [this.GOFLY_URL+'/chatIndex?kefu_id='+this.GOFLY_KEFU_ID+'&refer='+window.document.title, 'yes'], //iframe的urlno代表不显示滚动条
end: function(){ end: function(){
launchButtonFlag=false; this.launchButtonFlag=false;
$(".launchButton").show(); $(".launchButton").show();
} }
}); });
launchButtonFlag=true;
$(".launchButton").hide();
$("body").click(function () {
clearTimeout(titleTimer);
document.title = originTitle;
});
} }
function dynamicLoadCss(url) { GOFLY.windowOpen=function (){
var head = document.getElementsByTagName('head')[0]; window.open(this.GOFLY_URL+'/chatIndex?kefu_id='+this.GOFLY_KEFU_ID+'&refer='+window.document.title);
var link = document.createElement('link');
link.type='text/css';
link.rel = 'stylesheet';
link.href = url;
head.appendChild(link);
} }
function dynamicLoadJs(url, callback) { GOFLY.flashTitle=function () {
var head = document.getElementsByTagName('head')[0]; this.titleNum++;
var script = document.createElement('script'); if (this.titleNum >=3) {
script.type = 'text/javascript'; this.titleNum = 1;
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); if (this.titleNum == 1) {
} document.title = '【】' + this.originTitle;
function flashTitle() {
titleNum++;
if (titleNum >=3) {
titleNum = 1;
} }
if (titleNum == 1) { if (this.titleNum == 2) {
document.title = '【】' + originTitle; document.title = '【你有一条消息】' + this.originTitle;
} }
if (titleNum == 2) { this.titleTimer = setTimeout("this.flashTitle()", 500);
document.title = '【你有一条消息】' + originTitle;
}
titleTimer = setTimeout("flashTitle()", 500);
} }
window.addEventListener('message',function(e){
var msg=e.data;
if(msg.type=="message"){
flashTitle();//标题闪烁
}
});
window.onfocus = function () {
clearTimeout(titleTimer);
document.title = originTitle;
};

Loading…
Cancel
Save