Article.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. use think\Exception;
  6. class Article extends Auth
  7. {
  8. public function __construct(App $app = null) {
  9. parent::__construct($app);
  10. $this->model= new \app\common\model\Article();
  11. $this->table=$this->model->table;
  12. }
  13. public function index(){
  14. if(request()->isAjax()){
  15. //分页参数
  16. $length = input('rows',10,'intval'); //每页条数
  17. $page = input('page',1,'intval'); //第几页
  18. $start = ($page - 1) * $length; //分页开始位置
  19. //排序
  20. $sortRow = input('sidx','id','trim'); //排序列
  21. $sort = input('sord','desc','trim'); //排序方式
  22. $order = $sortRow.' '.$sort;
  23. $title = input('title','','trim');
  24. if($title){
  25. $map[] = ['title','like','%'.$title.'%'];
  26. }
  27. $enable = input('enable','','trim');
  28. if($enable != ''){
  29. $map[] = ['enable','=',$enable];
  30. }
  31. $cate = input('cate_id','','trim');
  32. if($cate != ''){
  33. $map[] = ['cate_id','=',$cate];
  34. }
  35. $map[] = ['del','=',0];
  36. $map[] = ['org_id','=',$this->orgId];
  37. $map= empty($map) ? true: $map;
  38. //数据查询
  39. $lists = db($this->table)->where($map)->limit($start,$length)->order($order)->select();
  40. foreach ($lists as $k=>$v){
  41. $lists[$k]['cate_name'] = db('article_cate')
  42. ->where('id',$v['cate_id'])
  43. ->value('title');
  44. }
  45. //数据返回
  46. $totalCount = db($this->table)->where($map)->count();
  47. $totalPage = ceil($totalCount/$length);
  48. $result['page'] = $page;
  49. $result['total'] = $totalPage;
  50. $result['records'] = $totalCount;
  51. $result['rows'] = $lists;
  52. return json($result);
  53. }else{
  54. $this->assign('m_name','文章列表');
  55. $cate = (new \app\common\model\ArticleCate())->getList();
  56. $this->assign('cate',$cate);
  57. return $this->fetch();
  58. }
  59. }
  60. /**
  61. * 新增/编辑
  62. */
  63. public function add($id=0){
  64. if(request()->isPost()){
  65. $res = $this->model->updates();
  66. if($res){
  67. $this->success('操作成功',url('index'));
  68. }else{
  69. $this->error($this->model->getError());
  70. }
  71. }else{
  72. if($id){
  73. $info =db($this->table)->where('id',$id)->find();
  74. $info['roles_ids'] = !empty($info['roles_ids'])?explode(',',$info['roles_ids']):[];
  75. $this->assign('info',$info);
  76. }
  77. $roles = (new \app\common\model\WorkTypeMode())->getRoles(13);
  78. $a = [];
  79. foreach ($roles as $k=>$v){
  80. $a[] = [
  81. 'id'=>$v['id'],
  82. 'title'=>$v['name'],
  83. ];
  84. }
  85. $cate = (new \app\common\model\ArticleCate())->getList();
  86. $this->assign('cate',$cate);
  87. $this->assign('roles',$a);
  88. return $this->fetch();
  89. }
  90. }
  91. /**
  92. * 删除记录
  93. * @param int $id
  94. */
  95. public function del($id=0){
  96. if(!$id){
  97. $this->error('参数错误');
  98. }
  99. $res = db($this->table)->where('id',$id)->setField('del',1);
  100. if($res){
  101. $this->success('删除成功');
  102. }else{
  103. $this->error('删除失败');
  104. }
  105. }
  106. /**
  107. * 改变字段值
  108. * @param int $fv
  109. * @param string $fn
  110. * @param int $fv
  111. */
  112. public function changeField($id=0,$fn='',$fv=0){
  113. if(!$fn||!$id){
  114. $this->error('参数错误');
  115. }
  116. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  117. if($res){
  118. $this->success('操作成功');
  119. }else{
  120. $this->error('操作失败');
  121. }
  122. }
  123. public function batchSort(){
  124. $data = input('data','','trim');
  125. if(!$data){
  126. $this->error('参数错误');
  127. }
  128. $data = json_decode($data,true);
  129. if(!$data){
  130. $this->error('参数错误');
  131. }
  132. Db::startTrans();
  133. try{
  134. foreach ($data as $k=>$v){
  135. Db::name('article')->where('id',$v['id'])->setField('sort',$v['sort']);
  136. }
  137. Db::commit();
  138. }catch (Exception $e){
  139. Db::rollback();
  140. $this->error('操作失败');
  141. }
  142. $this->success('操作成功');
  143. }
  144. }