ShopGoodsCate.php 3.5 KB

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