Category.php 2.3 KB

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