GGoods.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. class GGoods 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[] = ['del','=',0];
  26. $map= empty($map) ? true: $map;
  27. //数据查询
  28. $lists = Db::name('g_goods')->where($map)->limit($start,$length)->order($order)->select();
  29. foreach ($lists as $k=>$v){
  30. $lists[$k]['cate_name']=Db::name('g_goods_cate')->where('id',$v['cate_id'])->value('title');
  31. }
  32. //数据返回
  33. $totalCount = Db::name('g_goods')->where($map)->count();
  34. $totalPage = ceil($totalCount/$length);
  35. $result['page'] = $page;
  36. $result['total'] = $totalPage;
  37. $result['records'] = $totalCount;
  38. $result['rows'] = $lists;
  39. return json($result);
  40. }else{
  41. $this->assign('meta_title','商品列表');
  42. return $this->fetch();
  43. }
  44. }
  45. /**
  46. * 新增/编辑
  47. */
  48. public function add($id=0){
  49. if(request()->isPost()){
  50. $res = model('GGoods')->updates();
  51. if($res){
  52. $this->success('操作成功',url('index'));
  53. }else{
  54. $this->error(model('GGoods')->getError());
  55. }
  56. }else{
  57. $meta_title = '新增商品';
  58. if($id){
  59. $info = Db::name('g_goods')->where('id',$id)->find();
  60. $this->assign('info',$info);
  61. $meta_title = '编辑商品';
  62. }
  63. //获取分类
  64. $map['org_id'] = cur_org_id();
  65. $map['enable'] = 1;
  66. $cate = Db::name('g_goods_cate')->field('id as cate_id,title')->where($map)->select();
  67. $this->assign('goods_cate',$cate);
  68. $this->assign('meta_title',$meta_title);
  69. return $this->fetch();
  70. }
  71. }
  72. /**
  73. * 删除记录
  74. * @param int $id
  75. */
  76. public function del($id=0){
  77. if(!$id){
  78. $this->error('参数错误');
  79. }
  80. $res = Db::name('g_goods')->where('id',$id)->update(['del'=>1]);
  81. if($res){
  82. $this->success('删除成功');
  83. }else{
  84. $this->error('删除失败');
  85. }
  86. }
  87. /**
  88. * 改变字段值
  89. * @param int $fv
  90. * @param string $fn
  91. * @param int $fv
  92. */
  93. public function changeField($id=0,$fn='',$fv=0){
  94. if(!$fn||!$id){
  95. $this->error('参数错误');
  96. }
  97. $res = Db::name('g_goods')->where('id',$id)->update([$fn => $fv]);
  98. if($res){
  99. $this->success('操作成功');
  100. }else{
  101. $this->error('操作失败');
  102. }
  103. }
  104. }