ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

TP6管理后台实战第五天-文章管理

2021-10-27 16:32:59  阅读:193  来源: 互联网

标签:return cate 管理 array TP6 news 第五天 data id


第五天目标:

1、文章管理  

进入开发:

1、文章分类管理

  1.1 文章分类列表 -- ok     搜索项: 分类名称 分类状态     列表项:ID,分类名称,排序,文章数,是否显示 操作(编辑,删除)   1.2 添加文章分类 -- ok   1.3 修改文章分类 -- ok   1.4 删除文章分类 -- ok   控制器代码:
  1  /**
  2      * 文章分类列表
  3      */
  4     public function newsCateList()
  5     {
  6         if (!$this->access)  exit('无此访问权限!');
  7 
  8         $data = request()->param();
  9 
 10         $return_data = array(
 11             'admin_info' => $this->admin_info,
 12             'admin_id'   => $this->admin_id
 13         );
 14 
 15         //搜索条件
 16         $whereCond = array();
 17         if (!empty($data['cate_name'])) $whereCond[] = array('cate_name','like','%'.$data['cate_name'].'%');
 18         if (!empty($data['is_show'])) $whereCond[] = array('is_show','=',$data['is_show']);
 19         
 20         //搜索默认值
 21         $return_data['cate_name']     = empty($data['cate_name'])?'':$data['cate_name'];
 22         $return_data['is_show'] = empty($data['is_show'])?'':$data['is_show'];
 23       
 24 
 25         //获取列表
 26         $data_list = Db::name('yphp_news_cate')->where($whereCond)->order('cate_orders', 'desc')->paginate(array(
 27             'list_rows' => 10,
 28             'query'     => $data
 29         ))->each(function($item, $key){
 30 
 31             $item['news_count'] = Db::name('yphp_news')->where("cate_id",$item['id'])->count();
 32             return $item;
 33         });
 34 
 35 
 36         $return_data['data_list'] = $data_list;
 37         // 获取分页显示
 38         $return_data['page'] = $data_list->render();
 39 
 40        return view("news/news_cate_list",$return_data);
 41     }
 42 
 43     /**
 44      * 文章分类
 45      */
 46     public function newsCateDel()
 47     {
 48         if (!$this->access)  return json(array('status'=>'FAIL','msg'=>'无此访问权限!')); 
 49 
 50        $id  = request()->param('id');
 51 
 52        if (!empty($id)) 
 53        {
 54 
 55           $news_count = Db::name('yphp_news')->where("cate_id",$id)->count();
 56           if ($news_count > 0) 
 57           {
 58               return json(array('status'=>'FAIL','msg'=>'删除失败,该分类下还有'.$news_count.'篇文章'));
 59           }
 60           else
 61           {
 62             Db::name('yphp_news_cate')->where("id",$id)->delete();
 63             return json(array('status'=>'SUCCESS','msg'=>'删除成功'));
 64           }
 65        }
 66     }
 67 
 68     /**
 69      * 添加文章分类
 70      */
 71     public function newsCateAdd()
 72     {
 73         if (!$this->access)  exit('无此访问权限!');
 74 
 75        return view("news/news_cate_add");
 76     }
 77     /**
 78      * 修改文章分类
 79      */
 80     public function newsCateEdit()
 81     {
 82         if (!$this->access)  exit('无此访问权限!');
 83         
 84        $id = request()->param('id');
 85 
 86 
 87        $info = Db::name('yphp_news_cate')->where('id',$id)->find();
 88 
 89         return view("news/news_cate_edit",array('info'=>$info));
 90     }
 91     /**
 92      * 添加/修改文章分类操作
 93      */
 94     public function newsCateAddAct()
 95     {
 96        $data = request()->param();
 97 
 98        if(empty($data['id']))
 99        {
100          $cate_id = Db::name('yphp_news_cate')->strict(false)->insertGetId($data);
101          if ($cate_id > 0) 
102          {
103              return json(array('status'=>'SUCCESS','msg'=>'添加成功'));
104          }
105          else
106          {
107             return json(array('status'=>'FAIL','msg'=>'添加分类失败'));
108          }
109        }
110        else
111        {
112          //修改
113          Db::name('yphp_news_cate')->strict(false)->update($data);
114          return json(array('status'=>'SUCCESS','msg'=>'修改成功!'));
115        }
116     }
View Code

 

