Cate.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 = ShopGoodsCate::mQuery()->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. */
  42. public function add()
  43. {
  44. ShopGoodsCate::mForm('form');
  45. }
  46. /**
  47. * 编辑商品分类
  48. * @auth true
  49. */
  50. public function edit()
  51. {
  52. ShopGoodsCate::mForm('form');
  53. }
  54. /**
  55. * 表单数据处理
  56. * @param array $data
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. */
  61. protected function _form_filter(array &$data)
  62. {
  63. if ($this->request->isGet()) {
  64. $data['pid'] = intval($data['pid'] ?? input('pid', '0'));
  65. $cates = ShopGoodsCate::mk()->where(['deleted' => 0])->order('sort desc,id desc')->select()->toArray();
  66. $this->cates = DataExtend::arr2table(array_merge($cates, [['id' => '0', 'pid' => '-1', 'name' => '顶部分类']]));
  67. if (isset($data['id'])) foreach ($this->cates as $cate) if ($cate['id'] === $data['id']) $data = $cate;
  68. foreach ($this->cates as $key => $cate) if ((isset($data['spt']) && $data['spt'] <= $cate['spt'])) {
  69. unset($this->cates[$key]);
  70. }
  71. }
  72. }
  73. /**
  74. * 修改商品分类状态
  75. * @auth true
  76. */
  77. public function state()
  78. {
  79. ShopGoodsCate::mSave($this->_vali([
  80. 'status.in:0,1' => '状态值范围异常!',
  81. 'status.require' => '状态值不能为空!',
  82. ]));
  83. }
  84. /**
  85. * 删除商品分类
  86. * @auth true
  87. */
  88. public function remove()
  89. {
  90. ShopGoodsCate::mDelete();
  91. }
  92. }