utils-1.js
18.4 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
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
var GLSX = {};
/**
* @author alvin.zengqi
* @description 常用方法
*/
GLSX.Utils = {
/**
* 过滤对象中的空字符串属性
* @param obj
* @returns {___anonymous154_155}
*/
filterBlankParams: function(obj) {
var _newPar = {};
for (var key in obj) {
if (obj[key].toString().replace(/(^\s*)|(\s*$)/g, '') == '') {
continue;
}
_newPar[key] = obj[key];
}
return _newPar;
},
ajax: function(o) {
var d = {
url: '',
data: {},
cache: false,
type: 'POST',
dataType: 'JSON',
processData: true,
async: true,
timeout: 180 * 1000,
prefix: 'GLSX-User-'
},
h = { 'GLSX-User-HOST': location.host },
_k, _v; // sys = '/site/';
d = $.extend(d, o);
d['success'] = function(data) {
GLSX.DOM.get('#_AJAX_LOADING').hide();
try {
if (o['success']) {
o['success'](data);
}
} catch (e) {}
};
d['error'] = function(jqXHR, textStatus, errorThrown) {
try {
if (o['error']) {
o['error'](jqXHR, textStatus, errorThrown);
}
} catch (e) {}
};
d['complete'] = function(jqXHR, textStatus) {
GLSX.DOM.get('#_AJAX_LOADING').hide();
try {
if (o['complete']) {
o['complete'](jqXHR, textStatus);
}
} catch (e) {} finally {
}
};
if (d.data) {
for (var k in d.data) {
if (k) {
_k = d.prefix + k,
_v = encodeURIComponent(d.data[k]);
if (_v.length > 100) continue;
h[_k] = _v;
GLSX.Storage.set(_k.replace('User', 'S'), _v, true);
}
}
d['headers'] = h;
}
$.ajax(d);
},
exist$obj: function(obj) {
return $(obj).length > 0;
},
/**
* 取出相应的request过来的值,val是设置值的名称
*/
queryString: function(val) {
var uri = window.location.search;
var re = new RegExp("" + val + "\=([^\&\?]*)", "ig");
return ((uri.match(re)) ? (uri.match(re)[0].substr(val.length + 1)) : null);
},
/**
* 获取地址参数
*/
getParam: function(param) {
var uri = window.location.href;
var re = new RegExp("" + param + "\=([^\&\?]*)", "ig");
return ((uri.match(re)) ? (uri.match(re)[0].substr(param.length + 1)) : null);
},
/**
* 获取全部参数,并返回数组
*/
queryStrings: function(search) {
var uri = search || window.location.search;
var re = /\w*\=([^\&\?]*)/ig;
var retval = [];
while ((arr = re.exec(uri)) != null)
retval.push(arr[0]);
return retval;
},
/**
* 如果当前地址栏有此参数,那么将更新此参数,否则返回一个新的地址栏参数字符串。
*/
setQuery: function(val1, val2) {
var a = GLSX.Utils.queryStrings();
var retval = "";
var seted = false;
var re = new RegExp("^" + val1 + "\=([^\&\?]*)$", "ig");
for (var i = 0; i < a.length; i++) {
if (re.test(a[i])) {
seted = true;
a[i] = val1 + "=" + val2;
}
}
retval = a.join("&");
return "?" + retval +
(seted ? "" : (retval ? "&" : "") + val1 + "=" + val2);
},
/**
* 截取字符串
*/
interceptString: function(str, length) {
if (str) {
if (str.length > length) {
str = str.substr(0, length) + "...";
}
return str;
}
return "";
},
/**
* 截取字符串(按字节) str : 原字符串 length : 指定字节长度
*/
subStrByCharCode: function(str, length) {
var text = GLSX.Utils.getStrByCharCode(str),
retText = text.substring(0, length).replace(/([\u0391-\uffe5])a/ig, '$1');
if (text.length > length) {
retText += '...';
}
return retText;
},
/**
* 获取字符串(按字节)
*/
getStrByCharCode: function(str) {
var retText = '';
if (str && $.trim(str).length > 0) {
retText = str.replace(/([\u0391-\uffe5])/ig, '$1a');
}
return retText;
},
/**
* 计算字符串长度,中文按2计算
*/
getBytesLength: function(str) {
var len = 0;
if (str) {
len = str.replace(/[^\x00-\xff]/g, "xx").length;
}
return len;
},
/**
* 获取字符串长度(按字节)
*/
getLengthByCharCode: function(str) {
return GLSX.Utils.getStrByCharCode(str).length;
},
rand: function(number) {
var seed = new Date().getTime();
return Math.ceil((seed * 9301 + 49297) % 233280 / (233280.0) * number);
},
/**
* 浏览器判断
*
* @returns Sys
*/
browser: function() {
var ua = navigator.userAgent.toLowerCase();
var Sys = {
ios: !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
android: (ua.indexOf("Android") > -1 || ua.indexOf("Linux") > -1 || ua.indexOf("android") > -1 || ua.indexOf("linux") > -1),
iphone: ua.indexOf("iPhone") > -1 || ua.indexOf("iphone") > -1,
ipad: ua.indexOf("iPad") > -1 || ua.indexOf("ipad") > -1,
mobile: !!ua.match(/AppleWebKit.*Mobile.*/),
wechat: (ua.match(/MicroMessenger/i) == 'micromessenger'),
is360: ua.indexOf('360 aphone browser') > -1
};
var s;
(s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1]:
(s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
(s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
(s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
(s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
return Sys;
},
/*
GLSX.Utils.dialog({
title: '温馨提示',
content: '恭喜中奖',
buttons: {
'确定': function(e, d){
//d.find('#_you_dom_id');
e.close();
},
'取消': function(e){
e.close();
}
//可以配置多个按钮
},
autoOpen: true,
open: function(){
},
close: function(){
}
});
*/
dialog: function(options) {
if (!('AMUI' in window)) {
GLSX.Utils.prompt(options);
return;
}
// 默认参数配置,可自定义
var o = {
title: '温馨提示',
content: null, // 内容区,可传文本、html、jQuery对象
buttons: {
'确定': function(e, d) {
e.close();
}
}, // 按钮组
autoOpen: true, // 自动打开,默认false
timeout: 0, // 定时关闭
open: function() {}, // 打开窗口前操作
close: function() {} // 关闭窗口前操作
},
d = GLSX.DOM.get('#my-alert'),
s = {
open: function() {
o.open();
try {
d.modal('open');
} catch (e) {
d.show();
}
},
close: function() {
o.close();
try {
d.modal('close');
} catch (e) {
d.fadeOut(500);
}
},
remove: function() {
d.remove();
delete d;
delete o;
}
};
o = $.extend(o, options);
if (!GLSX.Utils.exist$obj(d)) {
d = GLSX.DOM.get([
'<div class="am-modal am-modal-alert " tabindex="-1" id="my-alert">',
' <div class="am-modal-dialog border-radius">',
' <div class="am-modal-hd title01"></div>',
' <div class="am-modal-bd title02"></div>',
' <div class="am-modal-footer"></div>',
' </div>',
'</div>'
].join(''));
GLSX.DOM.get('body').append(d);
}
d.find('.title01').html(o.title);
d.find('.title02').html(o.content);
if (o.buttons) {
d.find('.am-modal-footer').find('.title03').off('click');
d.find('.am-modal-footer').html('');
for (var b in o.buttons) {
GLSX.DOM.get(['<span class="am-modal-btn title03">',
b,
'</span>'
].join(''), true).appendTo(d.find('.am-modal-footer'))
.on('click', { f: o.buttons[b] }, function(e) {
(typeof e.data.f === 'function') ?
e.data.f(s, d, e): s.close();
});
}
}
o.autoOpen && s.open();
if (o.timeout > 0) {
setTimeout(s.close, o.timeout);
}
return s;
},
prompt: function(opt) {
// 默认参数配置,可自定义
var jQuery = jQuery || null
var o = {
title: '温馨提示',
content: null, // 内容区,可传文本、html、jQuery对象
autoOpen: true, // 自动打开,默认false
timeout: 0, // 定时关闭
okText: '确定',
open: function() {}, // 打开窗口前操作
close: function() {} // 关闭窗口前操作
},
d = GLSX.DOM.get('#my-prompt'),
s = {
open: function() {
o.open();
d.show();
},
close: function() {
o.close();
if (jQuery) {
d.fadeOut(500);
} else {
d.hide()
}
},
remove: function() {
d.remove();
delete d;
delete o;
}
};
o = $.extend(o, opt);
if (!GLSX.Utils.exist$obj(d)) {
d = GLSX.DOM.get([
'<div style="display: none;" id="my-prompt">',
' <div class="mask"></div>',
' <div class="mask_content">',
' <p class="error_title"><span id="prompt_title"></span></p>',
' <p class="error_reason" id="prompt_content"></p>',
' <a id="prompt_ok"></a>',
' </div>',
'</div>'
].join(''));
GLSX.DOM.get('body').append(d);
}
d.find('#prompt_content').html(o.content);
if (o.title) d.find('#prompt_title').html(o.title);
if (o.okText) d.find('#prompt_ok').html(o.okText);
d.find('#prompt_ok').one('click', s.close);
o.autoOpen && s.open();
if (o.timeout > 0) {
setTimeout(s.close, o.timeout);
}
return s;
},
/**
* 表单验证
*
* @param i
* 表单元素
* @returns {Boolean}
*/
validate: function(i) {
var required = i.attr('required') && i.attr('required') === 'required' ? true : false,
pattern = i.attr('pattern'),
minvalue = i.attr('minvalue'),
maxvalue = i.attr('maxvalue'),
isIdCard = i.attr('idcard') && i.attr('idcard') === 'true',
val = $.trim(i.val());
if (required && val.length == 0) {
GLSX.Utils.dialog({
content: i.attr('placeholder')
});
return false;
}
if (pattern && val.length > 0) {
pattern = new RegExp(pattern);
if (!pattern.test(val)) {
GLSX.Utils.dialog({
content: i.attr('patternholder') || '请匹配正确的格式'
});
return false;
}
}
if (parseInt(val) < minvalue || parseInt(val) > maxvalue) {
GLSX.Utils.dialog({
content: i.attr('patternholder') || '数值范围[' + minvalue + '~' + maxvalue + ']'
});
return false;
}
if (isIdCard) {
if (!GLSX.Utils.isIdCard(val)) {
GLSX.Utils.dialog({
content: '输入的身份证号有误'
});
return false;
}
}
return true;
},
isIdCard: function(sId) {
var aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" }
var iSum = 0;
var info = "";
if (!/^\d{17}(\d|x)$/i.test(sId)) return false;
sId = sId.replace(/x$/i, "a");
if (aCity[parseInt(sId.substr(0, 2))] == null) return false;
sBirthday = sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2));
//var d=new Date(sBirthday.replace(/-/g,"/")) ;
//if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))return false;
for (var i = 17; i >= 0; i--) iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11);
if (iSum % 11 != 1) return false;
return true; //aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女")
},
pageOnLoad: function(cb) {
window.onload = function() {
if (cb && typeof cb === 'function') cb();
}
},
pageLoading: function() {
showPageloading(basePath + '/images/loading3.gif', '100', '100', '#fff');
document.getElementById("bg_img").style.width = "auto"; //img样式重叠
GLSX.Utils.pageOnComplete(function() {
closePageloading();
});
},
pageOnComplete: function(cb) {
document.onreadystatechange = function() {
if (document.readyState == "complete") {
if (cb && typeof cb === 'function') cb();
}
}
},
throttle: (fn, gapTime) => {
if (gapTime == null || gapTime == undefined) {
gapTime = 1500
}
let _lastTime = null
// 返回新的函数
return function() {
let _nowTime = +new Date()
if (_nowTime - _lastTime > gapTime || !_lastTime) {
fn.apply(this, arguments) //将this和参数传给原函数
_lastTime = _nowTime
}
}
}
};
/**
* 本地存储
*/
GLSX.Storage = {
is: function() {
return !!window.localStorage;
},
set: function(a, d, b) {
if (!GLSX.Storage.is()) {
if (typeof(d) != "object") {
GLSX.Cookie.set(a, d);
}
return
}
var c = (b ? "session" : "local") + "Storage";
var jQuery = jQuery || null
if (jQuery) {
switch (jQuery.type(d)) {
case "object":
d = "object:" + JSON.stringify(d);
break;
case "string":
d = "string:" + d;
break;
}
}
try {
window[c].setItem(a, d);
} catch (f) {
}
},
get: function(a, b) {
if (!GLSX.Storage.is()) {
return GLSX.Cookie.get(a);
}
var c = (b ? "session" : "local") + "Storage";
var d = window[c].getItem(a);
if (/^object:/.test(d)) {
d = JSON.parse(d.replace(/^object\:/, ""));
} else {
if (/^string:/.test(d)) {
d = d.replace(/^string\:/, "");
}
}
return d;
},
remove: function(a, b) {
if (!GLSX.Storage.is()) {
GLSX.Cookie.del(a);
return;
}
var c = (b ? "session" : "local") + "Storage";
window[c].removeItem(a);
}
};
/**
* cookie存储
*/
GLSX.Cookie = {
set: function(d, f, e, g, a) {
if (a) {
var c = new Date();
var b = new Date();
b.setTime(parseFloat(c.getTime()) + 3600000 * a);
}
document.cookie = d + "=" + f + "; " + (a ? ("expires=" + b.toUTCString() + "; ") : "") + ("path=" + (g || "/") + "; domain=" + (e || "hhdatb.didihu.com.cn") + ";");
},
get: function(b) {
var c = new RegExp("(?:^|;+|\\s+)" + b + "=([^;]*)");
var a = document.cookie.match(c);
return (!a ? "" : a[1]);
},
del: function(a, b, c) {
if (b === null) {
document.cookie = a + "=; expires=Mon, 2 Mar 2009 19:00:00 UTC; path=" + (c || "/");
} else {
document.cookie = a + "=; expires=Mon, 2 Mar 2009 19:00:00 UTC; path=" + (c || "/") + "; domain=" + (b || "hhdatb.didihu.com.cn") + ";";
}
}
};
/**
* DOM操作
*/
GLSX.DOM = {
cache: {},
set: function(s) {
GLSX.DOM.del(s) && (GLSX.DOM.cache[s] = $(s));
return GLSX.DOM.cache[s];
},
get: function(s, n) {
return (n && GLSX.DOM.set(s)) || GLSX.DOM.cache[s] || GLSX.DOM.set(s);
},
del: function(s) {
if (GLSX.DOM.cache[s]) {
delete GLSX.DOM.cache[s];
}
return s;
}
};
GLSX.Base64 = {
keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
// public method for encoding
encode: function(input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = GLSX.Base64._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
GLSX.Base64.keyStr.charAt(enc1) + GLSX.Base64.keyStr.charAt(enc2) +
GLSX.Base64.keyStr.charAt(enc3) + GLSX.Base64.keyStr.charAt(enc4);
}
return output;
},
// public method for decoding
decode: function(input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = GLSX.Base64.keyStr.indexOf(input.charAt(i++));
enc2 = GLSX.Base64.keyStr.indexOf(input.charAt(i++));
enc3 = GLSX.Base64.keyStr.indexOf(input.charAt(i++));
enc4 = GLSX.Base64.keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = GLSX.Base64._utf8_decode(output);
return output;
},
// private method for UTF-8 encoding
_utf8_encode: function(string) {
string = string.replace(/\r\n/g, "\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} else if ((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
} else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
// private method for UTF-8 decoding
_utf8_decode: function(utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
} else if ((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i + 1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
} else {
c2 = utftext.charCodeAt(i + 1);
c3 = utftext.charCodeAt(i + 2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
}