Files
2018-12-03 17:31:53 +08:00

265 lines
10 KiB
JavaScript

define(function (require, exports, module) {
//判断当前日期是否大于某个日期
// yyyy-MM-dd HH:mm:ss
var greaterThanTime = function (time) {
var now = new Date();
var end = parseDate(time);
var ret = false;
if (now.getTime() > end.getTime()) {
ret = true;
}
// logger.debug([formatTime(now),">", formatTime(end),ret]);
return ret;
}
//跟greaterThanTime恰好相反
var lessThanTime = function (t) {
return !greaterThanTime(t);
}
//yyyy-MM-dd HH:mm:ss --> Date objet
var parseDate = function (time) {
var t = String(time);
var year = parseInt(t.substring(0, 4)),
month = parseInt(t.substring(5, 7)),
day = parseInt(t.substring(8, 10)),
hour = parseInt(t.substring(11, 13)),
min = parseInt(t.substring(14, 16)),
sec = parseInt(t.substring(17, 19));
// logger.debug([year,month,day,hour,min,sec])
var end = new Date();
end.setFullYear(year);
end.setMonth(month - 1, day);
end.setHours(hour);
end.setMinutes(min);
end.setSeconds(sec);
end.setMilliseconds(0);
return end;
}
var parseXML = function (data) {
var xml, tmp;
if (window.DOMParser) { // Standard
tmp = new DOMParser();
xml = tmp.parseFromString(data, "text/xml");
} else { // IE
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = "false";
xml.loadXML(data);
}
tmp = xml.documentElement;
if (!tmp || !tmp.nodeName || tmp.nodeName === "parsererror") {
return null;
}
return xml;
}
var xmlToJson = function (obj) {
if (this == null) return null;
var retObj = new Object;
buildObjectNode(retObj,
/*jQuery*/
obj);
return retObj;
function buildObjectNode(cycleOBJ,
/*Element*/
elNode) {
/*NamedNodeMap*/
var nodeAttr = elNode.attributes;
if (nodeAttr != null) {
if (nodeAttr.length && cycleOBJ == null) cycleOBJ = new Object;
for (var i = 0; i < nodeAttr.length; i++) {
cycleOBJ[nodeAttr[i].name] = nodeAttr[i].value;
}
}
var nodeText = "text";
if (elNode.text == null) nodeText = "textContent";
/*NodeList*/
var nodeChilds = elNode.childNodes;
if (nodeChilds != null) {
if (nodeChilds.length && cycleOBJ == null) cycleOBJ = new Object;
for (var i = 0; i < nodeChilds.length; i++) {
if (nodeChilds[i].tagName != null) {
if (nodeChilds[i].childNodes[0] != null && nodeChilds[i].childNodes.length <= 1 && (nodeChilds[i].childNodes[0].nodeType == 3 || nodeChilds[i].childNodes[0].nodeType == 4)) {
if (cycleOBJ[nodeChilds[i].tagName] == null) {
cycleOBJ[nodeChilds[i].tagName] = nodeChilds[i][nodeText];
} else {
if (typeof (cycleOBJ[nodeChilds[i].tagName]) == "object" && cycleOBJ[nodeChilds[i].tagName].length) {
cycleOBJ[nodeChilds[i].tagName][cycleOBJ[nodeChilds[i].tagName].length] = nodeChilds[i][nodeText];
} else {
cycleOBJ[nodeChilds[i].tagName] = [cycleOBJ[nodeChilds[i].tagName]];
cycleOBJ[nodeChilds[i].tagName][1] = nodeChilds[i][nodeText];
}
}
} else {
if (nodeChilds[i].childNodes.length) {
if (cycleOBJ[nodeChilds[i].tagName] == null) {
cycleOBJ[nodeChilds[i].tagName] = new Object;
buildObjectNode(cycleOBJ[nodeChilds[i].tagName], nodeChilds[i]);
} else {
if (cycleOBJ[nodeChilds[i].tagName].length) {
cycleOBJ[nodeChilds[i].tagName][cycleOBJ[nodeChilds[i].tagName].length] = new Object;
buildObjectNode(cycleOBJ[nodeChilds[i].tagName][cycleOBJ[nodeChilds[i].tagName].length - 1], nodeChilds[i]);
} else {
cycleOBJ[nodeChilds[i].tagName] = [cycleOBJ[nodeChilds[i].tagName]];
cycleOBJ[nodeChilds[i].tagName][1] = new Object;
buildObjectNode(cycleOBJ[nodeChilds[i].tagName][1], nodeChilds[i]);
}
}
} else {
cycleOBJ[nodeChilds[i].tagName] = nodeChilds[i][nodeText];
}
}
}
}
}
}
}
var o = {
xmlToJson:xmlToJson,
parseXML: parseXML,
parseDate: parseDate,
lessThanTime: lessThanTime,
greaterThanTime: greaterThanTime,
/*
* 获取 url 中指定变量的值
* @param {[String]} key name [变量名]
* @param {[String]} url [url地址]
* */
getUrlParam: function (name, url) {
var re = new RegExp("[\\?&#]" + name + "=([^&#]+)", "gi");
var ma = (url || location.href).match(re);
var strArr;
if (ma && ma.length > 0) {
strArr = (ma[ma.length - 1]).split("=");
if (strArr && strArr.length > 1) {
return strArr[1];
}
return ''
}
return '';
},
now: Date.now || function () { return new Date().getTime(); },
debounce: function (func, wait, immediate) {
var timeout, args, context, timestamp, result;
var later = function () {
var last = o.now() - timestamp;
if (last < wait) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
context = args = null;
}
}
};
return function () {
context = this;
args = arguments;
timestamp = o.now();
var callNow = immediate && !timeout;
if (!timeout) {
timeout = setTimeout(later, wait);
}
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
},
imageLoad: function (element, options) {
var imgs = (element instanceof $ ? element : $(element)).find('img');
imgs.length && imgs.on('load', function () {
$.isFunction(options.done) && options.done(this)
}).one('error', function () {
$.isFunction(options.fail) && options.fail(this)
})
},
format: function (text, obj) {
var c = obj || {};
var b = text;
for (var a in c) {
if (c.hasOwnProperty(a)) {
b = b.replace(new RegExp("\\$\\{" + a + "\\}", "gm"), c[a]);
}
}
return b;
},
htmlEscape: function (input) {
if (typeof (input) !== "string")
return input;
var result = [];
var length = input.length;
for (var i = 0; i < length; i++) {
var c = input.charAt(i).toString();
if (c == '&') {
result.push("&amp;");
} else if (c == '\"') {
result.push("&quot;");
} else if (c == '<') {
result.push("&lt;");
} else if (c == '>') {
result.push("&gt;");
} else if (c == '\'') {
result.push("&#39;");
} else if (c == '\\') {
result.push("&#092;");
} else {
result.push(c);
}
}
return result.join("");
},
JSONString: (window.JSON && JSON.stringify) || function (vContent) {
if (vContent instanceof Object) {
var sOutput = "";
if (vContent.constructor === Array) {
for (var nId = 0; nId < vContent.length; sOutput += this.JSONString(vContent[nId]) + ",", nId++);
return "[" + sOutput.substr(0, sOutput.length - 1) + "]";
}
if (vContent.toString !== Object.prototype.toString) {
return "\"" + vContent.toString().replace(/"/g, "\\$&") + "\"";
}
for (var sProp in vContent) {
sOutput += "\"" + sProp.replace(/"/g, "\\$&") + "\":" + this.JSONString(vContent[sProp]) + ",";
}
return "{" + sOutput.substr(0, sOutput.length - 1) + "}";
}
return typeof vContent === "string" ? "\"" + vContent.replace(/"/g, "\\$&") + "\"" : String(vContent);
},
trim : function(str){
str = str.replace(/\s+/g,"");
return str;
},
removeHtmlTab :function(tab) {
return tab;//删除所有HTML标签
} ,
html_encode:function(str)
{
var s = "";
if (!str || str.length == 0) return "";
s = str.replace(/&/g, "&gt;");
s = s.replace(/</g, "&lt;");
s = s.replace(/>/g, "&gt;");
s = s.replace(/ /g, "&nbsp;");
s = s.replace(/\'/g, "&#39;");
s = s.replace(/\"/g, "&quot;");
s = s.replace(/\n/g, "<br>");
return s;
}
};
module.exports = o;
})