Commit f5224f88 authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/ticker' into 'master'

fix See merge request !115
parents 5a9c02a0 8e2bb30c
......@@ -5,6 +5,7 @@
* Date: 2018/12/29
* Time: 16:26
*/
namespace api\controllers;
use api\base\BaseController;
......@@ -33,23 +34,23 @@ class ServiceController extends BaseController
public function actionCoinTickers()
{
$request = Yii::$app->request;
$coinItems = $request->post('names');
if(!$coinItems){
return ['code' => 1,'data' => [],'msg' => '币种不能为空'];
$coinItems = $request->post('names');
if (!$coinItems) {
return ['code' => 1, 'data' => [], 'msg' => '币种不能为空'];
}
if(!is_array($coinItems)){
if (!is_array($coinItems)) {
$coinItems = [$coinItems];
}
$tol_coins = ['ETC'];
$tickerData = [];
if($coinItems){
foreach($coinItems as $item){
if ($coinItems) {
foreach ($coinItems as $item) {
$item = strtoupper($item);
if(in_array($item,$tol_coins)){
if (in_array($item, $tol_coins)) {
$exchange = ExchangeFactory::createExchange('HuoBi');
if ($exchange->symbolExists($item)) {
$quotation = $exchange->getTicker($item);
if($quotation){
if ($quotation) {
//格式化行情数据
foreach ($quotation as $key => $value) {
$quotation[$key] = (float)sprintf("%0.4f", (double)$value);
......@@ -58,30 +59,34 @@ class ServiceController extends BaseController
$quotation['rmb'] = (float)sprintf("%0.4f", $rate * $quotation['last']);
}
}
}else{
$quotation = ExchangeBusiness::getquatation($item);
} else {
$quotation = ExchangeBusiness::getquatation($item);
}
if (!$quotation) {
//使用Coin服务
try {
$coinServer = CoinFactory::createCoin($item, '', '');
$tickerData[$item]['rmb'] = $coinServer->getPrice();
$coinServer = CoinFactory::createCoin($item, '', '');
$tickerData[$item]['rmb'] = $coinServer->getPrice();
$tickerData[$item]['last'] = $coinServer->getDollar();
$tickerData[$item]['low'] = $coinServer->getLow();
$tickerData[$item]['low'] = $coinServer->getLow();
$tickerData[$item]['high'] = $coinServer->getHigh();
$tickerData[$item]['open'] = $coinServer->getDollar();
$tickerData[$item]['usd'] = $coinServer->getDollar();
$coinServer->__destruct();
} catch (\Exception $exception) {
$tickerData[$item]['rmb'] = 0;
$tickerData[$item]['rmb'] = 0;
$tickerData[$item]['last'] = 0;
$tickerData[$item]['low'] = 0;
$tickerData[$item]['low'] = 0;
$tickerData[$item]['high'] = 0;
$tickerData[$item]['open'] = 0;
$tickerData[$item]['usd'] = 0;
\Yii::error($exception->getMessage());
}
}else{
} else {
$tickerData[$item] = $quotation;
}
}
return ['code' => 0,'data' => $tickerData,'msg' => '行情获取成功'];
return ['code' => 0, 'data' => $tickerData, 'msg' => '行情获取成功'];
}
}
......@@ -94,13 +99,13 @@ class ServiceController extends BaseController
$platform_id = Yii::$app->request->get('platform_id', 6);
$type = Yii::$app->request->get('type', 1);
$coin_recommendItems = $this->coinRecommendList($platform_id, $type);
$fields =['id', 'sid', 'icon', 'name', 'nickname','chain','platform'];
$rows = Coin::getSelectList(1, 999, $fields,[['in','id',$coin_recommendItems]]);
$fields = ['id', 'sid', 'icon', 'name', 'nickname', 'chain', 'platform'];
$rows = Coin::getSelectList(1, 999, $fields, [['in', 'id', $coin_recommendItems]]);
foreach ($rows['data'] as $key => &$value) {
$nickname = json_decode($value['nickname'], true);
$value['nickname'] = $nickname[$this->lang];
}
return ['code' => 0,'data' => $rows,'msg' => '币种列表获取成功'];
return ['code' => 0, 'data' => $rows, 'msg' => '币种列表获取成功'];
}
/**
......@@ -109,9 +114,9 @@ class ServiceController extends BaseController
*/
private function coinRecommendList($platform_id, $type = 1)
{
$recommend_list = CoinRecommend::find()->select('cid')->where(['platform_id' => $platform_id ,'type' => $type])->all();
if($recommend_list){
$coin_ids = array_column($recommend_list,'cid');
$recommend_list = CoinRecommend::find()->select('cid')->where(['platform_id' => $platform_id, 'type' => $type])->all();
if ($recommend_list) {
$coin_ids = array_column($recommend_list, 'cid');
return $coin_ids;
}
return [];
......@@ -124,15 +129,15 @@ class ServiceController extends BaseController
public function actionFee()
{
$request = Yii::$app->request;
$coin = $request->post('name');
if(!$coin){
return ['code' => 1,'data' => [],'msg' => '币种不能为空'];
$coin = $request->post('name');
if (!$coin) {
return ['code' => 1, 'data' => [], 'msg' => '币种不能为空'];
}
$fee = MinerFee::find()->where(['platform' => $coin,'type' => 2])->select('id,platform,type,fee,create_at,update_at')->asArray()->one();
if(!$fee){
return ['code' => 1,'data' => [],'msg' => '旷工费未设置'];
$fee = MinerFee::find()->where(['platform' => $coin, 'type' => 2])->select('id,platform,type,fee,create_at,update_at')->asArray()->one();
if (!$fee) {
return ['code' => 1, 'data' => [], 'msg' => '旷工费未设置'];
}
return ['code' => 0,'data' => $fee,'msg' => '旷工费获取成功'];
return ['code' => 0, 'data' => $fee, 'msg' => '旷工费获取成功'];
}
/**
......@@ -141,8 +146,8 @@ class ServiceController extends BaseController
*/
private function getRate()
{
$exchange = ExchangeFactory::createExchange("Bty");
$rate = $exchange->getTicker("BTY", "USDT");
$exchange = ExchangeFactory::createExchange("Bty");
$rate = $exchange->getTicker("BTY", "USDT");
return (float)$rate['rmb'] / $rate['last'];
}
......@@ -153,12 +158,12 @@ class ServiceController extends BaseController
public function actionChain()
{
$request = Yii::$app->request;
$currency = $request->post('currency','');
$currency = $request->post('currency', '');
$coin = Coin::find()->where(['name' => $currency])->select('name,nickname,chain')->asArray()->one();
if($coin){
return ['code' => 0,'data' => $coin];
if ($coin) {
return ['code' => 0, 'data' => $coin];
}
return ['code' => -1,'msg' => '币种不存在'];
return ['code' => -1, 'msg' => '币种不存在'];
}
}
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