Commit 2b85dcd2 authored by rlgy's avatar rlgy

CoinServer update&index API

parent d26e197c
......@@ -20,6 +20,15 @@ class BaseResponse extends Response
$this->data = ['code' => $excpetion->getCode(), 'msg' => $excpetion->getMessage()];
}
//TODO 在这里对数据进行format,这样控制器中可以直接return一个array,保存到数据域data中即可,eg:['code'=>0,'data'=>$data]
$data = \Yii::$app->response->data;
if (empty($data)) {
$return['code'] = 1;
$return['msg'] = '数据为空';
} else {
$return['code'] = 0;
$return['data'] = $data;
}
\Yii::$app->response->data = $return;
parent::send();
}
}
\ No newline at end of file
......@@ -123,9 +123,10 @@ class CoinController extends BaseController
/**
* app首页接口
*/
public function actionSearchByNames()
public function actionCoinIndex()
{
$names = Yii::$app->request->post('names');
$names = Yii::$app->request->post('names');
$condition = [['in', 'name', $names]];
Yii::$app->response->data = CoinBusiness::getApiListForIndex($condition);
}
}
\ No newline at end of file
......@@ -82,4 +82,25 @@ class CoinBusiness
}
return $rows;
}
/**
* @param array $condition 需要的币种sid列表
* @return array
*/
public static function getApiListForIndex($condition = [])
{
$rows = Coin::getSelectList(1, 999, ['id', 'sid', 'icon', 'name', 'nickname'], $condition);
if ($rows['count'] > 0) {
$rows = $rows['data'];
foreach ($rows as $key => $row) {
$coinServer = new CoinService($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 [];
}
}
\ No newline at end of file
......@@ -56,6 +56,24 @@ class Coin extends BaseActiveRecord
}
/**
* 获取币种信息列表
* @param int $page
* @param int $limit
* @param array $condition
* @return array|\yii\db\ActiveRecord[]
*/
public static function getSelectList($page = 1, $limit = 10, $columns = [], $condition = [])
{
$query = self::find();
foreach ($condition as $item) {
$query = $query->andWhere($item);
}
$count = $query->count();
$data = $query->select($columns)->offset(($page - 1) * 10)->limit($limit)->asArray()->all();
return ['count' => $count, 'data' => $data];
}
/**
* 添加一个币种信息
*/
public function addOne($params)
......
......@@ -18,6 +18,402 @@ use common\helpers\Curl;
*/
class CoinService
{
//行情信息
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; //流通总量
private $content;//行情html数据
public $id;//币种id
public $sid;//币种sid
public $cache_key_prifx = 'quotation_coin_';
public function __construct($id, $sid)
{
$this->id = $id;
$this->sid = $sid;
}
public function __destruct()
{
unset($this->content);
}
public function init()
{
$url = Yii::$app->params['feixiaohao_domain'] . Yii::$app->params['feixiaohao_page']['currencies'] . $this->sid . '/';
$ch = new Curl();
$content = $ch->get($url, true);
$this->content = $content;
}
/**
* @return mixed
*/
public function getPrice()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_price');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
$pattern = '/<div class=coinprice>(.*?)<span class=(tags-red|tags-green)>(.*?)<\/span><\/div>/is';
preg_match_all($pattern, $this->content, $matchs, PREG_SET_ORDER);
$this->setPrice($matchs[0][1]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_price', $this->price,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setPrice($result);
}
return $this->price;
}
/**
* @param mixed $price
*/
public function setPrice($price)
{
$this->price = $price;
}
/**
* @return mixed
*/
public function getDollar()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_usd');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
preg_match_all('/<div class=sub><span>(.*?)<\/span>(.*?)<span>(.*?)<\/span><\/div>/is', $this->content,
$matchs,
PREG_SET_ORDER);
$this->setDollar($matchs[0][1]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_usd', $this->dollar,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setDollar($result);
}
return $this->dollar;
}
/**
* @param mixed $dollar
*/
public function setDollar($dollar)
{
$this->dollar = $dollar;
}
/**
* @return mixed
*/
public function getBtc()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_btc');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
preg_match_all('/<div class=sub><span>(.*?)<\/span>(.*?)<span>(.*?)<\/span><\/div>/is', $this->content,
$matchs,
PREG_SET_ORDER);
$this->setBtc($matchs[0][3]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_btc', $this->btc,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setBtc($result);
}
return $this->btc;
}
/**
* @param mixed $btc
*/
public function setBtc($btc)
{
$this->btc = $btc;
}
/**
* @return mixed
*/
public function getHigh()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_high');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
preg_match_all('/<div class=lowHeight>(.*?)<\/div><div class=sub>/is', $$this->content, $matchs);
preg_match_all('/<span class=value>(.*?)<\/span>/is', $matchs[1][0], $matchs);
$this->setHigh($matchs[1][0]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_high', $this->high,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setBtc($result);
}
return $this->high;
}
/**
* @param mixed $high
*/
public function setHigh($high)
{
$this->high = $high;
}
/**
* @return mixed
*/
public function getLow()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_low');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
preg_match_all('/<div class=lowHeight>(.*?)<\/div><div class=sub>/is', $$this->content, $matchs);
preg_match_all('/<span class=value>(.*?)<\/span>/is', $matchs[1][0], $matchs);
$this->setLow($matchs[1][1]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_low', $this->low,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setLow($result);
}
return $this->low;
}
/**
* @param mixed $low
*/
public function setLow($low)
{
$this->low = $low;
}
/**
* @return mixed
*/
public function getChange()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_change');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
$pattern = '/<div class=coinprice>(.*?)<span class=(tags-red|tags-green)>(.*?)<\/span><\/div>/is';
preg_match_all($pattern, $this->content, $matchs, PREG_SET_ORDER);
$this->setChange($matchs[0][3]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_change', $this->change,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setChange($result);
}
return $this->change;
}
/**
* @param mixed $change
*/
public function setChange($change)
{
$this->change = $change;
}
/**
* @return mixed
*/
public function getRank()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_rank');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
$pattern = '/<div class=value>(.*?)<span class=tag-marketcap>(.*?)<\/span><\/div><div class=sub>(.*?)<\/div><div class=sub>(.*?)<\/div>/is';
preg_match_all($pattern, $$this->content, $matchs, PREG_SET_ORDER);
$this->setRank($matchs[0][2]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_rank', $this->rank,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setRank($result);
}
return $this->rank;
}
/**
* @param mixed $rank
*/
public function setRank($rank)
{
$this->rank = $rank;
}
/**
* @return mixed
*/
public function getCirculateValueRmb()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_circulate_value_rmb');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
$pattern = '/<div class=value>(.*?)<span class=tag-marketcap>(.*?)<\/span><\/div><div class=sub>(.*?)<\/div><div class=sub>(.*?)<\/div>/is';
preg_match_all($pattern, $$this->content, $matchs, PREG_SET_ORDER);
$this->setCirculateValueRmb($matchs[0][1]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_circulate_value_rmb',
$this->circulate_value_rmb,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setCirculateValueRmb($result);
}
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()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_circulate_value_usd');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
$pattern = '/<div class=value>(.*?)<span class=tag-marketcap>(.*?)<\/span><\/div><div class=sub>(.*?)<\/div><div class=sub>(.*?)<\/div>/is';
preg_match_all($pattern, $$this->content, $matchs, PREG_SET_ORDER);
$this->setCirculateValueUsd($matchs[0][3]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_circulate_value_usd',
$this->circulate_value_usd,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setCirculateValueUsd($result);
}
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()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_circulate_value_btc');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
$pattern = '/<div class=value>(.*?)<span class=tag-marketcap>(.*?)<\/span><\/div><div class=sub>(.*?)<\/div><div class=sub>(.*?)<\/div>/is';
preg_match_all($pattern, $$this->content, $matchs, PREG_SET_ORDER);
$this->setCirculateValueBtc($matchs[0][4]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_circulate_value_btc',
$this->circulate_value_btc,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setCirculateValueBtc($result);
}
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()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_publish_count');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
$pattern = '/<div class=tit>流通量<\/div><div class=value>(.*?)<\/div><div class=tit>总发行量<\/div><div class=value>(.*?)<\/div>/is';
preg_match_all($pattern, $$this->content, $matchs, PREG_SET_ORDER);
$this->setPublishCount($matchs[0][2]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_publish_count',
$this->publish_count,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setPublishCount($result);
}
return $this->publish_count;
}
/**
* @param mixed $publish_count
*/
public function setPublishCount($publish_count)
{
$this->publish_count = $publish_count;
}
/**
* @return mixed
*/
public function getCirculateCount()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_circulate_count');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
$pattern = '/<div class=tit>流通量<\/div><div class=value>(.*?)<\/div><div class=tit>总发行量<\/div><div class=value>(.*?)<\/div>/is';
preg_match_all($pattern, $$this->content, $matchs, PREG_SET_ORDER);
$this->setCirculateCount($matchs[0][1]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_publish_count',
$this->circulate_count,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setCirculateCount($result);
}
return $this->circulate_count;
}
/**
* @param mixed $circulate_count
*/
public function setCirculateCount($circulate_count)
{
$this->circulate_count = $circulate_count;
}
/**
* 爬取交易所列表,默认eos
*
......
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