2、文章管理

  2.1 文章列表 -- ok     搜索项:标题,文章分类,文章状态,是否推荐   2.2 添加文章 -- ok   2.3 修改文章 -- ok   2.3 删除文章 -- ok    控制器代码
  1 /**
  2      * 文章列表
  3      */
  4     public function newsList()
  5     {
  6 
  7         if (!$this->access)  exit('无此访问权限!');
  8 
  9         $data = request()->param();
 10 
 11         $return_data = array(
 12             'admin_info' => $this->admin_info,
 13             'admin_id'   => $this->admin_id
 14         );
 15 
 16         //搜索条件
 17         $whereCond = array();
 18         if (!empty($data['news_title'])) $whereCond[] = array('news_title','like','%'.$data['news_title'].'%');
 19         if (!empty($data['cate_id'])) $whereCond[] = array('cate_id','=',$data['cate_id']);
 20         if (!empty($data['is_show'])) $whereCond[] = array('is_show','=',$data['is_show']);
 21         if (!empty($data['is_recommed'])) $whereCond[] = array('is_recommed','=',$data['is_recommed']);
 22         
 23         //搜索默认值
 24         $return_data['news_title']     = empty($data['news_title'])?'':$data['news_title'];
 25         $return_data['cate_id'] = empty($data['cate_id'])?'':$data['cate_id'];
 26         $return_data['is_show'] = empty($data['is_show'])?'':$data['is_show'];
 27         $return_data['is_recommed'] = empty($data['is_recommed'])?'':$data['is_recommed'];
 28 
 29         //获取列表
 30         $data_list = Db::name('yphp_news')->where($whereCond)->where("is_del","1")->order('id', 'desc')->paginate(array(
 31             'list_rows' => 10,
 32             'query'     => $data
 33         ))->each(function($item, $key){
 34 
 35             $item['cate_name'] = Db::name('yphp_news_cate')->where("id",$item['cate_id'])->value('cate_name');
 36             return $item;
 37         });
 38         
 39         $return_data['cate_list'] = Db::name('yphp_news_cate')->where("is_show",1)->order('cate_orders', 'desc')->select();
 40 
 41         $return_data['data_list'] = $data_list;
 42         // 获取分页显示
 43         $return_data['page'] = $data_list->render();
 44 
 45        return view("news/news_list",$return_data);
 46     }
 47 
 48     /**
 49      * 文章回收站
 50      */
 51     public function newsTrash()
 52     {
 53         if (!$this->access)  exit('无此访问权限!');
 54 
 55         $data = request()->param();
 56 
 57         $return_data = array(
 58             'admin_info' => $this->admin_info,
 59             'admin_id'   => $this->admin_id
 60         );
 61 
 62         //搜索条件
 63         $whereCond = array();
 64         if (!empty($data['news_title'])) $whereCond[] = array('news_title','like','%'.$data['news_title'].'%');
 65         if (!empty($data['cate_id'])) $whereCond[] = array('cate_id','=',$data['cate_id']);
 66         if (!empty($data['is_show'])) $whereCond[] = array('is_show','=',$data['is_show']);
 67         if (!empty($data['is_recommed'])) $whereCond[] = array('is_recommed','=',$data['is_recommed']);
 68         
 69         //搜索默认值
 70         $return_data['news_title']     = empty($data['news_title'])?'':$data['news_title'];
 71         $return_data['cate_id'] = empty($data['cate_id'])?'':$data['cate_id'];
 72         $return_data['is_show'] = empty($data['is_show'])?'':$data['is_show'];
 73         $return_data['is_recommed'] = empty($data['is_recommed'])?'':$data['is_recommed'];
 74 
 75         //获取列表
 76         $data_list = Db::name('yphp_news')->where($whereCond)->where("is_del","2")->order('id', 'desc')->paginate(array(
 77             'list_rows' => 10,
 78             'query'     => $data
 79         ))->each(function($item, $key){
 80 
 81             $item['cate_name'] = Db::name('yphp_news_cate')->where("id",$item['cate_id'])->value('cate_name');
 82             return $item;
 83         });
 84         
 85         $return_data['cate_list'] = Db::name('yphp_news_cate')->where("is_show",1)->order('cate_orders', 'desc')->select();
 86 
 87         $return_data['data_list'] = $data_list;
 88         // 获取分页显示
 89         $return_data['page'] = $data_list->render();
 90 
 91        return view("news/news_trash_list",$return_data);
 92     }
 93 
 94      /**
 95      * 文章删除 放回收站
 96      */
 97     public function newsDel()
 98     {
 99         if (!$this->access)  return json(array('status'=>'FAIL','msg'=>'无此访问权限!')); 
100 
101        $id  = request()->param('id');
102 
103        if (!empty($id)) 
104        {
105 
106           $data = array(
107             'is_del'        => 2,
108             'del_datetime'  => date("Y-m-d H:i:s")
109           );
110            Db::name('yphp_news')->where("id",$id)->update($data);
111 
112            return json(array('status'=>'SUCCESS','msg'=>'删除成功,文章已放入回收站'));
113        }
114     }
115     /**
116      * 文章删除  彻底删除
117      */
118     public function newsDelReal()
119     {
120 
121         if (!$this->access)  return json(array('status'=>'FAIL','msg'=>'无此访问权限!')); 
122 
123         $id  = request()->param('id');
124 
125        if (!empty($id)) 
126        {
127 
128 
129            Db::name('yphp_news')->where("id",$id)->delete();
130 
131            return json(array('status'=>'SUCCESS','msg'=>'删除成功'));
132        }
133     }
134 
135     /**
136      * 文章恢复
137      */
138     public function newsDelRestore()
139     {
140         if (!$this->access)  return json(array('status'=>'FAIL','msg'=>'无此访问权限!')); 
141 
142         $id  = request()->param('id');
143 
144        if (!empty($id)) 
145        {
146 
147 
148            $data = array(
149             'is_del'        => 1,
150             'del_datetime'  => null
151           );
152            Db::name('yphp_news')->where("id",$id)->update($data);
153 
154            return json(array('status'=>'SUCCESS','msg'=>'删除成功'));
155        }
156     }
157 
158    
159 
160     /**
161      * 添加文章
162      */
163     public function newsAdd()
164     {
165         if (!$this->access)  exit('无此访问权限!');
166 
167        //获取文章分类
168        $cate_list = Db::name('yphp_news_cate')->where("is_show",1)->order('cate_orders', 'desc')->select();
169        
170        return view("news/news_add",array('cate_list'=>$cate_list));
171     }
172     /**
173      * 修改文章
174      */
175     public function newsEdit()
176     {
177         if (!$this->access)  exit('无此访问权限!');
178 
179        $id = request()->param('id');
180 
181        $cate_list = Db::name('yphp_news_cate')->where("is_show",1)->order('cate_orders', 'desc')->select();
182 
183        $info = Db::name('yphp_news')->where('id',$id)->find();
184 
185         return view("news/news_edit",array('info'=>$info,'cate_list'=>$cate_list));
186     }
187 
188     /**
189      * 上传图片
190      */
191     public function uploadImg()
192     {
193         // 获取表单上传文件 例如上传了001.jpg
194         $file = request()->file('file');
195         $fiels = request()->file();
196         // 上传到本地服务器
197          try {
198             validate(['image'=>'fileSize:5120|fileExt:jpg,png,gif,jpeg,bmp|fileMime:image/jpeg,image/gif,image/png,image/bmp'])->check($fiels);
199 
200             $savename = \think\facade\Filesystem::disk('public')->putFile( 'news', $file);
201 
202             return json(array('status'=>'SUCCESS','msg'=>"上传成功",'filename'=>"/uploads/".$savename));
203 
204         } catch (\think\exception\ValidateException $e) {
205 
206             return json(array('status'=>'FAIL','msg'=>"上传失败".$e->getMessage()));
207         }
208     }
209 
210     /**
211      *  添加文件操作
212      */
213     public function newsAddAct()
214     {
215         $data = request()->param();
216 
217         $data['news_desc'] = htmlspecialchars($data['news_desc']);
218         $data['news_content'] = htmlspecialchars($data['news_content']);
219 
220         if(empty($data['id']))
221        {
222 
223         $data['add_datetime'] = date("Y-m-d H:i:s");
224 
225          $id = Db::name('yphp_news')->strict(false)->insertGetId($data);
226          if ($id > 0) 
227          {
228              return json(array('status'=>'SUCCESS','msg'=>'添加成功'));
229          }
230          else
231          {
232             return json(array('status'=>'FAIL','msg'=>'添加失败'));
233          }
234        }
235        else
236        {
237          //修改
238          Db::name('yphp_news')->strict(false)->update($data);
239          return json(array('status'=>'SUCCESS','msg'=>'修改成功!'));
240        }
241     }
View Code

 

3、文章回收站

  3.1 回收站列表 -- ok   3.2 彻底删除文章 -- ok   3.3 恢复已删除的文章 -- ok

4、遇到的问题

    4.1  文章内容要有富文本编辑器编辑内容和文件上传。具体怎么操作呢        见文章: TP6框架中引用KindEditor编辑器      4.2 文章封面,需要有无刷新进行图片上传。具体怎么操作?       见文章:TP6框架中无刷新上传文件  

唠唠嗑:

遇到问题就要去解决,慢慢的解决的问题多了,在看到新的问题就有了思路。 遇到问题实在没解决也没关系,先绕过去或者找一个替代方案,后期多看看问题相关的内容,多思考,没准有一天就突然明白了。   附完成后部分效果图:   文章列表  

 

 

添加文章  

 

 

编辑文章

 

 

文章分类管理

 

 

 

标签:return,cate,管理,array,TP6,news,第五天,data,id
来源: https://www.cnblogs.com/ypeih/p/15471383.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有