Skip to content

Commit

Permalink
配置中心
Browse files Browse the repository at this point in the history
  • Loading branch information
guochen2 committed Feb 6, 2018
1 parent a6516ea commit 81986b7
Show file tree
Hide file tree
Showing 5 changed files with 490 additions and 0 deletions.
174 changes: 174 additions & 0 deletions NodeConfigCenter/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
//////
//单表操作 增删改查
//auth:guo
//time:2017年10月26日17:03:33
///////
var MongoClient = require('mongodb').MongoClient;
var config = require("./config");
var DB_CONN_STR = 'mongodb://' + config.mongodb.host + ':' + config.mongodb.post + '/' + config.mongodb.dbname;

var insertData = function* (tablename, data) {
return yield new Promise(function (resolve) {
MongoClient.connect(DB_CONN_STR, function (err, db) {
console.log("连接成功!");
//连接到表 site
var collection = db.collection(tablename);
//插入数据
collection.insertOne(data, function (err, result) {
if (err) {
console.log('Error:' + err);
return;
}
db.close();
resolve(result);
});
});
})
}
var updateData = function* (tablename, query, data) {
return yield new Promise(function (resolve) {
MongoClient.connect(DB_CONN_STR, function (err, db) {
console.log("连接成功!");
//连接到表 site
var collection = db.collection(tablename);
//插入数据
collection.updateOne(query, data, function (err, result) {
if (err) {
console.log('Error:' + err);
return;
}
db.close();
resolve(result);
});
});
})
}
var findData = function* (tablename, query) {
return yield new Promise(function (resolve) {
MongoClient.connect(DB_CONN_STR, function (err, db) {
console.log("连接成功!");
//连接到表 site
var collection = db.collection(tablename);
//插入数据
collection.findOne(query, function (err, result) {
if (err) {
console.log('Error:' + err);
return;
}
db.close();
resolve(result);
});
});
})
}

var removeData = function* (tablename, query) {
var collection = yield getCollection(tablename);
return new Promise(function (resolve, reject) {
collection.remove(query, function (err, result) {
if (err) {
console.error(err);
reject(err);
} else {
collection.close();
resolve(result);
}
});
});
}

var findCount = function* (tablename, query) {
var collection = yield getCollection(tablename);
return new Promise(function (resolve, reject) {
collection.count(query, function (err, result) {
if (err) {
console.error(err);
reject(err);
} else {
collection.close();
resolve(result);
}
});
});
}

var find = function* (tablename, query, limit, skip) {
var collection = yield getCollection(tablename);
return new Promise(function (resolve, reject) {
collection.find(query).limit(limit).skip(skip).toArray(function (err, result) {
if (err) {
reject(err);
} else {
collection.close();
resolve(result);
}
});
});

}

var getCollection = function* (tablename) {
var db = yield getConnect();
return new Promise(function (resolve) {
var collection = db.collection(tablename);
collection.close = db.close.bind(db);
resolve(collection);
});
}

var getConnect = function* () {
return new Promise(function (resolve, reject) {
MongoClient.connect(DB_CONN_STR, function (err, db) {
if (!err)
resolve(db);
else
reject(err);
})
});
}

Date.prototype.pattern = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
"H+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
var week = {
"0": "/u65e5",
"1": "/u4e00",
"2": "/u4e8c",
"3": "/u4e09",
"4": "/u56db",
"5": "/u4e94",
"6": "/u516d"
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
if (/(E+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]);
}
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;
};
String.prototype.toJson = function () {
return eval('(' + this + ')');
};

module.exports = {
insertData: insertData,
updateData: updateData,
findData, findData,
find,
removeData,
findCount
}
8 changes: 8 additions & 0 deletions NodeConfigCenter/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
post: 3012,
mongodb: {
host: '192.168.30.202',
post: '29018',
dbname: 'uranusconfig'
}
}
136 changes: 136 additions & 0 deletions NodeConfigCenter/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@

let common = require("./common");
let findhandler = function* (option, params) {
if (option == "currdateholiday") {//根据法定节假日 计算提现倍数
let d = yield common.findData("config", { operationtype: 4 });
let dd = new Date();
let newDate = dd.pattern("yyyy-MM-dd");
let multiple = 0;//倍数
let value = d.value.toJson();
for (var i in value) {
if (value[i].date == newDate) {
multiple = value[i].multiple;
break;
}
}
if (multiple == 0) {
if (new Date().getDay() == 5 || new Date().getDay() == 6 || new Date().getDay() == 0) {
multiple = 3;
}
}
if (multiple == 0) {
multiple = 1;
}
d.value = multiple;
return d;
}
else if (option == "appversion") {
let d = yield common.findData("config", { operationtype: 8 });
let value = d.value.toJson();
return value;
}
else if (option == "getappinit") {
let d = yield common.findData("config", { operationtype: 7 });
let value = d;
return value;
}
else if (option == "redpacketactiveproportion") {
var loanPeriod = params.lp;
var type = params.type;
var unit = params.unit;
var resdata = { code: 0, msg: '', data: 0 };
try {
var result = yield common.findData("config", { operationtype: 14 });
if (result.operationstatus == 1) {
var value = JSON.parse(result.value);
var propor = value.find(function (item) { return item.type == type });
if (propor && propor[unit]) {
var config = propor[unit].sort(function (a, b) { a - b });
var curPropor = config.find(function (item) { return loanPeriod <= item.max });
if (curPropor) {
resdata.code = 1;
resdata.data = curPropor.value;
}
}
}
} catch (e) {
console.log(e);
resdata.msg = '配置中心数据获取异常';
}
return resdata;
}
return 'option error';
}
let findhandlerid = function* (id, params) {
let d = yield common.findData("config", { operationtype: parseInt(id) });
return d;
}
let updatehandler = function* (id, params) {
var udata = { value: params.value };
if (params.starttime) {
udata.starttime = params.starttime;
}
if (params.endtime) {
udata.endtime = params.endtime;
}
if (params.value) {
udata.value = params.value;
}
if (params.desc) {
udata.desc = params.desc;
}
let d = yield common.updateData("config", { operationtype: parseInt(id) }, { $set: udata });
return d;
}

var addAliSafe = function* (mobile, name) {
if (!mobile) {
return null;
}
return yield common.insertData('fraud', { mobile, name, createtime: new Date() });
}

var removeAliSafe = function* (mobile) {
if (!mobile) {
return null;
}
return yield common.removeData('fraud', { mobile });
}

var getAliSafeList = function* (mobile, name, page, size) {
var page = parseInt(page || 1);
var size = parseInt(size || 20);
var query = {};
if (mobile) {
query.mobile = mobile;
} if (name) {
query.name = name;
}
var data = { list: [], total: 0 };
data.list = yield common.find('fraud', query, size, (page - 1) * size);
data.total = yield common.findCount('fraud', query);
return yield new Promise(function (resolve) {
resolve(data);
});
}

var getConfigList = function* (query) {
var page = parseInt(query.page || 1);
var size = parseInt(query.size || 20);
var data = { list: [], total: 0 };
data.list = yield common.find('config', {}, size, (page - 1) * size);
data.total = yield common.findCount('config', {});
return yield new Promise(function (resolve) {
resolve(data);
});
}
//var updateWhileList
module.exports = {
findhandler: findhandler,
findhandlerid: findhandlerid,
updatehandler: updatehandler,
getAliSafeList: getAliSafeList,
addAliSafe,
removeAliSafe,
getConfigList
}
Loading

0 comments on commit 81986b7

Please sign in to comment.