Commit 2a80c274 authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/ticker' into 'master'

Feature/ticker See merge request !247
parents 1ab6e089 33bd27cd
...@@ -10,15 +10,17 @@ class MarketController extends BaseController ...@@ -10,15 +10,17 @@ class MarketController extends BaseController
{ {
public function actionDetail() public function actionDetail()
{ {
$symbol = Yii::$app->request->get('symbol', ''); $currency = Yii::$app->request->post('currency', '');
if (false == $symbol) { $base_currency = Yii::$app->request->post('base_currency', '');
$exchange = Yii::$app->request->post("exchange", 'Zhaobi');
if (false == $currency || false == $base_currency) {
$msg = 'invalid symbol'; $msg = 'invalid symbol';
$code = -1; $code = -1;
goto doEnd; goto doEnd;
} }
$builder = ExchangeBuilderFactory::create('Zhaobi'); $builder = ExchangeBuilderFactory::create('Zhaobi');
$result = $builder->getDetail($symbol); $result = $builder->getDetail(strtoupper($currency), strtoupper($base_currency));
if (-1 == $result['code']){ if (-1 == $result['code']) {
$msg = 'invalid symbol'; $msg = 'invalid symbol';
$code = -1; $code = -1;
goto doEnd; goto doEnd;
......
...@@ -13,7 +13,7 @@ use linslin\yii2\curl\Curl; ...@@ -13,7 +13,7 @@ use linslin\yii2\curl\Curl;
abstract class FactoryService abstract class FactoryService
{ {
protected $code = -1; protected $code = -1;
protected $basic_coin = ['ETH', 'BTC', 'USDT', 'BTY', 'CNYT']; protected $basic_coin = ['ETH', 'BTC', 'USDT', 'BTY', 'CNYT', 'CCNY'];
protected $basic_price = []; protected $basic_price = [];
protected $redis; protected $redis;
......
...@@ -167,14 +167,109 @@ class ZhaobiBuilder extends FactoryService ...@@ -167,14 +167,109 @@ class ZhaobiBuilder extends FactoryService
return ['code' => $this->code, 'notice' => $data]; return ['code' => $this->code, 'notice' => $data];
} }
public function getDetail($symbol) /*
* @params $currency 交易货币
* @params $base_currency 基础货币
*/
public function getDetail($currency, $base_currency)
{ {
list($low, $high, $close, $open, $vol) = $this->redis->hmget($this->quotation_prefix . strtolower($symbol), 'low', 'high', 'last', 'open', 'vol'); $code = 0;
if (empty($low) && empty($high) && empty($close) && empty($open) && empty($vol)) { $ticker = [];
return ['code' => $this->code, 'ticker' => []]; $symbol = $currency . $base_currency;
list($low, $high, $close, $open) = $this->redis->hmget($this->quotation_prefix . strtolower($symbol), 'low', 'high', 'last', 'open');
if (empty($low) && empty($high) && empty($close) && empty($open)) {
if ($this->basic_coin[2] == $currency) { //交易货币为USDT
$symbol = $base_currency . $this->basic_coin[2];
list($low, $high, $close, $open) = $this->redis->hmget($this->quotation_prefix . strtolower($symbol), 'low', 'high', 'last', 'open');
if (!empty($low) && !empty($high) && !empty($close) && !empty($open)) {
$low = number_format(1 / $low, 6, '.', '');
$high = number_format(1 / $high, 6, '.', '');
$open = number_format(1 / $open, 6, '.', '');
$close = number_format(1 / $close, 6, '.', '');
$ticker = [
'low' => $low,
'high' => $high,
'open' => $open,
'close' => $close
];
goto doEnd;
}
}
if ($this->basic_coin[5] == $base_currency) { //基础货币为CCNY
$symbol_currency = $currency . $this->basic_coin[2];
$symbol_base_currency = $this->basic_coin[2] . $base_currency;
list($low_currency, $high_currency, $close_currency, $open_currency) = $this->redis->hmget($this->quotation_prefix . strtolower($symbol_currency), 'low', 'high', 'last', 'open');
if (empty($low_currency) && empty($high_currency) && empty($close_currency) && empty($open_currency)) {
$code = $this->code;
goto doEnd;
}
list($low_base_currency, $high_base_currency, $close_base_currency, $open_base_currency) = $this->redis->hmget($this->quotation_prefix . strtolower($symbol_base_currency), 'low', 'high', 'last', 'open');
if (empty($low_base_currency) && empty($high_base_currency) && empty($close_base_currency) && empty($open_base_currency)) {
$code = $this->code;
goto doEnd;
}
$low = number_format($low_currency * $low_base_currency, 6, '.', '');
$high = number_format($high_currency * $high_base_currency, 6, '.', '');
$close = number_format($close_currency * $close_base_currency, 6, '.', '');
$open = number_format($open_currency * $open_base_currency, 6, '.', '');
$ticker = [
'low' => $low,
'high' => $high,
'open' => $open,
'close' => $close
];
goto doEnd;
}
if ($this->basic_coin[5] == $currency) { //交易货币为CCNY
$symbol_currency = $base_currency . $this->basic_coin[2];
$symbol_base_currency = $this->basic_coin[2] . $currency;
if ($currency . $base_currency == 'CCNYUSDT') {
list($low, $high, $close, $open) = $this->redis->hmget($this->quotation_prefix . strtolower($base_currency . $currency), 'low', 'high', 'last', 'open');
if (empty($low) && empty($high) && empty($close) && empty($open)) {
$code = $this->code;
goto doEnd;
}
$low = number_format(1 / $low, 6, '.', '');
$high = number_format(1 / $high, 6, '.', '');
$close = number_format(1 / $close, 6, '.', '');
$open = number_format(1 / $open, 6, '.', '');
$ticker = [
'low' => $low,
'high' => $high,
'open' => $open,
'close' => $close
];
goto doEnd;
}
list($low_currency, $high_currency, $close_currency, $open_currency) = $this->redis->hmget($this->quotation_prefix . strtolower($symbol_currency), 'low', 'high', 'last', 'open');
if (empty($low_currency) && empty($high_currency) && empty($close_currency) && empty($open_currency)) {
$code = $this->code;
goto doEnd;
}
list($low_base_currency, $high_base_currency, $close_base_currency, $open_base_currency) = $this->redis->hmget($this->quotation_prefix . strtolower($symbol_base_currency), 'low', 'high', 'last', 'open');
if (empty($low_base_currency) && empty($high_base_currency) && empty($close_base_currency) && empty($open_base_currency)) {
$code = $this->code;
goto doEnd;
}
$low = number_format(1 / $low_currency * $low_base_currency, 6, '.', '');
$high = number_format(1 / $high_currency * $high_base_currency, 6, '.', '');
$close = number_format(1 / $close_currency * $close_base_currency, 6, '.', '');
$open = number_format(1 / $open_currency * $open_base_currency, 6, '.', '');
$ticker = [
'low' => $low,
'high' => $high,
'open' => $open,
'close' => $close
];
goto doEnd;
}
doEnd :
return ['code' => $code, 'ticker' => $ticker];
} }
$ticker = [ $ticker = [
'vol' => $vol,
'low' => $low, 'low' => $low,
'open' => $open, 'open' => $open,
'high' => $high, 'high' => $high,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment