-
Notifications
You must be signed in to change notification settings - Fork 22
/
util.js
481 lines (387 loc) · 13.8 KB
/
util.js
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
* 通用工具集
* @author muqin_deng
* @time 2015/03/16
*/
(function(window, undefined) {
var util = {
data: {
ONE_DATE: 24 * 60 * 60 * 1000
},
userAgent: navigator.userAgent,
loading: function(show) {
var $loading = $('.loading');
if (show === false) {
$loading.hide();
return;
}
$loading.show();
},
//时间补0
timeAddZero: function(time) {
if (time < 10) {
return '0' + time;
} else {
return '' + time;
}
},
//显示消息提示
showTips: function(tips, time) {
var me = this,
$userinfoPs = $("#tips-msg"),
closeTime = time || 1000;
if (tips == 'HIDE') {
$userinfoPs.fadeOut();
return;
}
$userinfoPs.html(tips).fadeIn(function() {
if (closeTime == 'NOHIDE') {
return;
}
me.delay(closeTime).done(function() {
$userinfoPs.fadeOut();
});
});
},
//延时函数
delay: function(t) {
var deferred = $.Deferred();
setTimeout(function() {
deferred.resolve();
}, t);
return deferred.promise();
},
//转义特殊字符<,>,",'
escapeHtml: function(html) {
var div = document.createElement('div');
if (div.innerText) {
div.innerText = html;
} else {
div.textContent = html;
}
return div.innerHTML;
},
//根据连接符取驼峰式单词
getCamelWord: function(str, connector) {
var strArr, i, len;
connector = connector || '-';
strArr = str.split(connector);
str = strArr[0];
for (i = 1, len = strArr.length; i < len; i++) {
str += strArr[i].slice(0, 1).toLocaleUpperCase() + strArr[i].slice(1);
}
return str;
},
//获取根路径(可含项目路径)
getRootPath: function(needProjectPath) {
var href = window.location.href, //完整路径
pathName = window.location.pathname, //主机后面的路径
pos = href.indexOf(pathName),
localhostPath = href.substring(0, pos), //主机路径
projectName = /\/[^\/]+\//.exec(pathName)[0];
if (needProjectPath) {
return (localhostPath + projectName);
}
return localhostPath;
},
//获取字节长度
getRealLength: function(str) {
return str.replace(/[^\x00-\xff]/g, "**").length;
},
//判断移动端系统
checkMobileSystem: function(type) {
var regObj = {
'ios': /\(i[^;]+;( U;)? CPU.+Mac OS X/,
'android': /Android/i
};
if (!regObj[type]) {
return;
}
return regObj[type].test(this.userAgent);
},
//获取url键值对
getUrlKeyValObj: function() {
var url = window.location.href,
arr, i, len,
paramsObj = {};
if (this.urlKeyValObj) {
return this.urlKeyValObj;
}
arr = url.substring(url.indexOf('?') + 1).split('&');
for (i = 0, len = arr.length; i < len; i++) {
var reg = /(.*)\=(.*)/g,
match = reg.exec(arr[i]);
if (match && match[1]) {
paramsObj[decodeURIComponent(match[1])] = decodeURIComponent(match[2]);
}
}
this.urlKeyValObj = paramsObj;
return paramsObj;
},
//获取url的对应参数的值
getUrlValue: function(param) {
if (!param) {
return '';
}
var paramsObj = this.getUrlKeyValObj();
if (paramsObj.hasOwnProperty(param)) {
return paramsObj[param];
} else {
return '';
}
},
/*
* 函数节流
* @param {function} method 要进行节流的函数
* @param {number} delay 延时时间(ms)
* @param {number} duration 经过duration时间(ms)必须执行函数method
*/
throttle: function(method, delay, duration) {
var timer = null,
begin = null;
return function() {
var context = this,
args = arguments,
current = new Date();
if (!begin) {
begin = current;
}
if (timer) {
window.clearTimeout(timer);
}
if (duration && current - begin >= duration) {
method.apply(context, args);
begin = null;
} else {
timer = window.setTimeout(function() {
method.apply(context, args);
begin = null;
}, delay);
}
};
},
/*
* 页面模板引擎解析
* @param {string} html 要进行解析的html字符串(在<##>里面的转换成js来执行)
* @param {object} data html中解析传入的数据对象
* @param {object} scope 作用域
* @return {string} 返回解析后的html字符串
*/
templateEngine = function(html, data, scope) {
var re = /<#(.*?)#>/g,
reExp = /(^(\s|\t|\n|\r)*((var|if|for|else|switch|case|break)\b|{|}))(.*)?/g,
code = 'var r=[];\n',
cursor = 0;
html = html.replace(/\n|\r|\t/g, '');
var add = function(line, js) {
js ? (code += line.match(reExp) ? line + '\n' : 'r.push(' + line + ');\n') :
(code += line !== '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");\n' : '');
return add;
};
while (match = re.exec(html)) {
var noJsStr = html.slice(cursor, match.index);
add(/[^\s]/g.test(noJsStr) ? noJsStr : '')(match[1], true);
cursor = match.index + match[0].length;
}
add(html.substr(cursor, html.length - cursor));
code += 'return r.join("");';
var variable = [],
value = [];
for (var key in data) {
variable.push(key);
value.push(data[key]);
}
return new Function(variable, code.replace(/[\r\t\n]/g, '')).apply(scope ? scope : data, value);
}
//元素滚动到可见区域
scrollIntoView: function(elem, always) {
if (always === true) {
elem.scrollIntoView();
return;
}
elem.scrollIntoViewIfNeeded();
},
//获取用户当前经纬度,使用HTML5 Geolocation API
getLocation: function(successFn, errorFn) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
successFn(position.coords.longitude, position.coords.latitude);
}, errorFn);
} else {
this.delay(100, errorFn);
}
},
//获取当前城市,使用百度地图api接口 http://api.map.baidu.com/api?v=2.0&ak=kx7GASe6dXLGpwUsvqKxQdMn
getCurrentCity: function() {
var deferred = $.Deferred();
//获取经纬度
this.getLocation(function(lng, lat) {
var point = new BMap.Point(lng, lat),
geoc = new BMap.Geocoder();
geoc.getLocation(point, function(rs) {
var addComp = rs.addressComponents;
if (!addComp.city) {
addComp.city = '海外';
}
deferred.resolve(addComp.city);
});
}, function(error) {
deferred.reject(error);
});
return deferred.promise();
}
};
if (typeof define == 'function') {
define(['Zepto'], function($) {
return util;
});
}
window.Util = window.Util || util;
/*** 时间格式化相关 ***/
var format = function(formatStr) {
var year, month, day, hour, minute, second,
reg, rule, afterFormat;
if (!formatStr) {
return this.getTime();
}
year = this.getFullYear();
month = this.getMonth() + 1;
day = this.getDate();
hour = this.getHours();
minute = this.getMinutes();
second = this.getSeconds();
rule = {
'yy': year - 2000,
'yyyy': year,
'M': month,
'MM': util.timeAddZero(month),
'd': day,
'dd': util.timeAddZero(day),
'h': hour,
'hh': util.timeAddZero(hour),
'm': minute,
'mm': util.timeAddZero(minute),
's': second,
'ss': util.timeAddZero(second)
};
reg = /y{2,4}|M{1,2}|d{1,2}|h{1,2}|m{1,2}|s{1,2}/g;
afterFormat = formatStr.replace(reg, function($) {
if ($ in rule) {
return rule[$];
} else {
return $;
}
});
return afterFormat;
};
Date.prototype.format = format;
//天,时,分,秒的毫秒数
var ONE_SECOND = 1000,
ONE_MINUTE = ONE_SECOND * 60,
ONE_HOUR = ONE_MINUTE * 60,
ONE_DAY = ONE_HOUR * 24;
var timeGone = function(infoObj) {
var now = new Date(),
currYear = now.getFullYear(), //当前年
currMonDays = new Date(currYear, now.getMonth() + 1, 0).getDate(), //当前月的总天数
firstDay = new Date(currYear, 0, 1), //当前年的第一天
lastDay = new Date(currYear, 11, 31), //当前年的最后一天
oneYearDays = lastDay.getTime() - firstDay.getTime() + ONE_DAY; //当前年的总毫秒数
var compareArr = [{
'type': 'year',
'time': oneYearDays
}, {
'type': 'month',
'time': currMonDays * ONE_DAY
}, {
'type': 'day',
'time': ONE_DAY
}, {
'type': 'hour',
'time': ONE_HOUR
}, {
'type': 'minute',
'time': ONE_MINUTE
}, {
'type': 'second',
'time': ONE_SECOND
}],
info = {
'year': '年',
'month': '个月',
'day': '天',
'hour': '小时',
'minute': '分钟',
'second': '秒'
},
i = 0,
differ = Math.abs(now.getTime() - this.getTime()), //传入时间与当前时间的毫秒差值
result = 0;
//从年开始算,不满1就继续往下推,大于等于1就结束
while (result < 1 && i < compareArr.length) {
result = differ / compareArr[i].time;
i++;
}
var type = compareArr[i - 1].type;
result = Math.floor(result) === 0 ? 1 : Math.floor(result);
return result + (infoObj && infoObj[type] ? infoObj[type] : info[type]) + '前';
};
Date.prototype.timeGone = timeGone;
if (window.localStorage) {
util.ls = {
set: function(key, value) {
var oldval = this.get(key);
//在setItem前先removeItem避免iPhone/ipad上偶尔的QUOTA_EXCEEDED_ERR错误
if (oldval !== '') {
this.remove(key);
}
window.localStorage.setItem(key, value);
return this;
},
get: function(key) {
var value = window.localStorage.getItem(key);
//如果为空统一返回null
return !value ? '' : value;
},
remove: function(key) {
window.localStorage.removeItem(key);
return this;
}
};
} else {
//操作cookie
util.ls = {
//expires传过期月数
set: function(name, value, expires, path, domain, secure) {
var cookieText = encodeURIComponent(name) + '=' + encodeURIComponent(value);
if (!expires) {
expires = 12; //默认一年过期
}
var today = new Date();
expires *= 2592000000;
var expires_date = new Date(today.getTime() + expires);
cookieText += '; expires=' + expires_date.toGMTString();
if (path) {
cookieText += '; path=' + path;
}
if (domain) {
cookieText += '; domain=' + domain;
}
if (secure) {
cookieText += '; secure';
}
document.cookie = cookieText;
return this;
},
get: function(name) {
var reg = new RegExp(name + '\=([^;=]+)');
var match = reg.exec(document.cookie);
return match ? match[1] : '';
},
remove: function(name, path, domain, secure) {
this.set(name, '', new Date(0), path, domain, secure);
return this;
}
};
}
})(window);