Commit 96a66170 authored by rlgy's avatar rlgy

币种权限控制

parent 8d008f67
...@@ -61,7 +61,6 @@ class AdminController extends BaseController ...@@ -61,7 +61,6 @@ class AdminController extends BaseController
$model->generateAuthKey(); $model->generateAuthKey();
$model->setPassword($data['password']); $model->setPassword($data['password']);
$model->group = $role;
$model->platform_id = Yii::$app->user->identity->platform_id; $model->platform_id = Yii::$app->user->identity->platform_id;
/* 保存用户数据到数据库 */ /* 保存用户数据到数据库 */
if ($model->save()) { if ($model->save()) {
......
...@@ -8,12 +8,12 @@ ...@@ -8,12 +8,12 @@
namespace backend\controllers; namespace backend\controllers;
use Yii;
use common\models\psources\Coin;
use backend\models\coin\CoinForm; use backend\models\coin\CoinForm;
use yii\web\UploadedFile;
use yii\validators\ImageValidator;
use common\business\CoinBusiness; use common\business\CoinBusiness;
use common\models\psources\Coin;
use Yii;
use yii\validators\ImageValidator;
use yii\web\UploadedFile;
/** /**
* 币种管理控制器 * 币种管理控制器
...@@ -29,27 +29,32 @@ class CoinController extends BaseController ...@@ -29,27 +29,32 @@ class CoinController extends BaseController
$page = $request->get('page', 1); $page = $request->get('page', 1);
$limit = $request->get('limit', 10); $limit = $request->get('limit', 10);
$name = $request->get('name', null); $name = $request->get('name', null);
$platform = $request->get('platform', 'all'); $platform = $request->get('platform', '');
$recommend = $request->get('recommend', 'all'); $recommend = $request->get('recommend', '');
$condition = []; $condition = [];
if ($name) { if ($name) {
$condition[] = ['like', 'name', $name]; $condition[] = ['like', 'name', $name];
} }
if ($platform != 'all') { if ($platform != '') {
$condition[] = ['chain' => $platform]; $condition[] = ['chain' => $platform];
} }
if ($recommend != 'all') { if (Yii::$app->user->id != Yii::$app->params['admin']) {
$recommend = $recommend ? 1 : 0; $platform_id = Yii::$app->user->identity->platform_id;
$condition[] = ['recommend' => $recommend]; $condition[] = ['>', "find_in_set($platform_id, platform_id)", 0];
} }
if (Yii::$app->user->identity->group != 'administrator') { if (!empty($recommend)) {
$condition[] = ['OR', ['group' => Yii::$app->user->identity->group], ['>', 'recommend', 0]]; //使用连表查询推荐币种
$data = CoinBusiness::getRecommendList($page, $limit, $condition);
} else {
$data = CoinBusiness::getList($page, $limit, $condition);
} }
$data = CoinBusiness::getList($page, $limit, $condition);
$data['code'] = 0; $data['code'] = 0;
//ajax return //ajax return
Yii::$app->response->format = 'json'; Yii::$app->response->format = 'json';
return $data; return $data;
} }
$platforms = Coin::getChainList(); $platforms = Coin::getChainList();
return $this->render('index', ['platforms' => $platforms]); return $this->render('index', ['platforms' => $platforms]);
...@@ -66,7 +71,7 @@ class CoinController extends BaseController ...@@ -66,7 +71,7 @@ class CoinController extends BaseController
* @var $coin \common\models\psources\Coin * @var $coin \common\models\psources\Coin
*/ */
$coin = Yii::createObject(Coin::className()); $coin = Yii::createObject(Coin::className());
$data = array_merge($request->post(), ['group' => Yii::$app->user->identity->group]); $data = array_merge($request->post(), ['platform_id' => Yii::$app->user->identity->platform_id]);
unset($data['id']); unset($data['id']);
$result = $coin->addOne($data); $result = $coin->addOne($data);
if ($result === true) { if ($result === true) {
...@@ -96,23 +101,26 @@ class CoinController extends BaseController ...@@ -96,23 +101,26 @@ class CoinController extends BaseController
$req = Yii::$app->request; $req = Yii::$app->request;
Yii::$app->response->format = 'json'; Yii::$app->response->format = 'json';
if ($model->load($req->post()) && $model->validate()) { if ($model->load($req->post()) && $model->validate()) {
$role = Yii::$app->user->identity->group; $platform_id = Yii::$app->user->identity->platform_id;
if ('administrator' != $role) {
$coin = Coin::find()->select(['group'])->where(['id' => $req->post('id')])->one(); $can = false;
if ($role == $coin->group) {
$coin = Yii::createObject(Coin::className()); if (Yii::$app->params['admin'] != Yii::$app->user->id) {
$result = $coin->updateOne($req->post()); $coin = Coin::find()->where(['id' => $req->post('id'), 'platform_id' => $platform_id])->one();
if ($result === true) { if ($coin) {
return ['code' => 0, 'msg' => 'succeed']; $can = true;
}
}else{
return ['code' => -1, 'msg' => '没有权限修改'];
} }
} else {
$can = true;
} }
$coin = Yii::createObject(Coin::className()); if ($can) {
$result = $coin->updateOne($req->post()); $coin = Yii::createObject(Coin::className());
if ($result === true) { $result = $coin->updateOne($req->post());
return ['code' => 0, 'msg' => 'succeed']; if ($result === true) {
return ['code' => 0, 'msg' => 'succeed'];
}
} else {
return ['code' => -1, 'msg' => '没有权限修改'];
} }
} }
$errors = $model->errors; $errors = $model->errors;
...@@ -164,23 +172,30 @@ class CoinController extends BaseController ...@@ -164,23 +172,30 @@ class CoinController extends BaseController
public function actionDelete() public function actionDelete()
{ {
Yii::$app->response->format = 'json'; Yii::$app->response->format = 'json';
$id = Yii::$app->request->get('id', 0); $id = Yii::$app->request->get('id', 0);
if ($id) { if ($id) {
$model = Coin::findOne(['id' => $id]); $model = Coin::findOne(['id' => $id]);
if ($model) { if ($model) {
$role = Yii::$app->user->identity->group; $platform_id = Yii::$app->user->identity->platform_id;
if ('administrator' != $role) { $can = false;
$coin = Coin::find()->select(['group'])->where(['id' => $id])->one();
if ($role == $coin->group) { if (Yii::$app->params['admin'] != Yii::$app->user->id) {
try { $coin = Coin::find()->where(['id' => $id, 'platform_id' => $platform_id])->one();
$model->delete(); if ($coin) {
return ['code' => 0, 'msg' => 'succeed']; $can = true;
} catch (\Throwable $t) {
} catch (\Exception $e) {
}
}else{
return ['code' => -1, 'msg' => '没有权限修改'];
} }
} else {
$can = true;
}
if ($can) {
try {
$model->delete();
return ['code' => 0, 'msg' => 'succeed'];
} catch (\Throwable $t) {
return ['code' => $t->getCode(), 'msg' => $t->getMessage()];
}
} else {
return ['code' => -1, 'msg' => '没有权限修改'];
} }
} }
} }
...@@ -197,4 +212,4 @@ class CoinController extends BaseController ...@@ -197,4 +212,4 @@ class CoinController extends BaseController
$this->layout = false; $this->layout = false;
return $this->render('exchange', ['exchanges' => $exchanges]); return $this->render('exchange', ['exchanges' => $exchanges]);
} }
} }
\ No newline at end of file
...@@ -35,7 +35,7 @@ IndexAsset::register($this); ...@@ -35,7 +35,7 @@ IndexAsset::register($this);
<label class="layui-form-label" style="margin-bottom: 0; width: 100px;">类型</label> <label class="layui-form-label" style="margin-bottom: 0; width: 100px;">类型</label>
<div class="layui-input-inline"> <div class="layui-input-inline">
<select name="platform"> <select name="platform">
<option value="all">全部</option> <option value="">全部</option>
<?php foreach ($platforms as $platform): ?> <?php foreach ($platforms as $platform): ?>
<option value="<?= $platform ?>"><?= $platform ?></option> <option value="<?= $platform ?>"><?= $platform ?></option>
<?php endforeach; ?> <?php endforeach; ?>
...@@ -46,7 +46,7 @@ IndexAsset::register($this); ...@@ -46,7 +46,7 @@ IndexAsset::register($this);
<label class="layui-form-label" style="margin-bottom: 0; width: 100px;">推介币种</label> <label class="layui-form-label" style="margin-bottom: 0; width: 100px;">推介币种</label>
<div class="layui-input-inline"> <div class="layui-input-inline">
<select name="recommend"> <select name="recommend">
<option value="all">全部</option> <option value="">全部</option>
<option value="0"></option> <option value="0"></option>
<option value="1"></option> <option value="1"></option>
</select> </select>
......
...@@ -21,9 +21,9 @@ class CoinBusiness ...@@ -21,9 +21,9 @@ class CoinBusiness
{ {
/** /**
* 管理员后台获取币种列表 * 管理员后台获取币种列表
* @param int $page * @param int $page
* @param int $limit * @param int $limit
* @param array $condition * @param array $condition
* @return array|\yii\db\ActiveRecord[] * @return array|\yii\db\ActiveRecord[]
*/ */
public static function getList($page = 1, $limit = 10, $condition = []) public static function getList($page = 1, $limit = 10, $condition = [])
...@@ -34,7 +34,34 @@ class CoinBusiness ...@@ -34,7 +34,34 @@ class CoinBusiness
foreach ($datas as $key => $value) { foreach ($datas as $key => $value) {
//获取交易所信息 //获取交易所信息
try { try {
$coin = CoinFactory::createCoin($value['name'], $value['id'], $value['sid']); $coin = CoinFactory::createCoin($value['name'], $value['id'], $value['sid']);
$rows['data'][$key]['exchange'] = $coin->exchange_count();
} catch (\Exception $exception) {
$rows['data'][$key]['exchange'] = 0;
}
}
}
return $rows;
}
/**
* 管理员后台获取币种列表
* @param int $page
* @param int $limit
* @param array $condition
* @return array|\yii\db\ActiveRecord[]
*/
public static function getRecommendList($page = 1, $limit = 10, $condition = [])
{
$rows = Coin::getRecommendList($page, $limit, $condition);
if ($rows['count'] > 0) {
$datas = $rows['data'];
foreach ($datas as $key => $value) {
//获取交易所信息
try {
$coin = CoinFactory::createCoin($value['name'], $value['id'], $value['sid']);
$rows['data'][$key]['exchange'] = $coin->exchange_count(); $rows['data'][$key]['exchange'] = $coin->exchange_count();
} catch (\Exception $exception) { } catch (\Exception $exception) {
$rows['data'][$key]['exchange'] = 0; $rows['data'][$key]['exchange'] = 0;
...@@ -47,7 +74,7 @@ class CoinBusiness ...@@ -47,7 +74,7 @@ class CoinBusiness
/** /**
* 返回交易所列表 * 返回交易所列表
* *
* @param $id * @param $id
* @return array * @return array
*/ */
public static function getExchangeListById($id = 0) public static function getExchangeListById($id = 0)
...@@ -68,9 +95,9 @@ class CoinBusiness ...@@ -68,9 +95,9 @@ class CoinBusiness
/** /**
* api获取行情列表 * api获取行情列表
* @param int $page * @param int $page
* @param int $limit * @param int $limit
* @param array $condition * @param array $condition
* @return array * @return array
*/ */
public static function getApiList($page = 1, $limit = 10, $condition = []) public static function getApiList($page = 1, $limit = 10, $condition = [])
...@@ -104,7 +131,7 @@ class CoinBusiness ...@@ -104,7 +131,7 @@ class CoinBusiness
/** /**
* 根据name返回币种信息 * 根据name返回币种信息
* @param array $condition 需要的币种sid列表 * @param array $condition 需要的币种sid列表
* @return array * @return array
*/ */
public static function getApiListForIndex($condition = []) public static function getApiListForIndex($condition = [])
...@@ -126,9 +153,9 @@ class CoinBusiness ...@@ -126,9 +153,9 @@ class CoinBusiness
/** /**
* 根据名称搜索 * 根据名称搜索
* @param int $page * @param int $page
* @param int $limit * @param int $limit
* @param array $condition * @param array $condition
* @return array|\yii\db\ActiveRecord|\yii\db\ActiveRecord[] * @return array|\yii\db\ActiveRecord|\yii\db\ActiveRecord[]
*/ */
public static function SearchByName($page = 1, $limit = 10, $condition = []) public static function SearchByName($page = 1, $limit = 10, $condition = [])
...@@ -150,7 +177,7 @@ class CoinBusiness ...@@ -150,7 +177,7 @@ class CoinBusiness
/** /**
* 获取币种的所有信息 * 获取币种的所有信息
* @param integer $id * @param integer $id
* @return array * @return array
*/ */
public static function getCoinAllById($id) public static function getCoinAllById($id)
...@@ -172,7 +199,7 @@ class CoinBusiness ...@@ -172,7 +199,7 @@ class CoinBusiness
if (empty($row['introduce'])) { if (empty($row['introduce'])) {
$row['introduce'] = ''; $row['introduce'] = '';
} }
$row['price'] = sprintf("%s",(double)$row['price']); $row['price'] = sprintf("%s", (double) $row['price']);
$coin = CoinFactory::createCoin($row['name'], $row['id'], $row['sid']); $coin = CoinFactory::createCoin($row['name'], $row['id'], $row['sid']);
$row['quotation'] = $coin->quotation(); $row['quotation'] = $coin->quotation();
$row['exchange_count'] = $coin->exchange_count(); $row['exchange_count'] = $coin->exchange_count();
...@@ -180,4 +207,4 @@ class CoinBusiness ...@@ -180,4 +207,4 @@ class CoinBusiness
} }
return $row ? [$row] : []; return $row ? [$row] : [];
} }
} }
\ No newline at end of file
...@@ -50,7 +50,6 @@ class Coin extends BaseActiveRecord ...@@ -50,7 +50,6 @@ class Coin extends BaseActiveRecord
$data = $query->offset(($page - 1) * 10)->limit($limit)->asArray()->all(); $data = $query->offset(($page - 1) * 10)->limit($limit)->asArray()->all();
foreach ($data as $key => $item) { foreach ($data as $key => $item) {
$data[$key]['price'] = sprintf("%0.6f", $item['price']); $data[$key]['price'] = sprintf("%0.6f", $item['price']);
// $data[$key]['release'] = date('Y-m-d', $item['release']);
} }
return ['count' => $count, 'data' => $data]; return ['count' => $count, 'data' => $data];
} }
...@@ -194,4 +193,27 @@ class Coin extends BaseActiveRecord ...@@ -194,4 +193,27 @@ class Coin extends BaseActiveRecord
} }
return array_column($data, null, 'id'); return array_column($data, null, 'id');
} }
/**
* 获取推介币种信息列表
*
* @param int $page
* @param int $limit
* @param array $condition
* @return array|\yii\db\ActiveRecord[]
*/
public static function getRecommendList($page = 1, $limit = 10, $condition = [])
{
$query = self::find();
foreach ($condition as $item) {
$query = $query->andWhere($item);
}
$query=$query->rightJoin('coin_recommend','coin.id=coin_recommend.cid');
$count = $query->count();
$data = $query->offset(($page - 1) * 10)->limit($limit)->asArray()->all();
foreach ($data as $key => $item) {
$data[$key]['price'] = sprintf("%0.6f", $item['price']);
}
return ['count' => $count, 'data' => $data];
}
} }
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