1. 程式人生 > 其它 >TP6管理後臺實戰第五天-文章管理

TP6管理後臺實戰第五天-文章管理

第五天目標:

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框架中無重新整理上傳檔案

嘮嘮嗑:

遇到問題就要去解決,慢慢的解決的問題多了,在看到新的問題就有了思路。 遇到問題實在沒解決也沒關係,先繞過去或者找一個替代方案,後期多看看問題相關的內容,多思考,沒準有一天就突然明白了。 附完成後部分效果圖: 文章列表 新增文章

編輯文章

文章分類管理

如果你感覺有收穫,歡迎給我打賞 —— 以激勵我輸出更多優質內容,聯絡QQ:466146588

蠻掛著看看,沒準有驚喜呢,當然了寫文章並不是為了賺打賞錢,最主要目的是更好的記錄!

本文來自部落格園,作者:yangphp,轉載請註明原文連結:https://www.cnblogs.com/ypeih/p/15471383.html