Commit 3759c4c5 authored by shajiaiming's avatar shajiaiming

Merge branch 'master' into feature/explore

parents 9046729c 2ddb27c9
...@@ -244,6 +244,7 @@ class CoinController extends BaseController ...@@ -244,6 +244,7 @@ class CoinController extends BaseController
$chain_quotation[$value] = ExchangeBusiness::getquatation($value); $chain_quotation[$value] = ExchangeBusiness::getquatation($value);
} }
foreach ($result['data'] as $key => &$value) { foreach ($result['data'] as $key => &$value) {
if (!isset($value['nickname']) || empty($value['nickname'])) continue;
$nickname = json_decode($value['nickname'], true); $nickname = json_decode($value['nickname'], true);
$introduce = json_decode($value['introduce'], true); $introduce = json_decode($value['introduce'], true);
$value['nickname'] = $nickname[$this->lang]; $value['nickname'] = $nickname[$this->lang];
......
...@@ -100,7 +100,7 @@ class ServiceController extends BaseController ...@@ -100,7 +100,7 @@ class ServiceController extends BaseController
$type = Yii::$app->request->get('type', 1); $type = Yii::$app->request->get('type', 1);
$coin_recommendItems = $this->coinRecommendList($platform_id, $type); $coin_recommendItems = $this->coinRecommendList($platform_id, $type);
$fields = ['id', 'sid', 'icon', 'name', 'nickname', 'chain', 'platform']; $fields = ['id', 'sid', 'icon', 'name', 'nickname', 'chain', 'platform'];
$rows = Coin::getSelectList(1, 999, $fields, [['in', 'id', $coin_recommendItems]]); $rows = Coin::getSelectList(1, 999, $fields, [['in', 'id', $coin_recommendItems]], $coin_recommendItems);
foreach ($rows['data'] as $key => &$value) { foreach ($rows['data'] as $key => &$value) {
$nickname = json_decode($value['nickname'], true); $nickname = json_decode($value['nickname'], true);
$value['nickname'] = $nickname[$this->lang]; $value['nickname'] = $nickname[$this->lang];
...@@ -114,7 +114,7 @@ class ServiceController extends BaseController ...@@ -114,7 +114,7 @@ class ServiceController extends BaseController
*/ */
private function coinRecommendList($platform_id, $type = 1) private function coinRecommendList($platform_id, $type = 1)
{ {
$recommend_list = CoinRecommend::find()->select('cid')->where(['platform_id' => $platform_id, 'type' => $type])->all(); $recommend_list = CoinRecommend::find()->select('cid')->where(['platform_id' => $platform_id, 'type' => $type])->orderBy('sort')->all();
if ($recommend_list) { if ($recommend_list) {
$coin_ids = array_column($recommend_list, 'cid'); $coin_ids = array_column($recommend_list, 'cid');
return $coin_ids; return $coin_ids;
......
...@@ -23,7 +23,7 @@ class TickerController extends BaseController ...@@ -23,7 +23,7 @@ class TickerController extends BaseController
$platform_id = Yii::$app->request->get('device_code', 0); $platform_id = Yii::$app->request->get('device_code', 0);
$exchange = Yii::$app->request->get('exchange', 'zhaobi'); $exchange = Yii::$app->request->get('exchange', 'zhaobi');
$data_value = Yii::$app->request->get('data-value', ''); $data_value = Yii::$app->request->get('data-value', '');
$sort = Yii::$app->request->get('sort', ''); $sort_value = Yii::$app->request->get('sort-value', '');
$exchange_arr = ['huobi', 'binance', 'okex', 'zhaobi']; $exchange_arr = ['huobi', 'binance', 'okex', 'zhaobi'];
...@@ -33,8 +33,15 @@ class TickerController extends BaseController ...@@ -33,8 +33,15 @@ class TickerController extends BaseController
$data = []; $data = [];
goto doEnd; goto doEnd;
} }
$condition = [];
if (false != $data_value && false != $sort_value) {
$condition = [
'data_value' => $data_value,
'sort_value' => $sort_value
];
}
$builder = ExchangeBuilderFactory::create($exchange); $builder = ExchangeBuilderFactory::create($exchange);
$result = $builder->getTickerFromCache($page); $result = $builder->getTickerFromCache($page, $condition);
$code = $result['code']; $code = $result['code'];
$data = $result['data']; $data = $result['data'];
......
<?php
namespace common\components;
use Yii;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
class Tools
{
/**
* 获取随机验证码
* @return string
*/
public static function getRandomNumber($count = 6, $type = 'mixed')
{
$chars = '1234567890abcdefghijklmnopqrstuvwxyz';
switch($type) {
case 'number':
$startIndex = 0;
$endIndex = 9;
break;
case 'letter':
$startIndex = 10;
$endIndex = 35;
break;
default :
$startIndex = 0;
$endIndex = 35;
break;
}
$randomNumber = '';
for($i = 0; $i<$count; $i++) {
$randomNumber .= substr($chars, rand($startIndex, $endIndex), 1);
}
return $randomNumber;
}
/**
* 获取文件全路径
* @param type $filename
* @param type $type
* @return string
*/
public static function getFileUrl($filename, $type)
{
return $filename;
switch ($type) {
case 'case':
return $filename?Url::to('@resDomain/' . $type . '/' . $filename):'';
case 'video':
return $filename?Url::to('@resDomain/' . $type . '/' . $filename):'';
case 'banner':
return $filename?Url::to('@resDomain/' . $type . '/' . $filename):'';
case 'weixin':
return $filename?Url::to('@resDomain/' . $type . '/' . $filename):'';
case 'public':
return $filename?Url::to('@resDomain/' . $type . '/' . $filename):'';
default:
return '';
}
}
public static function getGradeFile($grade)
{
return Url::to('@resDomain/grade/a' . $grade . '.png');
}
/**
* 判断当前日期是否是可用日期
* @param type $startData
* @param type $endData
*/
public static function isAvailableDate($start, $end)
{
$current = date('Y-m-d');
$start = $start?date('Y-m-d', strtotime($start)):$current;
$end = $end?date('Y-m-d', strtotime($end)):$current;
if($start <= $current && $end >= $current) {
return true;
} else {
return false;
}
}
/**
* 添加get查询数据
* @param type $values
*/
public static function addQueryParams($values)
{
Yii::$app->request->setQueryParams(ArrayHelper::merge(Yii::$app->request->get(), $values));
}
/**
* 获取post数据, 可附加额外数据
* @param array $values 附加数据,必须是数组形式
* @param string $key 指定数据附加到特定键
*/
public static function getPost(array $values, $key = null)
{
if(Yii::$app->request->isPost) {
$data = Yii::$app->request->post();
if($key !== null) {
$data[$key] = ArrayHelper::merge($data[$key], $values);
} else {
$data = ArrayHelper::merge($data, $values);
}
return $data;
} else {
return;
}
}
/*
* 截取字符串
*/
public static function truncate_utf8_string($string, $length, $etc = '...')
{
$result = '';
$string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'UTF-8');
$strlen = strlen($string);
for ($i = 0; (($i < $strlen) && ($length > 0)); $i++)
{
if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
{
if ($length < 1.0)
{
break;
}
$result .= substr($string, $i, $number);
$length -= 1.0;
$i += $number - 1;
}
else
{
$result .= substr($string, $i, 1);
$length -= 0.5;
}
}
$result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
if ($i < $strlen)
{
$result .= $etc;
}
return $result;
}
public static function arraySort($array, $keys, $sort = SORT_DESC)
{
$keysValue = [];
foreach ($array as $k => $v) {
$keysValue[$k] = $v[$keys];
}
array_multisort($keysValue, $sort, $array);
return $array;
}
}
\ No newline at end of file
...@@ -36,8 +36,8 @@ class Coin extends BaseActiveRecord ...@@ -36,8 +36,8 @@ class Coin extends BaseActiveRecord
/** /**
* 获取币种信息列表 * 获取币种信息列表
* *
* @param int $page * @param int $page
* @param int $limit * @param int $limit
* @param array $condition * @param array $condition
* @return array|\yii\db\ActiveRecord[] * @return array|\yii\db\ActiveRecord[]
*/ */
...@@ -48,7 +48,7 @@ class Coin extends BaseActiveRecord ...@@ -48,7 +48,7 @@ class Coin extends BaseActiveRecord
$query = $query->andWhere($item); $query = $query->andWhere($item);
} }
$count = $query->count(); $count = $query->count();
$data = $query->offset(($page - 1) * 10)->limit($limit)->asArray()->all(); $data = $query->offset(($page - 1) * 10)->limit($limit)->asArray()->all();
foreach ($data as $key => $item) { foreach ($data as $key => $item) {
$data[$key]['price'] = sprintf("%0.6f", $item['price']); $data[$key]['price'] = sprintf("%0.6f", $item['price']);
} }
...@@ -58,19 +58,28 @@ class Coin extends BaseActiveRecord ...@@ -58,19 +58,28 @@ class Coin extends BaseActiveRecord
/** /**
* 获取币种信息列表 * 获取币种信息列表
* *
* @param int $page * @param int $page
* @param int $limit * @param int $limit
* @param array $condition * @param array $condition
* @return array|\yii\db\ActiveRecord[] * @return array|\yii\db\ActiveRecord[]
*/ */
public static function getSelectList($page = 1, $limit = 10, $columns = [], $condition = []) public static function getSelectList($page = 1, $limit = 10, $columns = [], $condition = [], $orderBy = [])
{ {
$ids_str = implode(',', $orderBy);
$order = "FIELD(`id`,$ids_str)";
$query = self::find(); $query = self::find();
foreach ($condition as $item) { foreach ($condition as $item) {
$query = $query->andWhere($item); $query = $query->andWhere($item);
} }
$count = $query->count(); $count = $query->count();
$data = $query->select($columns)->offset(($page - 1) * $limit)->limit($limit)->asArray()->all(); if (false != $orderBy) {
$ids_str = implode(',', $orderBy);
$order = "FIELD(`id`,$ids_str)";
$data = $query->select($columns)->offset(($page - 1) * $limit)->limit($limit)->asArray()->orderBy([$order => true])->all();
} else {
$data = $query->select($columns)->offset(($page - 1) * $limit)->limit($limit)->asArray()->all();
}
return ['count' => $count, 'data' => $data]; return ['count' => $count, 'data' => $data];
} }
...@@ -173,8 +182,8 @@ class Coin extends BaseActiveRecord ...@@ -173,8 +182,8 @@ class Coin extends BaseActiveRecord
/** /**
* 根据id列表获取币种信息 * 根据id列表获取币种信息
* *
* @param array $ids [description] * @param array $ids [description]
* @param array $select [description] * @param array $select [description]
* @param string $sort_by [description] * @param string $sort_by [description]
* @return array [description] * @return array [description]
*/ */
...@@ -200,8 +209,8 @@ class Coin extends BaseActiveRecord ...@@ -200,8 +209,8 @@ class Coin extends BaseActiveRecord
/** /**
* 获取推介币种信息列表 * 获取推介币种信息列表
* *
* @param int $page * @param int $page
* @param int $limit * @param int $limit
* @param array $condition * @param array $condition
* @return array|\yii\db\ActiveRecord[] * @return array|\yii\db\ActiveRecord[]
*/ */
...@@ -213,7 +222,7 @@ class Coin extends BaseActiveRecord ...@@ -213,7 +222,7 @@ class Coin extends BaseActiveRecord
} }
$query = $query->rightJoin('coin_recommend', 'coin.id=coin_recommend.cid'); $query = $query->rightJoin('coin_recommend', 'coin.id=coin_recommend.cid');
$count = $query->count(); $count = $query->count();
$data = $query->offset(($page - 1) * 10)->limit($limit)->asArray()->all(); $data = $query->offset(($page - 1) * 10)->limit($limit)->asArray()->all();
foreach ($data as $key => $item) { foreach ($data as $key => $item) {
$data[$key]['price'] = sprintf("%0.6f", $item['price']); $data[$key]['price'] = sprintf("%0.6f", $item['price']);
} }
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace common\service\exchange\factory; namespace common\service\exchange\factory;
use common\components\Tools;
use linslin\yii2\curl\Curl; use linslin\yii2\curl\Curl;
class BinanceBuilder extends FactoryService class BinanceBuilder extends FactoryService
...@@ -15,48 +16,13 @@ class BinanceBuilder extends FactoryService ...@@ -15,48 +16,13 @@ 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 $supported_symbol_list = 'supported_symbol_binance_list';
protected $supported_symbol_close_sort_list = 'supported_symbol_close_sort_binance_list'; protected $supported_symbol_close_asc = 'supported_symbol_close_asc_binance';
protected $supported_symbol_change_sort_list = 'supported_symbol_change_sort_binance_list'; protected $supported_symbol_close_desc = 'supported_symbol_close_desc_binance';
protected $supported_symbol_change_asc = 'supported_symbol_change_asc_binance';
protected $supported_symbol_change_desc = 'supported_symbol_change_desc_binance';
protected $quotation_prefix = 'quotation_binance_'; protected $quotation_prefix = 'quotation_binance_';
public function getTicker() public function getTickerFromCache($page = 1, $condition = [])
{
$curl = new Curl();
$api = $this->base_url . '/api/v1/ticker/24hr';
$res = $curl->get($api, false);
$ticker = [];
if (is_array($res)) {
$this->code = 0;
foreach ($res as $val) {
foreach ($this->basic_coin as $k => $coin) {
$explode_arr = explode($coin, $val['symbol']);
if (2 == count($explode_arr) && empty($explode_arr[1])) {
$temp = [];
$temp['symbol'] = $explode_arr[0] . '/' . $coin;
$temp['currency'] = strtoupper($explode_arr[0]);
$temp['base_currency'] = strtoupper($coin);
$temp['close'] = number_format($val['lastPrice'], 6, '.', '');
$temp['close_usd'] = (float)sprintf("%0.6f", $val['lastPrice'] * $this->basic_price[$coin]['usd']);
$temp['close_rmb'] = (float)sprintf("%0.4f", $val['lastPrice'] * $this->basic_price[$coin]['rmb']);
$temp['change'] = (float)sprintf("%0.4f", $val['priceChangePercent']);
$temp['high_usd'] = (float)sprintf("%0.4f", $val['highPrice'] * $this->basic_price[$coin]['usd']);
$temp['low_usd'] = (float)sprintf("%0.4f", $val['lowPrice'] * $this->basic_price[$coin]['usd']);
$temp['high_rmb'] = (float)sprintf("%0.4f", $val['highPrice'] * $this->basic_price[$coin]['rmb']);
$temp['low_rmb'] = (float)sprintf("%0.4f", $val['lowPrice'] * $this->basic_price[$coin]['rmb']);
$temp['vol'] = (float)sprintf("%0.4f", $val['volume']);
$temp['optional'] = false;
$temp['platform_zh'] = '币安';
$temp['platform_us'] = 'binance';
array_push($ticker, $temp);
break;
}
}
}
}
return ['code' => $this->code, 'ticker' => $ticker];
}
public function getTickerFromCache($page = 1)
{ {
$size = 0; $size = 0;
for ($i = 0; $i < $page; $i++) { for ($i = 0; $i < $page; $i++) {
...@@ -76,20 +42,42 @@ class BinanceBuilder extends FactoryService ...@@ -76,20 +42,42 @@ class BinanceBuilder extends FactoryService
} }
} }
} }
$keys = $this->redis->lrange($this->supported_symbol_list, $this->start, $this->end); if (false != $condition) {
if ('price' == $condition['data_value']) {
if ('price-asc' == $condition['sort_value']) {
$keys = $this->redis_ticker->lrange($this->supported_symbol_close_asc, $this->start, $this->end);
}
if ('price-desc' == $condition['sort_value']) {
$keys = $this->redis_ticker->lrange($this->supported_symbol_close_desc, $this->start, $this->end);
}
}
if ('change' == $condition['data_value']) {
if ('change-asc' == $condition['sort_value']) {
$keys = $this->redis_ticker->lrange($this->supported_symbol_change_asc, $this->start, $this->end);
}
if ('change-desc' == $condition['sort_value']) {
$keys = $this->redis_ticker->lrange($this->supported_symbol_change_desc, $this->start, $this->end);
}
}
} else {
$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) {
$explode_arr = explode($coin, $val); $explode_arr = explode($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) = $this->redis->hmget($this->quotation_prefix . strtolower($val), 'low', 'high', 'last', 'open', 'vol'); if (false != $condition) {
list($low, $high, $close, $open, $vol) = $this->redis_ticker->hmget($this->quotation_prefix . strtoupper($val), 'low', 'high', 'last', 'open', 'vol');
} else {
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;
#if ('BCC/USDT' != $temp['symbol']) continue;
$temp['currency'] = strtoupper($explode_arr[0]); $temp['currency'] = strtoupper($explode_arr[0]);
$temp['base_currency'] = strtoupper($coin); $temp['base_currency'] = strtoupper($coin);
$temp['close'] = $this->sctonum($close, 8); $temp['close'] = rtrim(sprintf('%.8f', floatval($close)),'0');
$temp['close_usd'] = (float)sprintf("%0.6f", $close * $this->basic_price[$coin]['usd']); $temp['close_usd'] = rtrim(sprintf('%.6f', floatval($close * $this->basic_price[$coin]['usd'])),'0');
$temp['close_rmb'] = (float)sprintf("%0.4f", $close * $this->basic_price[$coin]['rmb']); $temp['close_rmb'] = (float)sprintf("%0.4f", $close * $this->basic_price[$coin]['rmb']);
$temp['change'] = (0 == $open) ? 0 : (float)sprintf("%0.2f", ($close - $open) / $open * 100); $temp['change'] = (0 == $open) ? 0 : (float)sprintf("%0.2f", ($close - $open) / $open * 100);
$temp['high_usd'] = (float)sprintf("%0.4f", $high * $this->basic_price[$coin]['usd']); $temp['high_usd'] = (float)sprintf("%0.4f", $high * $this->basic_price[$coin]['usd']);
...@@ -121,40 +109,35 @@ class BinanceBuilder extends FactoryService ...@@ -121,40 +109,35 @@ class BinanceBuilder extends FactoryService
$ticker = []; $ticker = [];
for ($i = 0; $i < $len; $i++) { for ($i = 0; $i < $len; $i++) {
$symbol = $this->redis->lindex($this->supported_symbol_list, $i); $symbol = $this->redis->lindex($this->supported_symbol_list, $i);
list($close, $open) = $this->redis->hmget($this->quotation_prefix . strtolower($symbol), 'last', 'open'); list($close, $open, $low, $high, $vol) = $this->redis->hmget($this->quotation_prefix . strtolower($symbol), 'last', 'open', 'low', 'high', 'vol');
if ('0.00000000' == $close) continue;
$temp = []; $temp = [];
$temp['symbol'] = strtoupper($symbol); $temp['symbol'] = strtoupper($symbol);
$temp['close'] = number_format($close, 6, '.', ''); $temp['close'] = $close;
$temp['low'] = $low;
$temp['high'] = $high;
$temp['open'] = $open;
$temp['vol'] = $vol;
$temp['change'] = (0 == $open) ? 0 : (float)sprintf("%0.2f", ($close - $open) / $open * 100); $temp['change'] = (0 == $open) ? 0 : (float)sprintf("%0.2f", ($close - $open) / $open * 100);
array_push($ticker, $temp); array_push($ticker, $temp);
$key = $this->quotation_prefix . strtoupper($symbol);
$this->redis_ticker->hmset($key, 'low', $low, 'high', $high, 'last', $close, 'open', $open, 'vol', $vol);
} }
$ticker_sort_close= $this->arraySort($ticker,'close');
foreach ($ticker_sort_close as $val) {
$this->redis->lpush($this->supported_symbol_close_sort_list, $val['symbol']);
}
$ticker_change_close= $this->arraySort($ticker,'change');
foreach ($ticker_change_close as $val) {
$this->redis->lpush($this->supported_symbol_change_sort_list, $val['symbol']);
}
}
protected function arraySort($array, $keys, $sort = SORT_DESC) $ticker_sort_close = Tools::arraySort($ticker, 'close');
{ $this->redis_ticker->del($this->supported_symbol_close_asc);
$keysValue = []; $this->redis_ticker->del($this->supported_symbol_close_desc);
foreach ($array as $k => $v) { foreach ($ticker_sort_close as $val) {
$keysValue[$k] = $v[$keys]; $this->redis_ticker->lpush($this->supported_symbol_close_asc, strtoupper($val['symbol']));
$this->redis_ticker->rpush($this->supported_symbol_close_desc, strtoupper($val['symbol']));
} }
array_multisort($keysValue, $sort, $array);
return $array;
}
protected function sctonum($num, $double = 5) $ticker_change_close = Tools::arraySort($ticker, 'change');
{ $this->redis_ticker->del($this->supported_symbol_change_asc);
if (false !== strpos($num, "e")) { $this->redis_ticker->del($this->supported_symbol_change_desc);
$a = explode("e", strtolower($num)); foreach ($ticker_change_close as $val) {
return bcmul($a[0], bcpow(10, $a[1], $double), $double); $this->redis_ticker->lpush($this->supported_symbol_change_asc, strtoupper($val['symbol']));
} else { $this->redis_ticker->rpush($this->supported_symbol_change_desc, strtoupper($val['symbol']));
return number_format($num, 6, '.', '');
} }
} }
} }
\ No newline at end of file
...@@ -17,6 +17,7 @@ abstract class FactoryService ...@@ -17,6 +17,7 @@ abstract class FactoryService
protected $basic_price = []; protected $basic_price = [];
protected $redis; protected $redis;
protected $redis_ticker;
protected $start; protected $start;
protected $end; protected $end;
...@@ -44,5 +45,6 @@ abstract class FactoryService ...@@ -44,5 +45,6 @@ abstract class FactoryService
} }
$this->redis = \Yii::$app->redis; $this->redis = \Yii::$app->redis;
$this->redis_ticker = \Yii::$app->redis_ticker;
} }
} }
\ No newline at end of file
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace common\service\exchange\factory; namespace common\service\exchange\factory;
use common\components\Tools;
use linslin\yii2\curl\Curl; use linslin\yii2\curl\Curl;
class HuobiBuilder extends FactoryService class HuobiBuilder extends FactoryService
...@@ -15,8 +16,10 @@ class HuobiBuilder extends FactoryService ...@@ -15,8 +16,10 @@ 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 $supported_symbol_list = 'supported_symbol_huobi_list';
protected $supported_symbol_close_sort_list = 'supported_symbol_close_sort_huobi_list'; protected $supported_symbol_close_asc = 'supported_symbol_close_asc_huobi';
protected $supported_symbol_change_sort_list = 'supported_symbol_change_sort_huobi_list'; protected $supported_symbol_close_desc = 'supported_symbol_close_desc_huobi';
protected $supported_symbol_change_asc = 'supported_symbol_change_asc_huobi';
protected $supported_symbol_change_desc = 'supported_symbol_change_desc_huobi';
protected $quotation_prefix = 'quotation_huobi_'; protected $quotation_prefix = 'quotation_huobi_';
public function hotTickerUpdate() public function hotTickerUpdate()
...@@ -39,7 +42,7 @@ class HuobiBuilder extends FactoryService ...@@ -39,7 +42,7 @@ class HuobiBuilder extends FactoryService
} }
} }
public function getTickerFromCache($page = 1) public function getTickerFromCache($page = 1, $condition = [])
{ {
$size = 0; $size = 0;
for ($i = 0; $i < $page; $i++) { for ($i = 0; $i < $page; $i++) {
...@@ -59,19 +62,43 @@ class HuobiBuilder extends FactoryService ...@@ -59,19 +62,43 @@ class HuobiBuilder extends FactoryService
} }
} }
} }
$keys = $this->redis->lrange($this->supported_symbol_list, $this->start, $this->end); if (false != $condition) {
if ('price' == $condition['data_value']) {
if ('price-asc' == $condition['sort_value']) {
$keys = $this->redis_ticker->lrange($this->supported_symbol_close_asc, $this->start, $this->end);
}
if ('price-desc' == $condition['sort_value']) {
$keys = $this->redis_ticker->lrange($this->supported_symbol_close_desc, $this->start, $this->end);
}
}
if ('change' == $condition['data_value']) {
if ('change-asc' == $condition['sort_value']) {
$keys = $this->redis_ticker->lrange($this->supported_symbol_change_asc, $this->start, $this->end);
}
if ('change-desc' == $condition['sort_value']) {
$keys = $this->redis_ticker->lrange($this->supported_symbol_change_desc, $this->start, $this->end);
}
}
} else {
$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) {
$explode_arr = explode(strtolower($coin), $val); $explode_arr = explode(strtoupper($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) = $this->redis->hmget($this->quotation_prefix . strtolower($val), 'low', 'high', 'last', 'open', 'vol'); if (false != $condition) {
list($low, $high, $close, $open, $vol) = $this->redis_ticker->hmget($this->quotation_prefix . strtoupper($val), 'low', 'high', 'last', 'open', 'vol');
} else {
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]);
$temp['base_currency'] = strtoupper($coin); $temp['base_currency'] = strtoupper($coin);
$temp['close'] = $this->sctonum($close, 8); $temp['close'] = rtrim(sprintf('%.8f', floatval($close)),'0');
$temp['close_usd'] = (float)sprintf("%0.6f", $close * $this->basic_price[$coin]['usd']); $temp['close_usd'] = rtrim(sprintf('%.6f', floatval($close * $this->basic_price[$coin]['usd'])),'0');
$temp['close_rmb'] = (float)sprintf("%0.4f", $close * $this->basic_price[$coin]['rmb']); $temp['close_rmb'] = (float)sprintf("%0.4f", $close * $this->basic_price[$coin]['rmb']);
$temp['change'] = (false == $open) ? 0 : (float)sprintf("%0.2f", ($close - $open) / $open * 100); $temp['change'] = (false == $open) ? 0 : (float)sprintf("%0.2f", ($close - $open) / $open * 100);
$temp['high_usd'] = (float)sprintf("%0.4f", $high * $this->basic_price[$coin]['usd']); $temp['high_usd'] = (float)sprintf("%0.4f", $high * $this->basic_price[$coin]['usd']);
...@@ -103,31 +130,36 @@ class HuobiBuilder extends FactoryService ...@@ -103,31 +130,36 @@ class HuobiBuilder extends FactoryService
$ticker = []; $ticker = [];
for ($i = 0; $i < $len; $i++) { for ($i = 0; $i < $len; $i++) {
$symbol = $this->redis->lindex($this->supported_symbol_list, $i); $symbol = $this->redis->lindex($this->supported_symbol_list, $i);
list($close, $open) = $this->redis->hmget($this->quotation_prefix . strtolower($symbol), 'last', 'open'); list($close, $open, $low, $high, $vol) = $this->redis->hmget($this->quotation_prefix . strtolower($symbol), 'last', 'open', 'low', 'high', 'vol');
if ('0.00000000' == $close) continue;
$temp = []; $temp = [];
$temp['symbol'] = strtoupper($symbol); $temp['symbol'] = strtoupper($symbol);
$temp['close'] = number_format($close, 6, '.', ''); $temp['close'] = $close;
$temp['low'] = $low;
$temp['high'] = $high;
$temp['open'] = $open;
$temp['vol'] = $vol;
$temp['change'] = (0 == $open) ? 0 : (float)sprintf("%0.2f", ($close - $open) / $open * 100); $temp['change'] = (0 == $open) ? 0 : (float)sprintf("%0.2f", ($close - $open) / $open * 100);
array_push($ticker, $temp); array_push($ticker, $temp);
$key = $this->quotation_prefix . strtoupper($symbol);
$this->redis_ticker->hmset($key, 'low', $low, 'high', $high, 'last', $close, 'open', $open, 'vol', $vol);
} }
$ticker_sort_close = $this->arraySort($ticker, 'close');
$ticker_sort_close = Tools::arraySort($ticker, 'close');
$this->redis_ticker->del($this->supported_symbol_close_asc);
$this->redis_ticker->del($this->supported_symbol_close_desc);
foreach ($ticker_sort_close as $val) { foreach ($ticker_sort_close as $val) {
$this->redis->lpush($this->supported_symbol_close_sort_list, $val['symbol']); $this->redis_ticker->lpush($this->supported_symbol_close_asc, strtoupper($val['symbol']));
$this->redis_ticker->rpush($this->supported_symbol_close_desc, strtoupper($val['symbol']));
} }
$ticker_change_close = $this->arraySort($ticker, 'change');
foreach ($ticker_change_close as $val) {
$this->redis->lpush($this->supported_symbol_change_sort_list, $val['symbol']);
}
}
protected function arraySort($array, $keys, $sort = SORT_DESC) $ticker_change_close = Tools::arraySort($ticker, 'change');
{ $this->redis_ticker->del($this->supported_symbol_change_asc);
$keysValue = []; $this->redis_ticker->del($this->supported_symbol_change_desc);
foreach ($array as $k => $v) { foreach ($ticker_change_close as $val) {
$keysValue[$k] = $v[$keys]; $this->redis_ticker->lpush($this->supported_symbol_change_asc, strtoupper($val['symbol']));
$this->redis_ticker->rpush($this->supported_symbol_change_desc, strtoupper($val['symbol']));
} }
array_multisort($keysValue, $sort, $array);
return $array;
} }
public function getHotTicker() public function getHotTicker()
...@@ -161,14 +193,4 @@ class HuobiBuilder extends FactoryService ...@@ -161,14 +193,4 @@ class HuobiBuilder extends FactoryService
} }
return ['code' => $this->code, 'ticker' => $ticker]; return ['code' => $this->code, 'ticker' => $ticker];
} }
protected function sctonum($num, $double = 5)
{
if (false !== stripos($num, "e")) {
$a = explode("e", strtolower($num));
return bcmul($a[0], bcpow(10, $a[1], $double), $double);
} else {
return number_format($num, 6, '.', '');
}
}
} }
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace common\service\exchange\factory; namespace common\service\exchange\factory;
use common\components\Tools;
use linslin\yii2\curl\Curl; use linslin\yii2\curl\Curl;
class ZhaobiBuilder extends FactoryService class ZhaobiBuilder extends FactoryService
...@@ -15,44 +16,63 @@ class ZhaobiBuilder extends FactoryService ...@@ -15,44 +16,63 @@ class ZhaobiBuilder extends FactoryService
protected $base_url = 'https://api.biqianbao.top'; protected $base_url = 'https://api.biqianbao.top';
protected $supported_symbol = 'supported_symbol_zhaobi'; protected $supported_symbol = 'supported_symbol_zhaobi';
protected $supported_symbol_list = 'supported_symbol_zhaobi_list'; protected $supported_symbol_list = 'supported_symbol_zhaobi_list';
protected $supported_symbol_close_sort_list = 'supported_symbol_close_sort_zhaobi_list'; protected $supported_symbol_close_asc = 'supported_symbol_close_asc_zhaobi';
protected $supported_symbol_change_sort_list = 'supported_symbol_change_sort_zhaobi_list'; protected $supported_symbol_close_desc = 'supported_symbol_close_desc_zhaobi';
protected $supported_symbol_change_asc = 'supported_symbol_change_asc_zhaobi';
protected $supported_symbol_change_desc = 'supported_symbol_change_desc_zhaobi';
protected $quotation_prefix = 'quotation_zhaobi_'; protected $quotation_prefix = 'quotation_zhaobi_';
public function getTickerFromCache() public function getTickerFromCache($page = 1, $condition = [])
{ {
$curl = new Curl(); $this->start = 0;
$api = $this->base_url . '/api/data/Ticker?sort=cname'; $this->end = 50;
$res = $curl->get($api, false); if (false != $condition) {
$ticker = []; if ('price' == $condition['data_value']) {
if (isset($res['message']) && 'OK' == $res['message']) { if ('price-asc' == $condition['sort_value']) {
$ticker_temp = []; $keys = $this->redis_ticker->lrange($this->supported_symbol_close_asc, $this->start, $this->end);
foreach ($res['data'] as $val) { }
$ticker_temp = array_merge($ticker_temp, $val); if ('price-desc' == $condition['sort_value']) {
$keys = $this->redis_ticker->lrange($this->supported_symbol_close_desc, $this->start, $this->end);
}
} }
foreach ($ticker_temp as $val) { if ('change' == $condition['data_value']) {
foreach ($this->basic_coin as $k => $coin) { if ('change-asc' == $condition['sort_value']) {
$explode_arr = explode($coin, $val['symbol']); $keys = $this->redis_ticker->lrange($this->supported_symbol_change_asc, $this->start, $this->end);
if (2 == count($explode_arr) && empty($explode_arr[1])) { }
$temp = []; if ('change-desc' == $condition['sort_value']) {
$temp['symbol'] = ('BCC' == strtoupper($explode_arr[0])) ? 'BCH' . '/' . $coin : strtoupper($explode_arr[0]) . '/' . $coin; $keys = $this->redis_ticker->lrange($this->supported_symbol_change_desc, $this->start, $this->end);
$temp['currency'] = ('BCC' == strtoupper($explode_arr[0])) ? 'BCH' : strtoupper($explode_arr[0]); }
$temp['base_currency'] = strtoupper($coin); }
$temp['close'] = number_format($val['last'], 6, '.', ''); } else {
if ('0.000000' == $temp['close']) continue; $keys = $this->redis->lrange($this->supported_symbol_list, $this->start, $this->end);
$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']); $ticker = [];
$temp['change'] = (0 == $val['open']) ? 0 : (float)sprintf("%0.2f", ($val['last'] - $val['open']) / $val['open'] * 100); foreach ($keys as $val) {
$temp['high_usd'] = (float)sprintf("%0.4f", $val['high'] * $this->basic_price[$coin]['usd']); foreach ($this->basic_coin as $k => $coin) {
$temp['low_usd'] = (float)sprintf("%0.4f", $val['low'] * $this->basic_price[$coin]['usd']); $explode_arr = explode($coin, $val);
$temp['high_rmb'] = (float)sprintf("%0.4f", $val['high'] * $this->basic_price[$coin]['rmb']); if (2 == count($explode_arr) && empty($explode_arr[1])) {
$temp['low_rmb'] = (float)sprintf("%0.4f", $val['low'] * $this->basic_price[$coin]['rmb']); if (false != $condition) {
$temp['vol'] = (float)sprintf("%0.4f", $val['vol']); list($low, $high, $close, $open, $vol) = $this->redis_ticker->hmget($this->quotation_prefix . strtoupper($val), 'low', 'high', 'last', 'open', 'vol');
$temp['optional'] = false; } else {
$temp['platform_zh'] = '找币'; list($low, $high, $close, $open, $vol) = $this->redis->hmget($this->quotation_prefix . strtolower($val), 'low', 'high', 'last', 'open', 'vol');
$temp['platform_us'] = 'zhaobi';
array_push($ticker, $temp);
} }
$temp = [];
$temp['symbol'] = ('BCC' == strtoupper($explode_arr[0])) ? 'BCH' . '/' . $coin : strtoupper($explode_arr[0]) . '/' . $coin;
$temp['currency'] = ('BCC' == strtoupper($explode_arr[0])) ? 'BCH' : strtoupper($explode_arr[0]);
$temp['base_currency'] = strtoupper($coin);
$temp['close'] = $close;
$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.2f", ($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'] = 'zhaobi';
array_push($ticker, $temp);
} }
} }
} }
...@@ -79,20 +99,30 @@ class ZhaobiBuilder extends FactoryService ...@@ -79,20 +99,30 @@ class ZhaobiBuilder extends FactoryService
$ticker = []; $ticker = [];
for ($i = 0; $i < $len; $i++) { for ($i = 0; $i < $len; $i++) {
$symbol = $this->redis->lindex($this->supported_symbol_list, $i); $symbol = $this->redis->lindex($this->supported_symbol_list, $i);
list($close, $open) = $this->redis->hmget($this->quotation_prefix . strtolower($symbol), 'last', 'open'); list($close, $open, $low, $high, $vol) = $this->redis->hmget($this->quotation_prefix . strtolower($symbol), 'last', 'open', 'low', 'high', 'vol');
$temp = []; $temp = [];
$temp['symbol'] = strtoupper($symbol); $temp['symbol'] = strtoupper($symbol);
$temp['close'] = number_format($close, 6, '.', ''); $temp['close'] = number_format($close, 6, '.', '');
$temp['change'] = (0 == $open) ? 0 : (float)sprintf("%0.2f", ($close - $open) / $open * 100); $temp['change'] = (0 == $open) ? 0 : (float)sprintf("%0.2f", ($close - $open) / $open * 100);
array_push($ticker, $temp); array_push($ticker, $temp);
$key = $this->quotation_prefix . strtoupper($symbol);
$this->redis_ticker->hmset($key, 'low', $low, 'high', $high, 'last', $close, 'open', $open, 'vol', $vol);
} }
$ticker_sort_close = $this->arraySort($ticker, 'close');
$ticker_sort_close = Tools::arraySort($ticker, 'close');
$this->redis_ticker->del($this->supported_symbol_close_asc);
$this->redis_ticker->del($this->supported_symbol_close_desc);
foreach ($ticker_sort_close as $val) { foreach ($ticker_sort_close as $val) {
$this->redis->lpush($this->supported_symbol_close_sort_list, $val['symbol']); $this->redis_ticker->lpush($this->supported_symbol_close_asc, strtoupper($val['symbol']));
$this->redis_ticker->rpush($this->supported_symbol_close_desc, strtoupper($val['symbol']));
} }
$ticker_change_close = $this->arraySort($ticker, 'change');
$ticker_change_close = Tools::arraySort($ticker, 'change');
$this->redis_ticker->del($this->supported_symbol_change_asc);
$this->redis_ticker->del($this->supported_symbol_change_desc);
foreach ($ticker_change_close as $val) { foreach ($ticker_change_close as $val) {
$this->redis->lpush($this->supported_symbol_change_sort_list, $val['symbol']); $this->redis_ticker->lpush($this->supported_symbol_change_asc, strtoupper($val['symbol']));
$this->redis_ticker->rpush($this->supported_symbol_change_desc, strtoupper($val['symbol']));
} }
} }
......
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