common.js
6.27 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
/*自定义异步请求*/
function ajax() {
var ajaxData = {
type: (arguments[0].type || "GET").toUpperCase(),
url: arguments[0].url || "",
async: arguments[0].async || "true",
data: arguments[0].data || null,
dataType: arguments[0].dataType || "json",
contentType: arguments[0].contentType || "application/json; charset=utf-8",
beforeSend: arguments[0].beforeSend || function () { },
success: arguments[0].success || function () { },
error: arguments[0].error || function () { }
}
ajaxData.beforeSend()
var xhr = createxmlHttpRequest();
xhr.responseType = ajaxData.dataType;
xhr.open(ajaxData.type, ajaxData.url, ajaxData.async);
xhr.setRequestHeader("Content-Type", ajaxData.contentType);
xhr.send(convertData(ajaxData.data));
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
ajaxData.success(xhr.response)
} else {
ajaxData.error()
}
}
}
}
function createxmlHttpRequest() {
if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
}
function convertData(data) {
if (typeof data === 'object') {
var convertResult = "";
for (var c in data) {
convertResult += c + "=" + data[c] + "&";
}
convertResult = convertResult.substring(0, convertResult.length - 1)
return convertResult;
} else {
return data;
}
}
////////////////////////////////demo
//ajax({
// type: "POST",
// url: "ajax.php",
// dataType: "json",
// data: {
// "name": "abc",
// "age": 123,
// "id": "456"
// },
// beforeSend: function () {
// //some js code
// },
// success: function (msg) {
// console.log(msg)
// },
// error: function () {
// console.log("error")
// }
//})
/*******common*********/
/*统一请求接口*/
function postRequest(url,params,successCallback, errorCallback) {
ajax({
type: "POST",
url: url,
dataType: "json",
data: JSON.stringify(params),
beforeSend: function () {
//some js code
},
success: function (msg) {
successCallback(msg);
//var res = msg;
//if (res.code == 0) {//进场
//} else {//其他情况如【该卡号场内已存在】
// console.log(res.message);
//}
},
error: function (err) {
errorCallback(err);
//console.log("网络地址出错...")
}
})
}
/*获取URL?参数*/
function getQueryString(location) {
//var url = location.search; //获取url中"?"符后的字串
var url = location.search;
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
}
} else {
theRequest = null;
}
return theRequest;
}
/*统一提示信息*/
window.alertMsg = function (txt) {
var alertFram = document.createElement("DIV");
alertFram.id = "alertFram";
alertFram.style.position = "fixed";
alertFram.style.width = "100%";
alertFram.style.textAlign = "center";
alertFram.style.top = "40%";
alertFram.style.zIndex = "10001";
strHtml = " <span style=\"font-family: 微软雅黑;display:inline-block;background:#333;color:#fff;padding:0 20px;line-height:36px;border-radius:6px; \">" + txt + "</span>";
alertFram.innerHTML = strHtml;
document.body.appendChild(alertFram);
setTimeout((function () {
alertFram.style.display = "none";
}), 2500);
};
//四舍五入保留2位小数(不够位数,则用0替补)
function keepTwoDecimalFull(num) {
var result = parseFloat(num);
if (isNaN(result)) {
alert('传递参数错误,请检查!');
return false;
}
result = Math.round(num * 100) / 100;
var s_x = result.toString();
var pos_decimal = s_x.indexOf('.');
if (pos_decimal < 0) {
pos_decimal = s_x.length;
s_x += '.';
}
while (s_x.length <= pos_decimal + 2) {
s_x += '0';
}
return s_x;
}
/**
* param 将要转为URL参数字符串的对象
* key URL参数字符串的前缀
* encode true/false 是否进行URL编码,默认为true
*
* return URL参数字符串
*/
var parseParams = function (data) {
try {
var tempArr = [];
for (var i in data) {
var key = (i);
var value = encodeURIComponent(data[i]);//decodeURIComponent
tempArr.push(key + '=' + value);
}
var urlParamsStr = tempArr.join('&');
return urlParamsStr;
} catch (err) {
return '';
}
};
/*获取对象*/
function getObjectByID(id) {
return document.getElementById(id);
}
/*秒转时分*/
function formatSeconds(value) {
var secondTime = parseInt(value);// 秒
var minuteTime = 0;// 分
var hourTime = 0;// 小时
if (secondTime > 60) {//如果秒数大于60,将秒数转换成整数
//获取分钟,除以60取整数,得到整数分钟
minuteTime = parseInt(secondTime / 60);
//获取秒数,秒数取佘,得到整数秒数
secondTime = parseInt(secondTime % 60);
//如果分钟大于60,将分钟转换成小时
if (minuteTime > 60) {
//获取小时,获取分钟除以60,得到整数小时
hourTime = parseInt(minuteTime / 60);
//获取小时后取佘的分,获取分钟除以60取佘的分
minuteTime = parseInt(minuteTime % 60);
}
}
var result = "" + parseInt(secondTime) + "秒";
if (minuteTime > 0) {
result = "" + parseInt(minuteTime) + "分" + result;
}
if (hourTime > 0) {
result = "" + parseInt(hourTime) + "小时" + result;
}
return result;
}
/*只能输入数字加字母*/
function checkCharAndNumber(ev) {
//this.value = this.value.toUpperCase();
var tmpValue = this.value.replace(/[^\d|chun]/g, '');
this.value = this.value.toUpperCase();
}