优化前端js

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

@ -79,5 +79,5 @@
var KEFU_ID='{{.KEFU_ID}}';
var REFER='{{.Refer}}';
</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>

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

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

@ -1,60 +1,82 @@
var launchButtonFlag=false;
var titleTimer,titleNum=0;
var originTitle = document.title;
if (typeof GOFLY_URL=="undefined"){
var GOFLY_URL="https://gofly.sopans.com";
}
if (typeof GOFLY_KEFU_ID=="undefined"){
var GOFLY_KEFU_ID="";
}
if (typeof GOFLY_BTN_TEXT=="undefined"){
var GOFLY_BTN_TEXT="Chat with me";
}
dynamicLoadCss(GOFLY_URL+"/static/css/gofly-front.css");
if (typeof $!="function"){
dynamicLoadJs("https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js",function () {
dynamicLoadJs("https://cdn.bootcdn.net/ajax/libs/layer/3.1.1/layer.min.js",function () {
clickBtn();
var GOFLY={
GOFLY_URL:"https://gofly.sopans.com",
GOFLY_KEFU_ID:"",
GOFLY_BTN_TEXT:"Chat with me",
};
GOFLY.launchButtonFlag=false;
GOFLY.titleTimer=0;
GOFLY.titleNum=0;
GOFLY.originTitle=document.title;
GOFLY.init=function(config){
if(typeof config=="undefined"){
return;
}
if (typeof config.GOFLY_URL!="undefined"){
this.GOFLY_URL=config.GOFLY_URL;
}
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{
dynamicLoadJs("https://cdn.bootcdn.net/ajax/libs/layer/3.1.1/layer.min.js",function () {
clickBtn();
});
window.onfocus = function () {
clearTimeout(this.titleTimer);
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(){
$('body').append('<div id="launchButton" class="launchButton animateUpDown"><div class="launchButtonText">'+GOFLY_BTN_TEXT+'</div></div>');
GOFLY.clickBtn=function (){
var _this=this;
$('body').append('<div id="launchButton" class="launchButton animateUpDown"><div class="launchButtonText">'+_this.GOFLY_BTN_TEXT+'</div></div>');
$("#launchButton").on("click",function() {
if (launchButtonFlag) return;
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;
_this.showKefu();
});
}
function isIE() {
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浏览器
@ -82,80 +104,54 @@ function isIE() {
return -1;//不是ie浏览器
}
}
function showKefu(){
if (launchButtonFlag) return;
GOFLY.showKefu=function (){
if (this.launchButtonFlag) return;
var width=$(window).width();
if(width<768 || isIE()>0){
window.open(GOFLY_URL+'/chatIndex?kefu_id='+GOFLY_KEFU_ID+'&refer='+window.document.title);
if(width<768 || this.isIE()>0){
this.windowOpen();
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({
type: 2,
title: GOFLY_BTN_TEXT,
title: this.GOFLY_BTN_TEXT,
closeBtn: 1, //不显示关闭按钮
shade: [0],
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代表不显示滚动条
content: [this.GOFLY_URL+'/chatIndex?kefu_id='+this.GOFLY_KEFU_ID+'&refer='+window.document.title, 'yes'], //iframe的urlno代表不显示滚动条
end: function(){
launchButtonFlag=false;
this.launchButtonFlag=false;
$(".launchButton").show();
}
});
launchButtonFlag=true;
$(".launchButton").hide();
$("body").click(function () {
clearTimeout(titleTimer);
document.title = originTitle;
});
}
function dynamicLoadCss(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.windowOpen=function (){
window.open(this.GOFLY_URL+'/chatIndex?kefu_id='+this.GOFLY_KEFU_ID+'&refer='+window.document.title);
}
function dynamicLoadJs(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;
}
};
GOFLY.flashTitle=function () {
this.titleNum++;
if (this.titleNum >=3) {
this.titleNum = 1;
}
head.appendChild(script);
}
function flashTitle() {
titleNum++;
if (titleNum >=3) {
titleNum = 1;
if (this.titleNum == 1) {
document.title = '【】' + this.originTitle;
}
if (titleNum == 1) {
document.title = '【】' + originTitle;
if (this.titleNum == 2) {
document.title = '【你有一条消息】' + this.originTitle;
}
if (titleNum == 2) {
document.title = '【你有一条消息】' + originTitle;
}
titleTimer = setTimeout("flashTitle()", 500);
this.titleTimer = setTimeout("this.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