Category.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\content\article;
  13. use think\response\Json;
  14. use app\store\controller\Controller;
  15. use app\store\model\article\Category as CategoryModel;
  16. /**
  17. * 文章分类
  18. * Class Category
  19. * @package app\store\controller\content\article
  20. */
  21. class Category extends Controller
  22. {
  23. /**
  24. * 文章分类列表
  25. * @return Json
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\DbException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. */
  30. public function list(): Json
  31. {
  32. $model = new CategoryModel;
  33. $list = $model->getList();
  34. return $this->renderSuccess(compact('list'));
  35. }
  36. /**
  37. * 添加文章分类
  38. * @return Json
  39. */
  40. public function add(): Json
  41. {
  42. // 新增记录
  43. $model = new CategoryModel;
  44. if ($model->add($this->postForm())) {
  45. return $this->renderSuccess('添加成功');
  46. }
  47. return $this->renderError($model->getError() ?: '添加失败');
  48. }
  49. /**
  50. * 编辑文章分类
  51. * @param int $categoryId
  52. * @return Json
  53. */
  54. public function edit(int $categoryId): Json
  55. {
  56. // 分类详情
  57. $model = CategoryModel::detail($categoryId);
  58. // 更新记录
  59. if ($model->edit($this->postForm())) {
  60. return $this->renderSuccess('更新成功');
  61. }
  62. return $this->renderError($model->getError() ?: '更新失败');
  63. }
  64. /**
  65. * 删除文章分类
  66. * @param int $categoryId
  67. * @return Json
  68. */
  69. public function delete(int $categoryId): Json
  70. {
  71. $model = CategoryModel::detail($categoryId);
  72. if (!$model->remove($categoryId)) {
  73. return $this->renderError($model->getError() ?: '删除失败');
  74. }
  75. return $this->renderSuccess('删除成功');
  76. }
  77. }