Comment.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use think\Request;
  5. class Comment extends Base
  6. {
  7. use PublicApi;
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. $this->check_config();
  12. }
  13. public function index()
  14. {
  15. }
  16. /**
  17. * 获取列表
  18. *
  19. * @param Request $request
  20. * @return \think\response\Json
  21. */
  22. public function get_list(Request $request)
  23. {
  24. $param = array_merge(
  25. [
  26. 'offset' => 0,
  27. 'limit' => 20,
  28. 'orderby' => 'time',
  29. ],
  30. $request->param()
  31. );
  32. $validate = validate($request->controller());
  33. if (!$validate->scene($request->action())->check($param)) {
  34. return json([
  35. 'code' => 1001,
  36. 'msg' => '参数错误: ' . $validate->getError(),
  37. ]);
  38. }
  39. $offset = (int) $param['offset'];
  40. $limit = (int) $param['limit'];
  41. $rid = (int) $param['rid'];
  42. $mid = (int) $param['mid'];
  43. $orderbyKey = isset($param['orderby']) ? trim((string) $param['orderby']) : 'time';
  44. if (!in_array($orderbyKey, ['time', 'up', 'down', 'id'], true)) {
  45. $orderbyKey = 'time';
  46. }
  47. $orderField = $orderbyKey === 'id' ? 'comment_id' : 'comment_' . $orderbyKey;
  48. $order = $orderField . ' DESC, comment_id DESC';
  49. $where = [
  50. 'comment_status' => 1,
  51. 'comment_pid' => 0,
  52. 'comment_rid' => $rid,
  53. 'comment_mid' => $mid,
  54. ];
  55. $total = model('Comment')->getCountByCond($where);
  56. $list = [];
  57. if ($total > 0) {
  58. $list = model('Comment')->getListByCond($offset, $limit, $where, $order, '*', []);
  59. foreach ($list as $k => $v) {
  60. $list[$k] = $this->commentRowForApi($v, false);
  61. $where2 = [
  62. 'comment_pid' => $v['comment_id'],
  63. 'comment_status' => 1,
  64. ];
  65. $sub = Db::name('Comment')->where($where2)->order($order)->select();
  66. $subArr = [];
  67. if ($sub) {
  68. foreach ($sub as $row) {
  69. $rowArr = is_array($row) ? $row : $row->toArray();
  70. $subArr[] = $this->commentRowForApi($rowArr, true);
  71. }
  72. }
  73. $list[$k]['sub'] = $subArr;
  74. }
  75. }
  76. $page = $limit > 0 ? (int) floor($offset / $limit) + 1 : 1;
  77. $pagecount = $limit > 0 ? (int) ceil($total / $limit) : 0;
  78. return json([
  79. 'code' => 1,
  80. 'msg' => '获取成功',
  81. 'info' => [
  82. 'offset' => $offset,
  83. 'limit' => $limit,
  84. 'total' => $total,
  85. 'page' => $page,
  86. 'pagecount' => $pagecount,
  87. 'rows' => $list,
  88. ],
  89. ]);
  90. }
  91. /**
  92. * 单条评论输出给前端(含头像 URL、表情 HTML、时间展示文案)
  93. *
  94. * @param array $row
  95. * @param bool $isReply
  96. */
  97. protected function commentRowForApi(array $row, $isReply)
  98. {
  99. $uid = isset($row['user_id']) ? (int) $row['user_id'] : 0;
  100. $row['user_portrait'] = mac_get_user_portrait($uid);
  101. $raw = isset($row['comment_content']) ? $row['comment_content'] : '';
  102. $row['comment_content'] = mac_em_replace(mac_restore_htmlfilter($raw));
  103. $ts = isset($row['comment_time']) ? (int) $row['comment_time'] : 0;
  104. $row['comment_time_iso'] = $ts > 0 ? date('c', $ts) : '';
  105. $row['comment_time_title'] = $ts > 0 ? date('Y-m-d H:i:s', $ts) : '';
  106. $row['comment_time_label'] = $ts > 0
  107. ? ($isReply ? date('H:i', $ts) : date('Y-m-d H:i:s', $ts))
  108. : '';
  109. return $row;
  110. }
  111. /**
  112. * 提交评论
  113. * api.php/comment/submit (POST)
  114. * 参数: comment_mid, comment_rid, comment_content, [comment_pid=0]
  115. */
  116. public function submit(Request $request)
  117. {
  118. $param = $request->param();
  119. $cmid = isset($param['comment_mid']) ? (string) $param['comment_mid'] : '';
  120. if (!in_array($cmid, ['1', '2', '3', '8', '9', '11', '12'], true)) {
  121. return json(['code' => 1006, 'msg' => lang('index/mid_err')]);
  122. }
  123. $content = trim($param['comment_content'] ?? '');
  124. if (empty($content)) return json(['code' => 1004, 'msg' => lang('index/require_content')]);
  125. $cookie = 'comment_timespan';
  126. if (!empty(cookie($cookie))) return json(['code' => 1005, 'msg' => lang('frequently')]);
  127. if ($GLOBALS['config']['comment']['login'] == 1) {
  128. $check = model('User')->checkLogin();
  129. if ($check['code'] > 1) return json(['code' => 1003, 'msg' => lang('index/require_login')]);
  130. }
  131. $data = [];
  132. $data['comment_mid'] = intval($param['comment_mid']);
  133. $data['comment_rid'] = intval($param['comment_rid'] ?? 0);
  134. $data['comment_pid'] = intval($param['comment_pid'] ?? 0);
  135. $data['comment_content'] = htmlentities(mac_filter_words($content));
  136. $data['comment_ip'] = mac_get_client_ip();
  137. $data['comment_time'] = time();
  138. if (!empty(cookie('user_id'))) {
  139. $uinfo = model('User')->field('user_nick_name,user_name')->where(['user_id' => intval(cookie('user_id'))])->find();
  140. $data['user_id'] = intval(cookie('user_id'));
  141. $data['comment_name'] = htmlentities($uinfo['user_nick_name'] ?: $uinfo['user_name']);
  142. } else {
  143. $data['user_id'] = 0;
  144. $data['comment_name'] = htmlentities(trim($param['comment_name'] ?? lang('controller/visitor')));
  145. }
  146. $data['comment_status'] = ($GLOBALS['config']['comment']['audit'] == 1) ? 0 : 1;
  147. $res = model('Comment')->saveData($data);
  148. cookie($cookie, 't', 30);
  149. return json($res);
  150. }
  151. /**
  152. * 举报评论
  153. * api.php/comment/report?id=1
  154. */
  155. public function report(Request $request)
  156. {
  157. $param = $request->param();
  158. $id = intval($param['id'] ?? 0);
  159. if ($id < 1) return json(['code' => 1001, 'msg' => '参数错误']);
  160. $cookie = 'comment-report-' . $id;
  161. if (!empty(cookie($cookie))) return json(['code' => 1002, 'msg' => lang('index/haved')]);
  162. model('Comment')->where(['comment_id' => $id])->setInc('comment_report');
  163. cookie($cookie, 't', 86400);
  164. return json(['code' => 1, 'msg' => 'ok']);
  165. }
  166. /**
  167. * 评论顶/踩
  168. * api.php/comment/digg?id=1&type=up
  169. */
  170. public function digg(Request $request)
  171. {
  172. $param = $request->param();
  173. $id = intval($param['id'] ?? 0);
  174. if ($id < 1) return json(['code' => 1001, 'msg' => '参数错误']);
  175. $type = trim($param['type'] ?? '');
  176. if ($type) {
  177. $cookie = 'comment-digg-' . $id;
  178. if (!empty(cookie($cookie))) return json(['code' => 1002, 'msg' => lang('index/haved')]);
  179. if ($type == 'up') { model('Comment')->where(['comment_id'=>$id])->setInc('comment_up'); cookie($cookie,'t',30); }
  180. elseif ($type == 'down') { model('Comment')->where(['comment_id'=>$id])->setInc('comment_down'); cookie($cookie,'t',30); }
  181. }
  182. $info = Db::name('comment')->field('comment_up,comment_down')->where(['comment_id'=>$id])->find();
  183. return json(['code'=>1,'msg'=>'ok','data'=>['up'=>$info['comment_up']??0,'down'=>$info['comment_down']??0]]);
  184. }
  185. }