TP6管理后台实战第五天-文章管理
第五天目标:
1、文章管理
进入开发:
1、文章分类管理
1.1 文章分类列表-好的
搜索项:分类名称分类状态
列表项:ID,分类名称,排序,文章数,是否显示操作(编辑,删除)
1.2 添加文章分类-好的
1.3 修改文章分类-好的
1.4 删除文章分类-好的
控制器代码:
1 /**
2 * 文章分类列表
3 */
四公共功能新闻类别列表()
5 {
6 if(!$this-access) exit('无此访问权限!');
七
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 $其中cond=array();
17如果(!空($ data[' cate _ name '])$ WHere cond[]=array(' cate _ name ',' like ',' % ').$data['cate_name'].'%');
18如果(!空($ data[' is _ show '])$ WHere cond[]=array(' is _ show ','=',$ data[' is _ show ']);
19
20 //搜索默认值
21 $return_data['cate_name']=空($ data[' cate _ name '])' ' : $ data[' cate _ name '];
22 $return_data['is_show']=空($ data[' is _ show '])' ' : $ data[' is _ show '];
23
24
25 //获取列表
26 $ data _ list=db : name(' yphp _ news _ cate ')-where($ where cond)-order(' cate _ orders ',' desc')-paginate(array(
27 'list_rows'=10,
28"查询"=$数据
29))-每个(功能($item,$ key){ 0
30
31 $ item[' news _ count ']=db : name(' yphp _ news ')-其中(' cate_id ',$ item[' id '])-count();
32返回$项;
33 });
34
35
36 $ return _ data[' data _ list ']=$ data _ list;
37 //获取分页显示
38 $ return _ data[' page ']=$ data _ list-render();
39
40返回视图(' news/news_cate_list ',$ return _ data);
41 }
42
43 /**
44 * 文章分类
45 */
46公共功能新闻目录(新闻目录)
47 {
48 if(!$this-access)返回json(数组(“状态”=“失败”,“消息”=)无此访问权限!'));
49
50 $ id=request()-param(' id ');
51
52 if(!空($id))
53 {
54
55 $ news _ count=db : name(' yphp _ news ')-其中(' cate_id ',$ id)-count();
56 if ($news_count 0)
57 {
58返回json(数组(“状态”=“失败”,“消息”=)删除失败,该分类下还有$news_count .篇文章'));
59 }
还有60个
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
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/52107.html
