0
0

Article.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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,$type=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. $ptitle = '新增文章';
  73. if($id){
  74. $ptitle = '编辑文章';
  75. $info =db($this->table)->where('id',$id)->find();
  76. $info['roles_ids'] = !empty($info['roles_ids'])?explode(',',$info['roles_ids']):[];
  77. $this->assign('info',$info);
  78. }
  79. $roles = (new \app\common\model\WorkTypeMode())->getRoles(13,$this->orgId);
  80. $a = [];
  81. foreach ($roles as $k=>$v){
  82. $a[] = [
  83. 'id'=>$v['id'],
  84. 'title'=>$v['name'],
  85. ];
  86. }
  87. $cate = (new \app\common\model\ArticleCate())->getList();
  88. $this->assign('cate',$cate);
  89. $this->assign('roles',$a);
  90. $this->assign('type',$type);
  91. $this->assign('meta_title',$ptitle);
  92. return $this->fetch();
  93. }
  94. }
  95. /**
  96. * 删除记录
  97. * @param int $id
  98. */
  99. public function del($id=0){
  100. if(!$id){
  101. $this->error('参数错误');
  102. }
  103. $res = db($this->table)->where('id',$id)->setField('del',1);
  104. if($res){
  105. $this->success('删除成功');
  106. }else{
  107. $this->error('删除失败');
  108. }
  109. }
  110. /**
  111. * 改变字段值
  112. * @param int $fv
  113. * @param string $fn
  114. * @param int $fv
  115. */
  116. public function changeField($id=0,$fn='',$fv=0){
  117. if(!$fn||!$id){
  118. $this->error('参数错误');
  119. }
  120. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  121. if($res){
  122. $this->success('操作成功');
  123. }else{
  124. $this->error('操作失败');
  125. }
  126. }
  127. public function batchSort(){
  128. $data = input('data','','trim');
  129. if(!$data){
  130. $this->error('参数错误');
  131. }
  132. $data = json_decode($data,true);
  133. if(!$data){
  134. $this->error('参数错误');
  135. }
  136. Db::startTrans();
  137. try{
  138. foreach ($data as $k=>$v){
  139. Db::name('article')->where('id',$v['id'])->setField('sort',$v['sort']);
  140. }
  141. Db::commit();
  142. }catch (Exception $e){
  143. Db::rollback();
  144. $this->error('操作失败');
  145. }
  146. $this->success('操作成功');
  147. }
  148. }