Commit 082e3c92 authored by rlgy's avatar rlgy

公告管理

parent 93f34aa9
......@@ -9,8 +9,10 @@
namespace backend\controllers;
use backend\models\coin\ArticleForm;
use backend\models\coin\NoticeForm;
use Yii;
use common\models\pwallet\Article;
use common\models\pwallet\Notice;
use yii\db\Exception;
class SourceController extends BaseController
......@@ -127,4 +129,129 @@ class SourceController extends BaseController
}
$this->error('删除失败', Yii::$app->request->getReferrer());
}
/**
* 公告列表
* @return string
*/
public function actionNotice()
{
if (Yii::$app->request->isAjax) {
$request = Yii::$app->request;
$title = $request->get('title', '');
$status = $request->get('status', 'all');
$page = $request->get('page', 1);
$limit = $request->get('limit', 10);
$condition = [];
if (!empty($title)) {
$condition[] = ['like', 'title', $title];
}
if ($status != 'all') {
$condition[] = ['status' => $status];
}
$results = Notice::getList($page, $limit, $condition);
Yii::$app->response->format = 'json';
Yii::$app->response->data = $results;
Yii::$app->response->send();
}
return $this->render('notice');
}
/**
* 添加公告
* @return string
*/
public function actionNoticeAdd()
{
$model = new NoticeForm();
$model->scenario = 'add';
if (Yii::$app->request->isPost) {
$request = Yii::$app->request;
if ($model->load($request->post()) && $model->validate()) {
$notice = new Notice();
$notice->title = $model->title;
$notice->author = $model->author;
$notice->content = $model->content;
$notice->status = $model->status;
$notice->create_at = date('Y-m-d H:i:s');
$notice->update_at = date('Y-m-d H:i:s');
try {
$notice->save();
$this->success('添加成功', '/admin/source/notice');
} catch (Exception $exception) {
$this->error($exception->getMessage(), '/admin/source/notice');
}
}
$errors = $model->errors;
if ($errors) {
foreach ($errors as $k => $v) {
$errors = $v[0];
break;
}
}
$this->error($errors, Yii::$app->request->getReferrer());
}
return $this->render('notice-add', ['model' => $model]);
}
/**
* 编辑公告
* @return string
*/
public function actionNoticeEdit()
{
$model = new NoticeForm();
$model->scenario = 'edit';
$id = Yii::$app->request->get('id', null);
if ($id) {
$notice = Notice::findOne(['id' => $id]);
if ($notice) {
if (Yii::$app->request->isPost) {
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$notice->title = $model->title;
$notice->author = $model->author;
$notice->content = $model->content;
$notice->status = $model->status;
$notice->update_at = date('Y-m-d H:i:s');
try {
$notice->save();
$this->success('修改成功', '/admin/source/notice');
} catch (Exception $exception) {
$this->error($exception->getMessage(), '/admin/source/notice');
}
}
$errors = $model->errors;
if ($errors) {
foreach ($errors as $k => $v) {
$errors = $v[0];
break;
}
}
$this->error($errors, Yii::$app->request->getReferrer());
}
return $this->render('notice-edit', ['model' => $notice]);
}
}
$this->error('公告不存在', Yii::$app->request->getReferrer());
}
/**
* 删除公告
*/
public function actionNoticeDel()
{
$id = Yii::$app->request->get('id', null);
if ($id) {
$notice = Notice::findOne(['id' => $id]);
if ($notice) {
try {
$notice->delete();
$this->success('删除成功', Yii::$app->request->getReferrer());
} catch (Exception $exception) {
}
}
}
$this->error('删除失败', Yii::$app->request->getReferrer());
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-4
* Time: 上午10:22
*/
namespace backend\models\coin;
use yii\base\Model;
class NoticeForm extends Model
{
public $id;
public $title;
public $author;
public $content;
public $status;
public function formName()
{
return '';
}
public function rules()
{
return [
[['title', 'content', 'author'], 'required', 'on' => 'add'],
[['id', 'title', 'content', 'author'], 'required', 'on' => 'edit'],
];
}
public function scenarios()
{
return [
'add' => ['title', 'content', 'status', 'author'],
'edit' => ['id', 'title', 'content', 'status', 'author'],
];
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-4
* Time: 上午10:18
*/
/**
* @var $model backend\models\coin\ArticleForm
*/
?>
<div class="layui-row">
<div class="layui-col-md6">
<form class="layui-form" lay-filter="form1" method="post" action="">
<input type="hidden" value="<?= Yii::$app->request->getCsrfToken() ?>" name="_csrf">
<input type="hidden" value="<?= $model->id ?>" name="id">
<div class="layui-form-item">
<label class="layui-form-label" style="margin-bottom: 0; width: 100px;">标题</label>
<div class="layui-input-block">
<input class="layui-input" type="text" value="<?= $model->title ?>" lay-verify="required"
name="title">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label" style="margin-bottom: 0; width: 100px;">作者</label>
<div class="layui-input-block">
<input class="layui-input" type="text" value="<?= Yii::$app->user->identity->username ?>"
lay-verify="required" name="author" readonly="readonly">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label" style="margin-bottom: 0; width: 100px;">内容</label>
<div class="layui-input-block">
<textarea class="layui-textarea" name="content" lay-verify="required"><?= $model->content ?></textarea>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label" style="margin-bottom: 0; width: 100px;">发布</label>
<div class="layui-input-block">
<select name="status">
<option value="1">发布</option>
<option value="0">暂不发布</option>
</select>
</div>
</div>
<div class="layui-form-item">
<button class="layui-btn" lay-submit>保存</button>
</div>
</form>
</div>
</div>
<script>
var form = layui.form;
form.render(null, 'form1');
</script>
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-4
* Time: 上午10:17
*/
?>
<h4>添加文章</h4>
<?= $this->render('_form-notice', ['model' => $model]) ?>
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-4
* Time: 下午2:19
*/
?>
<h4>编辑文章</h4>
<?= $this->render('_form-notice', ['model' => $model]) ?>
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-4
* Time: 上午9:19
*/
?>
<h4>文章列表</h4>
<div class="layui-row">
<div class="layui-col-md1">
<a href="/admin/source/notice-add">
<button class="layui-btn lauyi-btn-default" id="add">添加公告</button>
</a>
</div>
<div class="layui-col-md11">
<form class="layui-form" lay-filter="form1" method="get" action="">
<div class="layui-inline">
<label class="layui-form-label" style="width: 100px; margin-bottom: 0">公告标题</label>
<div class="layui-input-block">
<input class="layui-input" type="text" value="" name="title" title="公告标题">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label" style="width: 100px; margin-bottom: 0">公告状态</label>
<div class="layui-input-block">
<select name="status" title="公告状态">
<option value="all">所有</option>
<option value="0">未发布</option>
<option value="1">已发布</option>
</select>
</div>
</div>
<div class="layui-inline">
<button class="layui-btn" lay-submit lay-filter="search1">搜索</button>
</div>
</form>
</div>
</div>
<div class="layui-row">
<table class="layui-table" id="table1"></table>
</div>
<script>
var form = layui.form;
form.render(null, 'form1');
var table = layui.table;
table.render({
elem: "#table1",
cols: [[
{field: 'id', title: 'ID'},
{field: 'title', title: '标题'},
{field: 'content', title: '内容'},
{field: 'author', title: '作者'},
{field: 'status', title: '状态', templet: '#statusTpl'},
{field: 'create_at', title: '创建时间'},
{field: 'update_at', title: '更新时间'},
{field: 'id', title: '操作', templet: '#operationTpl'}
]],
page: 1,
limit: 10,
url: '/admin/source/notice'
});
form.on('submit(search1)', function (data) {
table.reload("table1", {
where: data.field,
page: {curr: 1}
});
return false;
});
</script>
<script type="text/html" id="statusTpl">
{{# if(d.status==1){ }}
已发布
{{# }else{ }}
未发布
{{# } }}
</script>
<script type="text/html" id="operationTpl">
<a href="/admin/source/notice-edit?id={{d.id}}" title="编辑">
<button class="layui-btn layui-btn-sm"><i class="layui-icon">&#xe642;</i></button>
</a>
<a href="/admin/source/notice-del?id={{d.id}}" title="删除">
<button class="layui-btn layui-btn-sm layui-btn-danger"><i class="layui-icon">&#xe640;</i></button>
</a>
</script>
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-4
* Time: 下午3:01
*/
namespace common\models\pwallet;
use Yii;
use common\core\BaseActiveRecord;
class Notice extends BaseActiveRecord
{
public static function getDb()
{
return Yii::$app->get('db_pwallet');
}
public static function getList($page = 1, $limit = 10, $condition = [])
{
$query = self::find();
foreach ($condition as $item) {
$query = $query->andFilterWhere($item);
}
$count = $query->count();
if ($count) {
$data = $query->offset(($page - 1) * $limit)->limit($limit)->asArray()->all();
return ['code' => 0, 'count' => $count, 'data' => $data];
}
return ['code' => 1, 'msg' => '数据为空'];
}
}
\ 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