util.js
4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* 采集系统的工具库
*/
//flash监测
exports.flashChecker = function() {
var hasFlash = 0; //是否安装了flash
var flashVersion = 0; //flash版本
var isIE = /*@cc_on!@*/ 0; //是否IE浏览器
var swf = null;
if (isIE) {
swf = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
if (swf) {
hasFlash = 1;
flashVersion = swf.GetVariable("$version");
}
} else {
if (navigator.plugins && navigator.plugins.length > 0) {
swf = navigator.plugins["Shockwave Flash"];
if (swf) {
hasFlash = 1;
flashVersion = swf.description.replace("Shockwave Flash", '');
}
}
}
return {
f: hasFlash,
v: flashVersion
};
};
//hash算法
exports.Hash = function(str) {
var hash = 1,
charCode = 0,
idx;
if (str) {
hash = 0;
for (idx = str.length - 1; idx >= 0; idx--) {
charCode = str.charCodeAt(idx);
hash = (hash << 6 & 268435455) + charCode + (charCode << 14);
charCode = hash & 266338304;
if (charCode !== 0) {
hash = hash ^ charCode >> 21;
}
}
}
return hash;
};
//生成随机数
exports.Random = function() {
return Math.round(Math.random() * 2147483647);
};
//hash客户端信息
exports.hashClientInfo = function() {
var navigator = window.navigator;
var history_length = window.history.length;
var arr = [
navigator.appName,
navigator.version,
navigator.language,
navigator.platform,
navigator.userAgent,
navigator.javaEnabled(),
window.screen,
window.screen.colorDepth, (window.document.cookie ? window.document.cookie : ""), (window.document.referrer ? window.document.referrer : "")
];
navigator = arr.join('');
for (var len = navigator.length; history_length > 0;) {
navigator += history_length-- ^ len++;
}
return exports.Hash(navigator);
};
//浅层合并对象
exports.merge = function(obj1, obj2) {
var ret = {};
for (var attr in obj1) {
ret[attr] = obj1[attr];
}
for (var attr2 in obj2) {
ret[attr2] = obj2[attr2];
}
return ret;
};
//生成URL键值对
exports.genParam = function(obj) {
var arr = [];
for (var key in obj) {
arr.push(key + '=' + obj[key]);
}
return arr.join('&');
};
//除去字符串前后的空格
exports.trim = function(text) {
if (String.prototype.trim) {
return text === null ? "" : String.prototype.trim.call(text);
} else {
var trimLeft = /^\s+/;
var trimRight = /\s+$/;
var ret = '';
if (text) {
ret = text.toString().replace(trimLeft, "");
ret = ret.replace(trimRight, "");
return ret;
}
}
};
//获取地理位置信息
exports.getGeo = function(callback) {
if (window.navigator.geolocation) {
var options = {
enableHighAccuracy: true,
};
window.navigator.geolocation.getCurrentPosition(handleSuccess, handleError, options);
} else {
callback(false);
}
function handleSuccess(position) {
// 获取到当前位置经纬度 本例中是chrome浏览器取到的是google地图中的经纬度
var lng = position.coords.longitude;
var lat = position.coords.latitude;
callback(lat, lng);
}
function handleError(error) {
callback(false);
}
};
exports.queryString = function() {
var vars = {},
hash,
i;
var hashes = window.location.search.slice(1).split('&');
for (i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars[hash[0]] = hash[1];
}
return vars;
};
exports.getChannel = function() {
function cookie(name) {
var cookies = document.cookie,
cookieVal;
if (cookies) {
cookies = cookies.split(';');
cookies.forEach(function(c) {
if (c.indexOf(name) > -1) {
cookieVal = decodeURIComponent($.trim(c.replace(name + '=', '')));
return;
}
});
}
return cookieVal;
}
return {
boys: 1,
girls: 2,
kids: 3,
lifestyle: 4
}[cookie('_Channel')];
}
var _getAppVersionByUa = function() {
var vars = navigator.userAgent.split(';') || [];
for (var i = 0; i < vars.length; i++) {
if (vars[i].indexOf('app_version') > -1) {
return vars[i].split('=')[1];
}
}
}
exports.getAppVersion = function() {
var appVer = window.qs.app_version || window.qs.appVersion;
if (!appVer) {
appVer = _getAppVersionByUa();
}
return appVer;
}