merged private server and console to private-cloud
This commit is contained in:
111
console/mini_poem/utils/date_utils.js
Normal file
111
console/mini_poem/utils/date_utils.js
Normal file
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Created by Strawmanbobi
|
||||
* 2014-08-30
|
||||
*/
|
||||
|
||||
function formatDate(date, fmt) {
|
||||
var o = {
|
||||
"M+" : date.getMonth() + 1,
|
||||
"d+" : date.getDate(),
|
||||
"h+" : date.getHours(),
|
||||
"m+" : date.getMinutes(),
|
||||
"s+" : date.getSeconds(),
|
||||
"q+" : Math.floor((date.getMonth() + 3) / 3),
|
||||
"S" : date.getMilliseconds()
|
||||
};
|
||||
if(/(y+)/.test(fmt))
|
||||
fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
|
||||
for(var k in o)
|
||||
if(new RegExp("("+ k +")").test(fmt))
|
||||
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
|
||||
return fmt;
|
||||
}
|
||||
|
||||
function getNextDay(dateString) {
|
||||
var nextDayStr;
|
||||
var nextMonthStr;
|
||||
var nextYearStr;
|
||||
var currentDate = new Date(dateString);
|
||||
var nextDate = new Date(currentDate.getTime() + 24 * 60 * 60 * 1000);
|
||||
var nextDay = nextDate.getDate();
|
||||
if(nextDay < 10) {
|
||||
nextDayStr = "0" + nextDay;
|
||||
} else {
|
||||
nextDayStr = nextDay;
|
||||
}
|
||||
var nextMonth = nextDate.getMonth() + 1;
|
||||
if(nextMonth < 10) {
|
||||
nextMonthStr = "0" + nextMonth;
|
||||
} else {
|
||||
nextMonthStr = nextMonth;
|
||||
}
|
||||
nextYearStr = nextDate.getFullYear();
|
||||
return nextYearStr + "-" + nextMonthStr + "-" + nextDayStr;
|
||||
}
|
||||
|
||||
function getPreviousDay(dateString) {
|
||||
var lastDayStr;
|
||||
var lastMonthStr;
|
||||
var lastYearStr;
|
||||
var currentDate = new Date(dateString);
|
||||
var lastDate = new Date(currentDate.getTime() - 24 * 60 * 60 * 1000);
|
||||
var lastDay = lastDate.getDate();
|
||||
if(lastDay < 10) {
|
||||
lastDayStr = "0" + lastDay;
|
||||
} else {
|
||||
lastDayStr = lastDay;
|
||||
}
|
||||
var lastMonth = lastDate.getMonth() + 1;
|
||||
if(lastMonth < 10) {
|
||||
lastMonthStr = "0" + lastMonth;
|
||||
} else {
|
||||
lastMonthStr = lastMonth;
|
||||
}
|
||||
lastYearStr = lastDate.getFullYear();
|
||||
return lastYearStr + "-" + lastMonthStr + "-" + lastDayStr;
|
||||
}
|
||||
|
||||
function getDateDiffer(startTime, endTime, diffType) {
|
||||
startTime = startTime.replace(/\-/g, "/");
|
||||
endTime = endTime.replace(/\-/g, "/");
|
||||
|
||||
diffType = diffType.toLowerCase();
|
||||
var sTime = new Date(startTime);
|
||||
var eTime = new Date(endTime);
|
||||
var divNum = 1;
|
||||
switch (diffType) {
|
||||
case "second":
|
||||
divNum = 1000;
|
||||
break;
|
||||
case "minute":
|
||||
divNum = 1000 * 60;
|
||||
break;
|
||||
case "hour":
|
||||
divNum = 1000 * 3600;
|
||||
break;
|
||||
case "day":
|
||||
divNum = 1000 * 3600 * 24;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
|
||||
}
|
||||
|
||||
function dateDiff(sDate1, sDate2) {
|
||||
|
||||
var aDate, oDate1, oDate2, iDays;
|
||||
aDate = sDate1.split("-");
|
||||
oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]); //转换为yyyy-MM-dd格式
|
||||
aDate = sDate2.split("-");
|
||||
oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
|
||||
iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数
|
||||
|
||||
return iDays;
|
||||
}
|
||||
|
||||
exports.getNextDay = getNextDay;
|
||||
exports.getPreviousDay = getPreviousDay;
|
||||
exports.getDateDiffer = getDateDiffer;
|
||||
exports.formatDate = formatDate;
|
||||
exports.dateDiff = dateDiff;
|
||||
120
console/mini_poem/utils/map.js
Normal file
120
console/mini_poem/utils/map.js
Normal file
@@ -0,0 +1,120 @@
|
||||
/**
|
||||
* Created by Strawmanbobi
|
||||
* 2014-03-14
|
||||
*/
|
||||
|
||||
var Map = function() {
|
||||
this.elements = [];
|
||||
};
|
||||
|
||||
Map.prototype.size = function() {
|
||||
return this.elements.length;
|
||||
};
|
||||
|
||||
Map.prototype.isEmpty = function() {
|
||||
return (this.elements.length < 1);
|
||||
};
|
||||
|
||||
Map.prototype.clear = function() {
|
||||
this.elements = [];
|
||||
};
|
||||
|
||||
Map.prototype.put = function(_key, _value) {
|
||||
this.elements.push( {
|
||||
key : _key,
|
||||
value : _value
|
||||
});
|
||||
};
|
||||
|
||||
Map.prototype.remove = function(_key) {
|
||||
var bln = false;
|
||||
try {
|
||||
for (i = 0; i < this.elements.length; i++) {
|
||||
if (this.elements[i].key == _key) {
|
||||
this.elements.splice(i, 1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
bln = false;
|
||||
}
|
||||
return bln;
|
||||
};
|
||||
|
||||
Map.prototype.get = function(_key) {
|
||||
try {
|
||||
for (i = 0; i < this.elements.length; i++) {
|
||||
if (this.elements[i].key == _key) {
|
||||
return this.elements[i].value;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
Map.prototype.set = function(_key, _value) {
|
||||
for(i = 0; i < this.elements.length; i++) {
|
||||
if (this.elements[i].key == _key) {
|
||||
this.elements[i].value = _value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.elements.push({
|
||||
key : _key,
|
||||
value : _value
|
||||
});
|
||||
};
|
||||
|
||||
Map.prototype.element = function(_index) {
|
||||
if (_index < 0 || _index >= this.elements.length) {
|
||||
return null;
|
||||
}
|
||||
return this.elements[_index];
|
||||
};
|
||||
|
||||
Map.prototype.containsKey = function(_key) {
|
||||
var bln = false;
|
||||
try {
|
||||
for (i = 0; i < this.elements.length; i++) {
|
||||
if (this.elements[i].key == _key) {
|
||||
bln = true;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
bln = false;
|
||||
}
|
||||
return bln;
|
||||
};
|
||||
|
||||
Map.prototype.containsValue = function(_value) {
|
||||
var bln = false;
|
||||
try {
|
||||
for (i = 0; i < this.elements.length; i++) {
|
||||
if (this.elements[i].value == _value) {
|
||||
bln = true;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
bln = false;
|
||||
}
|
||||
return bln;
|
||||
};
|
||||
|
||||
Map.prototype.values = function() {
|
||||
var arr = [];
|
||||
for (i = 0; i < this.elements.length; i++) {
|
||||
arr.push(this.elements[i].value);
|
||||
}
|
||||
return arr;
|
||||
};
|
||||
|
||||
Map.prototype.keys = function() {
|
||||
var arr = [];
|
||||
for (i = 0; i < this.elements.length; i++) {
|
||||
arr.push(this.elements[i].key);
|
||||
}
|
||||
return arr;
|
||||
};
|
||||
|
||||
module.exports = Map;
|
||||
22
console/mini_poem/utils/object_reflection.js
Normal file
22
console/mini_poem/utils/object_reflection.js
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Created by Strawmanbobi
|
||||
* 2014-08-30
|
||||
*/
|
||||
|
||||
function objectMemberCount(conditions) {
|
||||
var memberCount = 0;
|
||||
for(var f in conditions) {
|
||||
memberCount++;
|
||||
}
|
||||
var whereClause = "";
|
||||
var index = 0;
|
||||
for(var field in conditions) {
|
||||
whereClause += field + " = '" + conditions[field] + "'";
|
||||
if(index < memberCount - 1) {
|
||||
whereClause += " AND ";
|
||||
}
|
||||
}
|
||||
return whereClause;
|
||||
}
|
||||
|
||||
module.exports = objectMemberCount;
|
||||
38
console/mini_poem/utils/string_utils.js
Normal file
38
console/mini_poem/utils/string_utils.js
Normal file
@@ -0,0 +1,38 @@
|
||||
exports.randomChar = function(l) {
|
||||
var x = "0123456789qwertyuioplkjhgfdsazxcvbnm";
|
||||
var tmp = "";
|
||||
for(var i = 0;i < l; i++) {
|
||||
tmp += x.charAt(Math.ceil(Math.random()*100000000)%x.length);
|
||||
}
|
||||
return tmp;
|
||||
};
|
||||
|
||||
exports.randomNumber = function(l) {
|
||||
var x = "0123456789";
|
||||
var tmp = "";
|
||||
for(var i = 0;i < l; i++) {
|
||||
tmp += x.charAt(Math.ceil(Math.random()*100000000)%x.length);
|
||||
}
|
||||
return tmp;
|
||||
};
|
||||
|
||||
exports.validateEmail = function (email) {
|
||||
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
||||
return re.test(email);
|
||||
};
|
||||
|
||||
function rnd() {
|
||||
var today = new Date();
|
||||
var seed = today.getTime();
|
||||
seed = (seed * 9301 + 49297) % 233280;
|
||||
return seed / (233280.0);
|
||||
}
|
||||
|
||||
function cr(number) {
|
||||
return Math.ceil(rnd() * number);
|
||||
}
|
||||
|
||||
function isNumber() {
|
||||
var r = /^[0-9]*[1-9][0-9]*$/;
|
||||
return r.test(str);
|
||||
}
|
||||
43
console/mini_poem/utils/system_utils.js
Normal file
43
console/mini_poem/utils/system_utils.js
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Created by Strawmanbobi
|
||||
* 2016-12-02
|
||||
*/
|
||||
|
||||
var dateUtils = require('./date_utils');
|
||||
var platform = require('platform');
|
||||
var UAParser = require('ua-parser-js');
|
||||
|
||||
function startup(expressApp, port, serverName) {
|
||||
if(expressApp && expressApp.listen && typeof(expressApp.listen) == "function") {
|
||||
expressApp.listen(port);
|
||||
|
||||
console.log(serverName +' restful webservice server is listening at port : ' +
|
||||
port + " //" + dateUtils.formatDate(new Date(), "yyyy-MM-dd hh:mm:ss"));
|
||||
console.log("driven by " + ICODE);
|
||||
}
|
||||
}
|
||||
|
||||
function startupHttp(http, port, serverName) {
|
||||
if(http) {
|
||||
http.listen(port);
|
||||
|
||||
console.log(serverName +' restful webservice server is listening at port : ' +
|
||||
port + " //" + dateUtils.formatDate(new Date(), "yyyy-MM-dd hh:mm:ss"));
|
||||
console.log("driven by " + ICODE);
|
||||
}
|
||||
}
|
||||
|
||||
function getOS() {
|
||||
return platform.os;
|
||||
}
|
||||
|
||||
function getUAInfo(ua) {
|
||||
var parser = new UAParser();
|
||||
var result = parser.setUA(ua).getResult();
|
||||
return result;
|
||||
}
|
||||
|
||||
exports.startup = startup;
|
||||
exports.startupHttp = startupHttp;
|
||||
exports.getOS = getOS;
|
||||
exports.getUAInfo = getUAInfo;
|
||||
Reference in New Issue
Block a user