Comment.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 Base
  13. {
  14. protected $model = null;
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. $type = $this->request->post("type");
  19. if ($type == 'archives') {
  20. //检测ID是否加密
  21. $this->hashids('aid');
  22. }
  23. }
  24. /**
  25. * 发表评论
  26. */
  27. public function post()
  28. {
  29. try {
  30. $params = $this->request->post();
  31. CommentModel::postComment($params);
  32. } catch (CommentException $e) {
  33. if ($e->getCode() == 1) {
  34. $this->success($e->getMessage(), null, ['token' => $this->request->token()]);
  35. } else {
  36. $this->error($e->getMessage(), null, ['token' => $this->request->token()]);
  37. }
  38. } catch (Exception $e) {
  39. $this->error($e->getMessage(), null, ['token' => $this->request->token()]);
  40. }
  41. $this->success(__('评论成功!'), null, ['token' => $this->request->token()]);
  42. }
  43. /**
  44. * 取消评论订阅
  45. */
  46. public function unsubscribe()
  47. {
  48. $id = (int)$this->request->param('id');
  49. $key = $this->request->param('key');
  50. $comment = CommentModel::get($id, 'user');
  51. if (!$comment) {
  52. $this->error("评论未找到");
  53. }
  54. if ($key !== md5($comment['id'] . $comment['user']['email'])) {
  55. $this->error("无法进行该操作");
  56. }
  57. if (!$comment['subscribe']) {
  58. $this->error("评论已经取消订阅,请勿重复操作");
  59. }
  60. $comment->subscribe = 0;
  61. $comment->save();
  62. $this->success('取消评论订阅成功');
  63. }
  64. }