diff --git a/core/bitcoin.coffee b/core/bitcoin.coffee index 2b3dbf7..2b53db8 100644 --- a/core/bitcoin.coffee +++ b/core/bitcoin.coffee @@ -13,6 +13,18 @@ exports.genAddress = (blockchain_secret, callback) -> callback body.input_address +exports.getExchangeRate = (callback) -> + app.redis.get 'rp:exchange_rate:cnybtc', (err, rate) -> + if rate + callback rate + else + request 'https://blockchain.info/ticker', (err, res, body) -> + body = JSON.parse body + rate = 1 / parseFloat(body['CNY']['15m']) + + app.redis.setex 'rp:exchange_rate:cnybtc', 60, rate, -> + callback parseFloat rate + exports.doCallback = (data, callback) -> mAccount.byDepositAddress data.input_address, (err, account) -> unless data.secret == account.blockchain_secret diff --git a/core/index.coffee b/core/index.coffee index 2f0b9c8..994cd40 100644 --- a/core/index.coffee +++ b/core/index.coffee @@ -3,6 +3,7 @@ path = require 'path' harp = require 'harp' fs = require 'fs' mongomin = require 'mongo-min' +redis = require 'redis' global._ = require 'underscore' global.ObjectID = require('mongodb').ObjectID @@ -30,11 +31,22 @@ bindRouters = (app) -> plugin = require './plugin' plugin.loadPlugins app +exports.connectDatabase = (callback) -> + if global.app?.db + return callback null, app.db + + app.redis = redis.createClient() + + mongomin config.mongodb, (err, db) -> + global.app ?= {} + app.db = db + callback err, db + exports.runWebServer = -> global.app = exports.app = express() - mongomin config.mongodb, (db) -> - app.db = db + exports.connectDatabase (err) -> + throw err if err i18n.init default_language: 'zh_CN' diff --git a/core/router/panel.coffee b/core/router/panel.coffee index a68fe28..7e585e4 100644 --- a/core/router/panel.coffee +++ b/core/router/panel.coffee @@ -1,6 +1,7 @@ config = require '../config' billing = require '../billing' plugin = require '../plugin' +bitcoin = require '../bitcoin' {requestAuthenticate, renderAccount} = require './middleware' mAccount = require '../model/account' @@ -9,12 +10,14 @@ mBalance = require '../model/balance' module.exports = exports = express.Router() exports.get '/pay', requestAuthenticate, renderAccount, (req, res) -> - mBalance.find - account_id: req.account._id - .toArray (err, balance_log) -> - res.render 'panel/pay', - deposit_log: _.filter(balance_log, (i) -> i.type == 'deposit') - billing_log: _.filter(balance_log, (i) -> i.type == 'billing') + bitcoin.getExchangeRate (rate) -> + mBalance.find + account_id: req.account._id + .toArray (err, balance_log) -> + res.render 'panel/pay', + exchange_rate: rate + deposit_log: _.filter(balance_log, (i) -> i.type == 'deposit') + billing_log: _.filter(balance_log, (i) -> i.type == 'billing') exports.get '/', requestAuthenticate, (req, res) -> billing.checkBilling req.account, (account) -> diff --git a/core/view/panel/pay.jade b/core/view/panel/pay.jade index 36102e7..61b3cdb 100644 --- a/core/view/panel/pay.jade +++ b/core/view/panel/pay.jade @@ -10,8 +10,8 @@ block main div h2 比特币 p 你可以直接向该地址发送比特币。在经过一次确认后,系统会实时地,自动为你折算使用时间。 - p 实时汇率:10 CNY = 0.004 BTC - pre.bg-success 13v2BTCMZMHg5v87susgg86HFZqXERuwUd + p 实时汇率:10 CNY = #{(exchange_rate * 10).toFixed(4)} BTC + pre.bg-success= account.attribute.bitcoin_deposit_address div h2 淘宝 diff --git a/package.json b/package.json index 2046d9b..3e9fac7 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "mysql": "*", "moment": "*", "request": "*", - "mongo-min": "*" + "mongo-min": "*", + "redis": "*" } }