abstract:<?php namespace app\admin\controller; use think\facade\Request; use app\admin\model\News as NewsModel; use think\facade\Session; //引入公共类 use app\admin\controlle
<?php namespace app\admin\controller; use think\facade\Request; use app\admin\model\News as NewsModel; use think\facade\Session; //引入公共类 use app\admin\controller\Common; class News extends Common { public function index() { //渲染新闻内容 $res = NewsModel::paginate(1); $this->assign('res', $res); return $this->fetch(); } public function create(){ //添加新闻模板 return $this->fetch(); } //编辑新闻 public function edit(){ $id = Request::param('id'); $res = NewsModel::get($id); $this->assign('res',$res); return $this->fetch(); } //更新数据 public function update(){ $data = Request::param(); $data['username'] = Session::get('username'); $data['time'] = time(); $news = new NewsModel(); $res = $news ->save($data,['id'=>$data['id']]); if($res){ return ['status'=>1,'msg'=>'更新成功']; }else{ return ['status'=>0,'msg'=>'更新失败']; } } //处理上传文件 public function uploads(){ $file = Request::file('img'); $info = $file->validate(['ext'=>'jpg,png,jif'])->move('uploads'); if($info){ //json()返回json格式数据 return json(['errno'=>0,'data'=>['/uploads/'.$info->getSaveName()]]); }else{ return $file->getError(); } } //获取提交的数据 public function save(){ //静态方法获取提交的数据 $data = Request::param(); $data['time'] = time(); $data['username'] = Session::get('username'); $news = NewsModel::where('title', $data['title'])->find(); //判断标题是否重复 if ($news ) { return ['status' => 0, 'msg' => '新闻标题重复!']; } $new = new NewsModel(); //save方法返回boolear if ($new->save($data)) { return ['status' => 1, 'msg' => '发布成功!']; } else { return ['status' => 0, 'msg' => '发布失败!']; } } //软删除数据 public function delete(){ $id = Request::param('id'); $res = NewsModel::destroy($id); if($res){ return ['status' => 1, 'msg' => '删除成功!']; }else{ return ['status' => 0, 'msg' => '删除失败!']; } } }
Correcting teacher:天蓬老师Correction time:2018-11-03 22:30:51
Teacher's summary:其实,只要是ajax请交,返回的数据默认就是json格式,不必再用json助手函数进行重复转换了