WxBarrel.php 3.6 KB

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