/**
* 前台广告插件(不依赖jquery)
* by Bob Wang
*/
(function(){
/**
* 插件的封装
*/
this.error=function(str){
console.log(str);return;
}
this.setCookie=function(name,value,day){
//设置cookie,day为过期时间按天计算
var date=new Date();
date.setTime(date.getTime()+day*86400000);
document.cookie=name+"="+value+";expires="+date.toGMTString();
};
this.getCookie=function(name){
//取得cookie
if(document.cookie.indexOf(name)<0){
return false;
}
return document.cookie.split(name)[1].split(";")[0].split("=")[1];
};
this.deleteCookie=function(name){
//删除cookie
var date=new Date();
date.setTime(date.getTime());
document.cookie=name+"="+getCookie("name")+";expires="+date.toGMTString();
};
this.closeAdv=function(className){
//关闭广告
var ele=document.getElementsByClassName(className);
for(var i=ele.length-1;i>=0;i--){
ele[i].parentNode.removeChild(ele[i]);
}
if(document.body.style.overflow=='hidden'){
document.body.style.overflow='auto';
}
};
this.xmlHttp=null;//ajax句柄
this.ajax=function(url,method){
//发起ajax请求
try
{
// 火狐, 欧朋, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// 新IE
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
//老IE
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
if(xmlHttp==null){
error("Error:找不到xmlHttp句柄");
}
//数据返回后的操作
xmlHttp.onreadystatechange=method;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
};
/**
* 业务逻辑开始
*/
//取得url
var src=document.scripts[document.scripts.length - 1].src;
if(src.indexOf("?app=")<0){
error("Error:js文件没有传入应用名称,例如adv.js?app=en");
}
src=src.substr(src.indexOf("?app=")+5,src.length-src.indexOf("?app=")-5);
var url="/"+src+"/Adv/getAdv/sid/"+Math.random();
//中部,左侧,右侧广告的动作
this.action=[
function(data){
//中部
document.body.style.overflow="hidden";
var page="
";
page+="
";
page+="";
page+="
x![]("+data.adv_img+")
";
page+="
";
document.getElementById("advs").innerHTML+=page;
if(data.auto_close_time!=""){
setTimeout(
function (){
closeAdv('yongsymiddleadv');
},
1000*data.auto_close_time
)
}
if(data.if_cookie==1){
//设置cookie
setCookie('yongsyadv0',1,data.cookie_time);
}
},
function(data){
//左侧
var page="";
page+="
x![]("+data.adv_img+")
";
page+="
";
document.getElementById("advs").innerHTML+=page;
if(data.auto_close_time!=""){
setTimeout(
function (){
closeAdv('yongsyleftadv');
},
1000*data.auto_close_time
)
}
if(data.if_cookie==1){
//设置cookie
setCookie('yongsyadv1',1,data.cookie_time);
}
},
function(data){
//右侧
var page="";
page+="
x![]("+data.adv_img+")
";
page+="
";
document.getElementById("advs").innerHTML+=page;
if(data.auto_close_time!=""){
setTimeout(
function (){
closeAdv('yongsyrightadv');
},
1000*data.auto_close_time
)
}
if(data.if_cookie==1){
//设置cookie
setCookie('yongsyadv2',1,data.cookie_time);
}
}
];
ajax(
url,
function(){
if (xmlHttp.readyState==4||xmlHttp.readyState=="complete")
{
try{
var data=JSON.parse(xmlHttp.responseText);
}catch(e){
error("Error:网络错误,接口返回了错误的值,它不是一个对象");
}
//循环处理不同广告
for(var i in data){
if(data[i].status==1){
//广告开启
if(data[i].activity==1){
//做活动
if(data[i].in_time==1){
//在活动时间范围内
if(data[i].if_cookie==1){
//只在第一次打开显示
if(!getCookie('yongsyadv'+i)){
//执行
action[i](data[i]);
}
}else{
//执行
action[i](data[i]);
}
}
}else{
if(data[i].if_cookie==1){
//只在第一次打开显示
if(!getCookie('yongsyadv'+i)){
//执行
action[i](data[i]);
}
}else{
//执行
action[i](data[i]);
}
}
}
}
}
}
);
})();
/**
*接口示例(id,1,2,3分别为中部,左侧,右侧广告)
* [
{
"id": "1",//广告id
"adv_img": "/Public/Uploads/uploadfile/images/20170614/20170614151724_5940e3046c984.png",//广告图片
"adv_url": "http://www.yongsy.net",//广告链接
"status": "1",//广告是否开启
"activity": "1",//广告是否定时显示
"activity_time": [
1484582400,
1528905600
],
"auto_close_time": "3",//广告自动关闭时间,不填就不自动关闭
"if_cookie": "1",//是否启用cookie,即一定时间内广告只能打开一次
"cookie_time": "1",//cookie过期时间,以小时为单位
"name": "中部广告",//广告名
"in_time": 1//是否在广告定时显示时间段内
},
{
"id": "2",
"adv_img": "/Public/Uploads/uploadfile/images/20170614/20170614152554_5940e502bcdcb.png",
"adv_url": "http://www.yongsy.net",
"status": "1",
"activity": "1",
"activity_time": [
1483977600,
1516118400
],
"auto_close_time": "3",
"if_cookie": "1",
"cookie_time": "1",
"name": "左侧广告",
"in_time": 1
},
{
"id": "3",
"adv_img": "/Public/Uploads/uploadfile/images/20170614/20170614152609_5940e51161d82.png",
"adv_url": "http://www.yongsy.net",
"status": "1",
"activity": "1",
"activity_time": [
1484582400,
1516118400
],
"auto_close_time": "3",
"if_cookie": "1",
"cookie_time": "1",
"name": "右侧广告",
"in_time": 1
}
]
*/