You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.9 KiB
97 lines
2.9 KiB
<?php
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\controller;
|
|
|
|
use app\BaseController;
|
|
use app\model\Notice as NoticeModel;
|
|
use app\validate\Notice as NoticeValidate;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Request;
|
|
|
|
class Notice extends BaseController
|
|
{
|
|
/**
|
|
* 显示资源列表
|
|
*/
|
|
public function list()
|
|
{
|
|
$request = Request::param();
|
|
|
|
$limit = $request['limit'] ?? 10;
|
|
|
|
$where = [];
|
|
|
|
$CustomerServiceModel = new NoticeModel();
|
|
|
|
# 查询用户列表
|
|
$field = 'id,title,sort,content';
|
|
$res = $CustomerServiceModel->field($field)->where($where)->order('sort asc,id desc')->paginate($limit);
|
|
|
|
$list = $res->items();
|
|
$total = $res->total();
|
|
|
|
return $this->renderSuccess('数据返回成功', ['list' => $list, 'total' => $total]);
|
|
}
|
|
|
|
/**
|
|
* 保存新建的资源
|
|
*/
|
|
public function save()
|
|
{
|
|
$param = Request::param();
|
|
|
|
try {
|
|
$id = $param['id'] ?? 0;
|
|
|
|
if ($id) {
|
|
validate(NoticeValidate::class)->scene('edit')->check($param);
|
|
$CustomerServiceModel = NoticeModel::find($id);
|
|
$CustomerServiceModel->title = $param['title'];
|
|
$CustomerServiceModel->sort = $param['sort'] ?? 1;
|
|
$CustomerServiceModel->content = $param['content'];
|
|
$CustomerServiceModel->update_time = date("Y-m-d H:i:s",time());
|
|
$CustomerServiceModel->save();
|
|
} else {
|
|
validate(NoticeValidate::class)->scene('add')->check($param);
|
|
|
|
$CustomerService = new NoticeModel();
|
|
$CustomerService->save([
|
|
'title' => $param['title'],
|
|
'sort' => $param['sort'] ?? 1,
|
|
'content' => $param['content'],
|
|
'create_time' => date("Y-m-d H:i:s",time())
|
|
]);
|
|
}
|
|
|
|
return $this->renderSuccess($id ? '编辑成功' : '添加成功');
|
|
} catch (ValidateException $validateException) {
|
|
return $this->renderError($validateException->getMessage());
|
|
} catch (\Exception $e) {
|
|
return $this->renderError('操作失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除指定资源
|
|
*/
|
|
public function delete()
|
|
{
|
|
$param = Request::param();
|
|
|
|
try {
|
|
|
|
validate(NoticeValidate::class)->scene('del')->check($param);
|
|
|
|
$result = NoticeModel::destroy($param['id']);
|
|
|
|
if (!$result) throw new ValidateException('删除失败');
|
|
|
|
return $this->renderSuccess('已删除');
|
|
} catch (ValidateException $validateException) {
|
|
return $this->renderError($validateException->getMessage());
|
|
} catch (\Exception $e) {
|
|
return $this->renderError('操作失败');
|
|
}
|
|
}
|
|
}
|
|
|