0
0

Banner.php 3.4 KB

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