Commit 02f24050 authored by shajiaiming's avatar shajiaiming

闪兑

parent 60367fb0
...@@ -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;
...@@ -37,13 +37,13 @@ abstract class FactoryService ...@@ -37,13 +37,13 @@ abstract class FactoryService
$params = json_encode($data); $params = json_encode($data);
$curl->setHeader('Content-Type', 'application/json'); $curl->setHeader('Content-Type', 'application/json');
$curl->setRawPostData($params); $curl->setRawPostData($params);
$res = $curl->post(\Yii::$app->params['biqianbao'].'/interface/coin/coin-index', true); #$res = $curl->post(\Yii::$app->params['biqianbao'].'/interface/coin/coin-index', true);
$res = json_decode($res, true); #$res = json_decode($res, true);
foreach ($res['data'] as $val) { #foreach ($res['data'] as $val) {
$this->basic_price[$val['name']]['rmb'] = $val['rmb']; #$this->basic_price[$val['name']]['rmb'] = $val['rmb'];
$this->basic_price[$val['name']]['usd'] = $val['usd']; #$this->basic_price[$val['name']]['usd'] = $val['usd'];
} #}
$this->redis = \Yii::$app->redis; $this->redis = \Yii::$app->redis;
$this->redis_ticker = \Yii::$app->redis_ticker; $this->redis_ticker = \Yii::$app->redis_ticker;
......
...@@ -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