Comment.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. $this->success('获取成功', $stats);
  90. }
  91. /**
  92. * 审核评论 (管理员功能)
  93. */
  94. public function audit()
  95. {
  96. $params = $this->request->param();
  97. // 验证参数
  98. $validate = new CommentValidate();
  99. if (!$validate->scene('audit')->check($params)) {
  100. $this->error($validate->getError());
  101. }
  102. $comment_id = (int)$params['comment_id'];
  103. $evaluate_status = (int)$params['evaluate_status'];
  104. CommentService::auditComment($comment_id, $evaluate_status);
  105. $this->success('审核成功');
  106. }
  107. /**
  108. * 获取评论状态列表
  109. */
  110. public function getStatusList()
  111. {
  112. $data = [
  113. 'evaluate_status' => CommentEnum::getEvaluateStatusList(),
  114. 'status' => StatusEnum::getMap(),
  115. 'has_picture' => CommentEnum::getHasPictureList(),
  116. ];
  117. $this->success('获取成功', $data);
  118. }
  119. }