Comment.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /**
  13. * 评论
  14. */
  15. class Comment extends Base
  16. {
  17. protected $noNeedLogin = ['index'];
  18. /**
  19. * 评论列表
  20. */
  21. public function index()
  22. {
  23. $params = $this->request->param();
  24. // 验证参数
  25. $validate = new CommentValidate();
  26. if (!$validate->scene('index')->check($params)) {
  27. $this->error($validate->getError());
  28. }
  29. $list = CommentService::getCommentList($params);
  30. foreach ($list as $item) {
  31. if ($item->user) {
  32. $item->user->avatar = cdnurl($item->user->avatar, true);
  33. $item->user->visible(explode(',', 'id,nickname,avatar'));
  34. }
  35. $item->hidden(explode(',', 'subscribe,status,ip,useragent'));
  36. }
  37. $this->success('获取成功', $list);
  38. }
  39. /**
  40. * 添加评论
  41. */
  42. public function add()
  43. {
  44. $params = $this->request->param();
  45. // 验证参数
  46. $validate = new CommentValidate();
  47. if (!$validate->scene('add')->check($params)) {
  48. $this->error($validate->getError());
  49. }
  50. $pid = isset($params['pid']) ? (int)$params['pid'] : 0;
  51. $order_id = (int)$params['order_id'];
  52. $remark = $params['remark'];
  53. try {
  54. CommentService::addComment($this->auth->id, $order_id, $remark, $pid);
  55. $this->success('添加评论成功,等待审核!');
  56. } catch (\Exception $e) {
  57. $this->error($e->getMessage());
  58. }
  59. }
  60. /**
  61. * 我的评价
  62. */
  63. public function myList()
  64. {
  65. $params = $this->request->param();
  66. // 验证参数
  67. $validate = new CommentValidate();
  68. if (!$validate->scene('myList')->check($params)) {
  69. $this->error($validate->getError());
  70. }
  71. $page = isset($params['page']) ? (int)$params['page'] : 1;
  72. $pagesize = isset($params['pagesize']) ? (int)$params['pagesize'] : 10;
  73. $list = CommentService::getUserCommentList($this->auth->id, $page, $pagesize);
  74. foreach ($list as $item) {
  75. $item->hidden(['ip', 'subscribe', 'useragent', 'comments']);
  76. }
  77. $this->success('获取成功', $list);
  78. }
  79. /**
  80. * 获取商品评论统计
  81. */
  82. public function stats()
  83. {
  84. $params = $this->request->param();
  85. // 验证参数
  86. $validate = new CommentValidate();
  87. if (!$validate->scene('stats')->check($params)) {
  88. $this->error($validate->getError());
  89. }
  90. $goods_id = (int)$params['goods_id'];
  91. $stats = CommentService::getCommentStats($goods_id);
  92. $this->success('获取成功', $stats);
  93. }
  94. /**
  95. * 审核评论 (管理员功能)
  96. */
  97. public function audit()
  98. {
  99. $params = $this->request->param();
  100. // 验证参数
  101. $validate = new CommentValidate();
  102. if (!$validate->scene('audit')->check($params)) {
  103. $this->error($validate->getError());
  104. }
  105. $comment_id = (int)$params['comment_id'];
  106. $evaluate_status = (int)$params['evaluate_status'];
  107. try {
  108. CommentService::auditComment($comment_id, $evaluate_status);
  109. $this->success('审核成功');
  110. } catch (\Exception $e) {
  111. $this->error($e->getMessage());
  112. }
  113. }
  114. /**
  115. * 获取评论状态列表
  116. */
  117. public function getStatusList()
  118. {
  119. $data = [
  120. 'evaluate_status' => CommentEnum::getEvaluateStatusList(),
  121. 'status' => CommentEnum::getStatusList(),
  122. 'has_picture' => CommentEnum::getHasPictureList(),
  123. ];
  124. $this->success('获取成功', $data);
  125. }
  126. }