Comment.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace addons\cms\controller\wxapp;
  3. use addons\cms\library\CommentException;
  4. use think\Config;
  5. use think\Exception;
  6. /**
  7. * 评论
  8. */
  9. class Comment extends Base
  10. {
  11. protected $noNeedLogin = ['index'];
  12. /**
  13. * 评论列表
  14. */
  15. public function index()
  16. {
  17. $aid = (int)$this->request->request('aid');
  18. $page = (int)$this->request->request('page');
  19. Config::set('paginate.page', $page);
  20. $commentList = \addons\cms\model\Comment::getCommentList(['aid' => $aid]);
  21. $commentList = $commentList->getCollection();
  22. foreach ($commentList as $index => $item) {
  23. if ($item->user) {
  24. $item->user->avatar = cdnurl($item->user->avatar, true);
  25. $item->user->visible(explode(',', 'id,nickname,avatar,bio'));
  26. }
  27. $item->hidden(['ip', 'useragent', 'deletetime', 'aid', 'subscribe', 'status', 'type', 'updatetime']);
  28. }
  29. $this->success('', ['commentList' => $commentList]);
  30. }
  31. /**
  32. * 发表评论
  33. */
  34. public function post()
  35. {
  36. try {
  37. $params = $this->request->post();
  38. $comment = \addons\cms\model\Comment::postComment($params);
  39. $comment->user->visible(explode(',', 'id,nickname,avatar,email'));
  40. $comment->user->avatar = cdnurl($comment->user->avatar, true);
  41. } catch (CommentException $e) {
  42. $this->success($e->getMessage(), ['token' => $this->request->token()]);
  43. } catch (Exception $e) {
  44. $this->error($e->getMessage(), ['token' => $this->request->token()]);
  45. }
  46. $this->success(__('评论成功'), ['token' => $this->request->token()]);
  47. }
  48. }