Commit c2442c60 authored by 刘松's avatar 刘松

fix

parent 7dcc9b08
### Health check 'curl /ping'
FROM node:latest
FROM reg.yunpro.cn/library/node:9.11.1
WORKDIR /app
ADD ./package.json /app/
#front end
......
......@@ -8,7 +8,7 @@ const moment = require('moment');
const adminID = process.env.NODE_ENV === 'production' ? '5a9f9e6b46da1176a40e1082' : '5ab083b1f6134d82b40d95f2';
let db = {};
const dbpath = process.env.MONGO || "mongodb://localhost:27017/remarketing";
const dbpath = process.env.MONGO || "mongodb://127.0.0.1:27017/remarketing";
const salt = ",tom";
const EXPIRATION = 60 * 30;
// TODO ! put into init
......@@ -24,14 +24,13 @@ MongoClient.connect(dbpath, (err, conn) => {
router.post('/job/call',function (req,res) {
const { pubID, slotID, phone, expiration = EXPIRATION, unikey } = req.body;
getCallConsumeByUnikey(unikey, (err, data) => {
if(err) res.status(500).json({ error: err});
if(err) return res.status(500).json({ error: err});
if(data && data.number) {
console.dir(data)
checkBill({ accountID: pubID, number: data.number },(_err) => {
if(_err) {
return res.status(500).json({ error: _err});
} else {
if(!notEmpty(req.body)) res.sendStatus(500);
if(!notEmpty(req.body)) res.status(500).json({ error: "参数错误"});
else {
const host = "http://remarketing-job-yh.yoo.yunpro.cn/bind/" + pubID + '/' + slotID + '?caller=' + phone + '&unikey=' + unikey + '&expiration=' + expiration;
axios(host, {
......@@ -39,14 +38,15 @@ router.post('/job/call',function (req,res) {
headers: { "Content-Type": "application/json" },
timeout: 300000
})
.then(rep => {
console.dir(rep.data);
if(rep.data && rep.data.called && rep.data._id){
.then(async rep => {
console.dir(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, type: 'call', taskID: rep.data._id}, (err,_rep) => {
if(err) {
// log 代码;
}
res.send({ status: 'ok', called: rep.data.called })
res.send({ status: 'ok', called: rep.data.called, bindID: rep.data.bindID });
});
}
else{
......@@ -54,7 +54,8 @@ router.post('/job/call',function (req,res) {
}
})
.catch(err => {
if (err) return res.sendStatus(500);
console.dir(err)
if (err) return res.status(500).json({ error: '参数错误,取号失败'});
});
}
}
......@@ -65,15 +66,34 @@ router.post('/job/call',function (req,res) {
});
});
router.get('/unbind',function (req,res) {
const { bindID } = req.query;
const host = "http://remarketing-job-yh.yoo.yunpro.cn/unbind?bindID=" + bindID;
axios(host, {
method: "GET",
headers: { "Content-Type": "application/json" },
timeout: 300000
})
.then(rep => {
console.dir(rep);
res.send({status:'ok', data: rep});
})
.catch(err => {
console.dir(err);
if (err) return res.status(500).json({ error: '解绑错误'});
});
});
router.post('/login',async function (req,res) {
//token phone sessionID
checkSession(req.body, (err, rep) => {
if (err) return res.sendStatus(500);
if (err) return res.status(500).json({ error: "查询失败" });
if (!rep) {
//没有符合的session
return authorize(req.body, (err, rep) => {
if (err) return res.sendStatus(500);
if (!rep) return res.sendStatus(403);
if (err) return res.status(500).json({ error: "查询账户失败" });
if (!rep) return res.status(403).json({ error: "账户不存在" });
//验证通过
const token = _.merge(rep, { sessionID: genSessionID(rep._id) });
delete token.token;
......@@ -83,7 +103,7 @@ router.post('/login',async function (req,res) {
db
.collection("tokens")
.findOne({ _id: OID(rep.tokenID) }, (err, rep) => {
if (err || !rep) return res.sendStatus(500);
if (err || !rep) return res.status(500).json({ error: "查询账户失败"});
const token = _.merge(rep, { sessionID: req.body.sessionID });
delete token.token;
delete token.passwd;
......@@ -98,19 +118,24 @@ router.post("/logout",function(req,res) {
db
.collection('tokenSession')
.remove({ _id: OID(sessionID) }, (err, rep) => {
if (err || !rep) return res.sendStatus(500);
if (err || !rep) return res.status(500).json({ error: "更新session新失败"});
res.send({ status: "ok", rep });
});
});
router.get("/recognitions",function(req,res) {
let { sessionID, limit = 10, skip = 0, date, called = 'false' } = req.query;
let { sessionID, limit = 10, skip = 0, date = null, start, end, called = 'false' } = req.query;
checkSession(req.query, async (err, rep) => {
if (err || !rep) return res.sendStatus(500);
if (err || !rep) return res.status(500).json({ error: "session错误,重新登录", code: 302 });
else {
const tokenID = rep.tokenID;
let qs = { updateTimestamp: { '$gt': parseInt(moment(date, 'YYYYMMDD').startOf('day').format('x')), '$lte': parseInt(moment(date, 'YYYYMMDD').endOf('day').format('x')) }, 'tokenInfo.tokenID': OID(tokenID) };
_.merge(qs, { calledInfo: { $exists: called == 'true' } } );
if(date) {
start = (end = date);
}
let qs = getTimeRange({start, end, key:'updateTimestamp', type: 'millisecond'});
//let qs = { updateTimestamp: { '$gt': parseInt(moment(date, 'YYYYMMDD').startOf('day').format('x')), '$lte': parseInt(moment(date, 'YYYYMMDD').endOf('day').format('x')) }, 'tokenInfo.tokenID': OID(tokenID) };
_.merge(qs, { calledInfo: { $exists: called == 'true' } , 'tokenInfo.tokenID': OID(tokenID) } );
console.dir(qs);
const count = await db.collection('recognition').count(qs);
db
.collection('recognition')
......@@ -119,9 +144,10 @@ router.get("/recognitions",function(req,res) {
.skip(parseInt(skip * limit))
.limit(parseInt(limit))
.toArray(async (err, rep) => {
if (err) return res.sendStatus(500);
if (err) return res.status(500).json({ error: "数据查询失败" });
const arrs = await getStars(rep);
const _arrs = await getSlots(arrs);
let _arrs = await getSlots(arrs);
if(called == 'true') _arrs = await getTasks(_arrs);
res.send({ status: "ok", recognitions: _arrs, page: { skip: skip, total: count } })
});
}
......@@ -168,7 +194,7 @@ function md5token(str) {
}
function checkSession(data, callback) {
if (!data.sessionID) return callback(null);
if (!data.sessionID || !/^[0-9a-z]{24}$/.test(data.sessionID)) return callback(null);
db
.collection("tokenSession")
.findOne({ sessionID: OID(data.sessionID) }, (err, rep) => {
......@@ -291,6 +317,31 @@ async function getCallConsumeByUnikey(unikey,callback) {
}
}
async function getTasks(arrays) {
let _tasks = [];
arrays.forEach((x) => {
_tasks.push(new Promise(async (r,e) => {
if(x.calledInfo && x.calledInfo.length) {
let tasks = [];
x.calledInfo.forEach((y) => {
tasks.push(new Promise(async (r,e) => {
const TaskInfo = (y.taskID && exsists(y.taskID)) ? await db
.collection("callTask")
.findOne({ _id: OID(y.taskID) },{ startTime : 1 }) : { startTime: -1 };
r(_.merge( y, { TaskInfo } ));
}));
});
const arrs = await Promise.all(tasks);
x.calledInfo = arrs;
}
r(x);
}));
});
const _arr = await Promise.all(_tasks);
return _arr;
}
function OID(str) {
return typeof str === 'string' ? mongodb.ObjectID(str) : str;
}
......@@ -299,4 +350,25 @@ function wrapTime(obj) {
return _.merge(obj, { createdAt: new Date() })
}
async function getFromID(accountID) {
const account = await db.collection('account').findOne({ _id: OID(accountID) });
return (account && account.fromID ? account.fromID : 'self');
}
function getTimeRange({ start, end, type, key}) {
if(/^[0-9]{8}$/.test(start) && /^[0-9]{8}$/.test(end) ) {
if(type === 'millisecond') {
return { [ key ]: { '$gt': parseInt(moment(start,'YYYYMMDD').startOf('day').format('x')), '$lte': parseInt(moment(end,'YYYYMMDD').endOf('day').format('x')) } };
} else {
return { [ key ]: { '$gt': moment(start,'YYYYMMDD').startOf('day').toDate(), '$lte': moment(end,'YYYYMMDD').endOf('day').toDate() } };
}
} else {
if(type === 'millisecond') {
return { [ key ]: { '$gt': parseInt(moment().add(-7,'days').startOf('day').format('x')), '$lte': parseInt(moment().endOf('day').format('x')) } };
} else {
return { [ key ]: { '$gt': moment().add(-7,'days').startOf('day').toDate(), '$lte': moment().endOf('day').toDate() } };
}
}
}
module.exports = router;
......@@ -23,7 +23,7 @@ export default {
methods: {
onSubmit() {
const { phone, token } = this.form;
if(/[0-9]{11}$/.test(phone) && /[a-z0-9]{9}$/.test(token)) {
if(/[0-9]{11}$/.test(phone) && /[a-z0-9]{1,}$/.test(token)) {
this.login({ phone, token, callback: this.reqCallback });
} else {
Toast({
......
......@@ -44,11 +44,12 @@
<div class="dia-wrap">
<div class="inner">
<h3><pre><i class="fa fa-user-times fa-lg"></i>{{ " 来源:" + item.slot.slotName }}</pre></h3>
<mt-button type="default" size="small" disabled>拨打</mt-button>
<mt-button type="primary" size="small" @click="call(item)">一键拨打</mt-button>
</div>
<div class="inner-bottom">
<span><pre>{{ "拨打时间:" + item.updateCalledTimestamp }}</pre></span>
<span><pre>{{ "营销指数:" + (item.score.score < 0 ? 0 : item.score.score) }}</pre></span>
<span><pre>{{ "最近:" + item.startTime }}</pre></span>
<span><pre>{{ "次数:" + item.times }}</pre></span>
<span><pre>{{ "指数:" + (item.score.score < 0 ? 0 : item.score.score) }}</pre></span>
</div>
</div>
</div>
......@@ -261,7 +262,7 @@ export default {
return this.$store.state.session.currentUser.role === 1;
},
startDate () {
return moment().add(-30,'days').toDate();
return moment().add(-100,'days').toDate();
},
endDate () {
return moment().toDate();
......@@ -306,8 +307,8 @@ export default {
closeOnClickModal:false,
popupVisible: false,
selected: 'tasks',
currentDate: '20180130',
currentDateCalled: '20180130', //moment().format('YYYYMMDD')
currentDate: moment().format('YYYYMMDD'),
currentDateCalled: moment().format('YYYYMMDD'), //moment().format('YYYYMMDD')
target: '',
};
},
......
......@@ -36,7 +36,15 @@ const getters = {
},
getCalledConsumers() {
console.dir(state.calledNumbers)
return state.calledNumbers;
return state.calledNumbers.map(x => {
if( x.calledInfo && x.calledInfo.length) {
const last = x.calledInfo[x.calledInfo.length - 1 ];
console.dir(last);
x.startTime = last.TaskInfo && last.TaskInfo.startTime > 0 ? moment(last.TaskInfo.startTime,'x').format('YYYY/MM/DD HH:mm') : '未知';
}
x.times = x.calledInfo.length;
return x;
});
},
getConsumersLoading() {
return state.numbersLoading;
......
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