Comment.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\Comment as CommentModel;
  4. use app\common\model\Order;
  5. use app\common\model\OrderAftersales;
  6. use app\common\model\OrderAction;
  7. use app\common\Service\Goods\CommentService;
  8. use app\api\validate\Comment as CommentValidate;
  9. use app\common\Enum\CommentEnum;
  10. use think\Db;
  11. use app\common\Enum\OrderEnum;
  12. use app\common\Enum\StatusEnum;
  13. /**
  14. * 评论
  15. */
  16. class Comment extends Base
  17. {
  18. protected $noNeedLogin = ['index','stats'];
  19. /**
  20. * 评论列表
  21. */
  22. public function index()
  23. {
  24. $params = $this->request->post();
  25. // 验证参数
  26. $validate = new CommentValidate();
  27. if (!$validate->scene('index')->check($params)) {
  28. $this->error($validate->getError());
  29. }
  30. $list = CommentService::getCommentList($params);
  31. foreach ($list as $item) {
  32. if ($item->user) {
  33. $item->user->avatar = cdnurl($item->user->avatar, true);
  34. $item->user->visible(explode(',', 'id,nickname,avatar'));
  35. }
  36. $item->hidden(explode(',', 'subscribe,status,ip,useragent'));
  37. }
  38. $this->success('获取成功', $list);
  39. }
  40. /**
  41. * 添加评论
  42. */
  43. public function add()
  44. {
  45. $params = $this->request->param();
  46. // 验证参数
  47. $validate = new CommentValidate();
  48. if (!$validate->scene('add')->check($params)) {
  49. $this->error($validate->getError());
  50. }
  51. $pid = isset($params['pid']) ? (int)$params['pid'] : 0;
  52. $order_id = (int)$params['order_id'];
  53. $remark = $params['remark'];
  54. CommentService::addComment($this->auth->id, $order_id, $remark, $pid);
  55. $this->success('添加评论成功,等待审核!');
  56. }
  57. /**
  58. * 我的评价
  59. */
  60. public function myList()
  61. {
  62. $params = $this->request->param();
  63. // 验证参数
  64. $validate = new CommentValidate();
  65. if (!$validate->scene('myList')->check($params)) {
  66. $this->error($validate->getError());
  67. }
  68. $page = isset($params['page']) ? (int)$params['page'] : 1;
  69. $pagesize = isset($params['pagesize']) ? (int)$params['pagesize'] : 10;
  70. $list = CommentService::getUserCommentList($this->auth->id, $page, $pagesize);
  71. foreach ($list as $item) {
  72. $item->hidden(['ip', 'subscribe', 'useragent', 'comments']);
  73. }
  74. $this->success('获取成功', $list);
  75. }
  76. /**
  77. * 获取商品评论统计
  78. */
  79. public function stats()
  80. {
  81. $params = $this->request->param();
  82. // 验证参数
  83. $validate = new CommentValidate();
  84. if (!$validate->scene('stats')->check($params)) {
  85. $this->error($validate->getError());
  86. }
  87. $goods_id = (int)$params['goods_id'];
  88. $stats = CommentService::getCommentStats($goods_id);
  89. // 格式化统计数据,增加更多统计信息
  90. $formatted_stats = [
  91. 'total' => $stats['total'],
  92. 'degree' => $stats['degree'],
  93. 'picture_count' => $stats['picture_count'],
  94. 'picture_percentage' => $stats['total'] > 0 ? round(($stats['picture_count'] / $stats['total']) * 100, 1) : 0,
  95. 'star_counts' => $stats['star_counts'],
  96. 'rating_stats' => $stats['rating_stats'],
  97. 'star_summary' => [
  98. '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,
  99. 'total_stars' => array_sum($stats['star_counts'])
  100. ]
  101. ];
  102. $this->success('获取成功', $formatted_stats);
  103. }
  104. /**
  105. * 审核评论 (管理员功能)
  106. */
  107. public function audit()
  108. {
  109. $params = $this->request->param();
  110. // 验证参数
  111. $validate = new CommentValidate();
  112. if (!$validate->scene('audit')->check($params)) {
  113. $this->error($validate->getError());
  114. }
  115. $comment_id = (int)$params['comment_id'];
  116. $evaluate_status = (int)$params['evaluate_status'];
  117. CommentService::auditComment($comment_id, $evaluate_status);
  118. $this->success('审核成功');
  119. }
  120. /**
  121. * 获取评论状态列表
  122. */
  123. public function getStatusList()
  124. {
  125. $data = [
  126. 'evaluate_status' => CommentEnum::getEvaluateStatusList(),
  127. 'status' => StatusEnum::getMap(),
  128. 'has_picture' => CommentEnum::getHasPictureList(),
  129. 'rating_type' => CommentEnum::getRatingTypeList(),
  130. ];
  131. $this->success('获取成功', $data);
  132. }
  133. }