Comment.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. public function _initialize()
  13. {
  14. parent::_initialize();
  15. $type = $this->request->post("type", "archives");
  16. if ($type == 'archives') {
  17. //检测ID是否加密
  18. $this->hashids('aid');
  19. }
  20. }
  21. /**
  22. * 评论列表
  23. */
  24. public function index()
  25. {
  26. $aid = (int)$this->request->post('aid');
  27. $page = (int)$this->request->post('page');
  28. Config::set('paginate.page', $page);
  29. $commentList = \addons\cms\model\Comment::getCommentList(['aid' => $aid]);
  30. $commentList = $commentList->getCollection();
  31. foreach ($commentList as $index => $item) {
  32. if ($item->user) {
  33. $item->user->avatar = cdnurl($item->user->avatar, true);
  34. $item->user->visible(explode(',', 'id,nickname,avatar,bio'));
  35. }
  36. $item->hidden(['ip', 'useragent', 'deletetime', 'aid', 'subscribe', 'status', 'type', 'updatetime']);
  37. }
  38. $this->success('', ['commentList' => $commentList]);
  39. }
  40. /**
  41. * 发表评论
  42. */
  43. public function post()
  44. {
  45. try {
  46. $params = $this->request->post();
  47. $comment = \addons\cms\model\Comment::postComment($params);
  48. $comment->user->visible(explode(',', 'id,nickname,avatar,email'));
  49. $comment->user->avatar = cdnurl($comment->user->avatar, true);
  50. } catch (CommentException $e) {
  51. $this->success($e->getMessage(), ['token' => $this->request->token()]);
  52. } catch (Exception $e) {
  53. $this->error($e->getMessage(), ['token' => $this->request->token()]);
  54. }
  55. $this->success(__('评论成功'), ['token' => $this->request->token()]);
  56. }
  57. }