// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\index\controller; use app\common\model\User as UserAlias; use app\index\model\Order as OrderModel; use app\index\model\Comment as CommentModel; use app\index\model\OrderGoods as OrderGoodsModel; use app\index\model\User as UserModel; use think\facade\Session; /** * 订单评价管理 * Class Comment * @package app\api\controller\order */ class Comment extends Controller { /** * 待评价订单商品列表 * @param int $orderId * @return \think\response\Json * @throws \Exception * @throws \app\common\exception\BaseException * @throws \think\exception\DbException */ public function list(int $orderId) { // 订单信息 $orderInfo = OrderModel::getDetail($orderId); // 验证订单是否已完成 $model = new CommentModel; if (!$model->checkOrderAllowComment($orderInfo)) { return $this->renderError($model->getError()); } // 待评价商品列表 $goodsList = OrderGoodsModel::getNotCommentGoodsList($orderId); if ($goodsList->isEmpty()) { return $this->renderError('该订单没有可评价的商品'); } return $this->renderSuccess(compact('goodsList')); } /** * 创建商品评价 * @param int $orderId * @return array|\think\response\Json * @throws \app\common\exception\BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function submit(int $orderId) { $userId = Session::get('user_id'); if (empty($userId)) { return $this->renderJson(config('status.not_logged'), 'Log in please!'); } // 订单信息 $orderInfo = OrderModel::getDetail($orderId); // 验证订单是否已完成 $model = new CommentModel; if (!$model->checkOrderAllowComment($orderInfo)) { return $this->renderError($model->getError()); } // 待评价商品列表 $goodsList = OrderGoodsModel::getNotCommentGoodsList($orderId); if ($goodsList->isEmpty()) { return $this->renderError('There are no items to review for this order.'); } // 提交商品评价 $model = new CommentModel; if ($model->increased($orderInfo, $goodsList, $this->postForm())) { //评论获得积分 //只有前十条评论给积分 $totalReviews = $model->rowsUserTotal($userId); if ($totalReviews < 10) { UserModel::setIncPoints(intval($userId), UserAlias::POINTS_FOR_REVIEW, 'Post a review.'); return $this->renderSuccess([], 'Success'); } } return $this->renderError($model->getError() ?: 'Something wrong.'); } /** * 商品评价列表 * @param int $goodsId 商品ID * @param int|null $scoreType 评价评分 * @return \think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function listPage(int $goodsId, int $scoreType = null) { // 评价列表 $model = new CommentModel; $list = $model->getCommentList($goodsId, $scoreType); return $this->renderSuccess(compact('list')); } /** * 商品评分总数 * @param int $goodsId * @return array|\think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function total(int $goodsId) { // 指定评分总数 $model = new CommentModel; $total = $model->getTotal($goodsId); return $this->renderSuccess(compact('total')); } /** * 商品评价列表 (限制数量, 用于商品详情页展示) * @param int $goodsId * @param int $limit * @return array|\think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function listRows(int $goodsId, int $limit = 5) { // 评价列表 $model = new CommentModel; $list = $model->listRows($goodsId, $limit); // 评价总数量 $total = $model->rowsTotal($goodsId); return $this->renderSuccess(compact('list', 'total')); } }