105 lines
3.2 KiB
PHP
105 lines
3.2 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: zn
|
|
* Date: 2017/6/16
|
|
* Time: 14:32
|
|
*/
|
|
|
|
namespace app\controllers;
|
|
|
|
use common\models\Notice;
|
|
use common\services\BackMessageServices;
|
|
use common\services\ResultStatusServices;
|
|
use common\utils\CommUtil;
|
|
use Yii;
|
|
use yii\rest\Controller;
|
|
|
|
/**
|
|
* 公告
|
|
* @package app\controllers
|
|
*/
|
|
class NoticeController extends Controller
|
|
{
|
|
public function actionNoticeList()
|
|
{
|
|
$title = Yii::$app->request->get('title');
|
|
$message = Yii::$app->request->get('message');
|
|
$current_page = (int)Yii::$app->request->get('page');
|
|
$sort = Yii::$app->request->get('sort');
|
|
|
|
$find = Notice::find();
|
|
if (!empty($title)) $find->andWhere(['like', 'title', $title]);
|
|
if (!empty($message)) $find->andWhere(['like', 'message', $message]);
|
|
|
|
$total = $find->count();
|
|
|
|
$per_page = 12;
|
|
$last_page = ceil($total / $per_page);
|
|
|
|
$find->offset($per_page * ($current_page - 1))->limit($per_page);
|
|
|
|
if (!empty($sort)) {
|
|
list($field, $sortType) = explode("|", $sort);
|
|
if (!empty($field) && !empty($sortType)) {
|
|
$type = SORT_ASC;
|
|
if ($sortType == 'desc') $type = SORT_DESC;
|
|
$find->addOrderBy([$field => $type]);
|
|
}
|
|
}
|
|
|
|
$dbList = $find->all();
|
|
$list = [];
|
|
foreach ($dbList as $item) {
|
|
$itemData = $item->toArray();
|
|
$itemData['update_time'] = CommUtil::formatDate($item->updated_at);
|
|
array_push($list, $itemData);
|
|
}
|
|
|
|
$returnData['data'] = $list;
|
|
$returnData['total'] = $total;
|
|
$returnData['per_page'] = $per_page;
|
|
$returnData['current_page'] = $current_page;
|
|
$returnData['last_page'] = $last_page;
|
|
return BackMessageServices::sendMessage(ResultStatusServices::RS_OPERATION_SUCCESS, $returnData);
|
|
}
|
|
|
|
public function actionNoticeEdit()
|
|
{
|
|
$id = Yii::$app->request->get('id');
|
|
$title = Yii::$app->request->get('title');
|
|
$message = Yii::$app->request->get('message');
|
|
|
|
if (empty($title) || empty($message)) return BackMessageServices::sendMessage(ResultStatusServices::RS_PARAM_ERROR);
|
|
|
|
$notice = null;
|
|
if (!empty($id)) {
|
|
$notice = Notice::findById($id);
|
|
if (empty($notice)) return BackMessageServices::sendMessage(ResultStatusServices::RS_PARAM_ERROR);
|
|
} else {
|
|
$notice = new Notice();
|
|
}
|
|
|
|
$notice->title = $title;
|
|
$notice->message = $message;
|
|
if (!$notice->save()) return BackMessageServices::sendMessage(ResultStatusServices::RS_SAVE_DB_ERROR, $notice->getErrors());
|
|
|
|
return BackMessageServices::sendMessage(ResultStatusServices::RS_OPERATION_SUCCESS);
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
*/
|
|
public function actionNoticeDel()
|
|
{
|
|
$id = Yii::$app->request->get('id');
|
|
|
|
$notice = Notice::findById($id);
|
|
if (empty($notice)) return BackMessageServices::sendMessage(ResultStatusServices::RS_PARAM_ERROR);
|
|
|
|
if (!$notice->delete()) return BackMessageServices::sendMessage(ResultStatusServices::RS_SAVE_DB_ERROR, $notice->getErrors());
|
|
|
|
return BackMessageServices::sendMessage(ResultStatusServices::RS_OPERATION_SUCCESS);
|
|
}
|
|
|
|
} |