Comment.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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\api\controller;
  13. use think\response\Json;
  14. use app\api\model\Comment as CommentModel;
  15. /**
  16. * 商品评价控制器
  17. * Class Comment
  18. * @package app\api\controller
  19. */
  20. class Comment extends Controller
  21. {
  22. /**
  23. * 商品评价列表
  24. * @param int $goodsId 商品ID
  25. * @param int|null $scoreType 评价评分
  26. * @return Json
  27. * @throws \think\db\exception\DbException
  28. */
  29. public function list(int $goodsId, int $scoreType = null): Json
  30. {
  31. // 评价列表
  32. $model = new CommentModel;
  33. $list = $model->getCommentList($goodsId, $scoreType);
  34. return $this->renderSuccess(compact('list'));
  35. }
  36. /**
  37. * 商品评分总数
  38. * @param int $goodsId
  39. * @return Json
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function total(int $goodsId): Json
  45. {
  46. // 指定评分总数
  47. $model = new CommentModel;
  48. $total = $model->getTotal($goodsId);
  49. return $this->renderSuccess(compact('total'));
  50. }
  51. /**
  52. * 商品评价列表 (限制数量, 用于商品详情页展示)
  53. * @param int $goodsId
  54. * @param int $limit
  55. * @return Json
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\DbException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. */
  60. public function listRows(int $goodsId, int $limit = 5): Json
  61. {
  62. // 评价列表
  63. $model = new CommentModel;
  64. $list = $model->listRows($goodsId, $limit);
  65. // 评价总数量
  66. $total = $model->rowsTotal($goodsId);
  67. return $this->renderSuccess(compact('list', 'total'));
  68. }
  69. }