Comment.php 5.2 KB

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