NewsController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 NewsController extends AdminBaseController{
  14. // 后台新闻分类列表
  15. public function index(){
  16. $list=Db::name('news_cate')
  17. // ->where('status=1')
  18. ->order("sort DESC,id DESC")
  19. ->paginate(10);
  20. $list->each(function($v,$k){
  21. if($v['status']==1){
  22. $v['status_name'] = '启用';
  23. }else{
  24. $v['status_name'] = '隐藏';
  25. }
  26. $v['create_time'] = $v['create_time']? date('Y-m-d H:i:s',$v['create_time']):'';
  27. return $v;
  28. });
  29. $page = $list->render();
  30. $this->assign('list', $list);
  31. $this->assign('page', $page);
  32. // 渲染模板输出
  33. return $this->fetch();
  34. }
  35. //新闻分类添加
  36. public function add(){
  37. return $this->fetch();
  38. }
  39. public function add_post(){
  40. $data = input('post.');
  41. if($data['name']==''){
  42. $this->error('分类名称不能为空!');
  43. }
  44. $data['create_time'] = time();
  45. $add=Db::name('news_cate')->insertGetId($data);
  46. if($add){
  47. $this->success('添加成功!');
  48. }else{
  49. $this->error('添加失败!');
  50. }
  51. }
  52. //文章分类编辑
  53. public function edit(){
  54. $id = input('param.id');
  55. if($id){
  56. $info=Db::name('news_cate')->where('id',$id)->find();
  57. $this->assign('info',$info);
  58. }else{
  59. $this->error('数据传入失败!');
  60. }
  61. return $this->fetch();
  62. }
  63. public function edit_post(){
  64. $data = input('post.');
  65. if($data['name']==''){
  66. $this->error('分类名称不能为空!');
  67. }
  68. $save=Db::name('news_cate')->where('id',$data['id'])->update($data);
  69. if($save!==false){
  70. $url = 'news/index';
  71. $this->success('保存成功!',$url);
  72. }else{
  73. $this->error('保存失败!');
  74. }
  75. }
  76. // 新闻分类删除
  77. public function del(){
  78. $id = input('param.id');
  79. if($id){
  80. $result=Db::name('news_cate')->delete($id);
  81. if($result){
  82. //删除分类下的文章
  83. //Db::name('posts')->where('id',$id)->delete();
  84. $this->success('删除成功');
  85. }else{
  86. $this->error('删除失败');
  87. }
  88. }else{
  89. $this->error('数据传入失败!');
  90. }
  91. }
  92. // 文章分类排序
  93. public function listorder(){
  94. $listorders = input('listorders');
  95. foreach ($listorders as $key => $r) {
  96. $data=array();
  97. $data['listorder'] = $r;
  98. Db::name('terms')->where(['id'=>$key])->update($data);
  99. }
  100. $status = true;
  101. if ($status) {
  102. $this->success("排序更新成功!");
  103. } else {
  104. $this->error("排序更新失败!");
  105. }
  106. }
  107. }