Comment.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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')
  19. // ->paginate(request()->param('list_rows', 10));
  20. ->autopage()->select();
  21. $comments = json_decode(json_encode($comments),true);
  22. $comments = list_domain_image($comments,['user_avatar']);
  23. foreach($comments as $key => $val){
  24. $val['images'] = array_domain_image($val['images']);
  25. $comments[$key] = $val;
  26. }
  27. // ->each(function ($comment) {
  28. // if ($comment->user) {
  29. // $comment->user->nickname_hide = $comment->user->nickname_hide;
  30. // }
  31. // })->toArray();
  32. // $data = $comments['data'];
  33. // foreach ($data as $key => &$comment) {
  34. // if ($comment['user']) {
  35. // $userData['id'] = $comment['user']['id'];
  36. // $userData['nickname'] = $comment['user']['nickname_hide'];
  37. // $userData['avatar'] = $comment['user']['avatar'];
  38. // $userData['gender'] = $comment['user']['gender'];
  39. // $userData['gender_text'] = $comment['user']['gender_text'];
  40. // $comment['user'] = $userData;
  41. // }
  42. // }
  43. // $comments['data'] = $data;
  44. $this->success('获取成功', $comments);
  45. }
  46. public function getType()
  47. {
  48. $goods_id = $this->request->param('goods_id');
  49. $type = array_values(CommentModel::$typeAll);
  50. foreach ($type as $key => $val) {
  51. $comment = CommentModel::normal()->where('goods_id', $goods_id);
  52. if ($val['code'] != 'all') {
  53. $comment = $comment->{$val['code']}();
  54. }
  55. $comment = $comment->count();
  56. $type[$key]['num'] = $comment;
  57. }
  58. $this->success('筛选类型', $type);
  59. }
  60. }