IndexController.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | d_comment [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 DaliyCode All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: DaliyCode <3471677985@qq.com> <author_url:dalicode.com>
  8. // +----------------------------------------------------------------------
  9. namespace plugins\d_comment\controller;
  10. use app\admin\model\PluginModel;
  11. use app\portal\model\PortalPostModel;
  12. use app\user\model\CommentModel;
  13. use cmf\controller\PluginBaseController;
  14. use think\Db;
  15. class IndexController extends PluginBaseController {
  16. public function _initialize() {
  17. $where = ['status' => 1, 'name' => $this->getPlugin()->info['name']];
  18. $vo = PluginModel::where($where)->cache(60, true)->find();
  19. if (!$vo) {
  20. $this->error('评论插件未启用!');
  21. }
  22. }
  23. public function add() {
  24. $config = $this->getPlugin()->getConfig();
  25. if (!$config || $config['comment_type'] == 2) {
  26. $this->error('评论已关闭!');
  27. }
  28. $userid = cmf_get_current_user_id();
  29. if ($userid) {
  30. $i = (int) $config['comment_interval'] >= 0 ? (int) $config['comment_interval'] : 5;
  31. if (session('com') && $i && (time() - session('com')) < $i) {
  32. $this->error('请' . $i . '秒后再评论!');
  33. }
  34. $params = $this->request->param();
  35. $data['user_id'] = $userid;
  36. $data['object_id'] = $params['object_id'];
  37. $data['table_name'] = $params['table_name'];
  38. $data['to_user_id'] = $params['to_user_id'];
  39. $data['content'] = $params['content'];
  40. $data['parent_id'] = $params['parent_id'];
  41. $data['more'] = $params['object_title'];
  42. $data['create_time'] = time();
  43. $config['comment_check'] == 1 && $data['status'] = 0;
  44. $result = \db('comment')->insert($data);
  45. if (false === $result) {
  46. $this->error('评论失败');
  47. }
  48. session('com', time());
  49. PortalPostModel::where('id=' . $data['object_id'])->setInc('comment_count');
  50. $this->success("评论成功");
  51. } else {
  52. $this->error('请登录!');
  53. }
  54. }
  55. }