Comment.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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->post();
  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. CommentService::addComment($this->auth->id, $order_id, $remark, $pid);
  54. $this->success('添加评论成功,等待审核!');
  55. }
  56. /**
  57. * 我的评价
  58. */
  59. public function myList()
  60. {
  61. $params = $this->request->param();
  62. // 验证参数
  63. $validate = new CommentValidate();
  64. if (!$validate->scene('myList')->check($params)) {
  65. $this->error($validate->getError());
  66. }
  67. $page = isset($params['page']) ? (int)$params['page'] : 1;
  68. $pagesize = isset($params['pagesize']) ? (int)$params['pagesize'] : 10;
  69. $list = CommentService::getUserCommentList($this->auth->id, $page, $pagesize);
  70. foreach ($list as $item) {
  71. $item->hidden(['ip', 'subscribe', 'useragent', 'comments']);
  72. }
  73. $this->success('获取成功', $list);
  74. }
  75. /**
  76. * 获取商品评论统计
  77. */
  78. public function stats()
  79. {
  80. $params = $this->request->param();
  81. // 验证参数
  82. $validate = new CommentValidate();
  83. if (!$validate->scene('stats')->check($params)) {
  84. $this->error($validate->getError());
  85. }
  86. $goods_id = (int)$params['goods_id'];
  87. $stats = CommentService::getCommentStats($goods_id);
  88. $this->success('获取成功', $stats);
  89. }
  90. /**
  91. * 审核评论 (管理员功能)
  92. */
  93. public function audit()
  94. {
  95. $params = $this->request->param();
  96. // 验证参数
  97. $validate = new CommentValidate();
  98. if (!$validate->scene('audit')->check($params)) {
  99. $this->error($validate->getError());
  100. }
  101. $comment_id = (int)$params['comment_id'];
  102. $evaluate_status = (int)$params['evaluate_status'];
  103. CommentService::auditComment($comment_id, $evaluate_status);
  104. $this->success('审核成功');
  105. }
  106. /**
  107. * 获取评论状态列表
  108. */
  109. public function getStatusList()
  110. {
  111. $data = [
  112. 'evaluate_status' => CommentEnum::getEvaluateStatusList(),
  113. 'status' => CommentEnum::getStatusList(),
  114. 'has_picture' => CommentEnum::getHasPictureList(),
  115. ];
  116. $this->success('获取成功', $data);
  117. }
  118. }