param('page', 1); $pageSize = $request->param('pageSize', 10); $status = $request->param('status', ''); $title = $request->param('title', ''); $uid = $request->param('uid', ''); $where = []; if ($status !== '' && in_array((string)$status, ['-1', '0', '1'])) { $where[] = ['p.status', '=', (int)$status]; } if ($title) { $where[] = ['p.title', 'like', "%{$title}%"]; } if ($uid) { $where[] = ['p.uid', '=', (int)$uid]; } $query = Db::name('forum_post') ->alias('p') ->field('p.*, u.nickname as author_name, u.mobile as author_mobile') ->leftJoin('user u', 'u.uid = p.uid') ->where($where) ->order('p.create_time', 'desc'); $count = $query->count(); $list = $query->page($page, $pageSize)->select(); if (!empty($list)) { $list = $list->toArray(); foreach ($list as &$item) { if (!empty($item['create_time'])) { $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']); } if (!empty($item['update_time'])) { $item['update_time'] = date('Y-m-d H:i:s', $item['update_time']); } } } return app('json')->success(['list' => $list, 'count' => $count]); } /** * 帖子详情 */ public function postDetail(Request $request) { $id = $request->param('id'); $post = Db::name('forum_post') ->alias('p') ->field('p.*, u.nickname as author_name, u.mobile as author_mobile') ->leftJoin('user u', 'u.uid = p.uid') ->where('p.id', $id) ->find(); if (!$post) { return app('json')->fail('帖子不存在'); } if (!empty($post['create_time'])) { $post['create_time'] = date('Y-m-d H:i:s', $post['create_time']); } if (!empty($post['update_time'])) { $post['update_time'] = date('Y-m-d H:i:s', $post['update_time']); } if (!empty($post['images'])) { $post['images'] = json_decode($post['images'], true) ?: []; } return app('json')->success(['list' => [$post], 'count' => 1]); } /** * 审核帖子 */ public function auditPost(Request $request) { $id = $request->param('id'); $status = $request->param('status', ''); if (!$id) { return app('json')->fail('参数错误'); } if (!in_array((string)$status, ['-1', '1'])) { return app('json')->fail('状态值错误'); } $post = Db::name('forum_post')->where('id', $id)->find(); if (!$post) { return app('json')->fail('帖子不存在'); } Db::name('forum_post')->where('id', $id)->update([ 'status' => (int)$status, 'update_time' => time() ]); $msg = $status == 1 ? '审核通过' : '审核拒绝'; return app('json')->success($msg . '成功'); } /** * 设置帖子置顶 */ public function setTop(Request $request) { $id = $request->param('id'); $isTop = $request->param('is_top', 0); if (!$id) { return app('json')->fail('参数错误'); } Db::name('forum_post')->where('id', $id)->update([ 'is_top' => (int)$isTop, 'update_time' => time() ]); return app('json')->success('设置成功'); } /** * 编辑帖子 */ public function updatePost(Request $request) { $id = $request->param('id'); $title = $request->param('title', ''); $content = $request->param('content', ''); if (!$id) { return app('json')->fail('参数错误'); } $post = Db::name('forum_post')->where('id', $id)->find(); if (!$post) { return app('json')->fail('帖子不存在'); } $updateData = ['update_time' => time()]; if ($title !== '') { $updateData['title'] = trim($title); } if ($content !== '') { $updateData['content'] = trim($content); } Db::name('forum_post')->where('id', $id)->update($updateData); return app('json')->success('修改成功'); } /** * 删除帖子 */ public function deletePost(Request $request) { $id = $request->param('id'); if (!$id) { return app('json')->fail('参数错误'); } $post = Db::name('forum_post')->where('id', $id)->find(); if (!$post) { return app('json')->fail('帖子不存在'); } Db::startTrans(); try { // 删除帖子 Db::name('forum_post')->where('id', $id)->delete(); // 删除相关评论 Db::name('forum_comment')->where('post_id', $id)->delete(); Db::commit(); return app('json')->success('删除成功'); } catch (\Exception $e) { Db::rollback(); return app('json')->fail('删除失败'); } } // ==================== 评论管理 ==================== /** * 评论列表 */ public function commentList(Request $request) { $page = $request->param('page', 1); $pageSize = $request->param('pageSize', 10); $status = $request->param('status', ''); $postId = $request->param('post_id', ''); $content = $request->param('content', ''); $where = []; if ($status !== '' && in_array((string)$status, ['-1', '0', '1'])) { $where[] = ['c.status', '=', (int)$status]; } if ($postId) { $where[] = ['c.post_id', '=', (int)$postId]; } if ($content) { $where[] = ['c.content', 'like', "%{$content}%"]; } $query = Db::name('forum_comment') ->alias('c') ->field('c.*, u.nickname as author_name, u.mobile as author_mobile, p.title as post_title') ->leftJoin('user u', 'u.uid = c.uid') ->leftJoin('forum_post p', 'p.id = c.post_id') ->where($where) ->order('c.create_time', 'desc'); $count = $query->count(); $list = $query->page($page, $pageSize)->select(); if (!empty($list)) { $list = $list->toArray(); foreach ($list as &$item) { if (!empty($item['create_time'])) { $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']); } } } return app('json')->success(['list' => $list, 'count' => $count]); } /** * 审核评论 */ public function auditComment(Request $request) { $id = $request->param('id'); $status = $request->param('status', ''); if (!$id) { return app('json')->fail('参数错误'); } if (!in_array((string)$status, ['-1', '1'])) { return app('json')->fail('状态值错误'); } $comment = Db::name('forum_comment')->where('id', $id)->find(); if (!$comment) { return app('json')->fail('评论不存在'); } Db::name('forum_comment')->where('id', $id)->update([ 'status' => (int)$status, 'update_time' => time() ]); $msg = $status == 1 ? '审核通过' : '审核拒绝'; return app('json')->success($msg . '成功'); } /** * 删除评论 */ public function deleteComment(Request $request) { $id = $request->param('id'); if (!$id) { return app('json')->fail('参数错误'); } $comment = Db::name('forum_comment')->where('id', $id)->find(); if (!$comment) { return app('json')->fail('评论不存在'); } Db::startTrans(); try { // 删除评论 Db::name('forum_comment')->where('id', $id)->delete(); // 更新帖子评论数 Db::name('forum_post')->where('id', $comment['post_id'])->dec('comment_count')->update(); Db::commit(); return app('json')->success('删除成功'); } catch (\Exception $e) { Db::rollback(); return app('json')->fail('删除失败'); } } // ==================== 用户权限管理 ==================== /** * 用户权限列表 */ public function userPermissionList(Request $request) { $page = $request->param('page', 1); $pageSize = $request->param('pageSize', 10); $nickname = $request->param('nickname', ''); $mobile = $request->param('mobile', ''); $permission = $request->param('permission', ''); $where = []; if ($nickname) { $where[] = ['nickname', 'like', "%{$nickname}%"]; } if ($mobile) { $where[] = ['mobile', 'like', "%{$mobile}%"]; } if ($permission !== '' && in_array((string)$permission, ['0', '1'])) { $where[] = ['permission', '=', (int)$permission]; } $query = Db::name('user') ->field('uid, nickname, mobile, avatar, permission, regtime') ->where($where) ->order('uid', 'desc'); $count = $query->count(); $list = $query->page($page, $pageSize)->select(); if (!empty($list)) { $list = $list->toArray(); foreach ($list as &$item) { if (!empty($item['regtime'])) { $item['regtime'] = date('Y-m-d H:i:s', $item['regtime']); } } } return app('json')->success(['list' => $list, 'count' => $count]); } /** * 设置用户发帖权限 */ public function setUserPermission(Request $request) { $uid = $request->param('uid'); $permission = $request->param('permission', ''); if (!$uid) { return app('json')->fail('参数错误'); } if (!in_array((string)$permission, ['0', '1'])) { return app('json')->fail('权限值错误'); } $user = Db::name('user')->where('uid', $uid)->find(); if (!$user) { return app('json')->fail('用户不存在'); } Db::name('user')->where('uid', $uid)->update([ 'permission' => (int)$permission ]); $msg = $permission == 1 ? '开通' : '关闭'; return app('json')->success($msg . '发帖权限成功'); } /** * 批量设置用户发帖权限 */ public function batchSetPermission(Request $request) { $uids = $request->param('uids', ''); $permission = $request->param('permission', ''); if (!$uids) { return app('json')->fail('请选择用户'); } if (!in_array((string)$permission, ['0', '1'])) { return app('json')->fail('权限值错误'); } $uidArr = explode(',', $uids); $uidArr = array_filter(array_map('intval', $uidArr)); if (empty($uidArr)) { return app('json')->fail('用户ID格式错误'); } Db::name('user')->whereIn('uid', $uidArr)->update([ 'permission' => (int)$permission ]); $msg = $permission == 1 ? '开通' : '关闭'; return app('json')->success($msg . '发帖权限成功'); } // ==================== 统计 ==================== /** * 论坛统计 */ public function statistics(Request $request) { $postCount = Db::name('forum_post')->count(); $postPendingCount = Db::name('forum_post')->where('status', 0)->count(); $postNormalCount = Db::name('forum_post')->where('status', 1)->count(); $commentCount = Db::name('forum_comment')->count(); $commentPendingCount = Db::name('forum_comment')->where('status', 0)->count(); $userWithPermission = Db::name('user')->where('permission', 1)->count(); return app('json')->success([ 'post_count' => $postCount, 'post_pending' => $postPendingCount, 'post_normal' => $postNormalCount, 'comment_count' => $commentCount, 'comment_pending' => $commentPendingCount, 'user_with_permission' => $userWithPermission ]); } }