Category.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 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 app\store\model\Category as CategoryModel;
  14. /**
  15. * 商品分类
  16. * Class Category
  17. * @package app\store\controller\goods
  18. */
  19. class Category extends Controller
  20. {
  21. /**
  22. * 商品分类列表
  23. * @return array
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\DbException
  26. * @throws \think\db\exception\ModelNotFoundException
  27. */
  28. public function list()
  29. {
  30. $model = new CategoryModel;
  31. $list = $model->getList();
  32. return $this->renderSuccess(compact('list'));
  33. }
  34. /**
  35. * 删除商品分类
  36. * @param $categoryId
  37. * @return array
  38. * @throws \Exception
  39. */
  40. public function delete(int $categoryId)
  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 array|string
  52. */
  53. public function add()
  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 array
  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)
  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. }