request->post(); // 验证参数 $validate = new CommentValidate(); if (!$validate->scene('index')->check($params)) { $this->error($validate->getError()); } $list = CommentService::getCommentList($params); foreach ($list as $item) { if ($item->user) { $item->user->avatar = cdnurl($item->user->avatar, true); $item->user->visible(explode(',', 'id,nickname,avatar')); } $item->hidden(explode(',', 'subscribe,status,ip,useragent')); } $this->success('获取成功', $list); } /** * 添加评论 */ public function add() { $params = $this->request->param(); // 验证参数 $validate = new CommentValidate(); if (!$validate->scene('add')->check($params)) { $this->error($validate->getError()); } $pid = isset($params['pid']) ? (int)$params['pid'] : 0; $order_id = (int)$params['order_id']; $remark = $params['remark']; CommentService::addComment($this->auth->id, $order_id, $remark, $pid); $this->success('添加评论成功,等待审核!'); } /** * 我的评价 */ public function myList() { $params = $this->request->param(); // 验证参数 $validate = new CommentValidate(); if (!$validate->scene('myList')->check($params)) { $this->error($validate->getError()); } $page = isset($params['page']) ? (int)$params['page'] : 1; $pagesize = isset($params['pagesize']) ? (int)$params['pagesize'] : 10; $list = CommentService::getUserCommentList($this->auth->id, $page, $pagesize); foreach ($list as $item) { $item->hidden(['ip', 'subscribe', 'useragent', 'comments']); } $this->success('获取成功', $list); } /** * 获取商品评论统计 */ public function stats() { $params = $this->request->param(); // 验证参数 $validate = new CommentValidate(); if (!$validate->scene('stats')->check($params)) { $this->error($validate->getError()); } $goods_id = (int)$params['goods_id']; $stats = CommentService::getCommentStats($goods_id); // 格式化统计数据,增加更多统计信息 $formatted_stats = [ 'total' => $stats['total'], 'degree' => $stats['degree'], 'picture_count' => $stats['picture_count'], 'picture_percentage' => $stats['total'] > 0 ? round(($stats['picture_count'] / $stats['total']) * 100, 1) : 0, 'star_counts' => $stats['star_counts'], 'rating_stats' => $stats['rating_stats'], 'star_summary' => [ 'avg_star' => $stats['total'] > 0 ? round(array_sum(array_map(function($star, $count) { return $star * $count; }, array_keys($stats['star_counts']), $stats['star_counts'])) / $stats['total'], 1) : 0, 'total_stars' => array_sum($stats['star_counts']) ] ]; $this->success('获取成功', $formatted_stats); } /** * 审核评论 (管理员功能) */ public function audit() { $params = $this->request->param(); // 验证参数 $validate = new CommentValidate(); if (!$validate->scene('audit')->check($params)) { $this->error($validate->getError()); } $comment_id = (int)$params['comment_id']; $evaluate_status = (int)$params['evaluate_status']; CommentService::auditComment($comment_id, $evaluate_status); $this->success('审核成功'); } /** * 获取评论状态列表 */ public function getStatusList() { $data = [ 'evaluate_status' => CommentEnum::getEvaluateStatusList(), 'status' => StatusEnum::getMap(), 'has_picture' => CommentEnum::getHasPictureList(), 'rating_type' => CommentEnum::getRatingTypeList(), ]; $this->success('获取成功', $data); } }