'use strict'; 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'); const actionFilter = 'id type amount detail op createdAt'; const { inject } = require('../../common/Provider'); module.exports = { async getFundDetail (req, res) { const { userId } = res.locals.user; return Fund.findOne({ userId }, 'asset').then(fund => { return res.json({ code: 0, msg: 'success', data: fund }); }).catch(err => { mylog.error("[getFundDetail]", err); return res.json({ code: 1, error: err.errmsg }); }); }, async getActions (req, res) { const { userId } = res.locals.user; const { pageIndex = 1, pageSize = 0 } = req.query; return await Action.find({ 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: err.errmsg }); }); }, 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: err.errmsg })); }, async getSupportCoinsList (req, res) { // eslint-disable-next-line no-undef 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 }); }, async withdraw (req, res) { /** * 1.创建工单 * 2.创建用户action * 3.返回状态 */ const { userId } = res.locals.user; 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.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, { userId, amount, remain: fund.asset, op: 0, detail: { targetAddress } }); const ticket = createTicket('withdraw', { userId, amount, action, targetAddress }); try { await fund.decrease(coinId, +amount); } catch (error) { mylog.error('[Fund提现] ', error); return res.json({ code: 3, 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 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 }); 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 }); }; }), // 查询资金,直接拉数据库,区块链的查询交给定时任务 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(fund => { return res.json({ code: 0, data: fund.fundList }); }).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--) { 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); } return res.json({ code: 0, data: { activeFund, revenueLastday, revenueAll: fund.asset && fund.asset['vic'] } }); }).catch(err => { mylog.error(err); res.json({ code: 500, msg: '系统错误' }); }); }, };