WxGoodsCate.php 3.6 KB

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