init
This commit is contained in:
141
src/modules/fund/fund.controller.js
Normal file
141
src/modules/fund/fund.controller.js
Normal 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
|
||||
});
|
||||
};
|
||||
})
|
||||
|
||||
};
|
||||
36
src/modules/fund/fund.service.js
Normal file
36
src/modules/fund/fund.service.js
Normal file
@@ -0,0 +1,36 @@
|
||||
'use strict';
|
||||
const indexRoute = '/fund';
|
||||
const controller = require('./fund.controller');
|
||||
const services = [
|
||||
{
|
||||
url: '/detail',
|
||||
method: 'GET',
|
||||
controller: controller.getFundDetail
|
||||
},
|
||||
{
|
||||
url: '/actions',
|
||||
method: 'GET',
|
||||
controller: controller.getActions
|
||||
},
|
||||
{
|
||||
url: '/actionDetail',
|
||||
method: 'GET',
|
||||
controller: controller.getActionDetail
|
||||
},
|
||||
{
|
||||
url: '/coins',
|
||||
method: 'GET',
|
||||
controller: controller.getSupportCoinsList
|
||||
},
|
||||
{
|
||||
url: '/queryAddress',
|
||||
method: 'GET',
|
||||
controller: controller.getDepositAddress
|
||||
},
|
||||
];
|
||||
|
||||
module.exports = services.map(service => {
|
||||
// 可以在此处对要导出的服务map进行统一处理
|
||||
service.url = indexRoute + service.url;
|
||||
return service;
|
||||
});
|
||||
Reference in New Issue
Block a user