根据业务需求提出的统计代码。
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title>统计测试</title> </head> </head> <body> <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"> </script> <script src="https://cdn.wilddog.com/js/client/current/wilddog.js"></script> <script type="text/javascript" src="http://www.coding123.net/getip.ashx?js=1"></script> <script> // <summary> // 时间类 // </summary> Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(), //minute "s+" : this.getSeconds(), //second "q+" : Math.floor((this.getMonth()+3)/3), //quarter "S" : this.getMilliseconds() //millisecond } if(/(y+)/i.test(format)) { format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); } for(var k in o) { if(new RegExp("("+ k +")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); } } return format; } // <summary> // Guid // </summary> function newGuid() { var guid = ""; for (var i = 1; i <= 32; i++){ var n = Math.floor(Math.random()*16.0).toString(16); guid += n; if((i==8)||(i==12)||(i==16)||(i==20)) guid += "-"; } return guid.replace(/-/g,""); } // <summary> // 全局变量 // </summary> var data = new Wilddog("https://tongjiwx.wilddogio.com/"); //野狗对象 var Mydate = new Date(); //Date对象 var day = Mydate.format("yyyy年MM月dd日"); //日期 var time = Mydate.toLocaleTimeString(); //当前时间 var times = Mydate.getTime(); //时间戳 var ip = ip.replace("|",""); //ip地址 var PV = 0; //浏览次数 var UV = 0; //独立访客 var guid = newGuid(); //guid var lock = false; //单向锁(ip群锁) var lock2 = false; //单项锁(获取pv、uv锁) // <summary> // 插入新的ip群 // </summary> insert = function() { //alert(guid); data.child("IP").child(day).child(guid).set ({ "ip":ip, "time":time }) } // <summary> // 更新pv次数和uv次数 // </summary> update = function() { data.child("N").child(day).set ({ "PV":PV, "UV":UV }) } // <summary> // 获取pv次数和uv次数 // </summary> Get_PVUV = function() { data.child("N").child(day).on('value',function(snapshot) { if(lock2 == false) { lock2 = true; var json = snapshot.val(); console.log("我是Get_PVUV");console.log(json); //当天第一次读取的时候,会返回null if(json == null) { PV = 1; UV = 1; } else { PV = json.PV; UV = json.UV; } console.log(PV+ "|" +UV); Get_IPJSON(); } }) } // <summary> // 获取IPJSON // </summary> Get_IPJSON = function() { data.child("IP").child(day).on('value',function(snapshot) { if(lock == false) { lock = true; //加上单向锁,只能执行一遍 var jsonstr = JSON.stringify(snapshot.val()); console.log("我是Get_IPJSON");console.log(jsonstr); if(jsonstr != "null") //当天首次监听则会返回Null,那么直接按照Get_PVUV()的初始化值(pv=1|uv=1)来更新即可 { if(jsonstr.indexOf(ip) < 0) { UV = UV + 1; } PV = PV + 1; } update(); insert(); } }) } // <summary> // 函数调用区(Main) // </summary> Get_PVUV(); </script> </body> </html>