Category.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\store\controller\goods;
  3. use app\store\controller\Controller;
  4. use app\store\model\Category as CategoryModel;
  5. /**
  6. * 商品分类
  7. * Class Category
  8. * @package app\store\controller\goods
  9. */
  10. class Category extends Controller
  11. {
  12. /**
  13. * 商品分类列表
  14. * @return mixed
  15. */
  16. public function index()
  17. {
  18. $model = new CategoryModel;
  19. $list = $model->getCacheTree();
  20. return $this->fetch('index', compact('list'));
  21. }
  22. /**
  23. * 删除商品分类
  24. * @param $category_id
  25. * @return array|bool
  26. * @throws \think\Exception
  27. * @throws \think\exception\DbException
  28. */
  29. public function delete($category_id)
  30. {
  31. $model = CategoryModel::get($category_id);
  32. if (!$model->remove($category_id)) {
  33. return $this->renderError($model->getError() ?: '删除失败');
  34. }
  35. return $this->renderSuccess('删除成功');
  36. }
  37. /**
  38. * 添加商品分类
  39. * @return array|mixed
  40. */
  41. public function add()
  42. {
  43. $model = new CategoryModel;
  44. if (!$this->request->isAjax()) {
  45. // 获取所有地区
  46. $list = $model->getCacheTree();
  47. return $this->fetch('add', compact('list'));
  48. }
  49. // 新增记录
  50. if ($model->add($this->postData('category'))) {
  51. return $this->renderSuccess('添加成功', url('goods.category/index'));
  52. }
  53. return $this->renderError($model->getError() ?: '添加失败');
  54. }
  55. /**
  56. * 编辑商品分类
  57. * @param $category_id
  58. * @return array|mixed
  59. * @throws \think\exception\DbException
  60. */
  61. public function edit($category_id)
  62. {
  63. // 模板详情
  64. $model = CategoryModel::get($category_id, ['image']);
  65. if (!$this->request->isAjax()) {
  66. // 获取所有地区
  67. $list = $model->getCacheTree();
  68. return $this->fetch('edit', compact('model', 'list'));
  69. }
  70. // 更新记录
  71. if ($model->edit($this->postData('category'))) {
  72. return $this->renderSuccess('更新成功', url('goods.category/index'));
  73. }
  74. return $this->renderError($model->getError() ?: '更新失败');
  75. }
  76. }