ShopGoodsCate.php 2.8 KB

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