| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- <?php
- namespace app\system\controller;
- use app\model\api\ForumPost;
- use app\model\api\ForumComment;
- use think\Request;
- use think\facade\Db;
- /**
- * 后台论坛管理控制器
- */
- class Forum
- {
- // ==================== 帖子管理 ====================
- /**
- * 帖子列表
- */
- public function postList(Request $request)
- {
- $page = $request->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']);
- }
- if (!empty($item['images'])) {
- $item['images'] = json_decode($item['images'], true) ?: [];
- }
- }
- }
- 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 = (int)$request->param('page', 1);
- $pageSize = (int)$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
- ]);
- }
- }
|