Comment.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace addons\blog\controller;
  3. use addons\blog\model\Comment as CommentModel;
  4. use addons\blog\library\CommentException;
  5. use think\Exception;
  6. /**
  7. * 博客评论
  8. */
  9. class Comment extends Base
  10. {
  11. public function index()
  12. {
  13. $post_id = $this->request->get('post_id');
  14. $commentlist = CommentModel::where(['post_id' => $post_id, 'pid' => 0, 'status' => 'normal'])
  15. ->with('sublist')
  16. ->order('id desc')
  17. ->paginate($this->view->config['commentpagesize']);
  18. $this->view->assign("commentlist", $commentlist);
  19. return $this->view->fetch('/common/commentlist');
  20. }
  21. /**
  22. * 发表评论
  23. */
  24. public function post()
  25. {
  26. $result = false;
  27. $message = '';
  28. try {
  29. $params = $this->request->post();
  30. $result = CommentModel::postComment($params);
  31. } catch (CommentException $e) {
  32. if ($e->getCode() == 1) {
  33. $result = true;
  34. }
  35. $message = $e->getMessage();
  36. } catch (Exception $e) {
  37. $message = $e->getMessage();
  38. }
  39. if ($result) {
  40. $this->success(__($message), null, ['token' => $this->request->token()]);
  41. } else {
  42. $this->error(__($message), null, ['token' => $this->request->token()]);
  43. }
  44. }
  45. /**
  46. * 取消评论订阅
  47. */
  48. public function unsubscribe()
  49. {
  50. $id = (int)$this->request->param('id');
  51. $key = $this->request->param('key');
  52. $comment = Comment::get($id);
  53. if (!$comment) {
  54. $this->error("日志评论未找到");
  55. }
  56. if ($key !== md5($comment['id'] . $comment['email'])) {
  57. $this->error("无法进行该操作");
  58. }
  59. if (!$comment['subscribe']) {
  60. $this->error("评论已经取消订阅,请勿重复操作");
  61. }
  62. $comment->subscribe = 0;
  63. $comment->save();
  64. $this->success('取消评论订阅成功');
  65. }
  66. }