NewspostController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: Tuolaji <479923197@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\controller;
  10. use cmf\controller\AdminBaseController;
  11. use think\facade\Db;
  12. use think\db\Query;
  13. class NewspostController extends AdminBaseController{
  14. protected function initialize()
  15. {
  16. parent::initialize();
  17. $adminId = cmf_get_current_admin_id(); //获取后台管理员id,可判断是否登录
  18. if (!empty($adminId)) {
  19. $this->assign('admin_id', $adminId);
  20. }
  21. }
  22. // 后台新闻列表
  23. public function index(){
  24. //新闻分类
  25. $cate_list=Db::name('news_cate')
  26. ->order("sort DESC,id DESC")
  27. ->select();
  28. $this->assign('cate_list', $cate_list);
  29. //新闻
  30. $posts=Db::name('news')
  31. ->where(function (Query $query) {
  32. $data = $this->request->param();
  33. // $query->where('post_type', 0);
  34. if (isset($data['id']) && $data['id']!='') {
  35. $query->where('id', $data['id']);
  36. }
  37. if (isset($data['cate_id']) && $data['cate_id']!='') {
  38. $query->where('cate_id', $data['cate_id']);
  39. }
  40. if (isset($data['keyword']) && !empty($data['keyword'])) {
  41. $keyword = $data['keyword'];
  42. $query->where('title|keywords', 'like', "%$keyword%");
  43. }
  44. })
  45. ->order("istop DESC,id DESC")
  46. ->paginate(20);
  47. $posts->each(function($v,$k){
  48. $userinfo=Db::name("user")
  49. ->field("user_nickname")
  50. ->where("id={$v['author']}")
  51. ->find();
  52. if(!$userinfo){
  53. $userinfo=array(
  54. 'user_nickname'=>'已删除',
  55. );
  56. }
  57. $v['userinfo']= $userinfo;
  58. //
  59. $cate_info=Db::name("news_cate")
  60. ->field("name")
  61. ->where("id='$v[cate_id]'")
  62. ->find();
  63. if(!$cate_info){
  64. $termsinfo=array(
  65. 'name'=>'已删除'
  66. );
  67. }
  68. $v['cate_info']= $cate_info;
  69. if($v['status'] == 1){
  70. $v['status_name'] = '显示';
  71. }else{
  72. $v['status_name'] = '隐藏';
  73. }
  74. return $v;
  75. });
  76. //分页-->筛选条件参数
  77. $data = $this->request->param();
  78. $posts->appends($data);
  79. // 获取分页显示
  80. $page = $posts->render();
  81. $this->assign('posts', $posts);
  82. $this->assign('page', $page);
  83. $configpub=getConfigPub();
  84. $this->assign("site",$configpub['site']);
  85. return $this->fetch();
  86. }
  87. //文章添加
  88. public function add(){
  89. //文章分类
  90. $cate_list=Db::name('news_cate')
  91. ->where('status=1')
  92. ->order("sort DESC,id DESC")
  93. ->select();
  94. $this->assign('cate_list', $cate_list);
  95. return $this->fetch();
  96. }
  97. public function add_post(){
  98. $data = input('post.post');
  99. if(empty($data['cate_id'])){
  100. $this->error("请至少选择一个分类!");
  101. }else if(empty($data['title'])){
  102. $this->error('请输入标题!');
  103. }
  104. $data['content']=html_entity_decode($data['content']);
  105. $data['author']=cmf_get_current_admin_id(); //获取后台管理员id
  106. // $path='';
  107. // if($data['termid']!=0){
  108. // $info=Db::name('terms')->where('id',$data['termid'])->value('path');
  109. // $path=$info;
  110. // }
  111. // $data['path']=$path;
  112. $data['create_time']=time();
  113. $add=Db::name('news')->insert($data);
  114. if($add){
  115. $this->success('添加成功!','newspost/index');
  116. }else{
  117. $this->error('添加失败!');
  118. }
  119. }
  120. //新闻编辑
  121. public function edit(){
  122. $id = input('param.id');
  123. if($id){
  124. //新闻分类
  125. $cate_list=Db::name('news_cate')
  126. ->order("sort DESC,id DESC")
  127. ->select();
  128. $this->assign('cate_list', $cate_list);
  129. $info=Db::name('news')->where('id',$id)->find();
  130. $info['content']=str_replace('../../','/upload/',$info['content']);
  131. $this->assign('info',$info);
  132. }else{
  133. $this->error('数据传入失败!');
  134. }
  135. return $this->fetch();
  136. }
  137. public function edit_post(){
  138. $data = input('post.post');
  139. $data['content']=html_entity_decode($data['content']);
  140. if(empty($data['cate_id'])){
  141. $this->error("请至少选择一个分类!");
  142. }else if(empty($data['title'])){
  143. $this->error('请输入标题!');
  144. }
  145. $save=Db::name('news')->where('id',$data['id'])->update($data);
  146. if($save!==false){
  147. $this->success('保存成功!','newspost/index');
  148. }else{
  149. $this->error('保存失败!');
  150. }
  151. }
  152. // 文章分类删除
  153. public function del(){
  154. $id = input('param.id');
  155. if($id){
  156. $result=Db::name('news')->delete($id);
  157. if($result){
  158. $this->success('删除成功');
  159. }else{
  160. $this->error('删除失败');
  161. }
  162. }else{
  163. $this->error('数据传入失败!');
  164. }
  165. }
  166. // 文章批量审核或取消审核
  167. public function check(){
  168. $data = input();
  169. foreach ($data['ids'] as $k => $r) {
  170. $save=array();
  171. $save['post_status'] = $data['check'];
  172. Db::name('posts')->where(['id'=>$r])->update($save);
  173. }
  174. $status = true;
  175. if ($status) {
  176. $this->success("操作成功!");
  177. } else {
  178. $this->error("操作失败!");
  179. }
  180. }
  181. // 文章批量置顶或取消置顶
  182. public function tops(){
  183. $data = input();
  184. foreach ($data['ids'] as $k => $r) {
  185. $save=array();
  186. $save['istop'] = $data['top'];
  187. Db::name('posts')->where(['id'=>$r])->update($save);
  188. }
  189. $status = true;
  190. if ($status) {
  191. $this->success("操作成功!");
  192. } else {
  193. $this->error("操作失败!");
  194. }
  195. }
  196. // 文章批量删除
  197. public function deletes(){
  198. $data = input();
  199. foreach ($data['ids'] as $k => $r) {
  200. Db::name('news')->where(['id'=>$r])->delete();
  201. }
  202. $status = true;
  203. if ($status) {
  204. $this->success("操作成功!");
  205. } else {
  206. $this->error("操作失败!");
  207. }
  208. }
  209. // // 文章排序
  210. // public function listordersset() {
  211. //
  212. //
  213. // $ids=$this->request->param('listordersset');
  214. // foreach ($ids as $key => $r) {
  215. // $data['orderno'] = $r;
  216. // Db::name("posts")
  217. // ->where(array('id' => $key))
  218. // ->update($data);
  219. // }
  220. //
  221. // $status = true;
  222. // if ($status) {
  223. // $this->success("排序更新成功!");
  224. // } else {
  225. // $this->error("排序更新失败!");
  226. // }
  227. // }
  228. }