diff --git a/app/controller/Index.php b/app/controller/Index.php index 7dc4519..ca909dc 100644 --- a/app/controller/Index.php +++ b/app/controller/Index.php @@ -113,7 +113,7 @@ class Index extends BaseController try { validate()->rule(['id|公告id' => 'require|number'])->check($param); - $Notice = Notice::where('id',$param['id'])->order('create_time','desc')->find(); + $Notice = Notice::where('id',$param['id'])->order('sort asc,id desc')->find(); return $this->renderSuccess('数据返回成功',[ 'title' => $Notice->title, diff --git a/app/controller/Notice.php b/app/controller/Notice.php new file mode 100644 index 0000000..ad04392 --- /dev/null +++ b/app/controller/Notice.php @@ -0,0 +1,97 @@ +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('操作失败'); + } + } +} diff --git a/app/validate/Notice.php b/app/validate/Notice.php new file mode 100644 index 0000000..1e527b2 --- /dev/null +++ b/app/validate/Notice.php @@ -0,0 +1,36 @@ + ['规则1','规则2'...] + * + * @var array + */ + protected $rule = [ + 'id|公告id' => 'require|number', + 'title|公告标题' => 'require|max:40', + 'sort|排序' => 'number', + 'content|公告内容' => 'require' + ]; + + /** + * 定义错误信息 + * 格式:'字段名.规则名' => '错误信息' + * + * @var array + */ + protected $message = []; + + protected $scene = [ + 'add' => ['title','sort','content'], + 'edit' => ['title','sort','content','id'], + 'del' => ['id'] + ]; +}