Commit 123482d1 authored by rlgy's avatar rlgy

API规范

parent a4cf284f
...@@ -25,8 +25,9 @@ class BaseResponse extends Response ...@@ -25,8 +25,9 @@ class BaseResponse extends Response
$return['code'] = 1; $return['code'] = 1;
$return['msg'] = '数据为空'; $return['msg'] = '数据为空';
} else { } else {
$return['code'] = 0; $return['code'] = 0;
$return['data'] = $data; $return['count'] = count($data);
$return['data'] = $data;
} }
\Yii::$app->response->data = $return; \Yii::$app->response->data = $return;
parent::send(); parent::send();
......
...@@ -84,6 +84,10 @@ class CoinController extends BaseController ...@@ -84,6 +84,10 @@ class CoinController extends BaseController
Yii::$app->response->data = $data; Yii::$app->response->data = $data;
} }
/**
* 单币种按照id查询
* @return array|null|\yii\db\ActiveRecord
*/
public function actionGetCoinById() public function actionGetCoinById()
{ {
$request = Yii::$app->request; $request = Yii::$app->request;
...@@ -99,12 +103,11 @@ class CoinController extends BaseController ...@@ -99,12 +103,11 @@ class CoinController extends BaseController
*/ */
public function actionGetRecList() public function actionGetRecList()
{ {
$request = Yii::$app->request; $request = Yii::$app->request;
$page = $request->post('page', 1); $page = $request->post('page', 1);
$limit = $request->post('limit', 999); $limit = $request->post('limit', 999);
$condition = [['recommend' => '1']]; $condition = [['recommend' => '1']];
$data = CoinBusiness::getApiList($page, $limit, $condition); return CoinBusiness::getApiList($page, $limit, $condition);
Yii::$app->response->data = $data;
} }
/** /**
...@@ -120,13 +123,13 @@ class CoinController extends BaseController ...@@ -120,13 +123,13 @@ class CoinController extends BaseController
if ($coin) { if ($coin) {
$miner_fee = $coin->minerFee; $miner_fee = $coin->minerFee;
if (empty($miner_fee)) { if (empty($miner_fee)) {
return ['code' => 1, 'msg' => '数据为空']; return [];
} }
} else { } else {
//如果coin为null,$coin->minerFee会抛出Trying to get property 'minerFee' of non-object",code=>8 //如果coin为null,$coin->minerFee会抛出Trying to get property 'minerFee' of non-object",code=>8
throw new Exception('8', '币种不存在'); throw new Exception('8', '币种不存在');
} }
return ['code' => 0, 'data' => $miner_fee]; return [$miner_fee];
} }
...@@ -135,11 +138,15 @@ class CoinController extends BaseController ...@@ -135,11 +138,15 @@ class CoinController extends BaseController
*/ */
public function actionCoinIndex() public function actionCoinIndex()
{ {
$names = Yii::$app->request->post('names'); $names = Yii::$app->request->post('names');
$condition = [['in', 'name', $names]]; $condition = [['in', 'name', $names]];
Yii::$app->response->data = CoinBusiness::getApiListForIndex($condition); return CoinBusiness::getApiListForIndex($condition);
} }
/**
* 按照名称搜索币种
* @return array
*/
public function actionSearchCoinByName() public function actionSearchCoinByName()
{ {
$request = Yii::$app->request; $request = Yii::$app->request;
...@@ -148,9 +155,7 @@ class CoinController extends BaseController ...@@ -148,9 +155,7 @@ class CoinController extends BaseController
$limit = $request->post('limit', 10); $limit = $request->post('limit', 10);
if ($name) { if ($name) {
$condition = [['or', ['like', 'name', $name], ['like', 'nickname', $name]]]; $condition = [['or', ['like', 'name', $name], ['like', 'nickname', $name]]];
$row = Coin::getSelectList($page, $limit, ['id', 'sid', 'icon', 'name', 'nickname', 'chain'], return CoinBusiness::SearchByName($page, $limit, $condition);
$condition);
return $row;
} }
} }
} }
\ No newline at end of file
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
namespace common\business; namespace common\business;
use common\models\pwallet\Coin; use common\models\pwallet\Coin;
use common\service\coin\CoinFactory;
use common\service\CoinService; use common\service\CoinService;
/** /**
...@@ -70,20 +71,21 @@ class CoinBusiness ...@@ -70,20 +71,21 @@ class CoinBusiness
{ {
$rows = Coin::getList($page, $limit, $condition); $rows = Coin::getList($page, $limit, $condition);
if ($rows['count'] > 0) { if ($rows['count'] > 0) {
$datas = $rows['data']; $rows = $rows['data'];
foreach ($datas as $key => $value) { foreach ($rows as $key => $value) {
//获取行情信息 //获取行情信息
if (strtoupper($value['name']) == 'BTY') { if (strtoupper($value['name']) == 'BTY') {
$rows['data'][$key]['quotation'] = CoinService::quotationBTY(); $rows[$key]['quotation'] = CoinService::quotationBTY();
continue; continue;
} }
$rows['data'][$key]['quotation'] = CoinService::quotation($value['sid']); $rows[$key]['quotation'] = CoinService::quotation($value['sid']);
} }
} }
return $rows; return $rows;
} }
/** /**
* 根据name返回币种信息
* @param array $condition 需要的币种sid列表 * @param array $condition 需要的币种sid列表
* @return array * @return array
*/ */
...@@ -93,7 +95,31 @@ class CoinBusiness ...@@ -93,7 +95,31 @@ class CoinBusiness
if ($rows['count'] > 0) { if ($rows['count'] > 0) {
$rows = $rows['data']; $rows = $rows['data'];
foreach ($rows as $key => $row) { foreach ($rows as $key => $row) {
$coinServer = new CoinService($row['id'], $row['sid']); $coinServer = CoinFactory::createCoin($row['name'], $row['id'], $row['sid']);
$rows[$key]['sid'] = ucfirst($rows[$key]['sid']);
$rows[$key]['rmb'] = $coinServer->getPrice();
$rows[$key]['usd'] = $coinServer->getDollar();
$coinServer->__destruct();
}
return $rows;
}
return [];
}
/**
* 根据名称搜索
* @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) {
$coinServer = CoinFactory::createCoin($row['name'], $row['id'], $row['sid']);
$rows[$key]['sid'] = ucfirst($rows[$key]['sid']); $rows[$key]['sid'] = ucfirst($rows[$key]['sid']);
$rows[$key]['rmb'] = $coinServer->getPrice(); $rows[$key]['rmb'] = $coinServer->getPrice();
$rows[$key]['usd'] = $coinServer->getDollar(); $rows[$key]['usd'] = $coinServer->getDollar();
...@@ -106,6 +132,7 @@ class CoinBusiness ...@@ -106,6 +132,7 @@ class CoinBusiness
/** /**
* 获取币种的所有信息 * 获取币种的所有信息
* @return array
*/ */
public static function getCoinAllById($id) public static function getCoinAllById($id)
{ {
...@@ -113,9 +140,13 @@ class CoinBusiness ...@@ -113,9 +140,13 @@ class CoinBusiness
if ($row) { if ($row) {
//TODO 获取行情 //TODO 获取行情
$row['quotation'] = CoinService::quotation($row['sid']); $row['quotation'] = CoinService::quotation($row['sid']);
//行情BTY
if (strtoupper($row['name']) == 'BTY') {
$row['quotation'] = CoinService::quotationBTY();
}
//TODO 获取交易所详情 //TODO 获取交易所详情
$row['exchange'] = CoinService::exchange($row['sid']); $row['exchange'] = CoinService::exchange($row['sid']);
} }
return $row ? $row : []; return $row ? [$row] : [];
} }
} }
\ No newline at end of file
...@@ -69,7 +69,7 @@ class Coin extends BaseActiveRecord ...@@ -69,7 +69,7 @@ class Coin extends BaseActiveRecord
$query = $query->andWhere($item); $query = $query->andWhere($item);
} }
$count = $query->count(); $count = $query->count();
$data = $query->select($columns)->offset(($page - 1) * 10)->limit($limit)->asArray()->all(); $data = $query->select($columns)->offset(($page - 1) * $limit)->limit($limit)->asArray()->all();
return ['count' => $count, 'data' => $data]; return ['count' => $count, 'data' => $data];
} }
......
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-19
* Time: 下午2:25
*/
namespace common\service\coin;
abstract class Coin
{
//行情信息
protected $price; //价格
protected $dollar; //价格美元
protected $btc; //价格btc
protected $high; //最高价
protected $low; //最低价
protected $change; //涨幅(跌幅)
protected $rank; //流通市值排名
protected $circulate_value_rmb; //流通市值人民币
protected $circulate_value_usd; //流通市值美元
protected $circulate_value_btc; //流通市值btc
protected $publish_count; //发行总量
protected $circulate_count; //流通总量
protected $content;//行情html数据
public $id;//币种id
public $sid;//币种sid
public $cache_key_prifx = 'quotation_coin_';
/**
* @return mixed
*/
public function getPrice()
{
return $this->price;
}
/**
* @param mixed $price
*/
public function setPrice($price)
{
$this->price = $price;
}
/**
* @return mixed
*/
public function getDollar()
{
return $this->dollar;
}
/**
* @param mixed $dollar
*/
public function setDollar($dollar)
{
$this->dollar = $dollar;
}
/**
* @return mixed
*/
public function getBtc()
{
return $this->btc;
}
/**
* @param mixed $btc
*/
public function setBtc($btc)
{
$this->btc = $btc;
}
/**
* @return mixed
*/
public function getHigh()
{
return $this->high;
}
/**
* @param mixed $high
*/
public function setHigh($high)
{
$this->high = $high;
}
/**
* @return mixed
*/
public function getLow()
{
return $this->low;
}
/**
* @param mixed $low
*/
public function setLow($low)
{
$this->low = $low;
}
/**
* @return mixed
*/
public function getChange()
{
return $this->change;
}
/**
* @param mixed $change
*/
public function setChange($change)
{
$this->change = $change;
}
/**
* @return mixed
*/
public function getRank()
{
return $this->rank;
}
/**
* @param mixed $rank
*/
public function setRank($rank)
{
$this->rank = $rank;
}
/**
* @return mixed
*/
public function getCirculateValueRmb()
{
return $this->circulate_value_rmb;
}
/**
* @param mixed $circulate_value_rmb
*/
public function setCirculateValueRmb($circulate_value_rmb)
{
$this->circulate_value_rmb = $circulate_value_rmb;
}
/**
* @return mixed
*/
public function getCirculateValueUsd()
{
return $this->circulate_value_usd;
}
/**
* @param mixed $circulate_value_usd
*/
public function setCirculateValueUsd($circulate_value_usd)
{
$this->circulate_value_usd = $circulate_value_usd;
}
/**
* @return mixed
*/
public function getCirculateValueBtc()
{
return $this->circulate_value_btc;
}
/**
* @param mixed $circulate_value_btc
*/
public function setCirculateValueBtc($circulate_value_btc)
{
$this->circulate_value_btc = $circulate_value_btc;
}
/**
* @return mixed
*/
public function getPublishCount()
{
return $this->publish_count;
}
/**
* @param mixed $publish_count
*/
public function setPublishCount($publish_count)
{
$this->publish_count = $publish_count;
}
/**
* @return mixed
*/
public function getCirculateCount()
{
return $this->circulate_count;
}
/**
* @param mixed $circulate_count
*/
public function setCirculateCount($circulate_count)
{
$this->circulate_count = $circulate_count;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-19
* Time: 下午2:20
*/
namespace common\service\coin;
class CoinBTYService extends Coin implements CoinInterface
{
public function __construct($id, $sid)
{
}
public function init()
{
// TODO: Implement init() method.
}
public function __destruct()
{
// TODO: Implement __destruct() method.
}
public function getPrice()
{
return parent::getPrice(); // TODO: Change the autogenerated stub
}
public function setPrice($price)
{
parent::setPrice($price); // TODO: Change the autogenerated stub
}
public function getDollar()
{
return parent::getDollar(); // TODO: Change the autogenerated stub
}
public function setDollar($dollar)
{
parent::setDollar($dollar); // TODO: Change the autogenerated stub
}
public function getBtc()
{
return parent::getBtc(); // TODO: Change the autogenerated stub
}
public function setBtc($btc)
{
parent::setBtc($btc); // TODO: Change the autogenerated stub
}
public function getHigh()
{
return parent::getHigh(); // TODO: Change the autogenerated stub
}
public function setHigh($high)
{
parent::setHigh($high); // TODO: Change the autogenerated stub
}
public function getLow()
{
return parent::getLow(); // TODO: Change the autogenerated stub
}
public function setLow($low)
{
parent::setLow($low); // TODO: Change the autogenerated stub
}
public function getChange()
{
return parent::getChange(); // TODO: Change the autogenerated stub
}
public function setChange($change)
{
parent::setChange($change); // TODO: Change the autogenerated stub
}
public function getRank()
{
return parent::getRank(); // TODO: Change the autogenerated stub
}
public function setRank($rank)
{
parent::setRank($rank); // TODO: Change the autogenerated stub
}
public function getCirculateValueRmb()
{
return parent::getCirculateValueRmb(); // TODO: Change the autogenerated stub
}
public function setCirculateValueRmb($circulate_value_rmb)
{
parent::setCirculateValueRmb($circulate_value_rmb); // TODO: Change the autogenerated stub
}
public function getCirculateValueUsd()
{
return parent::getCirculateValueUsd(); // TODO: Change the autogenerated stub
}
public function setCirculateValueUsd($circulate_value_usd)
{
parent::setCirculateValueUsd($circulate_value_usd); // TODO: Change the autogenerated stub
}
public function getCirculateValueBtc()
{
return parent::getCirculateValueBtc(); // TODO: Change the autogenerated stub
}
public function setCirculateValueBtc($circulate_value_btc)
{
parent::setCirculateValueBtc($circulate_value_btc); // TODO: Change the autogenerated stub
}
public function getPublishCount()
{
return parent::getPublishCount(); // TODO: Change the autogenerated stub
}
public function setPublishCount($publish_count)
{
parent::setPublishCount($publish_count); // TODO: Change the autogenerated stub
}
public function getCirculateCount()
{
return parent::getCirculateCount(); // TODO: Change the autogenerated stub
}
public function setCirculateCount($circulate_count)
{
parent::setCirculateCount($circulate_count); // TODO: Change the autogenerated stub
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-19
* Time: 下午1:56
*/
namespace common\service\coin;
class CoinFactory
{
public static function createCoin($coinName = '', $id, $sid)
{
$coinName = strtoupper($coinName);
$className = __NAMESPACE__ . '\Coin' . $coinName . 'Service';
if (class_exists($className)) {
return new $className($id, $sid);
} else {
return new CoinService($id, $sid);
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-19
* Time: 下午2:12
*/
namespace common\service\coin;
interface CoinInterface
{
//币种实例化接口
public function __construct($id, $sid);
//币种初始化
public function init();
//币种销毁
public function __destruct();
}
\ No newline at end of file
This diff is collapsed.
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