fix: fund模型修改和创建api

This commit is contained in:
chaosBreaking
2019-09-07 12:01:57 +08:00
parent 8a493f3b10
commit 54674dd060
6 changed files with 276 additions and 104 deletions

View File

@@ -2,6 +2,7 @@
const User = require('../user/user.model');
const Action = require('../action/action.model');
const Fund = require('./fund.model');
const { createAction } = require('../action/action.handler');
const { createTicket } = require('../ticket/ticket.handler');
const { deriveNewAccount } = require('../../utils/account');
@@ -11,27 +12,25 @@ const { inject } = require('../../common/Provider');
module.exports = {
async getFundDetail (req, res) {
const { userId } = res.locals.user;
const user = await User.findById(userId, 'asset').exec().catch(err => {
mylog.error("[getFundDetail]", err);
return null;
});
if (user && user.asset) {
return Fund.findOne({ userId }, 'asset').then(fund => {
return res.json({
code: 0,
msg: 'success',
data: user.asset
data: fund
});
}).catch(err => {
mylog.error("[getFundDetail]", err);
return res.json({
code: 1,
error: err.errmsg
});
}
return res.json({
code: 1,
error: 'unknow user'
});
},
async getActions (req, res) {
const { userId } = res.locals.user;
const { pageIndex, pageSize } = req.query;
const { pageIndex = 0, pageSize = 0 } = req.query;
return await Action.find({
actor: userId
userId
}, actionFilter).skip((pageIndex - 1) * pageSize).limit(pageSize)
.exec().then(data => {
return res.json({
@@ -42,7 +41,7 @@ module.exports = {
.catch(err => {
return res.json({
code: 1,
error: JSON.stringify(err)
error: err.errmsg
});
});
},
@@ -55,12 +54,26 @@ module.exports = {
});
}).catch(err => res.json({
code: 1,
error: JSON.stringify(err)
error: err.errmsg
}));
},
async getSupportCoinsList (req, res) {
// eslint-disable-next-line no-undef
const coinList = await Provider.getModule('System').getValue('supportCoins');
let coinList;
try {
// eslint-disable-next-line no-undef
coinList = await Provider.getModule('System').getValue('supportCoins');
} catch (error) {
coinList = [{
id: 'vic',
label: 'vic',
fee: 0.005
}, {
id: 'usdt',
label: 'usdt',
fee: 0.005
}];
}
return res.json({
code: 0,
data: coinList
@@ -73,20 +86,33 @@ module.exports = {
* 3.返回状态
*/
const { userId } = res.locals.user;
const { amount } = req.body;
const { coinId, targetAddress, amount, password } = req.body;
// 暂且硬编码支持币种!!!!
if (coinId !== 'usdt' && coinId !== 'vic') return res.json({
code: 4,
msg: '不支持的提现币种',
error: '不支持的提现币种',
});
const user = await User.findById(userId).exec();
if (user.asset && user.asset.log < amount) return res.json({
if (user.fundPwd !== password) return res.json({
code: 1,
msg: '资金密码错误',
error: '资金密码错误',
});
const fund = await Fund.findOne({ userId }).exec();
if (fund.asset && fund.asset[coinId] < amount * 1.005 ) return res.json({
code: 2,
msg: '资金不足',
error: 'insufficient fund'
});
const action = createAction(1, { actor: userId, amount, remain: user.asset, op: 0 });
const ticket = createTicket('withdraw', { userId, amount, action });
const action = createAction(1, { userId, amount, remain: fund.asset, op: 0, detail: { targetAddress } });
const ticket = createTicket('withdraw', { userId, amount, action, targetAddress });
try {
await user.decrease('log', +amount);
await fund.decrease(coinId, +amount);
} catch (error) {
mylog.error('[Fund提现] ', error);
return res.json({
code: 2,
code: 3,
msg: '扣款失败',
error: JSON.stringify(error)
});
@@ -109,8 +135,8 @@ module.exports = {
if (!ROOTSECWORD) throw new Error('ROOTSECWORD is required');
return async (req, res) => {
const { userId } = res.locals.user;
const user = await User.findById(userId).exec();
let data = user.tokenAddress;
const userFund = await Fund.findOne({userId}).exec();
let data = userFund.tokenAddress;
if (!data.usdtBTC || !data.usdtETH) {
const seed = parseInt(userId.substring(0, 8), 16).toString().slice(4); //前4位与Date.now前4位重复;
const btc = deriveNewAccount(ROOTSECWORD, { coin: 'BTC', seed });
@@ -136,6 +162,62 @@ module.exports = {
error: res
});
};
})
}),
// 查询资金,直接拉数据库,区块链的查询交给定时任务
async refreshFund (req, res) {
// todo: 要求刷新程序去刷新
return this.getFundList(req, res);
},
async getFundList (req, res) {
const { userId } = res.locals.user;
return Fund.findOne({userId}, 'fundList').then(list => {
return res.json({
code: 0,
data: list
});
}).catch(err => {
mylog.error(err);
res.json({
code: 500,
msg: '系统错误'
});
});
},
async getBasicInfo (req, res) {
const { userId } = res.locals.user;
return Fund.findOne({userId}).then(async fund => {
const start = ''; // 前天
const end = ''; // 昨天
const actions = await Action.find({ userId, type: 1, createdAt: { $gt: start, $lt: end } }).exec();
// 计算昨日收益
let revenueLastday = 0;
if (actions && actions.length > 0) {
for (let i = actions.length; i >= 0; i--) {
revenueLastday += actions[i] && +actions[i].amount || 0;
}
}
// 筛选有效矿晶
let activeFund = 0;
for (let i = fund.fundList.length; i >= 0; i--) {
const fundItem = fund.fundList[i];
activeFund += fundItem.status === 1 ? +fundItem.amount : 0;
}
return res.json({
code: 0,
data: {
activeFund,
revenueLastday,
revenueAll: fund.asset && fund.asset['vic']
}
});
}).catch(err => {
mylog.error(err);
res.json({
code: 500,
msg: '系统错误'
});
});
},
};