Forum.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <?php
  2. namespace app\system\controller;
  3. use app\model\api\ForumPost;
  4. use app\model\api\ForumComment;
  5. use think\Request;
  6. use think\facade\Db;
  7. /**
  8. * 后台论坛管理控制器
  9. */
  10. class Forum
  11. {
  12. // ==================== 帖子管理 ====================
  13. /**
  14. * 帖子列表
  15. */
  16. public function postList(Request $request)
  17. {
  18. $page = $request->param('page', 1);
  19. $pageSize = $request->param('pageSize', 10);
  20. $status = $request->param('status', '');
  21. $title = $request->param('title', '');
  22. $uid = $request->param('uid', '');
  23. $where = [];
  24. if ($status !== '' && in_array((string)$status, ['-1', '0', '1'])) {
  25. $where[] = ['p.status', '=', (int)$status];
  26. }
  27. if ($title) {
  28. $where[] = ['p.title', 'like', "%{$title}%"];
  29. }
  30. if ($uid) {
  31. $where[] = ['p.uid', '=', (int)$uid];
  32. }
  33. $query = Db::name('forum_post')
  34. ->alias('p')
  35. ->field('p.*, u.nickname as author_name, u.mobile as author_mobile')
  36. ->leftJoin('user u', 'u.uid = p.uid')
  37. ->where($where)
  38. ->order('p.create_time', 'desc');
  39. $count = $query->count();
  40. $list = $query->page($page, $pageSize)->select();
  41. if (!empty($list)) {
  42. $list = $list->toArray();
  43. foreach ($list as &$item) {
  44. if (!empty($item['create_time'])) {
  45. $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
  46. }
  47. if (!empty($item['update_time'])) {
  48. $item['update_time'] = date('Y-m-d H:i:s', $item['update_time']);
  49. }
  50. if (!empty($item['images'])) {
  51. $item['images'] = json_decode($item['images'], true) ?: [];
  52. }
  53. }
  54. }
  55. return app('json')->success(['list' => $list, 'count' => $count]);
  56. }
  57. /**
  58. * 帖子详情
  59. */
  60. public function postDetail(Request $request)
  61. {
  62. $id = $request->param('id');
  63. $post = Db::name('forum_post')
  64. ->alias('p')
  65. ->field('p.*, u.nickname as author_name, u.mobile as author_mobile')
  66. ->leftJoin('user u', 'u.uid = p.uid')
  67. ->where('p.id', $id)
  68. ->find();
  69. if (!$post) {
  70. return app('json')->fail('帖子不存在');
  71. }
  72. if (!empty($post['create_time'])) {
  73. $post['create_time'] = date('Y-m-d H:i:s', $post['create_time']);
  74. }
  75. if (!empty($post['update_time'])) {
  76. $post['update_time'] = date('Y-m-d H:i:s', $post['update_time']);
  77. }
  78. if (!empty($post['images'])) {
  79. $post['images'] = json_decode($post['images'], true) ?: [];
  80. }
  81. return app('json')->success(['list' => [$post], 'count' => 1]);
  82. }
  83. /**
  84. * 审核帖子
  85. */
  86. public function auditPost(Request $request)
  87. {
  88. $id = $request->param('id');
  89. $status = $request->param('status', '');
  90. if (!$id) {
  91. return app('json')->fail('参数错误');
  92. }
  93. if (!in_array((string)$status, ['-1', '1'])) {
  94. return app('json')->fail('状态值错误');
  95. }
  96. $post = Db::name('forum_post')->where('id', $id)->find();
  97. if (!$post) {
  98. return app('json')->fail('帖子不存在');
  99. }
  100. Db::name('forum_post')->where('id', $id)->update([
  101. 'status' => (int)$status,
  102. 'update_time' => time()
  103. ]);
  104. $msg = $status == 1 ? '审核通过' : '审核拒绝';
  105. return app('json')->success($msg . '成功');
  106. }
  107. /**
  108. * 设置帖子置顶
  109. */
  110. public function setTop(Request $request)
  111. {
  112. $id = $request->param('id');
  113. $isTop = $request->param('is_top', 0);
  114. if (!$id) {
  115. return app('json')->fail('参数错误');
  116. }
  117. Db::name('forum_post')->where('id', $id)->update([
  118. 'is_top' => (int)$isTop,
  119. 'update_time' => time()
  120. ]);
  121. return app('json')->success('设置成功');
  122. }
  123. /**
  124. * 编辑帖子
  125. */
  126. public function updatePost(Request $request)
  127. {
  128. $id = $request->param('id');
  129. $title = $request->param('title', '');
  130. $content = $request->param('content', '');
  131. if (!$id) {
  132. return app('json')->fail('参数错误');
  133. }
  134. $post = Db::name('forum_post')->where('id', $id)->find();
  135. if (!$post) {
  136. return app('json')->fail('帖子不存在');
  137. }
  138. $updateData = ['update_time' => time()];
  139. if ($title !== '') {
  140. $updateData['title'] = trim($title);
  141. }
  142. if ($content !== '') {
  143. $updateData['content'] = trim($content);
  144. }
  145. Db::name('forum_post')->where('id', $id)->update($updateData);
  146. return app('json')->success('修改成功');
  147. }
  148. /**
  149. * 删除帖子
  150. */
  151. public function deletePost(Request $request)
  152. {
  153. $id = $request->param('id');
  154. if (!$id) {
  155. return app('json')->fail('参数错误');
  156. }
  157. $post = Db::name('forum_post')->where('id', $id)->find();
  158. if (!$post) {
  159. return app('json')->fail('帖子不存在');
  160. }
  161. Db::startTrans();
  162. try {
  163. // 删除帖子
  164. Db::name('forum_post')->where('id', $id)->delete();
  165. // 删除相关评论
  166. Db::name('forum_comment')->where('post_id', $id)->delete();
  167. Db::commit();
  168. return app('json')->success('删除成功');
  169. } catch (\Exception $e) {
  170. Db::rollback();
  171. return app('json')->fail('删除失败');
  172. }
  173. }
  174. // ==================== 评论管理 ====================
  175. /**
  176. * 评论列表
  177. */
  178. public function commentList(Request $request)
  179. {
  180. $page = (int)$request->param('page', 1);
  181. $pageSize = (int)$request->param('pageSize', 10);
  182. $status = $request->param('status', '');
  183. $postId = $request->param('post_id', '');
  184. $content = $request->param('content', '');
  185. $where = [];
  186. if ($status !== '' && in_array((string)$status, ['-1', '0', '1'])) {
  187. $where[] = ['c.status', '=', (int)$status];
  188. }
  189. if ($postId) {
  190. $where[] = ['c.post_id', '=', (int)$postId];
  191. }
  192. if ($content) {
  193. $where[] = ['c.content', 'like', "%{$content}%"];
  194. }
  195. $query = Db::name('forum_comment')
  196. ->alias('c')
  197. ->field('c.*, u.nickname as author_name, u.mobile as author_mobile, p.title as post_title')
  198. ->leftJoin('user u', 'u.uid = c.uid')
  199. ->leftJoin('forum_post p', 'p.id = c.post_id')
  200. ->where($where)
  201. ->order('c.create_time', 'desc');
  202. $count = $query->count();
  203. $list = $query->page($page, $pageSize)->select();
  204. if (!empty($list)) {
  205. $list = $list->toArray();
  206. foreach ($list as &$item) {
  207. if (!empty($item['create_time'])) {
  208. $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
  209. }
  210. }
  211. }
  212. return app('json')->success(['list' => $list, 'count' => $count]);
  213. }
  214. /**
  215. * 审核评论
  216. */
  217. public function auditComment(Request $request)
  218. {
  219. $id = $request->param('id');
  220. $status = $request->param('status', '');
  221. if (!$id) {
  222. return app('json')->fail('参数错误');
  223. }
  224. if (!in_array((string)$status, ['-1', '1'])) {
  225. return app('json')->fail('状态值错误');
  226. }
  227. $comment = Db::name('forum_comment')->where('id', $id)->find();
  228. if (!$comment) {
  229. return app('json')->fail('评论不存在');
  230. }
  231. Db::name('forum_comment')->where('id', $id)->update([
  232. 'status' => (int)$status,
  233. 'update_time' => time()
  234. ]);
  235. $msg = $status == 1 ? '审核通过' : '审核拒绝';
  236. return app('json')->success($msg . '成功');
  237. }
  238. /**
  239. * 删除评论
  240. */
  241. public function deleteComment(Request $request)
  242. {
  243. $id = $request->param('id');
  244. if (!$id) {
  245. return app('json')->fail('参数错误');
  246. }
  247. $comment = Db::name('forum_comment')->where('id', $id)->find();
  248. if (!$comment) {
  249. return app('json')->fail('评论不存在');
  250. }
  251. Db::startTrans();
  252. try {
  253. // 删除评论
  254. Db::name('forum_comment')->where('id', $id)->delete();
  255. // 更新帖子评论数
  256. Db::name('forum_post')->where('id', $comment['post_id'])->dec('comment_count')->update();
  257. Db::commit();
  258. return app('json')->success('删除成功');
  259. } catch (\Exception $e) {
  260. Db::rollback();
  261. return app('json')->fail('删除失败');
  262. }
  263. }
  264. // ==================== 用户权限管理 ====================
  265. /**
  266. * 用户权限列表
  267. */
  268. public function userPermissionList(Request $request)
  269. {
  270. $page = $request->param('page', 1);
  271. $pageSize = $request->param('pageSize', 10);
  272. $nickname = $request->param('nickname', '');
  273. $mobile = $request->param('mobile', '');
  274. $permission = $request->param('permission', '');
  275. $where = [];
  276. if ($nickname) {
  277. $where[] = ['nickname', 'like', "%{$nickname}%"];
  278. }
  279. if ($mobile) {
  280. $where[] = ['mobile', 'like', "%{$mobile}%"];
  281. }
  282. if ($permission !== '' && in_array((string)$permission, ['0', '1'])) {
  283. $where[] = ['permission', '=', (int)$permission];
  284. }
  285. $query = Db::name('user')
  286. ->field('uid, nickname, mobile, avatar, permission, regtime')
  287. ->where($where)
  288. ->order('uid', 'desc');
  289. $count = $query->count();
  290. $list = $query->page($page, $pageSize)->select();
  291. if (!empty($list)) {
  292. $list = $list->toArray();
  293. foreach ($list as &$item) {
  294. if (!empty($item['regtime'])) {
  295. $item['regtime'] = date('Y-m-d H:i:s', $item['regtime']);
  296. }
  297. }
  298. }
  299. return app('json')->success(['list' => $list, 'count' => $count]);
  300. }
  301. /**
  302. * 设置用户发帖权限
  303. */
  304. public function setUserPermission(Request $request)
  305. {
  306. $uid = $request->param('uid');
  307. $permission = $request->param('permission', '');
  308. if (!$uid) {
  309. return app('json')->fail('参数错误');
  310. }
  311. if (!in_array((string)$permission, ['0', '1'])) {
  312. return app('json')->fail('权限值错误');
  313. }
  314. $user = Db::name('user')->where('uid', $uid)->find();
  315. if (!$user) {
  316. return app('json')->fail('用户不存在');
  317. }
  318. Db::name('user')->where('uid', $uid)->update([
  319. 'permission' => (int)$permission
  320. ]);
  321. $msg = $permission == 1 ? '开通' : '关闭';
  322. return app('json')->success($msg . '发帖权限成功');
  323. }
  324. /**
  325. * 批量设置用户发帖权限
  326. */
  327. public function batchSetPermission(Request $request)
  328. {
  329. $uids = $request->param('uids', '');
  330. $permission = $request->param('permission', '');
  331. if (!$uids) {
  332. return app('json')->fail('请选择用户');
  333. }
  334. if (!in_array((string)$permission, ['0', '1'])) {
  335. return app('json')->fail('权限值错误');
  336. }
  337. $uidArr = explode(',', $uids);
  338. $uidArr = array_filter(array_map('intval', $uidArr));
  339. if (empty($uidArr)) {
  340. return app('json')->fail('用户ID格式错误');
  341. }
  342. Db::name('user')->whereIn('uid', $uidArr)->update([
  343. 'permission' => (int)$permission
  344. ]);
  345. $msg = $permission == 1 ? '开通' : '关闭';
  346. return app('json')->success($msg . '发帖权限成功');
  347. }
  348. // ==================== 统计 ====================
  349. /**
  350. * 论坛统计
  351. */
  352. public function statistics(Request $request)
  353. {
  354. $postCount = Db::name('forum_post')->count();
  355. $postPendingCount = Db::name('forum_post')->where('status', 0)->count();
  356. $postNormalCount = Db::name('forum_post')->where('status', 1)->count();
  357. $commentCount = Db::name('forum_comment')->count();
  358. $commentPendingCount = Db::name('forum_comment')->where('status', 0)->count();
  359. $userWithPermission = Db::name('user')->where('permission', 1)->count();
  360. return app('json')->success([
  361. 'post_count' => $postCount,
  362. 'post_pending' => $postPendingCount,
  363. 'post_normal' => $postNormalCount,
  364. 'comment_count' => $commentCount,
  365. 'comment_pending' => $commentPendingCount,
  366. 'user_with_permission' => $userWithPermission
  367. ]);
  368. }
  369. }