Comment.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace addons\cms\controller;
  3. use addons\cms\library\CommentException;
  4. use addons\cms\model\Comment as CommentModel;
  5. use think\addons\Controller;
  6. use think\Exception;
  7. /**
  8. * 评论控制器
  9. * Class Comment
  10. * @package addons\cms\controller
  11. */
  12. class Comment extends Controller
  13. {
  14. protected $model = null;
  15. /**
  16. * 发表评论
  17. */
  18. public function post()
  19. {
  20. try {
  21. $params = $this->request->post();
  22. CommentModel::postComment($params);
  23. } catch (CommentException $e) {
  24. if ($e->getCode() == 1) {
  25. $this->success($e->getMessage(), null, ['token' => $this->request->token()]);
  26. } else {
  27. $this->error($e->getMessage(), null, ['token' => $this->request->token()]);
  28. }
  29. } catch (Exception $e) {
  30. $this->error($e->getMessage(), null, ['token' => $this->request->token()]);
  31. }
  32. $this->success(__('评论成功!'), null, ['token' => $this->request->token()]);
  33. }
  34. /**
  35. * 取消评论订阅
  36. */
  37. public function unsubscribe()
  38. {
  39. $id = (int)$this->request->param('id');
  40. $key = $this->request->param('key');
  41. $comment = CommentModel::get($id);
  42. if (!$comment) {
  43. $this->error("评论未找到");
  44. }
  45. if ($key !== md5($comment['id'] . $comment['email'])) {
  46. $this->error("无法进行该操作");
  47. }
  48. if (!$comment['subscribe']) {
  49. $this->error("评论已经取消订阅,请勿重复操作");
  50. }
  51. $comment->subscribe = 0;
  52. $comment->save();
  53. $this->success('取消评论订阅成功');
  54. }
  55. }