130 lines
3.5 KiB
PHP
130 lines
3.5 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: zn
|
|
* Date: 2017/6/21
|
|
* Time: 17:20
|
|
*/
|
|
|
|
namespace app\controllers;
|
|
|
|
use app\utils\InfoUtil;
|
|
use common\models\Notice;
|
|
use common\utils\EnumUtil;
|
|
use Yii;
|
|
use yii\data\Pagination;
|
|
use yii\web\Controller;
|
|
|
|
/**
|
|
* 公共
|
|
* Class CommController
|
|
* @package app\controllers
|
|
*/
|
|
class OtherController extends Controller
|
|
{
|
|
/**
|
|
* 公告
|
|
*/
|
|
public function actionNoticeList()
|
|
{
|
|
$page = Yii::$app->request->get('page');
|
|
|
|
if (!isset($page)) $page = 1;
|
|
$page--;
|
|
|
|
$pageSize = 20;
|
|
|
|
$offset = $page * $pageSize;
|
|
|
|
$find = Notice::find()->where(['del' => EnumUtil::DELETE_NOT])->orderBy(['updated_at'=>SORT_DESC]);
|
|
$totalCount = $find->count();
|
|
|
|
$dbList = $find->offset($offset)->limit($pageSize)->all();
|
|
|
|
$pages = new Pagination(['totalCount' => $totalCount, 'pageSize' => $pageSize]);
|
|
|
|
$returnData['totalCount'] = $totalCount;
|
|
$returnData['list'] = $dbList;
|
|
$returnData['pages'] = $pages;
|
|
return $this->render('notice_list', $returnData);
|
|
}
|
|
|
|
public function actionNoticeGet()
|
|
{
|
|
$id = Yii::$app->request->get('id');
|
|
|
|
if (empty($id)) return InfoUtil::Info($this, '未找到数据');
|
|
|
|
$item = Notice::findByID($id);
|
|
if (empty($item)) return InfoUtil::Info($this, '未找到数据');
|
|
|
|
return $this->render('notice_edit', ['item' => $item]);
|
|
}
|
|
|
|
/**
|
|
* 编辑
|
|
* @return string
|
|
*/
|
|
public function actionNoticeEdit()
|
|
{
|
|
$id = Yii::$app->request->post('id');
|
|
$title = Yii::$app->request->post('title');
|
|
$message = Yii::$app->request->post('message');
|
|
|
|
$title = trim($title);
|
|
$message = trim($message);
|
|
|
|
if (!isset($id)) return InfoUtil::Info($this, '未找到数据');
|
|
if (!isset($title) || empty($title)) return InfoUtil::Info($this, '缺少标题');
|
|
if (!isset($message) || empty($message)) return InfoUtil::Info($this, '缺少内容');
|
|
|
|
$item = Notice::findByID($id);
|
|
if (empty($item)) return InfoUtil::Info($this, '未找到数据');
|
|
|
|
$item->title = $title;
|
|
$item->message = $message;
|
|
if (!$item->save()) return InfoUtil::Info($this, '保存失败');
|
|
|
|
return InfoUtil::Info($this, '操作成功');
|
|
}
|
|
|
|
public function actionNoticeAdd()
|
|
{
|
|
$title = Yii::$app->request->post('title');
|
|
$message = Yii::$app->request->post('message');
|
|
|
|
if(!isset($title) && !isset($message)) return $this->render('notice_add');
|
|
|
|
$title = trim($title);
|
|
$message = trim($message);
|
|
|
|
if (!isset($title) || empty($title)) return InfoUtil::Info($this, '缺少标题');
|
|
if (!isset($message) || empty($message)) return InfoUtil::Info($this, '缺少内容');
|
|
|
|
$item = new Notice();
|
|
$item->title = $title;
|
|
$item->message = $message;
|
|
if (!$item->save()) return InfoUtil::Info($this, '保存失败');
|
|
|
|
return InfoUtil::Info($this, '操作成功');
|
|
}
|
|
|
|
|
|
/**
|
|
* 删除公告
|
|
* @return string
|
|
*/
|
|
public function actionNoticeDel()
|
|
{
|
|
$id = Yii::$app->request->get('id');
|
|
if (!isset($id)) return InfoUtil::Info($this, '未找到数据');
|
|
|
|
$item = Notice::findByID($id);
|
|
if (empty($item)) return InfoUtil::Info($this, '未找到数据');
|
|
|
|
$item->del = EnumUtil::DELETE;
|
|
if (!$item->save()) return InfoUtil::Info($this, '删除失败');
|
|
|
|
return InfoUtil::Info($this, '删除成功');
|
|
}
|
|
} |