0
0

News.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. class News extends Auth
  5. {
  6. public function index(){
  7. if(request()->isAjax()){
  8. //分页参数
  9. $length = input('rows',10,'intval'); //每页条数
  10. $page = input('page',1,'intval'); //第几页
  11. $start = ($page - 1) * $length; //分页开始位置
  12. //排序
  13. $sortRow = input('sidx','id','trim'); //排序列
  14. $sort = input('sord','desc','trim'); //排序方式
  15. $order = $sortRow.' '.$sort;
  16. $title = input('title','','trim');
  17. if($title){
  18. $map[] = ['title','like','%'.$title.'%'];
  19. }
  20. $cate_id = input('cate_id','','trim');
  21. if($cate_id){
  22. $map[] = ['cate_id','=',$cate_id];
  23. }
  24. $enable = input('enable','','trim');
  25. if($enable != ''){
  26. $map[] = ['enable','=',$enable];
  27. }
  28. $map[] = ['del','=',0];
  29. $map[] = ['org_id','=',cur_org_id()];
  30. $map= empty($map) ? true: $map;
  31. //数据查询
  32. $lists = Db::name('news')->limit($start,$length)->where($map)->order(['sort'=>'desc','id'=>'desc'])->select();
  33. foreach ($lists as $k=>$v){
  34. $lists[$k]['cateTitle'] = db('news_cate')->where('id',$v['cate_id'])->value('title');
  35. $lists[$k]['likeNum'] = db('news_like')->where('news_id',$v['id'])
  36. ->group('user_id')->count();
  37. }
  38. //数据返回
  39. $totalCount = Db::name('news')->where($map)->count();
  40. $totalPage = ceil($totalCount/$length);
  41. $result['page'] = $page;
  42. $result['total'] = $totalPage;
  43. $result['records'] = $totalCount;
  44. $result['rows'] = $lists;
  45. return json($result);
  46. }else{
  47. $newsCate = db('news_cate')->where('org_id',cur_org_id())->select();
  48. $this->assign('newsCate',$newsCate);
  49. return $this->fetch();
  50. }
  51. }
  52. /**
  53. * 新增/编辑
  54. */
  55. public function add($id=0){
  56. if(request()->isPost()){
  57. $res = model('News')->updates();
  58. if($res){
  59. $this->success('操作成功',url('index'));
  60. }else{
  61. $this->error(model('News')->getError());
  62. }
  63. }else{
  64. $title = '新增';
  65. if($id){
  66. $title = '编辑';
  67. $info = Db::name('news')->where('id',$id)->find();
  68. $this->assign('info',$info);
  69. }
  70. $newsCate = Db::name('news_cate')->where('org_id',cur_org_id())->select();
  71. $this->assign('newsCate',$newsCate);
  72. $this->assign('title',$title);
  73. return $this->fetch();
  74. }
  75. }
  76. /**
  77. * 删除记录
  78. * @param int $id
  79. */
  80. public function del($id=0){
  81. if(!$id){
  82. $this->error('参数错误');
  83. }
  84. $res = Db::name('news')->where('id',$id)->setField('del',1);
  85. if($res){
  86. $this->success('删除成功');
  87. }else{
  88. $this->error('删除失败');
  89. }
  90. }
  91. /**
  92. * 改变字段值
  93. * @param int $fv
  94. * @param string $fn
  95. * @param int $fv
  96. */
  97. public function changeField($id=0,$fn='',$fv=0){
  98. if(!$fn||!$id){
  99. $this->error('参数错误');
  100. }
  101. $res = Db::name('news')->where('id',$id)->setField($fn,$fv);
  102. if($res){
  103. $this->success('操作成功');
  104. }else{
  105. $this->error('操作失败');
  106. }
  107. }
  108. /**
  109. * 排序
  110. * @param int $id
  111. * @param int $sort
  112. */
  113. public function changeSort($id=0,$sort=0){
  114. if($id<0||$sort<0){
  115. $this->error('参数错误');
  116. }
  117. $res = Db::name('news')->where('id',$id)->update(['sort'=>$sort]);
  118. if($res){
  119. $this->success('操作成功');
  120. }else{
  121. $this->error('操作失败');
  122. }
  123. }
  124. }