Commit e755ca87 authored by 刘松's avatar 刘松

fix login bug

parent 2c7f3f8f
...@@ -78,7 +78,7 @@ router.post('/remark',function (req,res) { ...@@ -78,7 +78,7 @@ router.post('/remark',function (req,res) {
let db = getDB(rep); let db = getDB(rep);
let collectionName = ((type === 'dsp') ? 'dspRecognition' : 'recognition'); let collectionName = ((type === 'dsp') ? 'dspRecognition' : 'recognition');
let _tags = Array.isArray(tags) ? tags : [tags]; let _tags = Array.isArray(tags) ? tags : [tags];
db.collection(collectionName).update({ _id: OID(recog) },{ $addToSet: { systemTags: { $each: _tags } }, $push: { remark: remark } }, (err, rep) => { db.collection(collectionName).update({ _id: OID(recog) },{ $set: { systemTags: _tags }, $push: { remark: remark } }, (err, rep) => {
if(err) res.status(500).json({ error: '服务出错'}); if(err) res.status(500).json({ error: '服务出错'});
else res.send({ status: 'ok',rep }); else res.send({ status: 'ok',rep });
}); });
...@@ -91,6 +91,8 @@ router.get('/tags',function (req,res) { ...@@ -91,6 +91,8 @@ router.get('/tags',function (req,res) {
if (err || !rep || !rep.tokenID) return res.status(500).json({ error: "session错误,重新登录", code: 302 }); if (err || !rep || !rep.tokenID) return res.status(500).json({ error: "session错误,重新登录", code: 302 });
else { else {
let db = getDB(rep); let db = getDB(rep);
let token = await db.collection('tokens').findOne({_id: OID(rep.tokenID)});
let accountID = token ? token.accountID : null;
let dspGroupIds = await db.collection('dspRecognition').distinct('groupID',{'tokenInfo.tokenID': OID(rep.tokenID) }); let dspGroupIds = await db.collection('dspRecognition').distinct('groupID',{'tokenInfo.tokenID': OID(rep.tokenID) });
let slotIds = await db.collection('recognition').distinct('slotID',{'tokenInfo.tokenID': OID(rep.tokenID) }); let slotIds = await db.collection('recognition').distinct('slotID',{'tokenInfo.tokenID': OID(rep.tokenID) });
let dspGroups = dspGroupIds.length ? await db.collection('dspCallGroups').find({ _id: { $in: dspGroupIds} }, { dspgroup : 1 }).toArray() : []; let dspGroups = dspGroupIds.length ? await db.collection('dspCallGroups').find({ _id: { $in: dspGroupIds} }, { dspgroup : 1 }).toArray() : [];
...@@ -98,7 +100,10 @@ router.get('/tags',function (req,res) { ...@@ -98,7 +100,10 @@ router.get('/tags',function (req,res) {
slotIds = slotIds.map( x => OID(x)); slotIds = slotIds.map( x => OID(x));
let slots = slotIds.length ? await db.collection('slotTemps').find({ _id: { $in: slotIds } },{ slotName: 1 }).toArray() : []; let slots = slotIds.length ? await db.collection('slotTemps').find({ _id: { $in: slotIds } },{ slotName: 1 }).toArray() : [];
slots = slots.map( x => { return { _id: x._id, name: x.slotName } }); slots = slots.map( x => { return { _id: x._id, name: x.slotName } });
res.send({ status:'ok',callstatus: ['已拨打','未拨打'], tags: ['有意向','已经加微信','需回拨','未接通','无意向'], dspGroups, slots }); console.dir({ accountID,status:true });
let tags = await db.collection('tags').find({ accountID,status:true }).limit(10).sort({ createdAt:-1 }).toArray();
tags = tags.map( x => x.tag );
res.send({ status:'ok',callstatus: ['已拨打','未拨打'], tags: tags.length ? tags : ['有意向','已经加微信','需回拨','未接通','无意向'], dspGroups, slots });
} }
}) })
}) })
...@@ -112,7 +117,7 @@ router.post('/job/call',function (req,res) { ...@@ -112,7 +117,7 @@ router.post('/job/call',function (req,res) {
else { else {
let db = getDB(rep); let db = getDB(rep);
let _host = getHost(rep); let _host = getHost(rep);
getCallConsumeByUnikey(unikey, db, (err, data) => { getCallConsume( db, 'call', (err, data) => {
if(err) return res.status(500).json({ error: err}); if(err) return res.status(500).json({ error: err});
if(data && data.number) { if(data && data.number) {
checkBill({ accountID: pubID, number: data.number }, db, async (_err) => { checkBill({ accountID: pubID, number: data.number }, db, async (_err) => {
...@@ -156,6 +161,64 @@ router.post('/job/call',function (req,res) { ...@@ -156,6 +161,64 @@ router.post('/job/call',function (req,res) {
}}); }});
}); });
// 电信绑定
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) => {
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;
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');
if(err) {
// log 代码;
console.dir(err);
}
console.dir(rep);
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: '运营商拒绝服务'});
});
}
}
});
} else {
res.status(500);
}
});
}
});
});
router.post('/job/dspcall',function (req,res) { router.post('/job/dspcall',function (req,res) {
const { pubID, groupID, phone, expiration = EXPIRATION, unikey } = req.body; const { pubID, groupID, phone, expiration = EXPIRATION, unikey } = req.body;
...@@ -165,7 +228,7 @@ router.post('/job/dspcall',function (req,res) { ...@@ -165,7 +228,7 @@ router.post('/job/dspcall',function (req,res) {
else { else {
let db = getDB(rep); let db = getDB(rep);
let _host = getHost(rep); let _host = getHost(rep);
getCallConsumeByUnikey(unikey, db, (err, data) => { getCallConsume( db, 'call', (err, data) => {
if(err) return res.status(500).json({ error: err}); if(err) return res.status(500).json({ error: err});
if(data && data.number) { if(data && data.number) {
checkBill({ accountID: pubID, number: data.number },db, async (_err) => { checkBill({ accountID: pubID, number: data.number },db, async (_err) => {
...@@ -237,6 +300,30 @@ router.get('/unbind',function (req,res) { ...@@ -237,6 +300,30 @@ router.get('/unbind',function (req,res) {
}); });
// 电信解绑
router.get('/ty/unbind',function (req,res) {
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 _host = getHost(rep);
const { caller, called, unikey } = req.query;
const host = _host + "/ty/unbind?caller=" + caller + '&called=' + called + '&unikey=' + unikey;
axios(host, {
method: "GET",
headers: { "Content-Type": "application/json" },
timeout: 300000
})
.then(rep => {
res.send({ status:'ok' });
})
.catch(err => {
console.dir(err);
if (err) return res.status(500).json({ error: '解绑错误' });
});
}
})
});
router.get('/dspUnbind',function (req,res) { router.get('/dspUnbind',function (req,res) {
let data = _.merge(req.query,req.body); let data = _.merge(req.query,req.body);
...@@ -290,7 +377,6 @@ router.get('/stats',function (req,res) { ...@@ -290,7 +377,6 @@ router.get('/stats',function (req,res) {
$group:{ $group:{
_id: { _id: {
date: "$date", date: "$date",
result: "$result"
}, },
duration: { $sum:"$duration" }, duration: { $sum:"$duration" },
count: { $sum: 1 } count: { $sum: 1 }
...@@ -309,28 +395,74 @@ router.get('/stats',function (req,res) { ...@@ -309,28 +395,74 @@ router.get('/stats',function (req,res) {
$group:{ $group:{
_id: { _id: {
date: "$date", date: "$date",
result: "$result"
}, },
duration: {$sum:"$duration"}, duration: {$sum:"$duration"},
count: { $sum: 1 } count: { $sum: 1 }
} }
}]).toArray(); }]).toArray();
let duration_stats = await db
.collection('callTask')
.aggregate([
{
$match:{
"caller" : phone,
"date": { $gte: start, $lte: end },
"duration": { $gt: 0 }
}
},
{
$group:{
_id: {
date: "$date",
},
count: { $sum: 1 }
}
}]).toArray();
let dsp_duration_stats = await db
.collection('dspCallTask')
.aggregate([
{
$match:{
"caller" : phone,
"date": { $gte: start, $lte: end },
"duration": { $gt: 0 }
}
},
{
$group:{
_id: {
date: "$date",
},
count: { $sum: 1 }
}
}]).toArray();
let _stats = stats.concat(dspstats); let _stats = stats.concat(dspstats);
let _duration_stats = duration_stats.concat(dsp_duration_stats);
_stats.forEach( x => { _stats.forEach( x => {
let date = x._id.date || 'unknown'; let date = x._id.date || 'unknown';
if(statistics[date]) { if(statistics[date]) {
statistics[date].count += x.count; statistics[date].count += x.count;
statistics[date].duration += x.duration; statistics[date].duration += x.duration;
statistics[date].pinged += (x._id.result ? x.count : 0); statistics[date].pinged = 0;
} else { } else {
statistics[date] = { statistics[date] = {
count: x.count, count: x.count,
duration: x.duration, duration: x.duration,
pinged: x._id.result ? x.count : 0, pinged: 0,
} }
} }
}); });
res.send({ status:'ok',totalstats: statistics ,stats,dspstats }); _duration_stats.forEach( x => {
let date = x._id.date || 'unknown';
if(statistics[date] && statistics[date].pinged) {
statistics[date].pinged += ( x.count || 0);
} else {
statistics[date].pinged = x.count || 0;
}
});
res.send({ status:'ok', statistics });
} }
}); });
}); });
...@@ -392,14 +524,15 @@ router.post("/recognitions",function(req,res) { ...@@ -392,14 +524,15 @@ router.post("/recognitions",function(req,res) {
let _slots = Array.isArray(slots) ? slots : [ slots ]; let _slots = Array.isArray(slots) ? slots : [ slots ];
qs.slotID = { $in: slots }; qs.slotID = { $in: slots };
} }
if( called ) { if( called === 'true' || called === true || called === 'false' || called === false ) {
qs.calledInfo = { $exists: called == 'true' } qs['calledInfo'] = { $exists: called === 'true' || called === true }
}
if(carrier === 'ctcc' || carrier === 'cmcc') {
qs.carrier = carrier
} else {
qs.carrier = { $nin: ['ctcc','cmcc'] }
} }
if(carrier) {
if(carrier === 'cucc') {
qs.carrier = { $nin: ['ctcc','cmcc'] }
} else
qs.carrier = carrier;
}
_.merge(qs, { 'tokenInfo.tokenID': OID(tokenID) , auditStatus: 2 } ); _.merge(qs, { 'tokenInfo.tokenID': OID(tokenID) , auditStatus: 2 } );
if(called === 'true' || called === true ) sort.calledTimestamp = -1; if(called === 'true' || called === true ) sort.calledTimestamp = -1;
else sort.updateTimestamp = -1; else sort.updateTimestamp = -1;
...@@ -439,17 +572,19 @@ router.post("/dsprecognitions",function(req,res) { ...@@ -439,17 +572,19 @@ router.post("/dsprecognitions",function(req,res) {
_groups = _groups.map( x => OID(x) ); _groups = _groups.map( x => OID(x) );
qs.groupID = { $in: _groups }; qs.groupID = { $in: _groups };
} }
if( called ) { if( called === 'true' || called === true || called === 'false' || called === false ) {
qs.calledInfo = { $exists: called == 'true' } qs['calledInfo'] = { $exists: called === 'true' || called === true }
} }
_.merge(qs, { 'tokenInfo.tokenID': OID(tokenID) } ); _.merge(qs, { 'tokenInfo.tokenID': OID(tokenID) } );
if(called === 'true' || called === true) sort.calledTimestamp = -1; if(called === 'true' || called === true) sort.calledTimestamp = -1;
else sort.createdAt = -1; else sort.createdAt = -1;
if(carrier === 'ctcc' || carrier === 'cmcc') { if(carrier) {
qs.carrier = carrier if(carrier === 'cucc') {
} else { qs.carrier = { $nin: ['ctcc','cmcc'] }
qs.carrier = { $nin: ['ctcc','cmcc'] } } else
qs.carrier = carrier;
} }
const count = await db.collection('dspRecognition').count(qs); const count = await db.collection('dspRecognition').count(qs);
db db
.collection('dspRecognition') .collection('dspRecognition')
...@@ -460,17 +595,6 @@ router.post("/dsprecognitions",function(req,res) { ...@@ -460,17 +595,6 @@ router.post("/dsprecognitions",function(req,res) {
.toArray(async (err, rep) => { .toArray(async (err, rep) => {
if (err) return res.status(500).json({ error: "数据查询失败" }); if (err) return res.status(500).json({ error: "数据查询失败" });
let _arrs = await getCallGroupInfo(rep, db); let _arrs = await getCallGroupInfo(rep, db);
_arrs.map( x => {
let body = {};
if(x.tags && x.tags.length)
x.tags.forEach( (x,i) => {
if(i === 1) body.city = x;
if(x.indexOf("岁") >= 0) body.age = x;
if(x.indexOf("男") >= 0) body.sex = '男';
if(x.indexOf("女") >= 0) body.sex = '女';
});
return _.merge(x,body);
});
res.send({ status: "ok", recognitions: _arrs, page: { skip: skip, total: count } }) res.send({ status: "ok", recognitions: _arrs, page: { skip: skip, total: count } })
}); });
} }
...@@ -561,9 +685,9 @@ function notEmpty(data) { ...@@ -561,9 +685,9 @@ function notEmpty(data) {
async function authorize(data, callback) { async function authorize(data, callback) {
let token = md5token(data.token); let token = md5token(data.token);
try { try {
let self_user = await db.collection("tokens").findOne({ phone: data.phone }); 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 }); 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 }); let xibao_user = await oem_db.xibao.collection("tokens").findOne({ phone: data.phone, removed: { $ne: true } });
if (self_user && self_user.passwd == token) { if (self_user && self_user.passwd == token) {
return callback(null, _.merge(self_user, {db: 'self'})); return callback(null, _.merge(self_user, {db: 'self'}));
} }
...@@ -610,6 +734,7 @@ async function checkBill(data, _db, callback) { ...@@ -610,6 +734,7 @@ async function checkBill(data, _db, callback) {
async function updateBill(data, _db, callback) { async function updateBill(data, _db, callback) {
if(!notEmpty(data)) return callback('params wrong'); if(!notEmpty(data)) return callback('params wrong');
let { pre, number, accountID, type, taskID, fromID = 'self', groupID, preNum = 30 } = data; let { pre, number, accountID, type, taskID, fromID = 'self', groupID, preNum = 30 } = data;
if(!/^[0-9a-z]{24}$/.test(accountID) ) return callback('params wrong'); if(!/^[0-9a-z]{24}$/.test(accountID) ) return callback('params wrong');
...@@ -665,9 +790,11 @@ async function getRechargeByAccount(accountID, _db) { ...@@ -665,9 +790,11 @@ async function getRechargeByAccount(accountID, _db) {
return ( (recharges && recharges.length) ? recharges[0].sum : 0 ); return ( (recharges && recharges.length) ? recharges[0].sum : 0 );
} }
async function getCallConsumeByUnikey(unikey,_db,callback) { async function getCallConsume(_db,key,callback) {
const price = _db.collection('price').findOne({type: 'call'}); const price = _db.collection('price').findOne({type: key || 'call'});
let number = (price && price.number) ? price.number : 1; 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 }); //const task = db.collection('callTask').findOne({ unikey });
callback(null, { number: Math.ceil((EXPIRATION / 60) ) * number }); callback(null, { number: Math.ceil((EXPIRATION / 60) ) * number });
} }
......
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