(function (w,d) { "use strict"; /** * app 基本信息 * @type {{id:string, client:string, uuid:string, api:string, sign:string, time:string, user:int}} */ var app = { id:'45', client:'1', uuid:'c9385b427bec7e965dbe227b70dc4e55', api:'https://spro.yingxiong.com/datastat/jsapi/record/file/record.js', user:'0', sign:'cbc3bb05f9752d317307506fec350723', time:'1710831330', }; //结构类 var ref = {}; var hasOwnProperty = ref.hasOwnProperty; var isArray = Array.isArray; // ------ 工具类 结束----------------------------- /** * 工具包 */ var Util = { versions:function () { var u = navigator.userAgent, app = navigator.appVersion; var ret = { trident:u.indexOf('Trident') > -1, //IE内核 presto:u.indexOf('Presto') > -1, //opera内核 webKit:u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核 gecko:u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核 mobile:!!u.match(/AppleWebKit.*Mobile.*/) || !!u.match(/Android/) || !!u.match(/iPhone/), //是否为移动终端 ios:!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端 android:u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器 iPhone:u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器 iPad:u.indexOf('iPad') > -1, //是否iPad webApp:u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部 win:u.toLowerCase().indexOf('windows') > -1 , wx:u.toLowerCase().indexOf('micromessenger') > -1 , }; // 1 安卓 // 2 IOS // 3 window ret.platform = 0; if(ret.android){ ret.platform = 1; }else if(ret.ios){ ret.platform = 2; }else if(ret.win){ ret.platform = 3; } return ret; }() }; /** * * @param text * @returns {*} */ Util.parse = function (text) { var data = text; if( Util.isString(text) ){ try{ data = JSON.parse(text); }catch(e){ try { data = eval("("+text+")"); }catch (e){ data = null; } } } return data; }; /** * 判断对象是否是一个函数 * @param obj * @returns {boolean} */ Util.isFunction = function ( obj ) { return Util.type(obj) === "function" ; }; /** * 是否是一个字符串 * @param val * @returns {boolean} */ Util.isString = function (val) { return Util.type(val) === 'string'; }; /** * 获取数据类型 * @param obj * @returns {string} */ Util.type = function (obj) { var t = typeof obj; return (t+"").toLowerCase() }; /** * 判断是否是 undefined * @param obj * @returns {boolean} */ Util.isUndefined = function (obj) { return Util.type(obj) == 'undefined'; }; /** * 判断对象 * @param obj * @returns {boolean} */ Util.isObject = function (obj) { return obj !== null && typeof obj === 'object'; }; /** * 判断对象 * @param obj * @returns {*|boolean} */ Util.isPlainObject = function (obj) { return Util.isObject(obj) && Object.getPrototypeOf(obj) == Object.prototype; }; /** * 对象遍历 * @param obj * @param iterator * @returns {*} */ Util.each = function (obj, iterator) { var i, key; if (isArray(obj)) { for (i = 0; i < obj.length; i++) { iterator.call(obj[i], obj[i], i); } } else if (Util.isObject(obj)) { for (key in obj) { if (hasOwnProperty.call(obj, key)) { iterator.call(obj[key], obj[key], key); } } } return obj; }; /** * 对象合并 * @param target * @param source * @param deep * @private */ Util._merge = function (target, source, deep) { for (var key in source) { if (deep && (Util.isPlainObject(source[key]) || isArray(source[key]))) { if (Util.isPlainObject(source[key]) && !Util.isPlainObject(target[key])) { target[key] = {}; } if (isArray(source[key]) && !isArray(target[key])) { target[key] = []; } Util._merge(target[key], source[key], deep); } else if (source[key] !== undefined) { target[key] = source[key]; } } }; /** * 合并数据 * @param target * @param source * @param deep * @returns {*} */ Util.extend = function (target, source, deep) { Util._merge(target, source, deep); return target; }; // ------ 工具类 结束----------------------------- // ------ QueryString 类 ----------------------------- /** * QueryString */ var QueryString = {}; QueryString.params = function (obj) { var params = [], escape = encodeURIComponent; params.add = function (key, value) { if (Util.isFunction(value)) { value = value(); } if (value === null) { value = ''; } this.push(escape(key) + '=' + escape(value)); }; QueryString.serialize(params, obj); return params.join('&').replace(/%20/g, '+'); }; QueryString.serialize = function(params, obj, scope) { var array = isArray(obj), plain = Util.isPlainObject(obj), hash; Util.each(obj, function (value, key) { hash = Util.isObject(value) || isArray(value); if (scope) { key = scope + '[' + (plain || hash ? key:'') + ']'; } if (!scope && array) { params.add(value.name, value.value); } else if (hash) { QueryString.serialize(params, value, key); } else { params.add(key, value); } }); }; // ------ QueryString 类 结束 ----------------------------- // ------ 请求器 类 ----------------------------- var Req = function (url,data,call,opt) { this.url = url; this.data = data; this.opt = Util.extend({ timeout:0 },opt,true); this.query = QueryString.params(this.data); this.call = Util.isFunction(call)?call:ef; this.script = null; this.timer = 0; //创建script this.createScript(); //注册事件 this.reg(); //执行 this.run(); }; Req.fn = Req.prototype; /** * 获取到最终资源地址 * @returns {string} */ Req.fn.getSrc = function () { var u = this.url + ""; var d = this.query; d+='&t='+Math.random(); if (d){ var us = u.split('?'); if(us[1]){ us[1] += '&' + d; }else{ us[1] = d; } return us.join('?'); } return u; }; /** * create */ Req.fn.createScript = function () { if (this.script) return this; this.script = d.createElement('script'); this.script.src = this.getSrc(); this.script.async = "async"; return this; }; /** * 注册事件监听 */ Req.fn.reg = function () { this.createScript(); var self = this; this.script.onload = function () { self.callBack('load'); }; this.script.onerror = function () { self.callBack('error'); }; }; /** * 执行请求 */ Req.fn.run = function () { var self = this; if (this.opt.timeout>0){ clearTimeout(this.timer); this.timer = setTimeout(function () { self.callBack('timeout'); },this.opt.timeout); } this.body().appendChild(this.script); }; /** * 操作回调 * @param status */ Req.fn.callBack = function (status) { clearTimeout(this.timer); this.remove(); //回调方法 this.call.call(this,status); }; /** * 删除节点 */ Req.fn.remove = function () { this.body().removeChild(this.script); this.script = null; }; /** * * @returns {Document} */ Req.fn.body = function () { return d.body?d.body:d.getElementsByTagName('script')[0].parentNode; }; // ------ 请求器 类 结束 ----------------------------- /** * 配置类 * @constructor */ var Config = function () { this.autoReport = 1; this.senseHash = 0; this.senseQuery = 0; this.ignoreParams = []; }; /** * 日志对象类 * @constructor */ var Log = function (data) { // * -uuid 用户唯一标识 // * -user_id 登录用户id // * -platform 应用所属端ID,在应用后台查看 // * -type 1:事件 2:页面 // * -name 如果类型是页面,则为页面名称,如果类型是事件则为事件id // * -args 请求完整url,包括参数 // * -behavior 行为:比如点赞,发帖,详情请看wiki文档 this.uuid = data.uuid?data.uuid:app.uuid; this.user_id = data.user_id?data.user_id:app.user; this.type = data.type?data.type:0; this.client_type = data.client_type?data.client_type:app.client; this.name = data.name?data.name:''; this.args = data.args?data.args:''; this.behavior = data.behavior?data.behavior:0; this.app_id = data.app_id?data.app_id:app.id; this.sign = app.sign; this.time = app.time; }; /** * 空方法 */ var ef = function () { }; //location var Location = function () { //console.log(w.location); /* hash:"" host:"" hostname:"" href:"file:///home/wolferhua/Desktop/41.html" origin:"file://" pathname:"/home/wolferhua/Desktop/41.html" port:"" protocol:"file:" search:"" */ this.href = w.location.href; this.host = w.location.host; this.hostname = w.location.hostname; this.hash = w.location.hash; this.origin = w.location.origin; this.pathname = w.location.pathname; this.name = w.location.pathname; this.port = w.location.port; this.protocol = w.location.protocol; this.search = w.location.search; this.toString = function () { var str = this.protocol+'//'+this.host+this.pathname; //query if (L.config.senseQuery){ var s = this.search; if(s){ //过滤参数 for (var i in L.config.ignoreParams){ var k = L.config.ignoreParams[i] + ''; if (!k) continue; try{ var reg = new RegExp('[&]?'+k+'=[^=&]*','i'); s = s.replace(reg,'') }catch (e) { console.error(e); } } } str += s; } //hash if (L.config.senseHash){ str += this.hash; } return str; }; }; /** * 操作对象 * @type {{push: ef, ready: ef, event: ef, config: {}}} */ var L = w.HLog?w.HLog:{push:ef,ready:ef,event:ef,config:{}}; //处理配置 var c = new Config();//默认配置 L.config = Util.extend(c,L.config?L.config:{},true);//对象处理 /** * 页面上报 * @param name * @param behavior */ L.push = function (name,behavior) { var url = new Location(); var log = new Log({ name:name?name:url.name, uuid:app.uuid, user_id:app.user, type:2, //页面 behavior:behavior?behavior:0, args: url.toString(), }); new Req(app.api,log,function () { //console.log('req'); },{timeout:0}); return 0; }; /** * 事件上报 * @param name string 事件名称 * @param args string 事件参数 * @param behavior int 事件行为 * @param opt {{x:int,y:int}} 事件点击位置 * @returns {number} */ L.event = function (name,args,behavior,opt) { var log = new Log({ name:name, uuid:app.uuid, user_id:app.user, type:1,//事件 behavior:behavior?behavior:0, args:args?args:'', opt:opt, }); new Req(app.api,log,function () { //console.log('req'); },{timeout:0}); return 0; }; /** * app * @returns {{id:string, client:string, uuid:string, api:string, user:int}} */ L.getApp = function () { return app; }; /** * appid * @returns {string} */ L.getAppId = function () { return app.id; }; /** * uuid * @returns {string} */ L.getUuid = function(){ return app.uuid; }; /** * 设置用户ID * @param user * @returns {{push:Function, event:Function, config:Config}} */ L.setUser = function (user) { app.user = user; return L; }; //回调ready if(Util.isFunction(L.ready)){ L.ready(); } })(window,document);