Cate.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\data\controller\shop;
  3. use app\data\model\ShopGoodsCate;
  4. use think\admin\Controller;
  5. use think\admin\extend\DataExtend;
  6. /**
  7. * 商品分类管理
  8. * Class Cate
  9. * @package app\data\controller\shop
  10. */
  11. class Cate extends Controller
  12. {
  13. /**
  14. * 商品分类管理
  15. * @auth true
  16. * @menu true
  17. * @throws \think\db\exception\DataNotFoundException
  18. * @throws \think\db\exception\DbException
  19. * @throws \think\db\exception\ModelNotFoundException
  20. */
  21. public function index()
  22. {
  23. $this->title = "商品分类管理";
  24. $query = $this->_query(ShopGoodsCate::class)->like('name')->dateBetween('create_at');
  25. $query->equal('status')->where(['deleted' => 0])->order('sort desc,id desc')->page(false);
  26. }
  27. /**
  28. * 列表数据处理
  29. * @param array $data
  30. */
  31. protected function _index_page_filter(array &$data)
  32. {
  33. foreach ($data as &$vo) {
  34. $vo['ids'] = join(',', DataExtend::getArrSubIds($data, $vo['id']));
  35. }
  36. $data = DataExtend::arr2table($data);
  37. }
  38. /**
  39. * 添加商品分类
  40. * @auth true
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function add()
  46. {
  47. $this->_form(ShopGoodsCate::mk(), 'form');
  48. }
  49. /**
  50. * 编辑商品分类
  51. * @auth true
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. */
  56. public function edit()
  57. {
  58. $this->_form(ShopGoodsCate::class, 'form');
  59. }
  60. /**
  61. * 表单数据处理
  62. * @param array $data
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. protected function _form_filter(array &$data)
  68. {
  69. if ($this->request->isGet()) {
  70. $data['pid'] = intval($data['pid'] ?? input('pid', '0'));
  71. $cates = ShopGoodsCate::mk()->where(['deleted' => 0])->order('sort desc,id desc')->select()->toArray();
  72. $this->cates = DataExtend::arr2table(array_merge($cates, [['id' => '0', 'pid' => '-1', 'name' => '顶部分类']]));
  73. if (isset($data['id'])) foreach ($this->cates as $cate) if ($cate['id'] === $data['id']) $data = $cate;
  74. foreach ($this->cates as $key => $cate) if ((isset($data['spt']) && $data['spt'] <= $cate['spt'])) {
  75. unset($this->cates[$key]);
  76. }
  77. }
  78. }
  79. /**
  80. * 修改商品分类状态
  81. * @auth true
  82. * @throws \think\db\exception\DbException
  83. */
  84. public function state()
  85. {
  86. $this->_save(ShopGoodsCate::mk(), $this->_vali([
  87. 'status.in:0,1' => '状态值范围异常!',
  88. 'status.require' => '状态值不能为空!',
  89. ]));
  90. }
  91. /**
  92. * 删除商品分类
  93. * @auth true
  94. * @throws \think\db\exception\DbException
  95. */
  96. public function remove()
  97. {
  98. $this->_delete(ShopGoodsCate::mk());
  99. }
  100. }