Commit 9626c654 authored by rlgy's avatar rlgy

搜索币api v2

parent f42616f8
......@@ -12,5 +12,18 @@ use yii\web\Controller;
class BaseController extends Controller
{
public $start;
public $end;
public function beforeAction($action)
{
$this->start = microtime(true);
return parent::beforeAction($action); // TODO: Change the autogenerated stub
}
public function afterAction($action, $result)
{
$this->end = microtime(true);
return parent::afterAction($action, $result); // TODO: Change the autogenerated stub
}
}
\ No newline at end of file
......@@ -31,6 +31,9 @@ class BaseResponse extends Response
} else {
$return = $data;
}
if (YII_ENV_DEV) {
$return['time'] = \Yii::$app->controller->end - \Yii::$app->controller->start;
}
\Yii::$app->response->data = $return;
parent::send();
}
......
......@@ -92,7 +92,7 @@ class CoinController extends BaseController
$limit = $request->post('limit', 10);
if ($name) {
$condition = [['or', ['like', 'name', $name], ['like', 'nickname', $name]]];
return CoinBusiness::SearchByName($page, $limit, $condition);
return ExchangeBusiness::SearchByName($page, $limit, $condition);
}
}
}
\ No newline at end of file
......@@ -19,6 +19,9 @@ use common\service\coin\CoinFactory;
*/
class ExchangeBusiness
{
/**支持的交易所
* @var array
*/
private static $exchanges = [
0 => 'HuoBi',
1 => 'Hadax',
......@@ -28,6 +31,59 @@ class ExchangeBusiness
];
/**
* 获取行情
* @param string $tag 目标币种
* @param string $aim 计数币种
* @return array|bool
*/
private static function getquatation($tag = 'btc', $aim = '')
{
$f = false;
$quotation = [];
foreach (self::$exchanges as $exchange) {
/**
* @var $exchange \common\service\exchange\Exchange
*/
$exchange = ExchangeFactory::createExchange($exchange);
if ($exchange->symbolExists($tag)) {
$quotation = $exchange->getTicker($tag);
$f = true;
break;
}
}
if (!$f) {
//所有交易所都不能直接获取交易对的行情就通过BTC去转换
//获取BTC_USD
$btc_usd = ExchangeFactory::createExchange(self::$exchanges[0])->getTicker('btc');
foreach (self::$exchanges as $exchange) {
/**
* @var $exchange \common\service\exchange\Exchange
*/
$exchange = ExchangeFactory::createExchange($exchange);
if ($exchange->symbolExists($tag, 'btc')) {
$price_btc = $exchange->getTicker($tag, 'btc');
//获取btcusdt
$result = array_map(function ($a, $b) {
return $a * $b;
}, $price_btc, $btc_usd);
$quotation = ['low' => $result[0], 'high' => $result[1], 'last' => $result[2]];
$f = true;
break;
}
}
}
if (!$f || empty($quotation)) {
return false;
}
//格式化行情数据
foreach ($quotation as $key => $item) {
$quotation[$key] = (double)$item;
}
$quotation['rmb'] = 6.6 * $quotation['last'];
return $quotation;
}
/**
* 保存各个交易所支持的交易对到redis,定时用crontab更新
* @return void
*/
......@@ -69,42 +125,40 @@ class ExchangeBusiness
$rows = $rows['data'];
foreach ($rows as $key => $row) {
$rows[$key]['sid'] = ucfirst($rows[$key]['sid']);
$f = false;//是否获取到行情
foreach (self::$exchanges as $exchange) {
/**
* @var $exchange \common\service\exchange\Exchange
*/
$exchange = ExchangeFactory::createExchange($exchange);
if ($exchange->symbolExists($row['name'])) {
$rows[$key] = array_merge($rows[$key], $exchange->getTicker($row['name']));
$f = true;
break;
}
$quotation = self::getquatation($row['name']);
if (!$quotation) {
//使用Coin服务
$coinServer = CoinFactory::createCoin($row['name'], $row['id'], $row['sid']);
$rows[$key]['sid'] = ucfirst($rows[$key]['sid']);
$rows[$key]['rmb'] = $coinServer->getPrice();
$rows[$key]['last'] = $coinServer->getDollar();
$rows[$key]['low'] = $coinServer->getLow();
$rows[$key]['high'] = $coinServer->getHigh();
$coinServer->__destruct();
}
if (!$f) {
//所有交易所都不能直接获取交易对的行情就通过BTC去转换
//获取BTC_USD
$btc_usd = ExchangeFactory::createExchange(self::$exchanges[0])->getTicker('btc');
$rows[$key] = array_merge($rows[$key], $quotation);
}
return $rows;
}
return [];
}
foreach (self::$exchanges as $exchange) {
/**
* @var $exchange \common\service\exchange\Exchange
*/
$exchange = ExchangeFactory::createExchange($exchange);
if ($exchange->symbolExists($row['name'], 'btc')) {
$price_btc = $exchange->getTicker($row['name'], 'btc');
//获取btcusdt
$result = array_map(function ($a, $b) {
return $a * $b;
}, $price_btc, $btc_usd);
$rows[$key] = array_merge($rows[$key],
['low' => $result[0], 'high' => $result[1], 'last' => $result[2]]);
$f = true;
break;
}
}
}
if (!$f) {
/**
* 根据名称搜索
* @param int $page
* @param int $limit
* @param array $condition
* @return array|\yii\db\ActiveRecord|\yii\db\ActiveRecord[]
*/
public static function SearchByName($page = 1, $limit = 10, $condition = [])
{
$rows = Coin::getSelectList($page, $limit, ['id', 'sid', 'icon', 'name', 'nickname', 'chain'], $condition);
if ($rows['count'] > 0) {
$rows = $rows['data'];
foreach ($rows as $key => $row) {
$rows[$key]['sid'] = ucfirst($rows[$key]['sid']);
$quotation = self::getquatation($row['name']);
if (!$quotation) {
//使用Coin服务
$coinServer = CoinFactory::createCoin($row['name'], $row['id'], $row['sid']);
$rows[$key]['sid'] = ucfirst($rows[$key]['sid']);
......@@ -114,13 +168,7 @@ class ExchangeBusiness
$rows[$key]['high'] = $coinServer->getHigh();
$coinServer->__destruct();
}
if (!isset($row[$key]['rmb'])) {
$rows[$key]['rmb'] = 6.6 * $rows[$key]['last'];
}
//转化价格为数字
$rows[$key]['low'] = (double)$rows[$key]['low'];
$rows[$key]['high'] = (double)$rows[$key]['high'];
$rows[$key]['last'] = (double)$rows[$key]['last'];
$rows[$key] = array_merge($rows[$key], $quotation);
}
return $rows;
}
......
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