Commit d72b83eb authored by 刘松's avatar 刘松

add io & 400

parent e755ca87
......@@ -22,6 +22,10 @@ ENV MONGO_OEM='mongodb://bjwjh:OhzFH7t7Qn0T@mongo-bjwjh-rs-1.localhost:1302,mong
ENV MONGO_DB_MTTY='oem_mtty_remarketing'
ENV MONGO_DB_XIBAO='oem_xibao_remarketing'
ENV MONGO_DB_MTTY_AGENT='oem_maitian_remarketingAgent'
ENV MONGO_DB_XIBAO_AGENT='oem_xibaodongli_remarketingAgent'
ENV NODE_ENV='production'
EXPOSE 6662
CMD node server.js
......@@ -7,7 +7,9 @@ const _ = require("lodash");
const moment = require('moment');
const adminID = process.env.NODE_ENV === 'production' ? '5a9f9e6b46da1176a40e1082' : '5ab083b1f6134d82b40d95f2';
let db = {};
let agentDb = {};
let oem_db = { mtty:{}, xibao:{} };
let oem_agentDb = { mtty:{}, xibao:{} };
const dbpath = process.env.MONGO || "mongodb://10.11.3.127:1301/remarketing";
const oem_dbpath = ( process.env.MONGO_OEM + process.env.MONGO_DB_MTTY || "mongodb://10.11.3.127:1301/remarketing" ) + (process.env.MONGO_OEM ? "?replicaSet=bjwjh-rs&authSource=admin" : '');
......@@ -21,6 +23,7 @@ MongoClient.connect(dbpath, (err, conn) => {
if (err) return console.log(err);
console.log("#### DB CONNECTED");
db = conn.db("remarketing");
agentDb = conn.db("remarketingAgent");
db
.collection("tokenSession")
.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 * 24 });
......@@ -32,6 +35,8 @@ MongoClient.connect(oem_dbpath, (err, conn) => {
console.log("#### OEM DB CONNECTED");
oem_db.mtty = conn.db(process.env.MONGO_DB_MTTY);
oem_db.xibao = conn.db(process.env.MONGO_DB_XIBAO);
oem_agentDb.mtty = conn.db(process.env.MONGO_DB_MTTY_AGENT);
oem_agentDb.xibao = conn.db(process.env.MONGO_DB_XIBAO_AGENT);
oem_db.mtty
.collection("tokenSession")
.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 * 24 });
......@@ -40,34 +45,30 @@ MongoClient.connect(oem_dbpath, (err, conn) => {
.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 * 24 });
});
/*router.post('/remark',function (req,res) {
const { recog, remark, unikey, sessionID } = req.body;
let data = _.merge(req.query,req.body);
checkSession(data, async (err, rep) => {
if (err || !rep) return res.status(500).json({ error: "session错误,重新登录", code: 302 });
else {
let db = getDB(rep);
let doc = { recog, remark, unikey };
if(doc.recog) delete doc.unikey;
if(doc.unikey) delete doc.recog;
const host = getHost(rep) + "/remark";
axios(host, {
method: "POST",
headers: { "Content-Type": "application/json" },
data: doc,
timeout: 300000
})
.then(async rep => {
res.send({ status:'ok', data: rep.data });
})
.catch(err => {
console.dir(err);
if (err) return res.status(500).json({ error: '服务出错'});
});
}});
});*/
router.post('/export/add400recogs',async function (req,res) {
const { recogs, token, email, task } = req.body;
if(token !== 'from_caobo') return res.status(500).json({ error: "token错误", code: 302 });
if(!recogs.length || !task || !email) return res.status(500).json({ error: "参数错误", code: 302 });
let account = await db.collection('account').findOne({ email });
if(!account) return res.status(500).json({ error: "找不到账户", code: 302 });
let accountID = account._id + '';
let fromID = account.fromID;
let data = [];
recogs.forEach( x => {
data.push({
unikey: x,
task,
origin: '400',
accountID,
fromID
})
})
db.collection('dspRecognition').insertMany(data, (err, rep) => {
if(err) res.status(500).json({ error: '服务出错'});
else res.send({ status: 'ok',rep });
});
});
router.post('/remark',function (req,res) {
const { recog, tags, remark, sessionID } = req.body;
......@@ -116,87 +117,34 @@ router.post('/job/call',function (req,res) {
if (err || !rep) return res.status(500).json({ error: "session错误,重新登录", code: 302 });
else {
let db = getDB(rep);
let agentDb = getAgentDB(rep);
let _host = getHost(rep);
getCallConsume( db, 'call', (err, data) => {
if(err) return res.status(500).json({ error: err});
if(data && data.number) {
checkBill({ accountID: pubID, number: data.number }, db, async (_err) => {
if(_err) {
return res.status(500).json({ error: _err});
} else {
if(!notEmpty(req.body)) res.status(500).json({ error: "参数错误"});
else {
const fromID = await getFromID(pubID,db);
const host = _host + "/bind/" + (pubID + "_" + fromID) + '/' + slotID + '?caller=' + phone + '&unikey=' + unikey + '&expiration=' + expiration;
axios(host, {
method: "GET",
headers: { "Content-Type": "application/json" },
timeout: 300000
})
.then(async rep => {
if(rep.data && rep.data.called && rep.data._id && rep.data.bindID){
//const fromID = await getFromID(pubID);
updateBill({pre: true, number: data.number, accountID: pubID, fromID, type: 'call', taskID: rep.data._id}, db, (err,_rep) => {
if(err) {
// log 代码;
}
res.send({ status: 'ok', called: rep.data.called, bindID: rep.data.bindID });
});
}
else{
res.status(500).json({ error: '取号失败'});
}
})
.catch(err => {
console.dir(err)
if (err) return res.status(500).json({ error: '运营商拒绝服务'});
});
}
}
});
} else {
res.status(500);
}
});
}});
});
// 电信绑定
router.post('/job/tycall',function (req,res) {
const { pubID, groupID, phone, expiration = EXPIRATION, unikey } = req.body;
let data = _.merge(req.query,req.body);
checkSession(data, async (err, rep) => {
if (err || !rep) return res.status(500).json({ error: "session错误,重新登录", code: 302 });
else {
let db = getDB(rep);
let _host = getHost(rep);
getCallConsume(db, 'tycall', (err, data) => {
if(err) return res.status(500).json({ error: err});
if(data && data.number) {
checkBill({ accountID: pubID, number: data.number },db, async (_err) => {
checkBillNew({ accountID: pubID, count: 30, type: 'call', discountType: 'pipeDiscount' }, db, agentDb, async (_err,_body) => {
if(_err) {
return res.status(500).json({ error: _err});
} else {
if(!notEmpty(req.body)) res.status(500).json({ error: "参数错误"});
else {
const fromID = await getFromID(pubID,db);
const host = _host + "/ty/bind/" + (pubID + "_" + fromID) + '/' + groupID + '?caller=' + phone + '&unikey=' + unikey + '&expiration=' + expiration;
const host = _host + "/bind/" + (pubID + "_" + fromID) + '/' + slotID + '?caller=' + phone + '&unikey=' + unikey + '&expiration=' + expiration;
console.dir('|||||| get host' + host);
axios(host, {
method: "GET",
headers: { "Content-Type": "application/json" },
timeout: 300000
})
.then(async rep => {
if(rep.data && rep.data.called && rep.data._id){
//const fromID = await getFromID(pubID);
updateBill({pre: true, number: data.number, accountID: pubID, fromID, type: 'tycall', groupID, taskID: rep.data._id }, db, (err,_rep) => {
console.dir('in callback');
console.log('start ===============>');
console.log(rep.data);
console.log(rep.status);
console.log(rep.statusText);
console.log('end ===============>');
if(rep.data && rep.data.called && rep.data._id && rep.data.bindID){
updateBillNew(_.merge({pre: true, number: data.number, accountID: pubID, fromID, type: 'call', taskID: rep.data._id},_body), db, (err,_rep) => {
if(err) {
// log 代码;
console.dir(err);
}
console.dir(rep);
res.send({ status: 'ok', called: rep.data.called, caller: phone, unikey });
res.send({ status: 'ok', called: rep.data.called, bindID: rep.data.bindID });
});
}
else{
......@@ -210,35 +158,116 @@ router.post('/job/tycall',function (req,res) {
}
}
});
} else {
res.status(500);
}
}});
});
// 电信绑定
router.post('/job/tycall',function (req,res) {
const { pubID, groupID, phone, expiration = EXPIRATION, unikey } = req.body;
let data = _.merge(req.query,req.body);
console.dir(data);
checkSession(data, async (err, rep) => {
if (err || !rep) return res.status(500).json({ error: "session错误,重新登录", code: 302 });
else {
let db = getDB(rep);
let agentDb = getAgentDB(rep);
let _host = getHost(rep);
console.dir(_host);
checkBillNew({ accountID: pubID, count: 30, type: 'tycall', discountType: 'pipeDiscount' }, db, agentDb, async (_err,_body) => {
console.dir(_body);
if(_err) {
return res.status(500).json({ error: _err});
} else {
if(!notEmpty(req.body)) res.status(500).json({ error: "参数错误"});
else {
const fromID = await getFromID(pubID,db);
const host = _host + "/ty/bind/" + (pubID + "_" + fromID) + '/' + groupID + '?caller=' + phone + '&unikey=' + unikey + '&expiration=' + expiration;
console.dir('|||||| get host' + host);
axios(host, {
method: "GET",
headers: { "Content-Type": "application/json" },
timeout: 300000
})
.then(async rep => {
console.log('start ===============>');
console.log(rep.data);
console.log(rep.status);
console.log(rep.statusText);
console.log('end ===============>');
if(rep.data && rep.data.called && rep.data._id){
//const fromID = await getFromID(pubID);
updateBillNew(_.merge({pre: true, number: data.number, accountID: pubID, fromID, type: 'tycall', groupID, taskID: rep.data._id },_body), db, (err,_rep) => {
console.dir('in callback');
if(err) {
// log 代码;
console.dir(err);
}
res.send({ status: 'ok', called: rep.data.called, caller: phone, unikey });
});
}
else{
res.status(500).json({ error: '取号失败'});
}
})
.catch(err => {
console.dir(err)
if (err) return res.status(500).json({ error: '运营商拒绝服务'});
});
}
}
});
}
});
});
router.post('/job/dspcall',function (req,res) {
const { pubID, groupID, phone, expiration = EXPIRATION, unikey } = req.body;
console.dir('=====>');
//if(unikey.indexOf('*') >=0) return res.status(500).json({ error: "当前通话服务维护中", code: 302 });
let data = _.merge(req.query,req.body);
checkSession(data, async (err, rep) => {
if (err || !rep) return res.status(500).json({ error: "session错误,重新登录", code: 302 });
else {
let db = getDB(rep);
let agentDb = getAgentDB(rep);
let _host = getHost(rep);
getCallConsume( db, 'call', (err, data) => {
if(err) return res.status(500).json({ error: err});
if(data && data.number) {
checkBill({ accountID: pubID, number: data.number },db, async (_err) => {
if(_err) {
return res.status(500).json({ error: _err});
} else {
if(!notEmpty(req.body)) res.status(500).json({ error: "参数错误"});
else {
const fromID = await getFromID(pubID,db);
const host = _host + "/dspBind/" + (pubID + "_" + fromID) + '/' + groupID + '?caller=' + phone + '&unikey=' + unikey + '&expiration=' + expiration;
checkBillNew({ accountID: pubID, count: 30, type: 'call', discountType: 'pipeDiscount' }, db, agentDb, async (_err,_body) => {
if(_err) {
return res.status(500).json({ error: _err});
} else {
if(!notEmpty(req.body)) res.status(500).json({ error: "参数错误"});
else {
const fromID = await getFromID(pubID,db);
const host = _host + "/dspBind/" + (pubID + "_" + fromID) + '/' + groupID + '?caller=' + phone + '&unikey=' + unikey + '&expiration=' + expiration;
// console.dir('|||||| get host' + host);
let recog = await db.collection('dspRecognition').findOne({unikey});
let account = await db.collection('account').findOne({_id: OID(pubID)});
if(!recog) return res.status(500).json({ error: "线索不存在", code: 302 });
if(recog.origin === '400') {
let email = account.email + '[' + (account.company || '~') + ']';
let doc = { unikey: recog.unikey, task: recog.task, account: email, phone: phone };
axios('http://yunying.yoo.yunpro.cn/io', {
method: "POST",
data: doc,
headers: { "Content-Type": "application/json" }
})
.then(rep => {
if(rep && rep.status == 200) {
return res.send({status:'ok'})
}
else {
return res.status(500).json({ error: 'api错误,发送失败' });
}
})
.catch(err => {
// 发送失败
cons
return res.status(500).json({ error: '后台错误,失败' });
});
}
else
axios(host, {
method: "GET",
headers: { "Content-Type": "application/json" },
......@@ -246,10 +275,11 @@ router.post('/job/dspcall',function (req,res) {
})
.then(async rep => {
if(rep.data && rep.data.called && rep.data._id && rep.data.bindID){
//const fromID = await getFromID(pubID);
updateBill({pre: true, number: data.number, accountID: pubID, fromID, type: 'dspcall', groupID,taskID: rep.data._id }, db, (err,_rep) => {
updateBillNew(_.merge({pre: true, number: data.number, accountID: pubID, fromID, type: 'dspcall', groupID, taskID: rep.data._id },_body), db, (err,_rep) => {
if(err) {
// log 代码;
console.dir(err);
console.dir('in err');
}
res.send({ status: 'ok', called: rep.data.called, bindID: rep.data.bindID });
});
......@@ -262,16 +292,11 @@ router.post('/job/dspcall',function (req,res) {
console.dir(err)
if (err) return res.status(500).json({ error: '运营商拒绝服务'});
});
}
}
});
} else {
res.status(500);
}
}
});
}
});
});
router.get('/unbind',function (req,res) {
......@@ -685,9 +710,11 @@ function notEmpty(data) {
async function authorize(data, callback) {
let token = md5token(data.token);
try {
let self_user = await db.collection("tokens").findOne({ phone: data.phone, removed: { $ne: true } });
let mtty_user = await oem_db.mtty.collection("tokens").findOne({ phone: data.phone, removed: { $ne: true } });
let xibao_user = await oem_db.xibao.collection("tokens").findOne({ phone: data.phone, removed: { $ne: true } });
console.dir(data);
let self_user = await db.collection("tokens").findOne({ phone: data.phone + "", removed: { $ne: true } });
let mtty_user = await oem_db.mtty.collection("tokens").findOne({ phone: data.phone + "", removed: { $ne: true } });
let xibao_user = await oem_db.xibao.collection("tokens").findOne({ phone: data.phone + "", removed: { $ne: true } });
if (self_user && self_user.passwd == token) {
return callback(null, _.merge(self_user, {db: 'self'}));
}
......@@ -732,6 +759,66 @@ async function checkBill(data, _db, callback) {
callback && callback();
}
function getFixedNumber(number) {
return parseFloat(Number(number || 0).toFixed(2));
}
// 新账单逻辑
async function checkBillNew(data, _db, agentDb, callback) {
if(!notEmpty(data)) return callback('参数错误');
let { count, accountID, type, discountType } = data; // discountType = 'pipeDiscount dataDiscount'
if(!/^[0-9a-z]{24}$/.test(accountID) ) return callback('参数格式错误');
let account = await _db.collection('account').findOne({ _id: OID(accountID) });
if(!account) return callback('参数错误');
let sum = 0;
if(account && account.callfavor) {
sum = account.callfavor.concat([ {number: 0 } ]).filter(x => (x.pre !== true)).map( x => x.number ).reduce((a,b) => a+b);
}
// 获取扣款额
let price = await _db.collection('price').findOne({ type });
let number = count * ((price && price.number)? price.number : priceMap[type]);
// 直客余额判断
number = getFixedNumber(number);
let recharge = await getRechargeByAccount( accountID, _db );
console.dir(recharge)
console.dir(number)
if( recharge <= 0 ) { return callback('请先充值再操作') }
let consume = await getBillByAccount( accountID, _db );
console.dir(consume)
console.dir(consume + number);
if( consume + number > recharge ) { return callback('账户余额不足,请充值再操作') }
if(account.agentDiscount) {
checkAgentBill({ number:(account.agentDiscount[discountType] || 1) * number, accountID: account.agentDiscount.agentID }, agentDb, async (error, result) => {
if(error) return callback(error);
if(account.juniorDiscount) {
checkAgentBill({ number:(account.juniorDiscount[discountType] || 1) * number, accountID: account.juniorDiscount.agentID }, agentDb, (error, result) => {
if(error) callback(error);
return callback && callback(null,{ callFavor: (sum - (account.callfavorused || 0 ) >= 30), number: (sum - (account.callfavorused || 0 ) >= 30 ? 0: number), discount: 1, platform: { number, discount: 1 }, agent: { agentID: account.agentDiscount.agentID, number:getFixedNumber((account.agentDiscount[discountType] || 1) * number), discount: (account.agentDiscount[discountType] || 1) }, junior: { agentID: account.juniorDiscount.agentID, number:getFixedNumber((account.juniorDiscount[discountType] || 1) * number), discount: (account.juniorDiscount[discountType] || 1) }});
});
} else {
callback(null,{ callFavor: (sum - (account.callfavorused || 0 ) >= 30), number: (sum - (account.callfavorused || 0 ) >= 30 ? 0: number), discount: 1, platform: { number, discount: 1 }, agent: { agentID: account.agentDiscount.agentID, number:getFixedNumber((account.agentDiscount[discountType] || 1) * number), discount: (account.agentDiscount[discountType] || 1) } });
}
})
} else {
callback(null,{ callFavor: (sum - (account.callfavorused || 0 ) >= 30), number: (sum - (account.callfavorused || 0 ) >= 30 ? 0: number), discount: 1, platform: { number, discount: 1 } });
}
}
async function checkAgentBill(data, _db, callback) {
if(!notEmpty(data)) return callback('参数错误');
let { number, accountID, key } = data;
if(!/^[0-9a-z]{24}$/.test(accountID) ) return callback('参数格式错误');
let recharge = await getRechargeByAgentAccount( accountID, _db );
console.dir(recharge + '====== recharge' + accountID);
if( recharge <= 0 ) return callback('代理商余额不足');
let consume = await getBillByAgentAccount( accountID, key, _db );
console.dir(consume + '======consume' + accountID);
if( consume + number > recharge ) { return callback('代理商账户余额不足,请充值再操作') }
callback && callback();
}
async function updateBill(data, _db, callback) {
......@@ -751,6 +838,38 @@ async function updateBill(data, _db, callback) {
});
}
// 新账单逻辑
async function updateBillNew(data, _db, callback) {
console.dir(' in updateBillNew');
console.dir(data);
//if(!notEmpty(data)) return callback('params wrong');
let { meta, pre, number, accountID, type, taskID, fromID = 'self', groupID, discount, platform, agent, junior, callFavor } = data;
if(!/^[0-9a-z]{24}$/.test(accountID) ) return callback('params wrong');
let _data = { pre, number, accountID: OID(accountID), fromID, type, meta, discount, platform, agent, junior, callFavor };
if(groupID) {
_data.groupID = OID(groupID);
}
if(taskID) {
_data.taskID = OID(taskID);
}
console.dir(_data);
_db
.collection('bills')
.insert(wrapTime(_data,true), async (err, rep) => {
if (err) return callback(err);
if (err || !rep || !rep.ops || !rep.ops[0]._id) return callback(err);
let billID = rep.ops[0]._id;
let updateAgent = ( updateJunior = null );
if(agent && agent.agentID) {
updateAgent = await agentDb.collection('account').update({_id:OID(agent.agentID)}, {$push: { preNumber: { billID, number: agent.number } }});
}
if(junior && junior.agentID) {
updateJunior = await agentDb.collection('account').update({_id:OID(junior.agentID)}, {$push: { preNumber: { billID, number: junior.number } }});
}
callback && callback(null, rep);
});
}
async function getBillByAccount( accountID, _db ) {
let consumes = await _db
.collection('bills')
......@@ -790,13 +909,59 @@ async function getRechargeByAccount(accountID, _db) {
return ( (recharges && recharges.length) ? recharges[0].sum : 0 );
}
async function getCallConsume(_db,key,callback) {
const price = _db.collection('price').findOne({type: key || 'call'});
let defaultNum = { call: 1, tycall: 1.5 }
let number = (price && price.number) ? price.number : (defaultNum[key] || 1);
console.dir(number);
//const task = db.collection('callTask').findOne({ unikey });
callback(null, { number: Math.ceil((EXPIRATION / 60) ) * number });
async function getRechargeByAgentAccount( accountID, _db ) {
let account = await _db
.collection('account')
.findOne({_id:OID(accountID)});
let _preNumber = 0;
if(account && account.preNumber) {
_preNumber = account.preNumber.concat([ { number: 0 } ]).map( x => x.number ).reduce((a,b) => a+b);
}
return ((account && account.balance) ? account.balance - _preNumber : 0 );
}
// 一级代理商 账单汇总
async function getBillByAgentAccount( accountID , key, _db ) {
let query = { $match: {} };
query['$match'][key] = OID(accountID);
let consumes = await _db
.collection('bills')
.aggregate([
query,
{
$group:{
_id: null,
sum: {$sum:"$number"}
}
}]).toArray();
return ( (consumes && consumes.length) ? consumes[0].sum : 0 );
}
async function getCallConsume(_db,key,pubID,callback) {
console.dir('getCallConsume' + key)
if(pubID === '') { // 测试新账单逻辑
const account = await _db.collection('account').findOne({ _id: OID(pubID) });
if(!account) return callback("账户信息错误");
let sum = 0;
if(account && account.callfavor) {
sum = account.callfavor.concat([ { number: 0 } ]).map( x => x.number ).reduce((a,b) => a+b);
}
if(account - account.callfavorused >= 30) {
callback(null,{ number:0, callfavor: true });
} else {
const price = await _db.collection('price').findOne({type: key || 'call'});
let defaultNum = { call: 1, tycall: 1.5 }
let number = (price && price.number) ? price.number : (defaultNum[key] || 1);
console.dir(number);
callback(null, { number: Math.ceil((EXPIRATION / 60) ) * number });
}
} else {
const price = await _db.collection('price').findOne({type: key || 'call'});
let defaultNum = { call: 1, tycall: 1.5 }
let number = (price && price.number) ? price.number : (defaultNum[key] || 1);
console.dir(number);
callback(null, { number: Math.ceil((EXPIRATION / 60) ) * number });
}
}
async function getTasks(arrays,_db) {
......@@ -842,8 +1007,14 @@ function getDB(session) {
else return db;
}
function getAgentDB(session) {
if(session.db == 'mtty') return oem_agentDb.mtty;
if(session.db == 'xibao') return oem_agentDb.xibao;
else return agentDb;
}
function getHost(session) {
if(session.db == 'mtty') return 'http://remarkering-mtty-yh.yoo.yunpro.cn';
if(session.db == 'mtty') return 'http://remarketing-mtty-yh.yoo.yunpro.cn';
if(session.db == 'xibao') return 'http://remarketing-xibao-yh.yoo.yunpro.cn';
else return 'http://remarketing-job-yh.yoo.yunpro.cn';
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment