Commit 2b31f7c5 authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/optimize' into 'master'

Feature/optimize See merge request !233
parents 74c5abaa cdca8abd
...@@ -291,7 +291,7 @@ class CoinController extends BaseController ...@@ -291,7 +291,7 @@ class CoinController extends BaseController
$query = Coin::find() $query = Coin::find()
->select('id, sid, icon, name, optional_name, nickname, platform, chain, address as contract_address, treaty') ->select('id, sid, icon, name, optional_name, nickname, platform, chain, address as contract_address, treaty')
->where(['platform_id' => (int)$platform_id]) ->where(['>', "find_in_set($platform_id, platform_id)", 0])
->orderBy('id'); ->orderBy('id');
if (false != $chain) { if (false != $chain) {
...@@ -303,7 +303,7 @@ class CoinController extends BaseController ...@@ -303,7 +303,7 @@ class CoinController extends BaseController
} }
if (false != $keyword) { if (false != $keyword) {
$query->andWhere(['or', ['like', 'address', $keyword], ['like', 'name', $keyword], ['like', 'nickname', $keyword]]); $query->andWhere(['or', ['like', 'address', $keyword], ['like', 'name', $keyword]]);
} }
$data = $query->offset(($page - 1) * $limit)->limit($limit)->asArray()->all(); $data = $query->offset(($page - 1) * $limit)->limit($limit)->asArray()->all();
......
...@@ -12,6 +12,7 @@ use common\service\exchange\ExchangeBuilderFactory; ...@@ -12,6 +12,7 @@ use common\service\exchange\ExchangeBuilderFactory;
use Yii; use Yii;
use api\base\BaseController; use api\base\BaseController;
use common\models\pwallet\Notice; use common\models\pwallet\Notice;
use yii\data\Pagination;
/** /**
* 公告控制器 * 公告控制器
...@@ -21,50 +22,80 @@ use common\models\pwallet\Notice; ...@@ -21,50 +22,80 @@ use common\models\pwallet\Notice;
class NoticeController extends BaseController class NoticeController extends BaseController
{ {
/** /**
* 返回公告列表 * 申请列表
* * @param integer page
* @var $page * @param integer size
* @var $limit
* @var $condition 筛选条件
* @return array * @return array
*/ */
public function actionList() public function actionList()
{ {
$request = Yii::$app->request; $data = null;
$page = $request->post('page', 1); $header = Yii::$app->request->headers;
$limit = $request->post('limit', 10); $platform_id = $header['FZM-PLATFORM-ID'] ?? null;
$post = $request->post(); $type = Yii::$app->request->get('type', '');
$condition = [];
if (empty($platform_id)) {
$post = array_filter($post, function ($value, $key) { $msg = '缺少必要的参数';
if ($key == 'status' && is_numeric($value)) { $code = -1;
return true; goto doEnd;
}
if ($key == 'id' && is_numeric($value)) {
return true;
}
return $value;
}, ARRAY_FILTER_USE_BOTH);
if (isset($post['id'])) {
$condition[] = ['id' => $post['id']];
}
if (isset($post['title'])) {
$condition[] = ['like', 'title', $post['title']];
}
if (isset($post['author'])) {
$condition[] = ['author' => $post['author']];
} }
if (isset($post['status'])) {
$condition[] = ['status' => $post['status']]; $page = \Yii::$app->request->get('page', 1);
$size = \Yii::$app->request->get('size', 10);
$query = Notice::find()
->select('id, title, content, author, type, is_top, create_time')
->where(['status' => Notice::STATUS_ON])
->orderBy('create_time desc');
if (false != $type) {
$query->andWhere(['type' => (int)$type]);
} }
if (isset($post['create_at'])) {
$condition[] = ['>=', 'create_at', $post['create_at']]; $countQuery = clone $query;
$models = $query->offset(($page - 1) * $size)->limit($size)->all();
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => $size]);
$data = [
'list' => $models,
'page' => [
'pageCount' => $pages->pageCount,
'pageSize' => (int)$size,
'currentPage' => (int)$page,
]
];
$msg = 'ok';
$code = 0;
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
} }
if (isset($post['update_at'])) {
$condition[] = ['>=', 'update_at', $post['update_at']]; /**
* 申请列表
* @param integer page
* @param integer size
* @return array
*/
public function actionDetail()
{
$data = null;
$header = Yii::$app->request->headers;
$platform_id = $header['FZM-PLATFORM-ID'] ?? null;
$id = Yii::$app->request->get('id', 0);
if (empty($platform_id) || empty($id)) {
$msg = '缺少必要的参数';
$code = -1;
goto doEnd;
} }
$data = Notice::getList($page, $limit, $condition);
return $data; $data = Notice::find()->select('title,content,author,type,create_time')->where(['id' => $id, 'platform_id' => $platform_id])->asArray()->one();
$msg = 'ok';
$code = 0;
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
} }
public function actionIndex() public function actionIndex()
......
...@@ -13,11 +13,82 @@ use common\core\BaseActiveRecord; ...@@ -13,11 +13,82 @@ use common\core\BaseActiveRecord;
class Notice extends BaseActiveRecord class Notice extends BaseActiveRecord
{ {
const STATUS_ON = 1; //激活
const STATUS_OFF = 0; //未激活
const TYPE_ROLL = 1; //滚动
const TYPE_POP = 0; //弹窗
//定义场景
const SCENARIOS_CREATE = 'create';
const SCENARIOS_UPDATE = 'update';
public static function getDb() public static function getDb()
{ {
return Yii::$app->get('db_pwallet'); return Yii::$app->get('db_pwallet');
} }
public function rules()
{
return [
[['title', 'content', 'type', 'status', 'platform_id', 'author'], 'required'],
[['status', 'type', 'platform_id', 'is_top'], 'integer'],
['is_top', 'verfiyIsTop'],
['type', 'verfiyType'],
[['is_top'], 'safe']
];
}
public function verfiyIsTop($attribute, $params)
{
$count = self::find()->where(['platform_id' => $this->platform_id, 'is_top' => $this->is_top])->count();
if ($count >= 1) {
$this->addError($attribute, '置顶公告只能1条');
return false;
}
}
public function verfiyType($attribute, $params)
{
if ('create' == self::getScenario()) {
$count = self::find()->where(['platform_id' => $this->platform_id, 'type' => $this->type])->count();
if (Notice::TYPE_ROLL == $this->type) {
if ($count >= 3) {
$this->addError($attribute, '滚动公告最多只能3条');
return false;
}
}
if (Notice::TYPE_POP == $this->type) {
if ($count >= 1) {
$this->addError($attribute, '弹窗公告最多只能3条');
return false;
}
}
}
}
public function scenarios()
{
$scenarios = [
self:: SCENARIOS_CREATE => ['title', 'content', 'author', 'status', 'type', 'platform_id', 'is_top'],
self:: SCENARIOS_UPDATE => ['id', 'title', 'content', 'author', 'status', 'type', 'platform_id', 'is_top'],
];
return array_merge(parent:: scenarios(), $scenarios);
}
public function attributeLabels()
{
return [
'title' => '公告标题',
'content' => '公告内容',
'author' => '作者',
'status' => '公告状态',
'type' => '公告类型',
'platform_id' => '钱包',
];
}
public static function getList($page = 1, $limit = 10, $condition = []) public static function getList($page = 1, $limit = 10, $condition = [])
{ {
$query = self::find(); $query = self::find();
......
<?php
namespace wallet\controllers;
use common\models\pwallet\Notice;
use Yii;
use yii\data\Pagination;
use wallet\base\BaseController;
class NoticeController extends BaseController
{
public function actionIndex()
{
$msg = 'ok';
$code = 0;
$data = null;
$page = Yii::$app->request->get('page', 1);
$size = Yii::$app->request->get('size', 10);
$group = Yii::$app->request->getGroup();
if ('administrator' == $group) {
$platform_id = Yii::$app->request->get('platform_id', Yii::$app->request->getPlatformId());
} else {
$platform_id = Yii::$app->request->getPlatformId();
}
if (Yii::$app->request->isPost) {
$model = new Notice();
$model->setScenario(Notice::SCENARIOS_CREATE);
$params = Yii::$app->request->post();
$params['platform_id'] = $platform_id;
$model->load($params, '');
if (!$model->validate()) {
$msg = $model->errors;
$code = -1;
goto doEnd;
}
$model->save();
goto doEnd;
}
if (Yii::$app->request->isGet) {
$status = Yii::$app->request->get('status', 0);
$type = Yii::$app->request->get('type', 0);
$query = Notice::find()->select('id, title, type, create_time')->where(['platform_id' => $platform_id])->asArray();
if (false != $status) {
$query->andWhere(['status' => (int)$status]);
}
if (false != $type) {
$query->andWhere(['type' => (int)$type]);
}
$model = $query->offset(($page - 1) * $size)->orderBy('create_time')->limit($size)->asArray()->all();
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => $size]);
$data = [
'list' => $model,
'page' => [
'pageCount' => $pages->pageCount,
'pageSize' => $size,
'currentPage' => $page,
]
];
}
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
}
public function actionUpdate()
{
$msg = 'ok';
$code = 0;
$data = null;
$group = Yii::$app->request->getGroup();
if ('administrator' == $group) {
$platform_id = Yii::$app->request->get('platform_id', Yii::$app->request->getPlatformId());
} else {
$platform_id = Yii::$app->request->getPlatformId();
}
if (Yii::$app->request->isGet) {
$id = Yii::$app->request->get('id');
$data = Notice::find()->select('id, title, content, author, status, type, is_top')->where(['platform_id' => $platform_id, 'id' => $id])->asArray()->one();
goto doEnd;
}
if (Yii::$app->request->isPut) {
$params = Yii::$app->request->post();
$id = isset($params['id']) ? $params['id'] : null;
if (false == $id) {
$msg = '参数错误';
$code = -1;
goto doEnd;
}
$model = Notice::findOne($id);
if (empty($model)) {
$msg = '数据错误';
$code = -1;
goto doEnd;
}
if ('administrator' != $group && $platform_id != $model->platform_id){
$msg = '无权操作';
$code = -1;
goto doEnd;
}
$model->setScenario(Notice::SCENARIOS_UPDATE);
$model->load($params, '');
if (!$model->validate()) {
$msg = $model->errors;
$code = -1;
goto doEnd;
}
$model->save();
goto doEnd;
}
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
}
public function actionRemove()
{
$msg = 'ok';
$code = 0;
$data = null;
$group = Yii::$app->request->getGroup();
$platform_id = Yii::$app->request->getPlatformId();
$id = Yii::$app->request->get('id');
if (false == $id) {
$msg = '参数错误';
$code = -1;
goto doEnd;
}
if (Yii::$app->request->isDelete) {
$model = Notice::findOne($id);
if ('administrator' != $group && $platform_id != $model->platform_id){
$msg = '无权删除';
$code = -1;
goto doEnd;
}
if (!$model->delete()) {
$msg = '删除失败';
$code = -1;
goto doEnd;
}
}
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
}
}
\ No newline at end of file
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