Commit fa7d82c0 authored by rlgy's avatar rlgy

推荐币种

parent eaa2e923
...@@ -8,13 +8,14 @@ ...@@ -8,13 +8,14 @@
namespace api\controllers; namespace api\controllers;
use Yii;
use common\base\Exception;
use api\base\BaseController; use api\base\BaseController;
use common\models\psources\Coin; use common\base\Exception;
use common\business\BrowerBusiness;
use common\business\CoinBusiness; use common\business\CoinBusiness;
use common\business\ExchangeBusiness; use common\business\ExchangeBusiness;
use common\business\BrowerBusiness; use common\models\psources\Coin;
use common\models\psources\CoinRecommend;
use Yii;
/** /**
* 币种信息管理控制器 * 币种信息管理控制器
...@@ -48,28 +49,55 @@ class CoinController extends BaseController ...@@ -48,28 +49,55 @@ class CoinController extends BaseController
$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);
$platform_ids = $request->post('platform_id', null); $platform_id = $request->post('platform_id', 1);
$condition = [['>', 'recommend', '0']]; $recommend = $request->post('recommend', '');
if ($platform_ids) {
$platform_id_arr = explode(',', $platform_ids); $condition = ['platform_id' => $platform_id];
$condition_arr = ['OR']; if ($recommend) {
foreach ($platform_id_arr as $key => $value) { $condition['recommend'] = $recommend;
array_push($condition_arr, ['>', "find_in_set($value, [[platform_id]])", 0]);
} }
$condition[] = $condition_arr; $select = ['id', 'sid', 'icon', 'name', 'nickname', 'platform', 'chain'];
$datas = CoinRecommend::getList($page, $limit, $condition, [], $select);
//获取详细信息
$coin_recommends = &$datas['data'];
if (!empty($coin_recommends)) {
$coin_ids = array_column($coin_recommends, 'cid');
//获取币种信息
$coin_infos = Coin::getCoinInfoByIds($coin_ids, $select, 'id');
//获取行情信息
$coin_names = array_column($coin_infos, 'name');
$coin_names = array_merge($coin_names, array_column($coin_infos, 'chain'));
$coin_quotations = ExchangeBusiness::getQuatationByNames($coin_names);
if ($coin_infos) {
array_shift($select);
foreach ($coin_recommends as $key => &$value) {
$temp_key = $coin_infos[$value['cid']]['name'];
foreach ($select as $item) {
$value[$item] = $coin_infos[$value['cid']][$item];
if ($value['platform_id'] != 2) {
//国盾币不需要行情
$value['low'] = $coin_quotations[$temp_key]['low'];
$value['high'] = $coin_quotations[$temp_key]['high'];
$value['last'] = $coin_quotations[$temp_key]['last'];
$value['rmb'] = $coin_quotations[$temp_key]['rmb'];
} else {
$value['low'] = 0;
$value['high'] = 0;
$value['last'] = 0;
$value['rmb'] = 0;
} }
$result = ExchangeBusiness::getApiListForIndex($page, $limit, $condition);
if ($result) {
$chains = array_unique(array_column($result['data'], 'chain'));
$chain_quotation = [];
foreach ($chains as $key => $value) {
$chain_quotation[$value] = ExchangeBusiness::getquatation($value);
} }
foreach ($result['data'] as $key => &$value) { $value['id'] = $value['cid'];
$value['chain_quotation'] = $chain_quotation[$value['chain']]; $value['sid'] = ucfirst($value['sid']);
$value['chain_quotation'] = $coin_quotations[$coin_infos[$value['cid']]['chain']];
unset($value['create_time'], $value['update_time'], $value['cid']);
}
unset($key, $value);
} }
return $result;
} }
return $datas;
} }
/** /**
...@@ -91,9 +119,9 @@ class CoinController extends BaseController ...@@ -91,9 +119,9 @@ class CoinController extends BaseController
//如果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', '币种不存在');
} }
$result = (array)$miner_fee->getAttributes(); $result = (array) $miner_fee->getAttributes();
$result['min'] = (float)$result['min']; $result['min'] = (float) $result['min'];
$result['max'] = (float)$result['max']; $result['max'] = (float) $result['max'];
return $result; return $result;
} }
......
...@@ -19,9 +19,9 @@ class CoreAsset extends AssetBundle ...@@ -19,9 +19,9 @@ class CoreAsset extends AssetBundle
]; ];
public $js = [ public $js = [
'global/plugins/bootstrap/js/bootstrap.min.js', // 'global/plugins/bootstrap/js/bootstrap.min.js',
//'global/plugins/js.cookie.min.js', //'global/plugins/js.cookie.min.js',
'global/scripts/app.min.js', // 'global/scripts/app.min.js',
]; ];
public $depends = [ public $depends = [
......
...@@ -24,8 +24,8 @@ class LayoutAsset extends AssetBundle ...@@ -24,8 +24,8 @@ class LayoutAsset extends AssetBundle
]; ];
public $js = [ public $js = [
'layouts/layout3/scripts/layout.min.js', // 'layouts/layout3/scripts/layout.min.js',
'other/js/common.js?01101' // 'other/js/common.js?01101'
]; ];
public $jsOptions = [ public $jsOptions = [
......
...@@ -20,4 +20,5 @@ class IndexAsset extends AssetBundle ...@@ -20,4 +20,5 @@ class IndexAsset extends AssetBundle
public $jsOptions = [ public $jsOptions = [
'position' => View::POS_END, 'position' => View::POS_END,
]; ];
} }
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-9-11
* Time: 下午5:04
*/
namespace backend\controllers;
use common\models\psources\Coin;
use common\models\psources\CoinPlatform;
use common\models\psources\CoinRecommend;
use Yii;
use yii\web\Response;
/**
* Class CoinRecommendController
*
* 推介币种管理
*
* @package backend\controllers
*/
class CoinRecommendController extends BaseController
{
public function actionIndex()
{
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
$get = Yii::$app->request->get();
$platform = $get['platform_id'] ?? 1;
$page = $get['page'] ?? 1;
$limit = $get['limit'] ?? 10;
$platform_id = $get['platform_id'] ?? '';
$recommend = $get['recommend'] ?? '';
$user_platform_id = Yii::$app->user->identity->platform_id;
if (1 != $user_platform_id) {
$platform_id = $user_platform_id;
}
$condition = [];
if (!empty($recommend)) {
$condition['recommend'] = $recommend;
}
if (!empty($platform_id)) {
$condition['platform_id'] = $platform_id;
}
//todo 获取推介币种
$datas = CoinRecommend::getList($page, $limit, $condition, ['sort' => SORT_DESC]);
return $datas;
}
$user_platform_id = Yii::$app->user->identity->platform_id;
if ($user_platform_id == 1) {
$platforms = CoinPlatform::find()->asArray()->all();
} else {
$platforms = CoinPlatform::find()->where(['id' => $user_platform_id])->asArray()->all();
}
return $this->render('index', ['platforms' => $platforms]);
}
public function actionAdd()
{
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
$get = Yii::$app->request->get();
$platform_id = $get['platform_id'] ?? 1;
$user_platform_id = Yii::$app->user->identity->platform_id;
if (1 != $user_platform_id && $user_platform_id != $platform_id) {
return ['code' => -1, 'msg' => '没有权限修改'];
}
return CoinRecommend::addRecommendCoin($get);
}
}
public function actionDel()
{
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
$id = Yii::$app->request->get('id', null);
if (empty($id)) {
return ['code' => -1, 'msg' => '删除失败'];
}
$coin_recommend = CoinRecommend::findOne($id);
if (empty($coin_recommend)) {
return ['code' => -1, 'msg' => '推荐币种不存在'];
}
$coin_platform_id = $coin_recommend->platform_id;
$user_platform_id = Yii::$app->user->identity->platform_id;
if ($user_platform_id == 1 || $coin_platform_id == $user_platform_id) {
if ($coin_recommend->delete()) {
return ['code' => 0, 'msg' => '删除成功'];
}
} else {
return ['code' => -1, 'msg' => '没有权限'];
}
return ['code' => -1, 'msg' => '删除失败'];
}
}
}
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-9-11
* Time: 下午5:45
*/
;?>
<style type="text/css">
.layui-form-label{
width: 100px;
}
</style>
<div class="layui-row">
<form class="layui-form" lay-filter="form2">
<div class="layui-inline">
<div class="layui-input-inline">
<select class="layui-select" name="platform_id">
<option value="">选择一个钱包</option>
<?php foreach($platforms as $key => $value): ?>
<option value="<?= $value['id'] ?>"><?= $value['name'] ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="layui-inline">
<div class="layui-input-inline">
<select name="recommend" class="layui-select">
<option value="">选择推介类型</option>
<option value="1">首页推介</option>
<option value="2">次页推介</option>
</select>
</div>
</div>
<div class="layui-inline">
<button class="layui-btn layui-btn-normal" lay-submit lay-filter="search">筛选</button>
<button class="layui-btn layui-btn-normal" type="reset">重置</button>
</div>
</form>
</div>
<div class="layui-col-md8">
<table class="layui-table" id="table1" lay-filter="table1"></table>
</div>
<div style="display: none; padding: 5px 5px;" id="_form">
<form class="layui-form" id="form1" lay-filter="form1">
<input type="hidden" name="id" value="">
<div class="layui-form-item">
<label class="layui-form-label">钱包</label>
<div class="layui-input-inline">
<select class="layui-select" name="platform_id">
<option value="">请选择一个钱包</option>
<?php foreach($platforms as $key => $value): ?>
<option value="<?= $value['id'] ?>"><?= $value['name'] ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">币种名称</label>
<div class="layui-input-block">
<input name="coin" class="layui-input" type="text">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">推荐类型</label>
<div class="layui-input-block">
<select name="recommend" class="layui-select">
<option value="1">首页推介</option>
<option value="2">次页推介</option>
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">排序值</label>
<div class="layui-input-block">
<input name="sort" class="layui-input" type="text">
</div>
</div>
</form>
</div>
<script type="text/html" id="platform_id_tpl">
{{# if(1 == d.platform_id){ }}
币钱包
{{# } else if(2 == d.platform_id){ }}
豆子钱包
{{# } else if(3 == d.platform_id){ }}
w钱包
{{# } }}
</script>
<script type="text/html" id="recommend_tpl">
{{# if(1 == d.recommend){ }}
首页推荐
{{# } else if(2 == d.recommend){ }}
次页推荐
{{# } }}
</script>
<script type="text/html" id="toolBar">
<button class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">编辑</button>
<button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</button>
</script>
<script type="text/html" id="toolBarTop">
<div class="layui-btn-container">
<button class="layui-btn layui-btn-sm" lay-event="recommend_add">添加</button>
</div>
</script>
<script type="text/javascript">
var table = layui.table;
table.render({
elem: '#table1',
toolbar: '#toolBarTop',
page: true,
limit: 10,
url: '/admin/coin-recommend/index',
cols: [[
{
field: 'id',
title: 'ID'
},
{
field: 'platform_id',
title: '钱包',
templet: '#platform_id_tpl'
},
{
field: 'coin',
title: '币种名称'
},
{
field: 'recommend',
title: '推荐类型',
templet: '#recommend_tpl'
},
{
field: 'sort',
title: '排序'
},
{
title: '操作',
toolbar: '#toolBar'
},
]]
});
var form = layui.form;
form.render();
table.on('toolbar(table1)', function(obj) {
var event = obj.event;
if (event == 'recommend_add') {
var index = layer.open({
title: '添加推介币种',
type: 1,
zindex: 1,
content: $("#_form"),
btn: ['添加', '取消'],
scrollbar: false,
success: function() {
form.val("form1", {
id: '',
platform_id: '',
coin: '',
sort: '',
recommend: ''
});
},
btn1: function() {
$.get('/admin/coin-recommend/add', $("#form1").serialize(), function(rev) {
layer.msg(rev.msg);
if (0 == rev.code) {
layer.close(index);
$("#_form").css('display', 'none');
table.reload('table1', {
page: {
curr: 1
}
});
}
});
},
btn2: function() {
$("#_form").css('display', 'none');
}
});
}
});
table.on('tool(table1)', function(obj) {
var event = obj.event;
var data = obj.data;
if (event == 'edit') {
var index = layer.open({
title: '添加推介币种',
type: 1,
zindex: 1,
content: $("#_form"),
btn: ['添加', '取消'],
scrollbar: false,
success: function() {
form.val("form1", {
id: data.id,
platform_id: data.platform_id,
coin: data.coin,
sort: data.sort,
recommend: data.recommend
});
},
btn1: function() {
$.get('/admin/coin-recommend/add', $("#form1").serialize(), function(rev) {
layer.msg(rev.msg);
if (0 == rev.code) {
layer.close(index);
$("#_form").css('display', 'none');
table.reload('table1', {
page: {
curr: 1
}
});
}
});
},
btn2: function() {
layer.close(index);
$("#_form").css('display', 'none');
}
});
} else if ('del' == event) {
var index = layer.confirm("确认删除?", {icon: 3, title: '删除'}, function () {
$.get('/admin/coin-recommend/del', {id: data.id}, function (rev) {
layer.msg(rev.msg);
if (0==rev.code) {
// layer.close(index);
table.reload('table1',{
page:{curr:1}
});
}
});
});
}
});
form.on('submit(search)', function (obj) {
console.log(obj.field);
table.reload("table1", {
where: obj.field,
page: {curr:1}
});
return false;
});
</script>
...@@ -53,22 +53,14 @@ ...@@ -53,22 +53,14 @@
<input type="text" class="layui-input" id="time1" value="<?= $model->release ?>"> <input type="text" class="layui-input" id="time1" value="<?= $model->release ?>">
</div> </div>
</div> </div>
<div class="layui-form-item">
<label class="layui-form-label">推介该币种</label>
<div class="layui-input-block">
<select name="recommend">
<option value="0"></option>
<option value="1">首页推荐</option>
<option value="2">次页推荐</option>
</select>
</div>
</div>
<div class="layui-form-item"> <div class="layui-form-item">
<label class="layui-form-label">合约地址</label> <label class="layui-form-label">合约地址</label>
<div class="layui-input-block"> <div class="layui-input-block">
<input type="text" class="layui-input" name="address" value="<?= $model->address ?>"> <input type="text" class="layui-input" name="address" value="<?= $model->address ?>">
</div> </div>
</div> </div>
<div class="layui-form-item"> <div class="layui-form-item">
<label class="layui-form-label">精度</label> <label class="layui-form-label">精度</label>
<div class="layui-input-block"> <div class="layui-input-block">
......
...@@ -54,25 +54,6 @@ ...@@ -54,25 +54,6 @@
</div> </div>
</div> </div>
<div class="layui-form-item"> <div class="layui-form-item">
<label class="layui-form-label">推介该币种</label>
<div class="layui-input-block">
<select name="recommend">
<option value="0" <?php if ($model->recommend == 0) {
echo "selected";
} ?>>
</option>
<option value="1" <?php if ($model->recommend == 1) {
echo "selected";
} ?>>首页推荐
</option>
<option value="2" <?php if ($model->recommend == 2) {
echo "selected";
} ?>>次页推荐
</option>
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">合约地址</label> <label class="layui-form-label">合约地址</label>
<div class="layui-input-block"> <div class="layui-input-block">
<input type="text" class="layui-input" name="address" value="<?= $model->address ?>"> <input type="text" class="layui-input" name="address" value="<?= $model->address ?>">
......
<?php <?php
/* @var $this \yii\web\View */ /* @var $this \yii\web\View */
/* @var $content string */ /* @var $content string */
use common\models\Menu; use common\models\Menu;
use backend\assets\AppAsset; use backend\assets\AppAsset;
use yii\helpers\Html; use yii\helpers\Html;
...@@ -34,7 +36,7 @@ $this->beginPage() ...@@ -34,7 +36,7 @@ $this->beginPage()
<meta charset="<?= Yii::$app->charset ?>"> <meta charset="<?= Yii::$app->charset ?>">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="<?=$picAsset.'/pages/img/favicon.ico'?>" /> <link rel="shortcut icon" href="<?= $picAsset . '/pages/img/favicon.ico' ?>"/>
<?= Html::csrfMetaTags() ?> <?= Html::csrfMetaTags() ?>
<title>管理后台<?= ' | ' . Html::encode($this->title) ?></title> <title>管理后台<?= ' | ' . Html::encode($this->title) ?></title>
<?php $this->head() ?> <?php $this->head() ?>
...@@ -51,7 +53,7 @@ $this->beginPage() ...@@ -51,7 +53,7 @@ $this->beginPage()
<div class="container" style="min-width: 300px"> <div class="container" style="min-width: 300px">
<a href="javascript:;" class="menu-toggler"></a> <a href="javascript:;" class="menu-toggler"></a>
<a href="<?=$logout?>" class="menu-logout">退出 </a> <a href="<?= $logout ?>" class="menu-logout">退出 </a>
<span href="javascript:;" class="menu-user"><?= $user ?> </span> <span href="javascript:;" class="menu-user"><?= $user ?> </span>
</div> </div>
...@@ -84,7 +86,7 @@ $this->beginPage() ...@@ -84,7 +86,7 @@ $this->beginPage()
<div class="page-header-right"> <div class="page-header-right">
<a href="<?=\yii\helpers\Url::toRoute('login/logout')?>" class="menu-logout">退出 </a> <a href="<?= \yii\helpers\Url::toRoute('login/logout') ?>" class="menu-logout">退出 </a>
<span class="menu-user"><?= $user ?></span> <span class="menu-user"><?= $user ?></span>
</div> </div>
...@@ -102,7 +104,7 @@ $this->beginPage() ...@@ -102,7 +104,7 @@ $this->beginPage()
<div class="layui-row"> <div class="layui-row">
<div class="col-md-12"> <div class="col-md-12">
<?=$content?> <?= $content ?>
</div> </div>
</div> </div>
......
...@@ -186,4 +186,32 @@ class ExchangeBusiness ...@@ -186,4 +186,32 @@ class ExchangeBusiness
} }
return []; return [];
} }
public static function getQuatationByNames($names)
{
$result = [];
foreach ($names as $key => $value) {
$quotation = self::getquatation($value);
if (!$quotation) {
$quotation = [];
//使用Coin服务
try {
$coinServer = CoinFactory::createCoin($row['name'], $row['id'], $row['sid']);
$quotation['rmb'] = $coinServer->getPrice();
$quotation['last'] = $coinServer->getDollar();
$quotation['low'] = $coinServer->getLow();
$quotation['high'] = $coinServer->getHigh();
$coinServer->__destruct();
} catch (\Exception $exception) {
$quotation['rmb'] = 0;
$quotation['last'] = 0;
$quotation['low'] = 0;
$quotation['high'] = 0;
\Yii::error($exception->getMessage());
}
}
$result[$value] = $quotation;
}
return $result;
}
} }
<?php
/**
* Created By Sublime Text 3
*
* @date 2018-09-12 14:32:44
* @authors rlgy <rlgyzhcn@qq.com>
*/
namespace common\models\psources;
use common\core\BaseActiveRecord as CoreBase;
use Yii;
class BaseActiveRecord extends CoreBase
{
public static function getDb()
{
return Yii::$app->get('p_sources');
}
}
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
namespace common\models\psources; namespace common\models\psources;
use Yii;
use common\core\BaseActiveRecord; use common\core\BaseActiveRecord;
use Yii;
/** /**
* Class Coin * Class Coin
...@@ -20,8 +20,8 @@ use common\core\BaseActiveRecord; ...@@ -20,8 +20,8 @@ use common\core\BaseActiveRecord;
class Coin extends BaseActiveRecord class Coin extends BaseActiveRecord
{ {
/** /**
* @return null|object|\yii\db\Connection
* @throws \yii\base\InvalidConfigException * @throws \yii\base\InvalidConfigException
* @return null|object|\yii\db\Connection
*/ */
public static function getDb() public static function getDb()
{ {
...@@ -86,7 +86,7 @@ class Coin extends BaseActiveRecord ...@@ -86,7 +86,7 @@ class Coin extends BaseActiveRecord
}); });
$this->setAttributes($params, false); $this->setAttributes($params, false);
try { try {
return (bool)$this->save(); return (bool) $this->save();
} catch (\Exception $exception) { } catch (\Exception $exception) {
return ['code' => $exception->getCode(), 'message' => $exception->getMessage()]; return ['code' => $exception->getCode(), 'message' => $exception->getMessage()];
} }
...@@ -112,7 +112,7 @@ class Coin extends BaseActiveRecord ...@@ -112,7 +112,7 @@ class Coin extends BaseActiveRecord
} }
$coin->setAttributes($params, false); $coin->setAttributes($params, false);
try { try {
return (bool)$coin->save(); return (bool) $coin->save();
} catch (\Exception $exception) { } catch (\Exception $exception) {
return ['code' => $exception->getCode(), 'message' => $exception->getMessage()]; return ['code' => $exception->getCode(), 'message' => $exception->getMessage()];
} }
...@@ -145,4 +145,31 @@ class Coin extends BaseActiveRecord ...@@ -145,4 +145,31 @@ class Coin extends BaseActiveRecord
$data = array_unique(array_column($data, 'chain')); $data = array_unique(array_column($data, 'chain'));
return $data; return $data;
} }
/**
* 根据id列表获取币种信息
*
* @param array $ids [description]
* @param array $select [description]
* @param string $sort_by [description]
* @return array [description]
*/
public static function getCoinInfoByIds($ids, $select, $sort_by)
{
if (!is_array($ids)) {
$ids = [$ids];
}
$query = self::find();
if (!empty($select)) {
$query = $query->select($select);
}
$data = $query->where(['in', 'id', $ids])->asArray()->all();
if (empty($data)) {
return false;
}
if (!in_array($sort_by, $select)) {
return $data;
}
return array_column($data, null, 'id');
}
} }
<?php
/**
* Created By Sublime Text 3
*
* @date 2018-09-12 14:30:49
* @authors rlgy <rlgyzhcn@qq.com>
*/
namespace common\models\psources;
class CoinPlatform extends BaseActiveRecord
{
public static function tableName()
{
return '{{%coin_platform}}';
}
}
<?php
/**
* Created By Sublime Text 3
*
* @date 2018-09-12 10:21:08
* @authors rlgy <rlgyzhcn@qq.com>
*/
namespace common\models\psources;
use common\core\BaseActiveRecord;
use common\models\psources\Coin;
use Yii;
class CoinRecommend extends BaseActiveRecord
{
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{%coin_recommend}}';
}
/**
* 获取推荐列表
*
* @param integer $page [description]
* @param integer $limit [description]
* @param array $condition [description]
* @param array $order_by [description]
* @return [type] [description]
*/
public static function getList($page = 1, $limit = 10, $condition = [], $order_by = [], $select = [])
{
$query = self::find();
if (!empty($condition)) {
$query = $query->where($condition);
}
$count = $query->count();
$data = $query->offset(($page - 1) * 10)->limit($limit);
if (!empty($order_by)) {
# code...
$data = $data->orderby($order_by);
}
$data = $data->asArray()->all();
//获取币种详情
$coin_ids = array_column($data, 'cid');
if (empty($select)) {
$coin_info = Coin::getCoinInfoByIds($coin_ids, ['id', 'name'], 'id');
} else {
$coin_info = Coin::getCoinInfoByIds($coin_ids, $select, 'id');
}
foreach ($data as $key => &$value) {
$value['coin'] = $coin_info[$value['cid']]['name'];
}
unset($key, $value);
// $sql = $query->createCommand()->getSql();
$data = ['count' => $count, 'data' => $data];
if ($count > 0) {
$data['code'] = 0;
} else {
$data['code'] = 1;
$data['msg'] = '数据为空';
}
return $data;
}
/**
* 添加推介币种
*
* @param [type] $params [description]
*/
public static function addRecommendCoin($params)
{
$id = $params['id'] ?? null;
$platform_id = $params['platform_id'] ?? 1;
$coin_name = $params['coin'] ?? '';
$recommend_type = $params['recommend'] ?? 1;
$sort = $params['sort'] ?? 0;
$coin = Coin::findOne(['name' => strtoupper($coin_name)]);
if (empty($coin)) {
return ['code' => -1, 'msg' => '币种不存在'];
}
if (!empty($id)) {
$coin_recommend = CoinRecommend::findOne($id);
} else {
$coin_recommend = new CoinRecommend();
$count = self::find()->where(['cid' => $coin->id, 'platform_id' => $platform_id])->count();
if ($count > 0) {
return ['code' => -1, 'msg' => '推荐币种已经存在'];
}
}
$recommend_type = $recommend_type == 1 ? 1 : 2;
$sort = (int) $sort;
$coin_recommend->cid = $coin->id;
$coin_recommend->recommend = $recommend_type;
$coin_recommend->platform_id = $platform_id;
$coin_recommend->sort = $sort;
if ($coin_recommend->save()) {
return ['code' => 0, 'msg' => '保存成功'];
}
return ['code' => -1, 'msg' => '保存失败'];
}
}
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