Comment.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use \think\Request;
  5. class Comment extends Base
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. //关闭中
  11. if($GLOBALS['config']['comment']['status'] == 0){
  12. echo 'comment is close';
  13. exit;
  14. }
  15. }
  16. public function index()
  17. {
  18. if (Request()->isPost()) {
  19. return $this->saveData();
  20. }
  21. $param = mac_param_url();
  22. $this->assign('param',$param);
  23. $this->assign('comment',$GLOBALS['config']['comment']);
  24. return $this->label_fetch('comment/index');
  25. }
  26. public function ajax() {
  27. $param = mac_param_url();
  28. $this->assign('param',$param);
  29. $this->assign('comment',$GLOBALS['config']['comment']);
  30. return $this->label_fetch('comment/ajax',0,'json');
  31. }
  32. public function saveData() {
  33. $param = input();
  34. if($GLOBALS['config']['comment']['verify'] == 1){
  35. if(!captcha_check($param['verify'])){
  36. return ['code'=>1002,'msg'=>'验证码错误'];
  37. }
  38. }
  39. if($GLOBALS['config']['comment']['login'] ==1){
  40. if(empty(cookie('user_id'))){
  41. return ['code' => 1003, 'msg' => '登录后才可以发表留言'];
  42. }
  43. $res = model('User')->checkLogin();
  44. if($res['code']>1) {
  45. return ['code' => 1003, 'msg' => '登录后才可以发表留言'];
  46. }
  47. }
  48. if(empty($param['comment_content'])){
  49. return ['code'=>1004,'msg'=>'留言内容不能为空'];
  50. }
  51. $cookie = 'comment_timespan';
  52. if(!empty(cookie($cookie))){
  53. return ['code'=>1005,'msg'=>'请不要频繁操作'];
  54. }
  55. $param['comment_content']= htmlentities(mac_filter_words($param['comment_content']));
  56. $pattern = '/[^\x00-\x80]/';
  57. if(!preg_match($pattern,$param['comment_content'])){
  58. return ['code'=>1005,'msg'=>'内容必须包含中文,请重新输入'];
  59. }
  60. if(!in_array($param['comment_mid'],['1','2','3','8','9','11'])){
  61. return ['code'=>1006,'msg'=>'模型mid错误'];
  62. }
  63. if(empty(cookie('user_id'))){
  64. $param['comment_name'] = '游客';
  65. }
  66. else{
  67. $param['comment_name'] = cookie('user_name');
  68. $param['user_id'] = intval(cookie('user_id'));
  69. }
  70. $param['comment_name'] = htmlentities($param['comment_name']);
  71. $param['comment_rid'] = intval($param['comment_rid']);
  72. $param['comment_pid'] = intval($param['comment_pid']);
  73. if($GLOBALS['config']['comment']['audit'] ==1){
  74. $param['comment_status'] = 0;
  75. }
  76. $ip = sprintf('%u',ip2long(request()->ip()));
  77. if($ip>2147483647){
  78. $ip=0;
  79. }
  80. $param['comment_ip'] = $ip;
  81. $res = model('Comment')->saveData($param);
  82. if($res['code']>1){
  83. return $res;
  84. }
  85. else{
  86. cookie($cookie, 't', $GLOBALS['config']['comment']['timespan']);
  87. if($GLOBALS['config']['comment']['audit'] ==1){
  88. $res['msg'] = '谢谢,我们会尽快审核你的评论!';
  89. }
  90. else{
  91. $res['msg'] = '感谢你的评论!';
  92. }
  93. return $res;
  94. }
  95. }
  96. public function report()
  97. {
  98. $param = input();
  99. $id = intval($param['id']);
  100. if(empty($id) ) {
  101. return json(['code'=>1001,'msg'=>'参数错误']);
  102. }
  103. $cookie = 'comment-report-' . $id;
  104. if(!empty(cookie($cookie))){
  105. return json(['code'=>1002,'msg'=>'您已提交举报了']);
  106. }
  107. $where = [];
  108. $where['comment_id'] = $id;
  109. model('comment')->where($where)->setInc('comment_report');
  110. cookie($cookie, 't', $GLOBALS['config']['comment']['timespan']);
  111. return json(['code'=>1,'msg'=>'操作成功!']);
  112. }
  113. public function digg()
  114. {
  115. $param = input();
  116. $id = intval($param['id']);
  117. $type = $param['type'];
  118. if(empty($id) || empty($type) ) {
  119. return json(['code'=>1001,'msg'=>'参数错误']);
  120. }
  121. $pre = 'comment';
  122. $where = [];
  123. $where[$pre.'_id'] = $id;
  124. $field = $pre.'_up,'.$pre.'_down';
  125. $model = model($pre);
  126. if($type) {
  127. $cookie = $pre . '-digg-' . $id;
  128. if(!empty(cookie($cookie))){
  129. return json(['code'=>1002,'msg'=>'您已参与过了']);
  130. }
  131. if ($type == 'up') {
  132. $model->where($where)->setInc($pre . '_up');
  133. } elseif ($type == 'down') {
  134. $model->where($where)->setInc($pre . '_down');
  135. }
  136. cookie($cookie, 't', $GLOBALS['config']['comment']['timespan']);
  137. }
  138. $res = $model->infoData($where,$field);
  139. if($res['code']>1) {
  140. return json($res);
  141. }
  142. $info = $res['info'];
  143. if ($info) {
  144. $data = $info;
  145. }
  146. else{
  147. $data[$pre.'_up'] = 0;
  148. $data[$pre.'_down'] = 0;
  149. }
  150. return json(['code'=>1,'msg'=>'操作成功!','data'=>$data]);
  151. }
  152. }