feat: 充值查询+刷新

This commit is contained in:
chaosBreaking
2019-09-08 17:24:36 +08:00
parent 8edcc85903
commit 383e24fbfa
9 changed files with 173 additions and 40 deletions

View File

@@ -10,6 +10,8 @@ const actionFilter = 'id type amount detail op createdAt';
const { inject } = require('../../common/Provider');
const ETH = require('../../utils/erc20').ERC20;
const { USDTAddr } = require('../../common/constant');
const { calRevenueLastday, getActiveFund } = require('./fund.handler');
const { getDate } = require('../../utils/date');
const depositCoinsList = [{
id: 'usdtBTC',
@@ -191,14 +193,40 @@ module.exports = {
async refreshFund (req, res) {
// todo: 要求刷新程序去刷新
const { userId } = res.locals.user;
const { inviter } = req.query;
const fund = await Fund.findOne({ userId }).exec();
const tokenAddress = fund && fund.tokenAddress;
if(!tokenAddress || !tokenAddress['usdtETH']) return res.json({
code: 0,
data: fund.fundList
});
const txActions = await ETH.getActions(tokenAddress['usdtETH'], USDTAddr);
return this.getFundList(req, res);
const { hasNew, list } = fund.hasNewDeposit();
if (!hasNew) return res.json({
code: 0,
msg: '刷新成功',
data: {}
});
const [ start, end ] = getDate(1);
const [newFund, actions] = await Promise.all([
fund.addFund(list, { inviter }),
Action.find({ userId, type: 1, createdAt: { $gt: start, $lt: end } })
]);
// 计算昨日收益
let revenueLastday = calRevenueLastday(actions);
// 筛选有效矿晶
let activeFund = getActiveFund(fund.fundList);
return res.json({
code: 0,
msg: '刷新成功',
data: {
list: newFund.fundList,
basic: {
activeFund,
revenueLastday,
revenueAll: newFund.fundSum
}
}
});
},
async getFundList (req, res) {
const { userId } = res.locals.user;
@@ -218,22 +246,12 @@ module.exports = {
async getBasicInfo (req, res) {
const { userId } = res.locals.user;
return Fund.findOne({userId}).then(async fund => {
const start = ''; // 前天
const end = ''; // 昨天
const [ start, end ] = getDate(1);
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--) {
actions[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];
fundItem && (activeFund += fundItem.status === 1 ? +fundItem.amount : 0);
}
let revenueLastday = calRevenueLastday(actions);
// 筛选有效矿晶
let activeFund = getActiveFund(fund.fundList);
return res.json({
code: 0,
data: {

View File

@@ -7,13 +7,16 @@ const DayLong = 24 * 60 * 60 * 1000;
* 2 不可用,已耗尽
*/
class Fund {
constructor (amount) {
constructor (data) {
const { amount, blockNumber, hash } = data;
this.amount = +amount;
this.status = 0;
this.blockNumber = blockNumber;
this.blockHash = hash;
const now = new Date();
const exp = calExpirePeriod(amount);
this.createdAt = now.toISOString();
this.expiresAt = new Date(now.getTime() + exp * DayLong);
this.status = 0;
}
}
const calExpirePeriod = amount => {
@@ -37,7 +40,24 @@ const calExpirePeriod = amount => {
};
module.exports = {
createNewFund (amount = 0) {
return new Fund(amount);
createNewFund (txData) {
return new Fund(txData);
},
calRevenueLastday (actionList = []) {
let revenueLastday = 0;
if (actionList && actionList.length > 0) {
for (let i = actionList.length; i >= 0; i--) {
actionList[i] && (revenueLastday += actionList[i] && +actionList[i].amount || 0);
}
}
return revenueLastday;
},
getActiveFund (fundList) {
let activeFund = 0;
for (let i = fundList.length; i >= 0; i--) {
const fundItem = fundList[i];
fundItem && (activeFund += fundItem.status === 1 ? +fundItem.amount : 0);
}
return activeFund;
}
};

View File

@@ -4,6 +4,7 @@ const mongoose = require('mongoose');
const ASSETTYPES = ['gin', 'vic', 'usdtBTC', 'usdtETH', 'usdtTRX'];
const { createAction } = require('../action/action.handler');
const { createNewFund } = require('./fund.handler');
const { getNewDepositTx, getUSDTTxList } = require('../../common/deposit');
const FundSchema = new mongoose.Schema({
userId: {
@@ -61,8 +62,20 @@ const FundSchema = new mongoose.Schema({
const FundModel = mongoose.model('Fund', FundSchema);
FundModel.prototype.hasNewDeposit = async function () {
if (this.tokenAddress.usdtETH) {
const usdtTx = await getUSDTTxList(this.tokenAddress.usdtETH);
const fundList = this.fundList;
const newDepositList = getNewDepositTx(usdtTx, fundList);
if (newDepositList && newDepositList.length > 0)
return { hasNew: true, list: newDepositList };
}
return { hasNew: false };
};
/**
[incFund] : 向用户添加矿晶在用户充值USDT时调用
[addFund] : 向用户添加矿晶在用户充值USDT时调用
* 当一个用户充值,
• 4% 给上面第一个 金 或 银 或 铜 会员
• 3%
@@ -70,34 +83,47 @@ const FundModel = mongoose.model('Fund', FundSchema);
• 如果直接上家不是金,就平分 3% 给上线所有银会员
• 2% 给上线所有金会员平分
*/
FundModel.prototype.incFund = async function (amount, option = {}) {
if (!amount || !Number.isFinite(amount) || amount <= 0) return null;
FundModel.prototype.addFund = async function (txlist = [], option) {
let newSum = 0;
const actionPromiseList = [];
const newFundList = txlist.map(txObj => {
newSum += +txObj.amount;
const action = createAction(0, {
userId: this.userId,
amount: txObj.amount,
tokenType: 'vic', // 目前设定只能挖出矿晶vic
remain: this.asset
});
actionPromiseList.push(action.save());
return createNewFund(txObj);
});
// 1.注入资金
const newFund = createNewFund(amount);
const res = await FundModel.findByIdAndUpdate(this.id, {
$push: {
fundList: newFund
fundList: newFundList
},
$inc: {
fundSum: amount
fundSum: newSum
},
$set: {
lastDeposit: new Date().toISOString()
}
// eslint-disable-next-line no-unused-vars
}, { ...option, returnOriginal: false }).exec().catch(err => null);
// 2.记录action
const action = createAction(0, {
userId: this.userId,
amount,
tokenType: 'vic', // 目前设定只能挖出矿晶vic
remain: this.asset
});
await action.save().catch(err => {
Promise.all(actionPromiseList).catch(err => {
throw new Error('充值行为记录失败', err.errmsg);
});
// 3.奖励团队
// 3.增加上家团队总额
FundModel.findOneAndUpdate({ userId: option.inviter }, {
$inc: {
groupFundSum: newSum
}
});
// 4.奖励团队
// todo
return res;
};
FundModel.prototype.increase = async function (assetName, amount, option = {}) {

View File

@@ -45,7 +45,7 @@ const services = [
},
{
indexRoute: '/mine',
url: '/refreshFund',
url: '/refresh',
method: 'GET',
controller: controller.refreshFund
},

View File

@@ -8,7 +8,7 @@ module.exports = {
changePwd,
async getUserInfo (req, res) {
const { userId } = res.locals.user;
return User.findById(userId, 'id nickname phone inviteCode').then(data => {
return User.findById(userId, 'id nickname phone inviteCode inviter').then(data => {
res.json({
code: 0,
msg: 'success',