实时显示 bitcoin 汇率

This commit is contained in:
jysperm
2014-06-21 13:44:51 +08:00
parent a6eed089dd
commit 71747bff27
5 changed files with 39 additions and 11 deletions

View File

@@ -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

View File

@@ -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'

View File

@@ -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) ->

View File

@@ -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 淘宝

View File

@@ -39,6 +39,7 @@
"mysql": "*",
"moment": "*",
"request": "*",
"mongo-min": "*"
"mongo-min": "*",
"redis": "*"
}
}