Forum.php 13 KB

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