Commit 9518c599 authored by rlgy's avatar rlgy

速度优化 后台 币种列表

parent b5a4553b
...@@ -148,12 +148,23 @@ class CoinController extends BaseController ...@@ -148,12 +148,23 @@ class CoinController extends BaseController
if ($model) { if ($model) {
try { try {
// $model->delete(); // $model->delete();
$this->success('删除成功', '/admin/coin/index',true); $this->success('删除成功', '/admin/coin/index', true);
} catch (\Throwable $t) { } catch (\Throwable $t) {
} catch (\Exception $e) { } catch (\Exception $e) {
} }
} }
} }
$this->error('删除失败', Yii::$app->request->getReferrer(),true); $this->error('删除失败', Yii::$app->request->getReferrer(), true);
}
public function actionGetExchangeListById()
{
$id = Yii::$app->request->get('id', 0);
$exchanges = [];
if ($id) {
$exchanges = CoinBusiness::getExchangeListById($id);
}
$this->layout = false;
return $this->render('exchange', ['exchanges' => $exchanges]);
} }
} }
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-15
* Time: 下午2:10
*/
/**
* @var array $exchanges
*/
?>
<?php foreach ($exchanges as $exchange): ?>
<li><a href="<?= $exchange['url'] ?>"><?= $exchange['name'] ?></a></li>
<?php endforeach; ?>
...@@ -102,15 +102,12 @@ ...@@ -102,15 +102,12 @@
table.on('tool(table1)', function (obj) { table.on('tool(table1)', function (obj) {
var data = obj.data; var data = obj.data;
if (obj.event === 'getExchange') { if (obj.event === 'getExchange') {
var content = ''; $.get('/admin/coin/get-exchange-list-by-id?id=' + obj.data.id, {}, function (str) {
$.each(data.exchange.data, function (index, item) { layer.open({
content += '<li>' + item + '</li>'; type: 1,
}); title: '上架交易所列表',
layer.open({ content: str,
type: 0, });
title: '上架交易所列表',
content: content,
tips: 3
}); });
} else if (obj.event == 'delete') { } else if (obj.event == 'delete') {
layer.confirm('真的删除行么', function (index) { layer.confirm('真的删除行么', function (index) {
...@@ -155,7 +152,7 @@ ...@@ -155,7 +152,7 @@
</a> </a>
</script> </script>
<script type="text/html" id="exchangeTpl"> <script type="text/html" id="exchangeTpl">
{{d.exchange.count}} {{d.exchange}}
</script> </script>
<script type="text/html" id="recommendTpl"> <script type="text/html" id="recommendTpl">
{{# if(d.recommend==0){ }} {{# if(d.recommend==0){ }}
......
...@@ -29,17 +29,37 @@ class CoinBusiness ...@@ -29,17 +29,37 @@ class CoinBusiness
public static function getList($page = 1, $limit = 10, $condition = []) public static function getList($page = 1, $limit = 10, $condition = [])
{ {
$rows = Coin::getList($page, $limit, $condition); $rows = Coin::getList($page, $limit, $condition);
// if ($rows['count'] > 0) { if ($rows['count'] > 0) {
// $datas = $rows['data']; $datas = $rows['data'];
// foreach ($datas as $key => $value) { foreach ($datas as $key => $value) {
// //获取交易所信息 //获取交易所信息
// $rows['data'][$key]['exchange'] = CoinService::exchange($value['sid']); $rows['data'][$key]['exchange'] = CoinService::exchange_count($value['sid']);
// } }
// } }
return $rows; return $rows;
} }
/** /**
* 返回交易所列表
*
* @param $id
* @return array
*/
public static function getExchangeListById($id = 0)
{
if ($id) {
$coin = Coin::findOne(['id' => $id]);
if ($coin) {
$datas = CoinService::exchange($coin->sid);
if ($datas['count'] > 0) {
return $datas['data'];
}
}
}
return [];
}
/**
* api获取行情列表 * api获取行情列表
* @param int $page * @param int $page
* @param int $limit * @param int $limit
......
...@@ -72,6 +72,9 @@ return [ ...@@ -72,6 +72,9 @@ return [
'cache' => [ 'cache' => [
'class' => 'yii\caching\FileCache', 'class' => 'yii\caching\FileCache',
], ],
// 'cache' => [
// 'class' => 'yii\redis\Connection',
// ],
'i18n' => [ 'i18n' => [
'translations' => [ 'translations' => [
'*' => [ '*' => [
......
...@@ -36,4 +36,10 @@ return [ ...@@ -36,4 +36,10 @@ return [
'currencies' => '/currencies/',//币种详情 'currencies' => '/currencies/',//币种详情
'coinmarket' => '/coinmarket/',//上市交易所 'coinmarket' => '/coinmarket/',//上市交易所
], ],
/** 爬虫数据缓存时间 */
'curl_cache_time' => [
'exchange' => 60 * 5,
'exchange_count' => 60 * 5,
]
]; ];
...@@ -34,7 +34,7 @@ class CoinService ...@@ -34,7 +34,7 @@ class CoinService
$name,//sid $name,//sid
]; ];
$result = Yii::$app->cache->get($key); $result = Yii::$app->cache->get($key);
if ($result === false || YII_ENV_DEV) { 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'] . $name . '/';
$ch = new Curl(); $ch = new Curl();
try { try {
...@@ -64,7 +64,7 @@ class CoinService ...@@ -64,7 +64,7 @@ class CoinService
} }
} }
unset($content, $i, $r, $url, $matchs2); unset($content, $i, $r, $url, $matchs2);
Yii::$app->cache->set($key, $result, 180);//set caching Yii::$app->cache->set($key, $result, Yii::$app->params['curl_cache_time']['exchange']);//set caching
return ['count' => count($result), 'data' => $result]; return ['count' => count($result), 'data' => $result];
} catch (\Exception $exception) { } catch (\Exception $exception) {
return ['count' => 0, 'data' => []]; return ['count' => 0, 'data' => []];
...@@ -80,6 +80,33 @@ class CoinService ...@@ -80,6 +80,33 @@ class CoinService
*/ */
public static function exchange_count($sid = 'eos') 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;
} }
/** /**
......
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