Comment.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\index\controller;
  13. use app\index\model\Order as OrderModel;
  14. use app\index\model\Comment as CommentModel;
  15. use app\index\model\OrderGoods as OrderGoodsModel;
  16. use think\facade\Session;
  17. /**
  18. * 订单评价管理
  19. * Class Comment
  20. * @package app\api\controller\order
  21. */
  22. class Comment extends Controller
  23. {
  24. /**
  25. * 待评价订单商品列表
  26. * @param int $orderId
  27. * @return \think\response\Json
  28. * @throws \Exception
  29. * @throws \app\common\exception\BaseException
  30. * @throws \think\exception\DbException
  31. */
  32. public function list(int $orderId)
  33. {
  34. // 订单信息
  35. $orderInfo = OrderModel::getDetail($orderId);
  36. // 验证订单是否已完成
  37. $model = new CommentModel;
  38. if (!$model->checkOrderAllowComment($orderInfo)) {
  39. return $this->renderError($model->getError());
  40. }
  41. // 待评价商品列表
  42. $goodsList = OrderGoodsModel::getNotCommentGoodsList($orderId);
  43. if ($goodsList->isEmpty()) {
  44. return $this->renderError('该订单没有可评价的商品');
  45. }
  46. return $this->renderSuccess(compact('goodsList'));
  47. }
  48. /**
  49. * 创建商品评价
  50. * @param int $orderId
  51. * @return array|\think\response\Json
  52. * @throws \app\common\exception\BaseException
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public function submit(int $orderId)
  58. {
  59. $userId = Session::get('user_id');
  60. if (empty($userId)) {
  61. return $this->renderJson(config('status.not_logged'), 'Log in please!');
  62. }
  63. // 订单信息
  64. $orderInfo = OrderModel::getDetail($orderId);
  65. // 验证订单是否已完成
  66. $model = new CommentModel;
  67. if (!$model->checkOrderAllowComment($orderInfo)) {
  68. return $this->renderError($model->getError());
  69. }
  70. // 待评价商品列表
  71. $goodsList = OrderGoodsModel::getNotCommentGoodsList($orderId);
  72. if ($goodsList->isEmpty()) {
  73. return $this->renderError('该订单没有可评价的商品');
  74. }
  75. // 提交商品评价
  76. $model = new CommentModel;
  77. if ($model->increased($orderInfo, $goodsList, $this->postForm())) {
  78. return $this->renderSuccess([], '评价发表成功');
  79. }
  80. return $this->renderError($model->getError() ?: '评价发表失败');
  81. }
  82. /**
  83. * 商品评价列表
  84. * @param int $goodsId 商品ID
  85. * @param int|null $scoreType 评价评分
  86. * @return \think\response\Json
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\DbException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. */
  91. public function listPage(int $goodsId, int $scoreType = null)
  92. {
  93. // 评价列表
  94. $model = new CommentModel;
  95. $list = $model->getCommentList($goodsId, $scoreType);
  96. return $this->renderSuccess(compact('list'));
  97. }
  98. /**
  99. * 商品评分总数
  100. * @param int $goodsId
  101. * @return array|\think\response\Json
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. public function total(int $goodsId)
  107. {
  108. // 指定评分总数
  109. $model = new CommentModel;
  110. $total = $model->getTotal($goodsId);
  111. return $this->renderSuccess(compact('total'));
  112. }
  113. /**
  114. * 商品评价列表 (限制数量, 用于商品详情页展示)
  115. * @param int $goodsId
  116. * @param int $limit
  117. * @return array|\think\response\Json
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\DbException
  120. * @throws \think\db\exception\ModelNotFoundException
  121. */
  122. public function listRows(int $goodsId, int $limit = 5)
  123. {
  124. // 评价列表
  125. $model = new CommentModel;
  126. $list = $model->listRows($goodsId, $limit);
  127. // 评价总数量
  128. $total = $model->rowsTotal($goodsId);
  129. return $this->renderSuccess(compact('list', 'total'));
  130. }
  131. }