GGoodsCate.php 3.2 KB

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