Comment.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace addons\shopro\controller\goods;
  3. use addons\shopro\controller\Common;
  4. use app\admin\model\shopro\goods\Comment as CommentModel;
  5. class Comment extends Common
  6. {
  7. protected $noNeedLogin = ['index', 'getType'];
  8. protected $noNeedRight = ['*'];
  9. public function index()
  10. {
  11. $params = $this->request->param();
  12. $type = $params['type'] ?? 'all';
  13. $goods_id = $params['goods_id'] ?? 0;
  14. $comments = CommentModel::normal()->where('goods_id', $goods_id);
  15. if ($type != 'all' && isset(CommentModel::$typeAll[$type])) {
  16. $comments = $comments->{$type}();
  17. }
  18. $comments = $comments->order('id', 'desc')->paginate(request()->param('list_rows', 10));
  19. // ->each(function ($comment) {
  20. // if ($comment->user) {
  21. // $comment->user->nickname_hide = $comment->user->nickname_hide;
  22. // }
  23. // })->toArray();
  24. // $data = $comments['data'];
  25. // foreach ($data as $key => &$comment) {
  26. // if ($comment['user']) {
  27. // $userData['id'] = $comment['user']['id'];
  28. // $userData['nickname'] = $comment['user']['nickname_hide'];
  29. // $userData['avatar'] = $comment['user']['avatar'];
  30. // $userData['gender'] = $comment['user']['gender'];
  31. // $userData['gender_text'] = $comment['user']['gender_text'];
  32. // $comment['user'] = $userData;
  33. // }
  34. // }
  35. // $comments['data'] = $data;
  36. $this->success('获取成功', $comments);
  37. }
  38. public function getType()
  39. {
  40. $goods_id = $this->request->param('goods_id');
  41. $type = array_values(CommentModel::$typeAll);
  42. foreach ($type as $key => $val) {
  43. $comment = CommentModel::normal()->where('goods_id', $goods_id);
  44. if ($val['code'] != 'all') {
  45. $comment = $comment->{$val['code']}();
  46. }
  47. $comment = $comment->count();
  48. $type[$key]['num'] = $comment;
  49. }
  50. $this->success('筛选类型', $type);
  51. }
  52. }