DeviceCate.php 3.1 KB

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