Comment.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace addons\blog\controller\wxapp;
  3. use addons\blog\library\CommentException;
  4. use addons\blog\model\Comment as CommentModel;
  5. use think\Config;
  6. use think\Exception;
  7. /**
  8. * 评论
  9. */
  10. class Comment extends Base
  11. {
  12. protected $noNeedLogin = ['*'];
  13. /**
  14. * 评论列表
  15. */
  16. public function index()
  17. {
  18. $post_id = (int)$this->request->request('post_id/d');
  19. $page = (int)$this->request->request('page/d', 1);
  20. Config::set('paginate.page', $page);
  21. $commentList = \addons\blog\model\Comment::where('post_id', $post_id)
  22. ->order('createtime', 'desc')
  23. ->where('status', 'normal')
  24. ->page($page)->select();
  25. foreach ($commentList as $index => $item) {
  26. $item->visible(['id', 'username', 'avatar', 'content', 'comments']);
  27. }
  28. $this->success('', ['commentList' => $commentList]);
  29. }
  30. /**
  31. * 发表评论
  32. */
  33. public function post()
  34. {
  35. $result = false;
  36. $message = '';
  37. try {
  38. $params = $this->request->post();
  39. $result = CommentModel::postComment($params);
  40. } catch (CommentException $e) {
  41. if ($e->getCode() == 1) {
  42. $result = true;
  43. }
  44. $message = $e->getMessage();
  45. } catch (Exception $e) {
  46. $message = $e->getMessage();
  47. }
  48. if ($result) {
  49. $this->success(__($message ? $message : "发布成功"), ['token' => $this->request->token()]);
  50. } else {
  51. $this->error(__($message ? $message : "发布失败"), ['token' => $this->request->token()]);
  52. }
  53. }
  54. }