Commit 9518c599 authored by rlgy's avatar rlgy

速度优化 后台 币种列表

parent b5a4553b
......@@ -148,12 +148,23 @@ class CoinController extends BaseController
if ($model) {
try {
// $model->delete();
$this->success('删除成功', '/admin/coin/index',true);
$this->success('删除成功', '/admin/coin/index', true);
} catch (\Throwable $t) {
} 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 @@
table.on('tool(table1)', function (obj) {
var data = obj.data;
if (obj.event === 'getExchange') {
var content = '';
$.each(data.exchange.data, function (index, item) {
content += '<li>' + item + '</li>';
});
layer.open({
type: 0,
title: '上架交易所列表',
content: content,
tips: 3
$.get('/admin/coin/get-exchange-list-by-id?id=' + obj.data.id, {}, function (str) {
layer.open({
type: 1,
title: '上架交易所列表',
content: str,
});
});
} else if (obj.event == 'delete') {
layer.confirm('真的删除行么', function (index) {
......@@ -155,7 +152,7 @@
</a>
</script>
<script type="text/html" id="exchangeTpl">
{{d.exchange.count}}
{{d.exchange}}
</script>
<script type="text/html" id="recommendTpl">
{{# if(d.recommend==0){ }}
......
......@@ -29,17 +29,37 @@ class CoinBusiness
public static function getList($page = 1, $limit = 10, $condition = [])
{
$rows = Coin::getList($page, $limit, $condition);
// if ($rows['count'] > 0) {
// $datas = $rows['data'];
// foreach ($datas as $key => $value) {
// //获取交易所信息
// $rows['data'][$key]['exchange'] = CoinService::exchange($value['sid']);
// }
// }
if ($rows['count'] > 0) {
$datas = $rows['data'];
foreach ($datas as $key => $value) {
//获取交易所信息
$rows['data'][$key]['exchange'] = CoinService::exchange_count($value['sid']);
}
}
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获取行情列表
* @param int $page
* @param int $limit
......
......@@ -72,6 +72,9 @@ return [
'cache' => [
'class' => 'yii\caching\FileCache',
],
// 'cache' => [
// 'class' => 'yii\redis\Connection',
// ],
'i18n' => [
'translations' => [
'*' => [
......
......@@ -36,4 +36,10 @@ return [
'currencies' => '/currencies/',//币种详情
'coinmarket' => '/coinmarket/',//上市交易所
],
/** 爬虫数据缓存时间 */
'curl_cache_time' => [
'exchange' => 60 * 5,
'exchange_count' => 60 * 5,
]
];
......@@ -34,7 +34,7 @@ class CoinService
$name,//sid
];
$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 . '/';
$ch = new Curl();
try {
......@@ -64,7 +64,7 @@ class CoinService
}
}
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];
} catch (\Exception $exception) {
return ['count' => 0, 'data' => []];
......@@ -80,6 +80,33 @@ class CoinService
*/
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