re commit

This commit is contained in:
Jay
2017-03-22 13:35:45 +08:00
commit 8947715671
109 changed files with 161369 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
var fs = require('fs');
var path = require('path');
var confLoader = {
_conf: null,
_path: null,
load: (p) => {
var self = this;
if (!p || p == undefined) {
p = path.resolve(__dirname, '..', 'config.json');
}
self._path = p;
if (!fs.existsSync(p)) {
throw `Config file not exists , file location : ${p}`;
}
var filecontent = fs.readFileSync(p);
if (!filecontent) {
throw `Config file read fail`;
}
filecontent = filecontent.toString();
var conf = null;
try {
conf = JSON.parse(filecontent);
self._conf = conf;
} catch (e) {
throw e;
}
return self._conf;
},
get: (key) => {
return this._conf[key];
}
};
module.exports = confLoader;
+40
View File
@@ -0,0 +1,40 @@
var fs = require('fs');
var path = require('path');
class confLoader {
constructor() {
this._conf = null;
this._path = null;
}
load(p) {
var self = this;
if (!p || p == undefined) {
p = path.resolve(__dirname, '..', 'config.json');
}
self._path = p;
if (!fs.existsSync(p)) {
throw `Config file not exists , file location : ${p}`;
}
var filecontent = fs.readFileSync(p);
if (!filecontent) {
throw `Config file read fail`;
}
filecontent = filecontent.toString();
var conf = null;
try {
conf = JSON.parse(filecontent);
self._conf = conf;
} catch (e) {
throw e;
}
return self;
}
get(key) {
return this._conf[key];
}
}
module.exports = confLoader;
+31
View File
@@ -0,0 +1,31 @@
var crypto = require('crypto');
var random = (len = 32) => {
var buf = crypto.randomBytes(len);
return buf.toString("hex");
}
var sha256 = (str) => {
return crypto.createHash("sha256").update(str).digest('base64');
}
var genPassHash = (str) => {
var hash = random(16);
var pass = sha256(str + hash);
return `$${hash}$${pass}`;
}
var comparePass = (plain, hash) => {
var match = hash.match(/^\$(.+?)\$(.+)$/);
if (match == null || match.length < 3 || !match[1] || !match[2]) return false;
var pass = sha256(plain + match[1]);
if (pass == match[2]) return true;
return false;
}
module.exports = {
random: random,
sha256: sha256,
genPassHash: genPassHash,
comparePass: comparePass
}
+30
View File
@@ -0,0 +1,30 @@
var mysql = require('mysql');
var config = require('./confLoader.js');
config.load();
var dbConf = config.get('db');
var database = function(){
this.conf = dbConf;
}
database.prototype.connect = function(){
var self = this;
self._con = mysql.createConnection(self.conf);
}
database.prototype.formatQuery = function(str, arr){
return mysql.format(str, arr);
}
database.prototype.query = function(query, cb){
var self = this;
self._con.query(query,cb);
}
database.prototype.close = function(){
this._con.end();
}
module.exports = database;
+102
View File
@@ -0,0 +1,102 @@
var mysql = require('mysql');
class database {
constructor() {
this._user = '';
this._password = '';
this._host = '';
this._port = 3306;
this._database = '';
this._con = null;
this.autoclose = false;
}
connect() {
if (!this._user || !this._host || !this._password || !this._database) throw 'mysql connect arg is empty';
this._con = mysql.createConnection({
user: this._user,
password: this._password,
host: this._host,
database: this._database,
port: this._port
});
}
checkConnect() {
if (!this._con) {
this.connect();
}
}
formatQuery(query, arg) {
return mysql.format(query, arg);
}
escape(val) {
return mysql.escape(val);
}
query(query, params, cb) {
this.checkConnect();
let self = this;
if (typeof params == 'function') {
cb = params;
params = [];
};
if (typeof params == 'object' && !Array.isArray(params)) params = [];
this._con.query(query, params, (err, rows, field) => {
if (self.autoclose) {
self.close(() => {
cb.apply(this, [err, rows, field]);
});
} else {
cb.apply(this, [err, rows, field]);
}
});
}
recordPage(rows, page, maxPage) {
if (!page || !isFinite(page) || page < 1) page = 1;
let totalpage = Math.ceil(rows / maxPage);
let prevpage = page - 1;
let nextpage = page + 1;
if (prevpage < 1) prevpage = 1;
if (nextpage > totalpage) nextpage = totalpage;
let rec_start = (page - 1) * maxPage + 1
let rec_end = (rec_start + maxPage - 1);
if (rec_end > rows) rec_end = rows;
let json = {
rec_start,
rec_end,
total: rows,
prevpage,
nextpage,
totalpage,
page
};
return json;
}
close(cb) {
let self = this;
this._con.end((err) => {
self._con = null;
if (cb) cb();
});
}
set user(str) { this._user = str; }
set host(str) { this._host = str; }
set password(str) { this._password = str; }
set port(str) { this._port = str; }
set database(str) { this._database = str; }
}
module.exports = database;