Commit 1e1420a9 authored by shajiaiming's avatar shajiaiming

行情走cach

parent 95db9035
...@@ -15,27 +15,6 @@ class TickerController extends BaseController ...@@ -15,27 +15,6 @@ class TickerController extends BaseController
protected $basic_coin = ['ETH', 'BTC', 'USDT', 'BTY']; protected $basic_coin = ['ETH', 'BTC', 'USDT', 'BTY'];
protected $basic_price = []; protected $basic_price = [];
public function init()
{
$curl = new Curl();
$data = [
"names" => [
"eth,ethereum",
"btc,btc",
"usdt,ethereum",
"bty,bty",
]
];
$params = json_encode($data);
$curl->setHeader('Content-Type', 'application/json');
$curl->setRawPostData($params);
$res = $curl->post('https://b.biqianbao.net/interface/coin/coin-index', true);
$res = json_decode($res, true);
foreach ($res['data'] as $val) {
$this->basic_price[$val['name']] = $val['rmb'];
}
}
public function actionIndex() public function actionIndex()
{ {
$device_code = Yii::$app->request->get('device_code', ''); $device_code = Yii::$app->request->get('device_code', '');
...@@ -71,6 +50,24 @@ class TickerController extends BaseController ...@@ -71,6 +50,24 @@ class TickerController extends BaseController
public function actionOptional() public function actionOptional()
{ {
$curl = new Curl();
$data = [
"names" => [
"eth,ethereum",
"btc,btc",
"usdt,ethereum",
"bty,bty",
]
];
$params = json_encode($data);
$curl->setHeader('Content-Type', 'application/json');
$curl->setRawPostData($params);
$res = $curl->post('https://b.biqianbao.net/interface/coin/coin-index', true);
$res = json_decode($res, true);
foreach ($res['data'] as $val) {
$this->basic_price[$val['name']] = $val['rmb'];
}
$code = -1; $code = -1;
$msg = 'fail'; $msg = 'fail';
$data = null; $data = null;
...@@ -134,7 +131,7 @@ class TickerController extends BaseController ...@@ -134,7 +131,7 @@ class TickerController extends BaseController
$temp['close'] = (float)sprintf("%0.6f", $quotation['last']); $temp['close'] = (float)sprintf("%0.6f", $quotation['last']);
$temp['close_usd'] = (float)sprintf("%0.6f", $quotation['last'] * $this->basic_price[$tag_second]['usd']); $temp['close_usd'] = (float)sprintf("%0.6f", $quotation['last'] * $this->basic_price[$tag_second]['usd']);
$temp['close_rmb'] = (float)sprintf("%0.4f", $quotation['last'] * $this->basic_price[$tag_second]['rmb']); $temp['close_rmb'] = (float)sprintf("%0.4f", $quotation['last'] * $this->basic_price[$tag_second]['rmb']);
$temp['change'] = (float)sprintf("%0.4f", ($quotation['last'] - $quotation['open']) / $quotation['open'] * 100); $temp['change'] = (0 == $quotation['open']) ? 0 : (float)sprintf("%0.4f", ($quotation['last'] - $quotation['open']) / $quotation['open'] * 100);
$temp['high_usd'] = (float)sprintf("%0.4f", $quotation['high'] * $this->basic_price[$tag_second]['usd']); $temp['high_usd'] = (float)sprintf("%0.4f", $quotation['high'] * $this->basic_price[$tag_second]['usd']);
$temp['low_usd'] = (float)sprintf("%0.4f", $quotation['low'] * $this->basic_price[$tag_second]['usd']); $temp['low_usd'] = (float)sprintf("%0.4f", $quotation['low'] * $this->basic_price[$tag_second]['usd']);
$temp['high_rmb'] = (float)sprintf("%0.4f", $quotation['high'] * $this->basic_price[$tag_second]['rmb']); $temp['high_rmb'] = (float)sprintf("%0.4f", $quotation['high'] * $this->basic_price[$tag_second]['rmb']);
...@@ -142,12 +139,15 @@ class TickerController extends BaseController ...@@ -142,12 +139,15 @@ class TickerController extends BaseController
$temp['platform_us'] = $val['platform']; $temp['platform_us'] = $val['platform'];
if ('ZHAOBI' == strtoupper($val['platform'])) { if ('ZHAOBI' == strtoupper($val['platform'])) {
$temp['platform_zh'] = '找币'; $temp['platform_zh'] = '找币';
$temp['platform_us'] = 'zhaobi';
} }
if ('HUOBI' == strtoupper($val['platform'])) { if ('HUOBI' == strtoupper($val['platform'])) {
$temp['platform_zh'] = '火币'; $temp['platform_zh'] = '火币';
$temp['platform_us'] = 'huobi';
} }
if ('BINANCE' == strtoupper($val['platform'])) { if ('BINANCE' == strtoupper($val['platform'])) {
$temp['platform_zh'] = '币安'; $temp['platform_zh'] = '币安';
$temp['platform_us'] = 'binance';
} }
array_push($data, $temp); array_push($data, $temp);
} }
......
...@@ -13,6 +13,8 @@ use linslin\yii2\curl\Curl; ...@@ -13,6 +13,8 @@ use linslin\yii2\curl\Curl;
class BinanceBuilder extends FactoryService class BinanceBuilder extends FactoryService
{ {
protected $base_url = 'https://api.binance.com'; protected $base_url = 'https://api.binance.com';
protected $supported_symbol = 'supported_symbol_binance';
protected $quotation_prefix = 'quotation_binance_';
public function getTicker() public function getTicker()
{ {
...@@ -50,4 +52,39 @@ class BinanceBuilder extends FactoryService ...@@ -50,4 +52,39 @@ class BinanceBuilder extends FactoryService
} }
return ['code' => $this->code, 'ticker' => $ticker]; return ['code' => $this->code, 'ticker' => $ticker];
} }
public function getTickerFromCache()
{
$keys = $this->redis->smembers($this->supported_symbol);
$ticker = [];
foreach ($keys as $val) {
foreach ($this->basic_coin as $k => $coin) {
$explode_arr = explode($coin, $val);
if (2 == count($explode_arr) && empty($explode_arr[1])) {
list($low, $high, $close, $open, $vol) = $this->redis->hmget($this->quotation_prefix . strtolower($val), 'low', 'high', 'last', 'open', 'vol');
$temp = [];
$temp['symbol'] = strtoupper($explode_arr[0]) . '/' . $coin;
$temp['currency'] = strtoupper($explode_arr[0]);
$temp['base_currency'] = strtoupper($coin);
$temp['close'] = number_format($close, 6, '.', '');
$temp['close_usd'] = (float)sprintf("%0.6f", $close * $this->basic_price[$coin]['usd']);
$temp['close_rmb'] = (float)sprintf("%0.4f", $close * $this->basic_price[$coin]['rmb']);
$temp['change'] = (0 == $open) ? 0 : (float)sprintf("%0.4f", ($close - $open) / $open * 100);
$temp['high_usd'] = (float)sprintf("%0.4f", $high * $this->basic_price[$coin]['usd']);
$temp['low_usd'] = (float)sprintf("%0.4f", $low * $this->basic_price[$coin]['usd']);
$temp['high_rmb'] = (float)sprintf("%0.4f", $high * $this->basic_price[$coin]['rmb']);
$temp['low_rmb'] = (float)sprintf("%0.4f", $low * $this->basic_price[$coin]['rmb']);
$temp['vol'] = (float)sprintf("%0.4f", $vol);
$temp['optional'] = false;
$temp['platform_zh'] = '币安';
$temp['platform_us'] = 'binance';
array_push($ticker, $temp);
}
}
}
if (count($ticker) > 0) {
$this->code = 0;
}
return ['code' => $this->code, 'ticker' => $ticker];
}
} }
\ No newline at end of file
...@@ -16,7 +16,7 @@ abstract class FactoryService ...@@ -16,7 +16,7 @@ abstract class FactoryService
protected $basic_coin = ['ETH', 'BTC', 'USDT', 'BTY']; protected $basic_coin = ['ETH', 'BTC', 'USDT', 'BTY'];
protected $basic_price = []; protected $basic_price = [];
protected $redis;
public function __construct() public function __construct()
{ {
...@@ -39,5 +39,7 @@ abstract class FactoryService ...@@ -39,5 +39,7 @@ abstract class FactoryService
$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;
} }
} }
\ No newline at end of file
...@@ -35,7 +35,7 @@ class HuobiBuilder extends FactoryService ...@@ -35,7 +35,7 @@ class HuobiBuilder extends FactoryService
$temp['close'] = number_format($val['close'], 6, '.', ''); $temp['close'] = number_format($val['close'], 6, '.', '');
$temp['close_usd'] = (float)sprintf("%0.6f", $val['close'] * $this->basic_price[$coin]['usd']); $temp['close_usd'] = (float)sprintf("%0.6f", $val['close'] * $this->basic_price[$coin]['usd']);
$temp['close_rmb'] = (float)sprintf("%0.4f", $val['close'] * $this->basic_price[$coin]['rmb']); $temp['close_rmb'] = (float)sprintf("%0.4f", $val['close'] * $this->basic_price[$coin]['rmb']);
$temp['change'] = (float)sprintf("%0.4f", ($val['close'] - $val['open']) / $val['open'] * 100); $temp['change'] = (false == $val['open']) ? 0 : (float)sprintf("%0.4f", ($val['close'] - $val['open']) / $val['open'] * 100);
$temp['high_usd'] = (float)sprintf("%0.4f", $val['high'] * $this->basic_price[$coin]['usd']); $temp['high_usd'] = (float)sprintf("%0.4f", $val['high'] * $this->basic_price[$coin]['usd']);
$temp['low_usd'] = (float)sprintf("%0.4f", $val['low'] * $this->basic_price[$coin]['usd']); $temp['low_usd'] = (float)sprintf("%0.4f", $val['low'] * $this->basic_price[$coin]['usd']);
$temp['high_rmb'] = (float)sprintf("%0.4f", $val['high'] * $this->basic_price[$coin]['rmb']); $temp['high_rmb'] = (float)sprintf("%0.4f", $val['high'] * $this->basic_price[$coin]['rmb']);
...@@ -43,7 +43,7 @@ class HuobiBuilder extends FactoryService ...@@ -43,7 +43,7 @@ class HuobiBuilder extends FactoryService
$temp['vol'] = (float)sprintf("%0.4f", $val['vol']); $temp['vol'] = (float)sprintf("%0.4f", $val['vol']);
$temp['optional'] = false; $temp['optional'] = false;
$temp['platform_zh'] = '火币'; $temp['platform_zh'] = '火币';
$temp['platform_us'] = 'huoBi'; $temp['platform_us'] = 'huobi';
array_push($ticker, $temp); array_push($ticker, $temp);
break; break;
} }
...@@ -55,14 +55,13 @@ class HuobiBuilder extends FactoryService ...@@ -55,14 +55,13 @@ class HuobiBuilder extends FactoryService
public function getTickerFromCache() public function getTickerFromCache()
{ {
$redis = \Yii::$app->redis; $keys = $this->redis->smembers($this->supported_symbol);
$keys = $redis->smembers($this->supported_symbol);
$ticker = []; $ticker = [];
foreach ($keys as $val) { foreach ($keys as $val) {
foreach ($this->basic_coin as $k => $coin) { foreach ($this->basic_coin as $k => $coin) {
$explode_arr = explode(strtolower($coin), $val); $explode_arr = explode(strtolower($coin), $val);
if (2 == count($explode_arr) && empty($explode_arr[1])) { if (2 == count($explode_arr) && empty($explode_arr[1])) {
list($low, $high, $close, $open, $vol) = $redis->hmget($this->quotation_prefix.strtolower($val), 'low', 'high', 'last', 'open', 'vol'); list($low, $high, $close, $open, $vol) = $this->redis->hmget($this->quotation_prefix . strtolower($val), 'low', 'high', 'last', 'open', 'vol');
$temp = []; $temp = [];
$temp['symbol'] = strtoupper($explode_arr[0]) . '/' . $coin; $temp['symbol'] = strtoupper($explode_arr[0]) . '/' . $coin;
$temp['currency'] = strtoupper($explode_arr[0]); $temp['currency'] = strtoupper($explode_arr[0]);
...@@ -78,46 +77,45 @@ class HuobiBuilder extends FactoryService ...@@ -78,46 +77,45 @@ class HuobiBuilder extends FactoryService
$temp['vol'] = (float)sprintf("%0.4f", $vol); $temp['vol'] = (float)sprintf("%0.4f", $vol);
$temp['optional'] = false; $temp['optional'] = false;
$temp['platform_zh'] = '火币'; $temp['platform_zh'] = '火币';
$temp['platform_us'] = 'huoBi'; $temp['platform_us'] = 'huobi';
array_push($ticker, $temp); array_push($ticker, $temp);
} }
} }
} }
echo json_encode($ticker);exit; if (count($ticker) > 0) {
$this->code = 0;
}
return ['code' => $this->code, 'ticker' => $ticker];
} }
public function getHotTicker() public function getHotTicker()
{ {
$curl = new Curl();
$api = $this->base_url . '/market/detail';
$symbol = [ $symbol = [
'btcusdt', 'btcusdt',
'ethusdt', 'ethusdt',
'eosusdt' 'eosusdt'
]; ];
$ticker = []; $ticker = [];
$this->code = 0; foreach ($symbol as $val) {
foreach ($symbol as $key => $val) { list($low, $high, $close, $open, $vol) = $this->redis->hmget($this->quotation_prefix . strtolower($val), 'low', 'high', 'last', 'open', 'vol');
$url = $api . '?symbol=' . $val;
$res = $curl->get($url, false);
if (isset($res['status']) && 'ok' == $res['status']) {
$explode_arr = explode('usdt', $val); $explode_arr = explode('usdt', $val);
$temp = []; $temp = [];
$temp['symbol'] = strtoupper($explode_arr[0]) . '/USDT'; $temp['symbol'] = strtoupper($explode_arr[0]) . '/USDT';
$temp['currency'] = strtoupper($explode_arr[0]); $temp['currency'] = strtoupper($explode_arr[0]);
$temp['base_currency'] = 'USDT'; $temp['base_currency'] = 'USDT';
$temp['close'] = (float)sprintf("%0.6f", $res['tick']['close']); $temp['close'] = (float)sprintf("%0.6f", $close);
$temp['close_usd'] = (float)sprintf("%0.6f", $res['tick']['close'] * $this->basic_price['USDT']['usd']); $temp['close_usd'] = (float)sprintf("%0.6f", $close * $this->basic_price['USDT']['usd']);
$temp['close_rmb'] = (float)sprintf("%0.4f", $res['tick']['close'] * $this->basic_price['USDT']['rmb']); $temp['close_rmb'] = (float)sprintf("%0.4f", $close * $this->basic_price['USDT']['rmb']);
$temp['change'] = (float)sprintf("%0.4f", ($res['tick']['close'] - $res['tick']['open']) / $res['tick']['open'] * 100); $temp['change'] = (0 == $open) ? 0 : (float)sprintf("%0.4f", ($close - $open) / $open * 100);
$temp['high_usd'] = (float)sprintf("%0.4f", $res['tick']['high'] * $this->basic_price['USDT']['usd']); $temp['high_usd'] = (float)sprintf("%0.4f", $high * $this->basic_price['USDT']['usd']);
$temp['low_usd'] = (float)sprintf("%0.4f", $res['tick']['low'] * $this->basic_price['USDT']['usd']); $temp['low_usd'] = (float)sprintf("%0.4f", $low * $this->basic_price['USDT']['usd']);
$temp['high_rmb'] = (float)sprintf("%0.4f", $res['tick']['high'] * $this->basic_price['USDT']['rmb']); $temp['high_rmb'] = (float)sprintf("%0.4f", $high * $this->basic_price['USDT']['rmb']);
$temp['low_rmb'] = (float)sprintf("%0.4f", $res['tick']['low'] * $this->basic_price['USDT']['rmb']); $temp['low_rmb'] = (float)sprintf("%0.4f", $low * $this->basic_price['USDT']['rmb']);
$temp['vol'] = (float)sprintf("%0.4f", $res['tick']['amount']); $temp['vol'] = (float)sprintf("%0.4f", $vol);
array_push($ticker, $temp); array_push($ticker, $temp);
} }
if (count($ticker) > 0) {
$this->code = 0;
} }
return ['code' => $this->code, 'ticker' => $ticker]; return ['code' => $this->code, 'ticker' => $ticker];
} }
......
...@@ -14,7 +14,7 @@ class ZhaobiBuilder extends FactoryService ...@@ -14,7 +14,7 @@ class ZhaobiBuilder extends FactoryService
{ {
protected $base_url = 'https://api.biqianbao.top'; protected $base_url = 'https://api.biqianbao.top';
public function getTicker() public function getTickerFromCache()
{ {
$curl = new Curl(); $curl = new Curl();
$api = $this->base_url . '/api/data/Ticker?sort=cname'; $api = $this->base_url . '/api/data/Ticker?sort=cname';
...@@ -37,7 +37,7 @@ class ZhaobiBuilder extends FactoryService ...@@ -37,7 +37,7 @@ class ZhaobiBuilder extends FactoryService
$temp['close'] = (float)sprintf("%0.6f", $val['last']); $temp['close'] = (float)sprintf("%0.6f", $val['last']);
$temp['close_usd'] = (float)sprintf("%0.6f", $val['last'] * $this->basic_price[$coin]['usd']); $temp['close_usd'] = (float)sprintf("%0.6f", $val['last'] * $this->basic_price[$coin]['usd']);
$temp['close_rmb'] = (float)sprintf("%0.4f", $val['last'] * $this->basic_price[$coin]['rmb']); $temp['close_rmb'] = (float)sprintf("%0.4f", $val['last'] * $this->basic_price[$coin]['rmb']);
$temp['change'] = (float)sprintf("%0.4f", ($val['last'] - $val['open']) / $val['open'] * 100); $temp['change'] = (0 == $val['open']) ? 0 : (float)sprintf("%0.4f", ($val['last'] - $val['open']) / $val['open'] * 100);
$temp['high_usd'] = (float)sprintf("%0.4f", $val['high'] * $this->basic_price[$coin]['usd']); $temp['high_usd'] = (float)sprintf("%0.4f", $val['high'] * $this->basic_price[$coin]['usd']);
$temp['low_usd'] = (float)sprintf("%0.4f", $val['low'] * $this->basic_price[$coin]['usd']); $temp['low_usd'] = (float)sprintf("%0.4f", $val['low'] * $this->basic_price[$coin]['usd']);
$temp['high_rmb'] = (float)sprintf("%0.4f", $val['high'] * $this->basic_price[$coin]['rmb']); $temp['high_rmb'] = (float)sprintf("%0.4f", $val['high'] * $this->basic_price[$coin]['rmb']);
...@@ -47,7 +47,6 @@ class ZhaobiBuilder extends FactoryService ...@@ -47,7 +47,6 @@ class ZhaobiBuilder extends FactoryService
$temp['platform_zh'] = '找币'; $temp['platform_zh'] = '找币';
$temp['platform_us'] = 'zhaobi'; $temp['platform_us'] = 'zhaobi';
array_push($ticker, $temp); array_push($ticker, $temp);
break;
} }
} }
} }
......
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