Commit 19ecbec7 authored by shajiaiming's avatar shajiaiming

行情分页

parent 4f03b113
...@@ -17,6 +17,7 @@ class TickerController extends BaseController ...@@ -17,6 +17,7 @@ class TickerController extends BaseController
public function actionIndex() public function actionIndex()
{ {
$page = Yii::$app->request->get('page', 1);
$device_code = Yii::$app->request->get('device_code', ''); $device_code = Yii::$app->request->get('device_code', '');
$exchange = Yii::$app->request->get('exchange', 'zhaobi'); $exchange = Yii::$app->request->get('exchange', 'zhaobi');
...@@ -28,14 +29,14 @@ class TickerController extends BaseController ...@@ -28,14 +29,14 @@ class TickerController extends BaseController
goto doEnd; goto doEnd;
} }
$builder = ExchangeBuilderFactory::create($exchange); $builder = ExchangeBuilderFactory::create($exchange);
$result = $builder->getTickerFromCache(); $result = $builder->getTickerFromCache($page);
$code = $result['code']; $code = $result['code'];
$data = $result['ticker']; $data = $result['data'];
if (false != $device_code) { if (false != $device_code) {
$coin_optional = CoinOptional::find()->select('symbol')->where(['platform' => $exchange, 'device_code' => $device_code])->asArray()->all(); $coin_optional = CoinOptional::find()->select('symbol')->where(['platform' => $exchange, 'device_code' => $device_code])->asArray()->all();
$coin_optional = ArrayHelper::getColumn($coin_optional, 'symbol'); $coin_optional = ArrayHelper::getColumn($coin_optional, 'symbol');
foreach ($data as &$val) { foreach ($data['ticker'] as &$val) {
if (in_array($val['symbol'], $coin_optional)) { if (in_array($val['symbol'], $coin_optional)) {
$val['optional'] = true; $val['optional'] = true;
} }
......
...@@ -14,6 +14,7 @@ class BinanceBuilder extends FactoryService ...@@ -14,6 +14,7 @@ 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 $supported_symbol = 'supported_symbol_binance';
protected $supported_symbol_list = 'supported_symbol_binance_list';
protected $quotation_prefix = 'quotation_binance_'; protected $quotation_prefix = 'quotation_binance_';
public function getTicker() public function getTicker()
...@@ -53,9 +54,27 @@ class BinanceBuilder extends FactoryService ...@@ -53,9 +54,27 @@ class BinanceBuilder extends FactoryService
return ['code' => $this->code, 'ticker' => $ticker]; return ['code' => $this->code, 'ticker' => $ticker];
} }
public function getTickerFromCache() public function getTickerFromCache($page = 1)
{ {
$size = 0;
for ($i = 0; $i < $page; $i ++) {
$size += 50;
$this->end = $size;
}
$this->start = $this->end - 50;
$keys = $this->redis->smembers($this->supported_symbol); $keys = $this->redis->smembers($this->supported_symbol);
if (false == $this->redis->exists($this->supported_symbol_list)) {
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])) {
$this->redis->lpush($this->supported_symbol_list, $val);
}
}
}
}
$keys = $this->redis->lrange($this->supported_symbol_list, $this->start, $this->end);
$ticker = []; $ticker = [];
foreach ($keys as $val) { foreach ($keys as $val) {
foreach ($this->basic_coin as $k => $coin) { foreach ($this->basic_coin as $k => $coin) {
...@@ -82,9 +101,14 @@ class BinanceBuilder extends FactoryService ...@@ -82,9 +101,14 @@ class BinanceBuilder extends FactoryService
} }
} }
} }
if (count($ticker) > 0) {
$this->code = 0; $this->code = 0;
} $data = [
return ['code' => $this->code, 'ticker' => $ticker]; 'ticker' => $ticker,
'page' => [
'pageSize' => 50,
'currentPage' => (int)$page,
]
];
return ['code' => $this->code, 'data' => $data];
} }
} }
\ No newline at end of file
...@@ -18,6 +18,9 @@ abstract class FactoryService ...@@ -18,6 +18,9 @@ abstract class FactoryService
protected $basic_price = []; protected $basic_price = [];
protected $redis; protected $redis;
protected $start;
protected $end;
public function __construct() public function __construct()
{ {
$curl = new Curl(); $curl = new Curl();
...@@ -32,7 +35,7 @@ abstract class FactoryService ...@@ -32,7 +35,7 @@ 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('https://b.biqianbao.net/interface/coin/coin-index', true); $res = $curl->post('http://127.0.0.1:8082/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) {
......
...@@ -14,6 +14,7 @@ class HuobiBuilder extends FactoryService ...@@ -14,6 +14,7 @@ class HuobiBuilder extends FactoryService
{ {
protected $base_url = 'https://api.huobi.pro'; protected $base_url = 'https://api.huobi.pro';
protected $supported_symbol = 'supported_symbol_huobi'; protected $supported_symbol = 'supported_symbol_huobi';
protected $supported_symbol_list = 'supported_symbol_huobi_list';
protected $quotation_prefix = 'quotation_huobi_'; protected $quotation_prefix = 'quotation_huobi_';
public function getTicker() public function getTicker()
...@@ -53,9 +54,27 @@ class HuobiBuilder extends FactoryService ...@@ -53,9 +54,27 @@ class HuobiBuilder extends FactoryService
return ['code' => $this->code, 'ticker' => $ticker]; return ['code' => $this->code, 'ticker' => $ticker];
} }
public function getTickerFromCache() public function getTickerFromCache($page = 1)
{ {
$size = 0;
for ($i = 0; $i < $page; $i ++) {
$size += 50;
$this->end = $size;
}
$this->start = $this->end - 50;
$keys = $this->redis->smembers($this->supported_symbol); $keys = $this->redis->smembers($this->supported_symbol);
if (false == $this->redis->exists($this->supported_symbol_list)) {
foreach ($keys as $val) {
foreach ($this->basic_coin as $k => $coin) {
$explode_arr = explode(strtolower($coin), $val);
if (2 == count($explode_arr) && empty($explode_arr[1])) {
$this->redis->lpush($this->supported_symbol_list, $val);
}
}
}
}
$keys = $this->redis->lrange($this->supported_symbol_list, $this->start, $this->end);
$ticker = []; $ticker = [];
foreach ($keys as $val) { foreach ($keys as $val) {
foreach ($this->basic_coin as $k => $coin) { foreach ($this->basic_coin as $k => $coin) {
...@@ -82,10 +101,15 @@ class HuobiBuilder extends FactoryService ...@@ -82,10 +101,15 @@ class HuobiBuilder extends FactoryService
} }
} }
} }
if (count($ticker) > 0) {
$this->code = 0; $this->code = 0;
} $data = [
return ['code' => $this->code, 'ticker' => $ticker]; 'ticker' => $ticker,
'page' => [
'pageSize' => 50,
'currentPage' => (int)$page,
]
];
return ['code' => $this->code, 'data' => $data];
} }
public function getHotTicker() public function getHotTicker()
......
...@@ -21,7 +21,6 @@ class ZhaobiBuilder extends FactoryService ...@@ -21,7 +21,6 @@ class ZhaobiBuilder extends FactoryService
$res = $curl->get($api, false); $res = $curl->get($api, false);
$ticker = []; $ticker = [];
if (isset($res['message']) && 'OK' == $res['message']) { if (isset($res['message']) && 'OK' == $res['message']) {
$this->code = 0;
$ticker_temp = []; $ticker_temp = [];
foreach ($res['data'] as $val) { foreach ($res['data'] as $val) {
$ticker_temp = array_merge($ticker_temp, $val); $ticker_temp = array_merge($ticker_temp, $val);
...@@ -51,7 +50,15 @@ class ZhaobiBuilder extends FactoryService ...@@ -51,7 +50,15 @@ class ZhaobiBuilder extends FactoryService
} }
} }
} }
return ['code' => $this->code, 'ticker' => $ticker]; $this->code = 0;
$data = [
'ticker' => $ticker,
'page' => [
'pageSize' => 50,
'currentPage' => 1,
]
];
return ['code' => $this->code, 'data' => $data];
} }
public function getNotice($params = []) public function getNotice($params = [])
......
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