Commit 9c807aa6 authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/ws_ticker' into 'master'

行情走cache See merge request !119
parents 45ebad99 1e1420a9
......@@ -15,27 +15,6 @@ class TickerController extends BaseController
protected $basic_coin = ['ETH', 'BTC', 'USDT', 'BTY'];
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()
{
$device_code = Yii::$app->request->get('device_code', '');
......@@ -71,6 +50,24 @@ class TickerController extends BaseController
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;
$msg = 'fail';
$data = null;
......@@ -134,7 +131,7 @@ class TickerController extends BaseController
$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_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['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']);
......@@ -142,12 +139,15 @@ class TickerController extends BaseController
$temp['platform_us'] = $val['platform'];
if ('ZHAOBI' == strtoupper($val['platform'])) {
$temp['platform_zh'] = '找币';
$temp['platform_us'] = 'zhaobi';
}
if ('HUOBI' == strtoupper($val['platform'])) {
$temp['platform_zh'] = '火币';
$temp['platform_us'] = 'huobi';
}
if ('BINANCE' == strtoupper($val['platform'])) {
$temp['platform_zh'] = '币安';
$temp['platform_us'] = 'binance';
}
array_push($data, $temp);
}
......
......@@ -13,6 +13,8 @@ use linslin\yii2\curl\Curl;
class BinanceBuilder extends FactoryService
{
protected $base_url = 'https://api.binance.com';
protected $supported_symbol = 'supported_symbol_binance';
protected $quotation_prefix = 'quotation_binance_';
public function getTicker()
{
......@@ -50,4 +52,39 @@ class BinanceBuilder extends FactoryService
}
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
protected $basic_coin = ['ETH', 'BTC', 'USDT', 'BTY'];
protected $basic_price = [];
protected $redis;
public function __construct()
{
......@@ -39,5 +39,7 @@ abstract class FactoryService
$this->basic_price[$val['name']]['rmb'] = $val['rmb'];
$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
$temp['close'] = number_format($val['close'], 6, '.', '');
$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['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['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']);
......@@ -43,7 +43,7 @@ class HuobiBuilder extends FactoryService
$temp['vol'] = (float)sprintf("%0.4f", $val['vol']);
$temp['optional'] = false;
$temp['platform_zh'] = '火币';
$temp['platform_us'] = 'huoBi';
$temp['platform_us'] = 'huobi';
array_push($ticker, $temp);
break;
}
......@@ -55,14 +55,13 @@ class HuobiBuilder extends FactoryService
public function getTickerFromCache()
{
$redis = \Yii::$app->redis;
$keys = $redis->smembers($this->supported_symbol);
$keys = $this->redis->smembers($this->supported_symbol);
$ticker = [];
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])) {
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['symbol'] = strtoupper($explode_arr[0]) . '/' . $coin;
$temp['currency'] = strtoupper($explode_arr[0]);
......@@ -78,46 +77,45 @@ class HuobiBuilder extends FactoryService
$temp['vol'] = (float)sprintf("%0.4f", $vol);
$temp['optional'] = false;
$temp['platform_zh'] = '火币';
$temp['platform_us'] = 'huoBi';
$temp['platform_us'] = 'huobi';
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()
{
$curl = new Curl();
$api = $this->base_url . '/market/detail';
$symbol = [
'btcusdt',
'ethusdt',
'eosusdt'
];
$ticker = [];
$this->code = 0;
foreach ($symbol as $key => $val) {
$url = $api . '?symbol=' . $val;
$res = $curl->get($url, false);
if (isset($res['status']) && 'ok' == $res['status']) {
$explode_arr = explode('usdt', $val);
$temp = [];
$temp['symbol'] = strtoupper($explode_arr[0]) . '/USDT';
$temp['currency'] = strtoupper($explode_arr[0]);
$temp['base_currency'] = 'USDT';
$temp['close'] = (float)sprintf("%0.6f", $res['tick']['close']);
$temp['close_usd'] = (float)sprintf("%0.6f", $res['tick']['close'] * $this->basic_price['USDT']['usd']);
$temp['close_rmb'] = (float)sprintf("%0.4f", $res['tick']['close'] * $this->basic_price['USDT']['rmb']);
$temp['change'] = (float)sprintf("%0.4f", ($res['tick']['close'] - $res['tick']['open']) / $res['tick']['open'] * 100);
$temp['high_usd'] = (float)sprintf("%0.4f", $res['tick']['high'] * $this->basic_price['USDT']['usd']);
$temp['low_usd'] = (float)sprintf("%0.4f", $res['tick']['low'] * $this->basic_price['USDT']['usd']);
$temp['high_rmb'] = (float)sprintf("%0.4f", $res['tick']['high'] * $this->basic_price['USDT']['rmb']);
$temp['low_rmb'] = (float)sprintf("%0.4f", $res['tick']['low'] * $this->basic_price['USDT']['rmb']);
$temp['vol'] = (float)sprintf("%0.4f", $res['tick']['amount']);
array_push($ticker, $temp);
}
foreach ($symbol as $val) {
list($low, $high, $close, $open, $vol) = $this->redis->hmget($this->quotation_prefix . strtolower($val), 'low', 'high', 'last', 'open', 'vol');
$explode_arr = explode('usdt', $val);
$temp = [];
$temp['symbol'] = strtoupper($explode_arr[0]) . '/USDT';
$temp['currency'] = strtoupper($explode_arr[0]);
$temp['base_currency'] = 'USDT';
$temp['close'] = (float)sprintf("%0.6f", $close);
$temp['close_usd'] = (float)sprintf("%0.6f", $close * $this->basic_price['USDT']['usd']);
$temp['close_rmb'] = (float)sprintf("%0.4f", $close * $this->basic_price['USDT']['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['USDT']['usd']);
$temp['low_usd'] = (float)sprintf("%0.4f", $low * $this->basic_price['USDT']['usd']);
$temp['high_rmb'] = (float)sprintf("%0.4f", $high * $this->basic_price['USDT']['rmb']);
$temp['low_rmb'] = (float)sprintf("%0.4f", $low * $this->basic_price['USDT']['rmb']);
$temp['vol'] = (float)sprintf("%0.4f", $vol);
array_push($ticker, $temp);
}
if (count($ticker) > 0) {
$this->code = 0;
}
return ['code' => $this->code, 'ticker' => $ticker];
}
......
......@@ -14,7 +14,7 @@ class ZhaobiBuilder extends FactoryService
{
protected $base_url = 'https://api.biqianbao.top';
public function getTicker()
public function getTickerFromCache()
{
$curl = new Curl();
$api = $this->base_url . '/api/data/Ticker?sort=cname';
......@@ -37,7 +37,7 @@ class ZhaobiBuilder extends FactoryService
$temp['close'] = (float)sprintf("%0.6f", $val['last']);
$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['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['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']);
......@@ -47,7 +47,6 @@ class ZhaobiBuilder extends FactoryService
$temp['platform_zh'] = '找币';
$temp['platform_us'] = 'zhaobi';
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