Commit 4f70fb8a authored by rlgy's avatar rlgy

结构规范

parent cf355d7f
......@@ -10,7 +10,6 @@ namespace common\business;
use common\models\pwallet\Coin;
use common\service\coin\CoinFactory;
use common\service\CoinService;
/**
* 币种逻辑层
......@@ -34,7 +33,8 @@ class CoinBusiness
$datas = $rows['data'];
foreach ($datas as $key => $value) {
//获取交易所信息
$rows['data'][$key]['exchange'] = CoinService::exchange_count($value['sid']);
$coin = CoinFactory::createCoin($value['name'], $value['id'], $value['sid']);
$rows['data'][$key]['exchange'] = $coin->exchange_count();
}
}
return $rows;
......@@ -51,7 +51,8 @@ class CoinBusiness
if ($id) {
$coin = Coin::findOne(['id' => $id]);
if ($coin) {
$datas = CoinService::exchange($coin->sid);
$model = CoinFactory::createCoin($coin->name, $coin->id, $coin->sid);
$datas = $model->exchange();
if ($datas['count'] > 0) {
return $datas['data'];
}
......@@ -73,12 +74,8 @@ class CoinBusiness
if ($rows['count'] > 0) {
$rows = $rows['data'];
foreach ($rows as $key => $value) {
//获取行情信息
if (strtoupper($value['name']) == 'BTY') {
$rows[$key]['quotation'] = CoinService::quotationBTY();
continue;
}
$rows[$key]['quotation'] = CoinService::quotation($value['sid']);
$coin = CoinFactory::createCoin($value['name'], $value['id'], $value['sid']);
$rows[$key]['quotation'] = $coin->quotation();
}
}
return $rows;
......@@ -138,14 +135,11 @@ class CoinBusiness
{
$row = Coin::find()->where(['id' => $id])->asArray()->One();
if ($row) {
$coin = CoinFactory::createCoin($row['name'], $row['id'], $row['sid']);
//TODO 获取行情
$row['quotation'] = CoinService::quotation($row['sid']);
//行情BTY
if (strtoupper($row['name']) == 'BTY') {
$row['quotation'] = CoinService::quotationBTY();
}
$row['quotation'] = $coin->quotation($row['sid']);
//TODO 获取交易所详情
$row['exchange'] = CoinService::exchange($row['sid']);
$row['exchange'] = $coin->exchange($row['sid']);
}
return $row ? [$row] : [];
}
......
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-5
* Time: 上午11:17
*/
namespace common\service;
use Yii;
use common\helpers\Curl;
/**
* 币种服务类
* Class CoinService
* @package common\service
*/
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
*
* @param $name string 币种名称
* @return array
*/
public static function exchange($name = 'eos')
{
//缓存数据
$key = [
__CLASS__,
__METHOD__,
'coin',//表名
$name,//sid
];
$result = Yii::$app->cache->get($key);
if ($result === false) {
$url = Yii::$app->params['feixiaohao_domain'] . Yii::$app->params['feixiaohao_page']['coinmarket'] . $name . '/';
$ch = new Curl();
try {
$content = $ch->get($url, true);
preg_match_all("/<div class=boxContain><table class=table3 id=markets>(.*)?<\/table>/is", $content,
$matchs);
$content = $matchs[1][0];
preg_match_all('/<a href="(.*?)"(.*?)alt=(.*?)>/is', $content, $matchs);
//匹配到了
//保存匹配数据
$count = count($matchs[1]);
$result = [];
$r = [];
for ($i = 0; $i < $count; $i++) {
if (!in_array($matchs[3][$i], $r)) {
$r[] = $matchs[3][$i];
//爬取正确的官网
if (strpos($matchs[1][$i], '/exchange') === false) {
$matchs[1][$i] .= 'https:';
} else {
$url = 'https://www.feixiaohao.com' . $matchs[1][$i];
$content = $ch->get($url, true);
preg_match_all('/官网地址:<a(.*?)>(.*?)<\/a><\/span>/', $content, $matchs2);
$matchs[1][$i] = $matchs2[2][0];
}
$result[] = ['url' => $matchs[1][$i], 'name' => $matchs[3][$i]];
}
}
unset($content, $i, $r, $url, $matchs2);
Yii::$app->cache->set($key, $result, Yii::$app->params['curl_cache_time']['exchange']);//set caching
return ['count' => count($result), 'data' => $result];
} catch (\Exception $exception) {
return ['count' => 0, 'data' => []];
}
}
return ['count' => count($result), 'data' => $result];
}
/**
* 爬取交易所数量
* @param string $sid
* @return integer|bool
*/
public static function exchange_count($sid = 'eos')
{
$key = [
__CLASS__,
__METHOD__,
'coin',//表名
$sid,//sid
];
$result = Yii::$app->cache->get($key);
if (!$result) {
$url = Yii::$app->params['feixiaohao_domain'] . Yii::$app->params['feixiaohao_page']['coinmarket'] . $sid . '/';
$ch = new Curl();
try {
$content = $ch->get($url, true);
preg_match_all("/<div class=boxContain><table class=table3 id=markets>(.*)?<\/table>/is", $content,
$matchs);
$content = $matchs[1][0];
preg_match_all('/alt=(.*?)>/is', $content, $matchs);
//匹配到了
$result = array_unique($matchs[1]);
$result = count($result);
unset($content, $matchs2);
Yii::$app->cache->set($key, $result,
Yii::$app->params['curl_cache_time']['exchange_count']);//set caching
} catch (\Exception $exception) {
}
}
return $result ? $result : 0;
}
/**
* 爬取币种的实时行情,默认eos
*
* @param string $sid
* @return object|string
*/
public static function quotation($sid = 'eos')
{
//使用缓存
$key = [
__CLASS__,
__METHOD__,
'coin',//表名
$sid,//sid
];
$result = Yii::$app->cache->get($key);
if ($result === false) {
$url = Yii::$app->params['feixiaohao_domain'] . Yii::$app->params['feixiaohao_page']['currencies'] . $sid . '/';
$ch = new Curl();
try {
$content = $ch->get($url, true);
$result = [
'price' => '',//价格
'dollar' => '',//价格美元
'btc' => '',//价格btc
'high' => '',//最高价
'low' => '',//最低价
'change' => '',//涨幅(跌幅)
'rank' => '',//流通市值排名
'circulate_value_rmb' => '',//流通市值人民币
'circulate_value_usd' => '',//流通市值美元
'circulate_value_btc' => '',//流通市值btc
'publish_count' => '',//发行总量
'circulate_count' => '',//流通总量
];
//价格与涨幅
$pattern = '/<div class=coinprice>(.*?)<span class=(tags-red|tags-green)>(.*?)<\/span><\/div>/is';
preg_match_all($pattern, $content, $matchs, PREG_SET_ORDER);
$result['price'] = $matchs[0][1];
$result['change'] = $matchs[0][3];
//最高最低值
preg_match_all('/<div class=lowHeight>(.*?)<\/div><div class=sub>/is', $content, $matchs);
preg_match_all('/<span class=value>(.*?)<\/span>/is', $matchs[1][0], $matchs);
$result['high'] = $matchs[1][0];
$result['low'] = $matchs[1][1];
//美元 btc
preg_match_all('/<div class=sub><span>(.*?)<\/span>(.*?)<span>(.*?)<\/span><\/div>/is', $content,
$matchs,
PREG_SET_ORDER);
$result['dollar'] = $matchs[0][1];
$result['btc'] = $matchs[0][3];
//爬取市值与排名
$pattern = '/<div class=value>(.*?)<span class=tag-marketcap>(.*?)<\/span><\/div><div class=sub>(.*?)<\/div><div class=sub>(.*?)<\/div>/is';
preg_match_all($pattern, $content, $matches, PREG_SET_ORDER);
$result['circulate_value_rmb'] = $matches[0][1];
$result['rank'] = $matches[0][2];
$result['circulate_value_usd'] = $matches[0][3];
$result['circulate_value_btc'] = $matches[0][4];
//爬取流通总量,发行总量
$pattern = '/<div class=tit>流通量<\/div><div class=value>(.*?)<\/div><div class=tit>总发行量<\/div><div class=value>(.*?)<\/div>/is';
preg_match_all($pattern, $content, $matches, PREG_SET_ORDER);
$result['circulate_count'] = $matches[0][1];
$result['publish_count'] = $matches[0][2];
$result = array_map(function ($value) {
return trim($value);
}, $result);
Yii::$app->cache->set($key, $result, Yii::$app->params['curl_cache_time']['quotation']);//set cache
return $result;
} catch (\Exception $exception) {
return new \stdClass();
}
}
return $result;
}
/**
* 快速修复方法,之后再改
*/
public static function quotationBTY()
{
$result = [
'price' => '',//价格
'dollar' => '',//价格美元
'btc' => '',//价格btc
'high' => '',//最高价
'low' => '',//最低价
'change' => '',//涨幅(跌幅)
'rank' => '',//流通市值排名
'circulate_value_rmb' => '',//流通市值人民币
'circulate_value_usd' => '',//流通市值美元
'circulate_value_btc' => '',//流通市值btc
'publish_count' => '',//发行总量
'circulate_count' => '',//流通总量
];
$ch = curl_init('https://kdata.zhaobi.com:4062/kdata?datafile=db&c=BTYUSDT&p=H1&action=init&count=24&ind=volumes&out=json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$content = curl_exec($ch);
$content = json_decode($content, true);
$content = $content['main']['y'];
$min = 10e9;
$max = 0;
foreach ($content as $item) {
for ($i = 0; $i < 4; $i++) {
$min = min($min, $item[$i]);
$max = max($max, $item[$i]);
}
}
$change = ($content[23][3] - $content[0][0]) / $content[0][0] * 100;
$result['dollar'] = $content[23][3];
$result['low'] = $min;
$result['high'] = $max;
$result['change'] = sprintf("%0.2f%%", $change);
return $result;
}
}
\ No newline at end of file
......@@ -10,6 +10,23 @@ namespace common\service\coin;
class CoinBTYService extends Coin implements CoinInterface
{
public function exchange()
{
// TODO: Implement exchange() method.
return ['count' => 0, 'data' => []];
}
public function exchange_count()
{
// TODO: Implement exchange_count() method.
return 0;
}
public function quotation()
{
// TODO: Implement quotation() method.
}
public function __construct($id, $sid)
{
......
......@@ -18,4 +18,10 @@ interface CoinInterface
//币种销毁
public function __destruct();
public function exchange();
public function exchange_count();
public function quotation();
}
\ No newline at end of file
......@@ -399,18 +399,18 @@ class CoinService extends Coin implements CoinInterface
* @param $name string 币种名称
* @return array
*/
public static function exchange($name = 'eos')
public function exchange()
{
//缓存数据
$key = [
__CLASS__,
__METHOD__,
'coin',//表名
$name,//sid
$this->sid,//sid
];
$result = Yii::$app->cache->get($key);
if ($result === false) {
$url = Yii::$app->params['feixiaohao_domain'] . Yii::$app->params['feixiaohao_page']['coinmarket'] . $name . '/';
$url = Yii::$app->params['feixiaohao_domain'] . Yii::$app->params['feixiaohao_page']['coinmarket'] . $this->sid . '/';
$ch = new Curl();
try {
$content = $ch->get($url, true);
......@@ -453,17 +453,17 @@ class CoinService extends Coin implements CoinInterface
* @param string $sid
* @return integer|bool
*/
public static function exchange_count($sid = 'eos')
public function exchange_count()
{
$key = [
__CLASS__,
__METHOD__,
'coin',//表名
$sid,//sid
$this->sid,//sid
];
$result = Yii::$app->cache->get($key);
if (!$result) {
$url = Yii::$app->params['feixiaohao_domain'] . Yii::$app->params['feixiaohao_page']['coinmarket'] . $sid . '/';
$url = Yii::$app->params['feixiaohao_domain'] . Yii::$app->params['feixiaohao_page']['coinmarket'] . $this->sid . '/';
$ch = new Curl();
try {
$content = $ch->get($url, true);
......@@ -490,18 +490,18 @@ class CoinService extends Coin implements CoinInterface
* @param string $sid
* @return object|string
*/
public static function quotation($sid = 'eos')
public function quotation()
{
//使用缓存
$key = [
__CLASS__,
__METHOD__,
'coin',//表名
$sid,//sid
$this->sid,//sid
];
$result = Yii::$app->cache->get($key);
if ($result === false) {
$url = Yii::$app->params['feixiaohao_domain'] . Yii::$app->params['feixiaohao_page']['currencies'] . $sid . '/';
$url = Yii::$app->params['feixiaohao_domain'] . Yii::$app->params['feixiaohao_page']['currencies'] . $this->sid . '/';
$ch = new Curl();
try {
$content = $ch->get($url, true);
......
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