Commit 7dcc9b08 authored by 刘松's avatar 刘松

fix pickdate

parent 070c44af
......@@ -13,9 +13,9 @@ RUN \
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN npm i --only=production --registry https://registry.npm.taobao.org
ENV SERVICE_PORT=8080
ENV SERVICE_PORT=6662
ENV PROJECT_LEVEL=production
ENV MONGO='mongodb://mongo-adpro-ssp-v2-rs-1.localhost:1301/remarketing'
ENV MONGO='mongodb://mongo-adpro-ssp-v2-rs-1.localhost:1301/remarketing?replicaSet=adpro_ssp_v2_rs'
ENV NODE_ENV='production'
EXPOSE 8080
EXPOSE 6662
CMD node server.js
......@@ -10,7 +10,7 @@ const adminID = process.env.NODE_ENV === 'production' ? '5a9f9e6b46da1176a40e108
let db = {};
const dbpath = process.env.MONGO || "mongodb://localhost:27017/remarketing";
const salt = ",tom";
const EXPIRATION = 60 * 30;
// TODO ! put into init
MongoClient.connect(dbpath, (err, conn) => {
if (err) return console.log(err);
......@@ -21,29 +21,48 @@ MongoClient.connect(dbpath, (err, conn) => {
.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 * 24 });
});
router.post('/job/call',function (req,res) {
const { pubID, slotID, phone, expiration = 60 * 30, unikey } = req.body;
if(!notEmpty(req.body)) res.sendStatus(500);
else {
const host = "http://remarketing-job-yh.yoo.yunpro.cn/bind/" + pubID + '/' + slotID + '?caller=' + phone + '&unikey=' + unikey + '&expiration=' + expiration+ '&test=true';
axios(host, {
method: "GET",
headers: { "Content-Type": "application/json" },
timeout: 30000
})
.then(rep => {
if(rep.data && rep.data.called){
res.send({ status: "ok", called: rep.data.called });
}
else{
res.sendStatus(500);
const { pubID, slotID, phone, expiration = EXPIRATION, unikey } = req.body;
getCallConsumeByUnikey(unikey, (err, data) => {
if(err) 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);
else {
const host = "http://remarketing-job-yh.yoo.yunpro.cn/bind/" + pubID + '/' + slotID + '?caller=' + phone + '&unikey=' + unikey + '&expiration=' + expiration;
axios(host, {
method: "GET",
headers: { "Content-Type": "application/json" },
timeout: 300000
})
.then(rep => {
console.dir(rep.data);
if(rep.data && rep.data.called && rep.data._id){
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 })
});
}
else{
res.status(500).json({ error: '取号失败'});
}
})
.catch(err => {
if (err) return res.sendStatus(500);
});
}
}
})
.catch(err => {
if (err) return res.sendStatus(500);
});
} else {
res.status(500);
}
});
});
router.post('/login',async function (req,res) {
......@@ -67,6 +86,7 @@ router.post('/login',async function (req,res) {
if (err || !rep) return res.sendStatus(500);
const token = _.merge(rep, { sessionID: req.body.sessionID });
delete token.token;
delete token.passwd;
res.send({ status: "ok", token });
});
}
......@@ -89,11 +109,11 @@ router.get("/recognitions",function(req,res) {
if (err || !rep) return res.sendStatus(500);
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')) }, tokenID };
_.merge(qs, (called === 'true' ? { called:true } : { called: { $ne: true} }));
const count = await db.collection('numbers').count(qs);
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' } } );
const count = await db.collection('recognition').count(qs);
db
.collection('numbers')
.collection('recognition')
.find(qs)
.sort({ updateTimestamp: -1 })
.skip(parseInt(skip * limit))
......@@ -175,7 +195,7 @@ function notEmpty(data) {
function authorize(data, callback) {
db.collection("tokens").findOne({ phone: data.phone }, (err, rep) => {
if (err || !rep) return callback(err, null);
if (md5token(data.token) !== rep.token)
if (md5token(data.token) !== rep.passwd)
return callback("password wrong", null);
callback(null, rep);
});
......@@ -197,8 +217,86 @@ function genSessionID(tokenID) {
}
async function checkBill(data, callback) {
if(!notEmpty(data)) return callback('参数错误');
let { number, accountID } = data;
if(!/^[0-9a-z]{24}$/.test(accountID) ) return callback('参数错误');
let recharge = await getRechargeByAccount( accountID );
if( recharge <= 0 ) { return callback('余额不足') }
let consume = await getBillByAccount( accountID );
if( consume + number > recharge ) { return callback('余额不足') }
callback && callback();
}
async function updateBill(data, callback) {
if(!notEmpty(data)) return callback('params wrong');
let { pre, number, accountID, type, taskID } = data;
if(!/^[0-9a-z]{24}$/.test(accountID) ) return callback('参数错误');
db
.collection('bills')
.insert(wrapTime({ pre, number, accountID: OID(accountID), type, taskID: OID(taskID) }), (err, rep) => {
if (err) return callback(err);
callback && callback(null, rep);
});
}
async function getBillByAccount( accountID ) {
let consumes = await db
.collection('bills')
.aggregate([
{
$match:{
"accountID": OID(accountID),
"removed": { $ne: true }
/* "createdAt": { '$gt': start, '$lte': end }*/
}
},
{
$group:{
_id: null,
sum: {$sum:"$number"}
}
}]).toArray();
return ( (consumes && consumes.length) ? consumes[0].sum : 0 );
}
async function getRechargeByAccount(accountID) {
let recharges = await db
.collection('recharge')
.aggregate([
{
$match:{
"accountID": OID(accountID),
"removed": { $ne: true }
}
},
{
$group:{
_id:null,
sum:{$sum:"$number"}
}
}]).toArray();
return ( (recharges && recharges.length) ? recharges[0].sum : 0 );
}
async function getCallConsumeByUnikey(unikey,callback) {
const price = db.collection('price').findOne({type: 'call'});
let number = (price && price.number) ? price.number : 1;
const task = db.collection('callTask').findOne({ unikey });
if(!task) {
callback(null, { number: 3 + Math.ceil((EXPIRATION / 60) -1) * number });
} else {
callback(null, { number: Math.ceil((EXPIRATION / 60) ) * number });
}
}
function OID(str) {
return typeof str === 'string' ? mongodb.ObjectID(str) : str;
}
function wrapTime(obj) {
return _.merge(obj, { createdAt: new Date() })
}
module.exports = router;
......@@ -17,6 +17,6 @@ app.use(express.static(__dirname + '/dist'));
app.use('/api', api);
server.listen(8081, function() {
server.listen(6662, function() {
console.log('server started');
});
This diff is collapsed.
......@@ -2,6 +2,7 @@
import * as _ from 'lodash';
import testDatas from '@/tests/consumers';
import moment from 'moment';
import { Toast } from 'mint-ui';
const types = {
GET_RECOGNITIONS: 'GET_RECOGNITIONS',
......@@ -15,8 +16,14 @@ const state = {
numbers: [],
calledNumbers: [],
numbersLoading: false,
total: 0,
totalCalled: 0,
page: {
skip: 0,
total: 0,
},
pageCalled: {
skip: 0,
total: 0,
}
};
const getters = {
......@@ -24,20 +31,28 @@ const getters = {
return formatNumbers(state.numbers).length;
},
getConsumers() {
console.dir(state.numbers);
return state.numbers;
},
getCalledConsumers() {
console.dir(state.calledNumbers)
return state.calledNumbers;
},
getConsumersLoading() {
return state.numbersLoading;
},
allLoaded() {
return state.numbers.length == state.total;
return state.numbers.length == state.page.total;
},
allCalledLoaded() {
return state.calledNumbers.length == state.totalCalled;
return state.calledNumbers.length == state.pageCalled.total;
},
getPage() {
return state.page;
},
getPageCalled() {
return state.pageCalled;
}
};
const actions = {
......@@ -50,12 +65,11 @@ const actions = {
}
return Promise.reject(res.status);
}).then((data) => {
callback();
commit(types.NUMBERS_LOADING, false);
commit(types.GET_NUMBERS, _.merge(data,{ refresh,called }));
callback(null,data);
commit(types.GET_NUMBERS, _.merge(data,{ refresh, called: (!called || called == false ? false : true) }));
}).catch((err) => {
commit(types.NUMBERS_LOADING, false);
console.log(err);
callback(err);
});
},
......@@ -65,18 +79,17 @@ const actions = {
headers: {
'Content-Type': 'application/json;charset=UTF-8',
},
timeout: 30000,
timeout: 300000,
body: JSON.stringify({ slotID, pubID, unikey, phone, expiration }),
}).then((res) => {
}).then(async (res) => {
if (res.ok) {
return res.json();
}
return Promise.reject(res.status);
return Promise.reject(await res.json());
}).then((data) => {
callback(null, data);
}).catch((err) => {
console.log(err);
callback(err);
}).catch((data) => {
callback(data.error ? data.error : '未知错误');
});
},
};
......@@ -90,10 +103,10 @@ const mutations = {
return x;
});
if(data.called) {
state.totalCalled = data.page.total;
state.pageCalled = _.cloneDeep(data.page);
state.calledNumbers = (data.refresh ? data.recognitions : data.recognitions.concat(state.calledNumbers));
} else {
state.total = data.page.total;
state.page = _.cloneDeep(data.page);
state.numbers = (data.refresh ? data.recognitions : data.recognitions.concat(state.numbers));
}
},
......
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