Category.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\store\controller;
  13. use think\response\Json;
  14. use app\store\model\Category as CategoryModel;
  15. /**
  16. * 商品分类
  17. * Class Category
  18. * @package app\store\controller\goods
  19. */
  20. class Category extends Controller
  21. {
  22. /**
  23. * 商品分类列表
  24. * @return Json
  25. * @throws \think\db\exception\DataNotFoundException
  26. * @throws \think\db\exception\DbException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. */
  29. public function list(): Json
  30. {
  31. $model = new CategoryModel;
  32. $list = $model->getList();
  33. return $this->renderSuccess(compact('list'));
  34. }
  35. /**
  36. * 删除商品分类
  37. * @param int $categoryId
  38. * @return Json
  39. */
  40. public function delete(int $categoryId): Json
  41. {
  42. // 分类详情
  43. $model = CategoryModel::detail($categoryId);
  44. if (!$model->remove()) {
  45. return $this->renderError($model->getError() ?: '删除失败');
  46. }
  47. return $this->renderSuccess('删除成功');
  48. }
  49. /**
  50. * 添加商品分类
  51. * @return Json
  52. */
  53. public function add(): Json
  54. {
  55. // 新增记录
  56. $model = new CategoryModel;
  57. if ($model->add($this->postForm())) {
  58. return $this->renderSuccess('添加成功');
  59. }
  60. return $this->renderError($model->getError() ?: '添加失败');
  61. }
  62. /**
  63. * 编辑商品分类
  64. * @param int $categoryId
  65. * @return Json
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. */
  70. public function edit(int $categoryId): Json
  71. {
  72. // 分类详情
  73. $model = CategoryModel::detail($categoryId, ['image']);
  74. // 更新记录
  75. if ($model->edit($this->postForm())) {
  76. return $this->renderSuccess('更新成功');
  77. }
  78. return $this->renderError($model->getError() ?: '更新失败');
  79. }
  80. }