05c5f7f1
liuqimichale
init
|
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
|
/**
* amWiki Web端·工具集
* @author Tevin
*/
;
(function (win) {
'use strict';
return win.tools = {
/**
* 获取url参数
* @param {String} name
* @returns {String|Null} - 获取的参数
*/
getURLParameter: function (name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) {
return r[2];
} else {
return null;
}
},
/**
* 转换字符中每个汉字为两个字符
* @param {String} str - 要编码的字符串
* @param {String} [mod] - 编码模式选择,可选,normal(默认)一个汉字对应两位字符,short一个汉字对应一位字符
* @returns {String} 编码后的字符串
*/
simString: function (str, mod) {
mod = mod == 'short'; //短字符串
var str2 = '';
var s = '';
var encodeURI = win.encodeURI;
for (var i = 0; i < str.length; i++) {
s = str.substr(i, 1);
if (/[\u4e00-\u9fa5]/.test(s)) {
encodeURI(s).split('%').forEach(function (item) {
if (item == '') {
s = [];
} else {
s.push(parseInt('0x' + item));
}
});
if (mod) {
str2 += (s[0] + s[1] + s[2]).toString(16).substr(-1, 1);
} else {
str2 += (s[0] + s[1] + s[2]).toString(16).substr(-2, 2);
}
} else {
str2 += s;
}
}
return str2;
},
/**
* json格式化
* @param {String} str - 需要格式化的json字符串
* @returns {String} 格式化后的json字符串
*/
formatJson: function (str) {
var json = decodeURI(str.replace(/%([^0-9A-Z]{0,2})/g, '%25$1'));
var reg = null,
formatted = '',
pad = 0,
PADDING = ' ';
var options = {};
// remove newline where '{' or '[' follows ':'
options.newlineAfterColonIfBeforeBraceOrBracket = options.newlineAfterColonIfBeforeBraceOrBracket === true;
// use a space after a colon
options.spaceAfterColon = options.spaceAfterColon !== false;
// begin formatting...
if (typeof json !== 'string') {
json = JSON.stringify(json);
} else {
json = JSON.parse(json);
json = JSON.stringify(json);
}
// add newline before and after curly braces
reg = /([\{\}])/g;
json = json.replace(reg, '\r\n$1\r\n');
// add newline before and after square brackets
reg = /([\[\]])/g;
json = json.replace(reg, '\r\n$1\r\n');
// add newline after comma
reg = /(\,)/g;
json = json.replace(reg, '$1\r\n');
// remove multiple newlines
reg = /(\r\n\r\n)/g;
json = json.replace(reg, '\r\n');
// remove newlines before commas
reg = /\r\n\,/g;
json = json.replace(reg, ',');
// optional formatting...
if (!options.newlineAfterColonIfBeforeBraceOrBracket) {
reg = /\:\r\n\{/g;
json = json.replace(reg, ':{');
reg = /\:\r\n\[/g;
json = json.replace(reg, ':[');
}
if (options.spaceAfterColon) {
reg = /"\s*\:/g;
json = json.replace(reg, '": ');
}
$.each(json.split('\r\n'), function (index, node) {
var i = 0,
indent = 0,
padding = '';
if (node.match(/\{$/) || node.match(/\[$/)) {
indent = 1;
} else if (node.match(/\}/) || node.match(/\]/)) {
if (pad !== 0) {
pad -= 1;
}
} else {
indent = 0;
}
for (i = 0; i < pad; i++) {
padding += PADDING;
}
formatted += padding + node + '\r\n';
pad += indent;
});
return formatted;
},
/**
* 时间戳格式化为日期时间
* @param {Number} timestamp - 时间戳
* @returns {String} 日期时间
*/
formatTime: function (timestamp) {
var time = new Date(timestamp);
var year = time.getFullYear() + '';
var month = time.getMonth() + 1;
month = month <= 9 ? '0' + month : month;
var date = time.getDate();
date = date <= 9 ? '0' + date : date;
var hour = time.getHours();
hour = hour <= 9 ? '0' + hour : hour;
var minute = time.getMinutes();
minute = minute <= 9 ? '0' + minute : minute;
var second = time.getSeconds();
second = second <= 9 ? '0' + second : second;
return year + '-' + month + '-' + date + ' ' + hour + ':' + minute + ':' + second;
}
}
})(window);
|