/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ "use strict" var app_id="fc1d3ac8-1c3c-11e8-8899-10c37b907e7f"; var GPSOPEN = false; ;(function(global){ var CONFIG_URL = null; //定义请求路径 var GPS = { latitude : null, longitude : null }; var DBI = function(config_url = null){ if (GPSOPEN) { Geolocation(); //获取定位信息 } this.app_key = null; this.DATA = null; CONFIG_URL = config_url || "http://hub.cp.cn";//"http://api.xdata.gli.cn";// api.xdata.gli.cn, hub.h5angel.cn 测试站点, hub.cp.cn 商务印书馆 } DBI.prototype = { /** * [管理密钥配置方法] * @param {[string]} key [密钥] * @return {[Boole]} [description] */ setAppKey : function (key){ if (typeof key === 'string') { this.app_key = key; return true; } console.error("Parameter is a string type."); return false; }, /** * [创建字典] * @param {[Object]} data [数据] * @param {[Function]} cb_fun [回调函数] */ createDict : function(data,cb_fun = null) { if (typeof data === 'object') { var url = ''; if (data.dict_id) { url = '/dict/'+data.dict_id; } else { url = '/dict'; } data.app_key = this.app_key; data.app_id = app_id; return request(url,data,function(json){ if (json.status) { if (typeof cb_fun === 'function') { cb_fun(json); } } else { consle.error(json.error); } }); } console.error("Parameter 'data' is the object type.") return false; }, /** * [创建上报数据对象实体方法] * @param {[object]} data [事件数据,可为空。] * @return {[type]} [description] */ createEvent : function(data = {}) { if (typeof data === 'object') { var sys_terminal = myBrowser(); var temporaryData = { event_tim : data.event_tim ? data.event_tim : getNowFormatDate(), event_system : data.event_system ? data.event_system : getPhoneSys() || detectOS(), event_terminal : data.event_terminal ? data.event_terminal : sys_terminal.sys, event_terminal_verison : data.event_terminal_verison ? data.event_terminal_verison : sys_terminal.versions[0], event_source : data.event_source ? data.event_source : sys_terminal.source, event_sources : data.event_sources ? data.event_sources : window.location.href, positioning_gps_latitude : '', positioning_gps_longitude : '', }; return new EventData(Object.assign({},data,temporaryData)); } console.error("Parameter 'data' is the object type.") return false; }, /** * [创建上报数据对象实体方法] * @param {[Array]} data [对象实体定义数据,可为空。] * @return {[object]} [description] */ createData : function(data){ if (typeof data === 'object') { data.is_data = true; return new DBIObeject(data); } console.error("Parameter 'data' is the object type."); return false; }, /** * [申报数据] * @param {[object]} data [通过DBI::createEvent或DBI::createData创建的DBIEvent对象,或者直接传入数据由本接口自动创建] * @param {[function]} cb_fun [回调函数] * @return {[type]} [description] */ declare : function(data,cb_fun) { if (typeof data === 'object') { var url = '/declare'; if (data.data && data.event) { //判断是由createData方法和createEvent一起合并的数据 || 或者自己传入 if(data.event._event) { delayRequest(function(){ data.event._event.positioning_gps_latitude = GPS.latitude; data.event._event.positioning_gps_longitude = GPS.longitude; var temporaryData = { data : data.data.data ? data.data.data : data.data, event : data.event._event ? data.event._event : data.event, action : "create", app_id : app_id }; try { delete temporaryData.data.is_data; } catch (e) {} return request(url,temporaryData,cb_fun); }) } else { var temporaryData = { data : data.data.data ? data.data.data : data.data, event : data.event._event ? data.event._event : data.event, action : "create", app_id : app_id }; try { delete temporaryData.data.is_data; } catch (e) {} return request(url,temporaryData,cb_fun); } } else if (data.data) { //判断是由createData方法创建的数据 if (data.data.is_data) { delete data.data.is_data; var temporaryData = { data : data.data, action : "create", app_id : app_id } return request(url,temporaryData,cb_fun); } else { var temporaryData = { data : data.data, action : "create", app_id : app_id } return request(url,temporaryData,cb_fun); } } else if (data._event) { //判断是由createEvent方法创建的数据 delayRequest(function(){ data._event.positioning_gps_latitude = GPS.latitude; data._event.positioning_gps_longitude = GPS.longitude; var temporaryData = { event : data._event, action : "create", app_id : app_id } return request(url,temporaryData,cb_fun); }) } else { if (data.event_class) { //自定义事件注册申报 return request(url,{ event : data, action : "create", app_id : app_id },cb_fun); } else { if (data) { //自定义对象申报 return request(url,{ data : data, action : "create", app_id : app_id },cb_fun); } console.error("传入参数非法:请严格按照js-sdk接口文档传入参数。"); } } } else { console.error("parameter 'data' is an object type or an array type.") } } } //DBI数据实体对象 var DBIObeject = function(data = null){ this.data = data || {}; } DBIObeject.prototype = { /** * [设定对象属性的值] * @param {[string]} name [属性名称] * @param {[Object]} value [属性值] * @return {[type]} [description] */ setAttr : function(name,value) { if (typeof name === 'string') { this.data[name] = value; return this.data; } console.error("parameter 'name' is the string type.") return false; } } //DBI event创建数据 var EventData = function (data) { this._event = {} if (typeof data === 'object') { this._event = data; } } EventData.prototype = { addData : function(Data) { if (typeof Data === 'object') { if (typeof this._event.event_data == 'undefined') { this._event.event_data = {}; } for (var i in Data) { if (this._event.event_data[i]) { this._event.event_data[i].push(Data[i]); } else { this._event.event_data[i] = Data[i]; } } return this._event; } console.error("parameter 'Data' is the object type.") return false; }, /** * [设定对象属性的值] * @param {[string]} name [属性名称] * @param {[Object]} value [属性值] * @return {[type]} [description] */ setAttr : function(name,value) { if (typeof name === 'string') { this._event[name] = value; return this._event; } console.error("parameter 'name' is the string type.") return false; } } //延迟请求 var delayRequest = function(callback){ if (GPS.latitude == null && GPSOPEN) { setTimeout(function(){ delayRequest(callback); },500) }else { callback() } } //请求数据 var request = function(url,data,cb_fun){ try { $.ajax({ url : CONFIG_URL+url, type : "POST", dataType : "json", data : data, success : function(result) { return cb_fun(result); }, error:function(msg){ console.error(msg); } }) }catch(e) { loadScript('http://libs.baidu.com/jquery/1.9.1/jquery.min.js',function(){ $.ajax({ url : CONFIG_URL+url, type : "POST", dataType : "json", data : data, success : function(result) { return cb_fun(result); }, error:function(msg){ console.error(msg); } }) }) } } var loadScript = function(url, callback) {   var script = document.createElement("script");   script.type = "text/javascript";   if(typeof(callback) != "undefined"){     if (script.readyState) {       script.onreadystatechange = function () {         if (script.readyState == "loaded" || script.readyState == "complete") {           script.onreadystatechange = null;           callback();         }       };     } else {       script.onload = function () {         callback();       };     }   }   script.src = url;   document.body.appendChild(script); } //获取电脑操作系统 var detectOS = function() { navigator.userAgent; return navigator.platform == "Win32" ? "Windows" : navigator.platform } //获取手机操作系统 var getPhoneSys = function () { var u = navigator.userAgent; if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {//安卓手机 return "Android"; } else if (u.indexOf('iPhone') > -1) {//苹果手机 return "iPhone"; } else if (u.indexOf('Windows Phone') > -1) {//winphone手机 return "winphone手机"; } } //获取当前时间,格式YYYY-MM-DD var getNowFormatDate = function () { var date = new Date(); var seperator1 = "-"; var seperator2 = ":"; var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds(); return currentdate; } //判断是pc还是phoen var IsPC = function () { var userAgentInfo = navigator.userAgent; var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"]; var flag = true; for (var v = 0; v < Agents.length; v++) { if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; } } return flag; } //获取浏览器 var myBrowser = function (){ var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 if (IsPC()){ if (userAgent.indexOf("OPR") > -1||userAgent.indexOf("Opera") > -1) { //判断是否Opera浏览器 return { sys : "Opera", versions : userAgent.match(/Opera\/[\d.]+/gi), source : 'WEB' } } else if (userAgent.indexOf("Firefox") > -1) { //判断是否Firefox浏览器 return { sys : "Firefox", versions : userAgent.match(/Firefox\/[\d.]+/gi), source : 'WEB' } } else if (userAgent.indexOf("Chrome") > -1){ return { sys : "Chrome", versions : userAgent.match(/chrome\/[\d.]+/gi), source : 'WEB' } } else if (userAgent.indexOf("Safari") > -1) { //判断是否Safari浏览器 return { sys : "Safari", versions : userAgent.match(/Safari\/[\d.]+/gi), source : 'WEB' } } else if (userAgent.indexOf("compatible") > -1) { return { sys : "IE", versions : userAgent.match(/compatible [\d.]+/gi), source : 'WEB' } } else { return { sys : "", versions : "", source : 'WEB' } } } else { var ua = navigator.userAgent.toLowerCase();//获取判断用的对象 if (ua.match(/MicroMessenger/i) == "micromessenger") { //在微信中打开 return { sys : "micromessenger", versions : userAgent.match(/micromessenger\/[\d.]+/gi), source : "WeiXin" } } else if (ua.match(/WeiBo/i) == "weibo") { //在新浪微博客户端打开 return { sys : "weibo", versions : userAgent.match(/weibo\/[\d.]+/gi), source : "weibo" } } else if (ua.match(/QQ/i) == "qq") { return { sys : "qq", versions : userAgent.match(/QQ\/[\d.]+/gi), source : "qq" } } else { return { sys : "", versions : "", source : "PHONE" } } } } var Geolocation = function () { if(navigator.geolocation){ navigator.geolocation.getCurrentPosition( function(position){ GPS = { latitude : position.coords.latitude, //获取维度 longitude : position.coords.longitude, //获取经度 } return GPS; } , function(error) { switch(error.code) { case error.PERMISSION_DENIED: console.log("您拒绝对获取地理位置的请求"); break; case error.POSITION_UNAVAILABLE: console.log("位置信息是不可用的"); break; case error.TIMEOUT: console.log("请求您的地理位置超时"); break; case error.UNKNOWN_ERROR: console.log("未知错误"); break; } GPS = { latitude : 0, //获取维度 longitude : 0, //获取经度 } return GPS; } ); }else{ console.error("您的浏览器不支持使用HTML5来获取地理位置服务"); } } //兼容CommonJs规范 if (typeof module !== 'undefined' && module.exports) { module.exports = DBI; } //兼容AMD/CMD规范 if (typeof define === 'function') { define(function() { return DBI; }); } global.DBI = DBI })(this); var DBI = new DBI();