123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace app\api\controller;
- use app\common\model\Comment as CommentModel;
- use app\common\model\Order;
- use app\common\model\OrderAftersales;
- use app\common\model\OrderAction;
- use app\common\Service\Goods\CommentService;
- use app\api\validate\Comment as CommentValidate;
- use app\common\Enum\CommentEnum;
- use think\Db;
- use app\common\Enum\OrderEnum;
- use app\common\Enum\StatusEnum;
- /**
- * 评论
- */
- class Comment extends Base
- {
- protected $noNeedLogin = ['index','stats'];
- /**
- * 评论列表
- */
- public function index()
- {
- $params = $this->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);
- }
-
- }
|