This commit is contained in:
chaosBreaking
2019-08-31 14:59:13 +08:00
commit dc5423d356
65 changed files with 3813 additions and 0 deletions

View File

@@ -0,0 +1,141 @@
'use strict';
const User = require('../user/user.model');
const Action = require('../action/action.model');
const { createAction } = require('../action/action.handler');
const { createTicket } = require('../ticket/ticket.handler');
const { deriveNewAccount } = require('../../utils/account');
const actionFilter = 'id type amount detail op createdAt';
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 res.json({
code: 0,
msg: 'success',
data: user.asset
});
}
return res.json({
code: 1,
error: 'unknow user'
});
},
async getActions (req, res) {
const { userId } = res.locals.user;
const { pageIndex, pageSize } = req.query;
return await Action.find({
actor: userId
}, actionFilter).skip((pageIndex - 1) * pageSize).limit(pageSize)
.exec().then(data => {
return res.json({
code: 0,
data
});
})
.catch(err => {
return res.json({
code: 1,
error: JSON.stringify(err)
});
});
},
async getActionDetail (req, res) {
const { id } = req.query;
return await Action.findById(id, actionFilter).exec().then(data => {
res.json({
code: 0,
data
});
}).catch(err => res.json({
code: 1,
error: JSON.stringify(err)
}));
},
async getSupportCoinsList (req, res) {
// eslint-disable-next-line no-undef
const coinList = await Provider.getModule('System').getValue('supportCoins');
return res.json({
code: 0,
data: coinList
});
},
async withdraw (req, res) {
/**
* 1.创建工单
* 2.创建用户action
* 3.返回状态
*/
const { userId } = res.locals.user;
const { amount } = req.body;
const user = await User.findById(userId).exec();
if (user.asset && user.asset.log < amount) return res.json({
code: 1,
error: 'insufficient fund'
});
const action = createAction(1, { actor: userId, amount, remain: user.asset, op: 0 });
const ticket = createTicket('withdraw', { userId, amount, action });
try {
await user.decrease('log', +amount);
} catch (error) {
mylog.error('[Fund提现] ', error);
return res.json({
code: 2,
msg: '扣款失败',
error: JSON.stringify(error)
});
}
Promise.all([ticket.save(), action.save()]).then(() => {
return res.json({
code: 0,
msg: '工单已创建'
});
}).catch(err => {
return res.json({
code: 3,
msg: '工单创建失败',
error: JSON.stringify(err)
});
});
},
getDepositAddress: inject(function (SysConfig) {
const { ROOTSECWORD } = SysConfig;
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;
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 });
const eth = deriveNewAccount(ROOTSECWORD, { coin: 'ETH', seed });
const user = await User.findByIdAndUpdate(userId, {
$set: {
tokenAddress: {
usdtBTC: btc,
usdtETH: eth
}
}
}, { returnOriginal: false }).exec().catch(err => {
mylog.error(err);
return null;
});
data = user && user.tokenAddress;
}
return data ? res.json({
code: 0,
data
}) : res.json({
code: 1,
error: res
});
};
})
};