order('priority ASC') ->select(); $this->success('success', $data); } //获取版块置顶公告 public function getCategoryNotice() { $categoryId = $this->request->post('categoryId'); //版块ID if (!is_numeric($categoryId)) { $this->fail(500, 'categoryId参数校验错误'); } $data = Db::table('tb_forum_category_notice') ->where(['category_id' => $categoryId]) ->find(); if (!$data) { $this->fail(501, '找不到categoryId对应的版块公告'); } $this->success('success', $data); } //获取版块数据 public function getCategoryData() { $categoryId = $this->request->post('categoryId'); //版块ID if (!is_numeric($categoryId)) { $this->fail(500, 'categoryId参数校验错误'); } $category = Db::table('tb_forum_category') ->where(['id' => $categoryId]) ->find(); if (!$category) { $this->fail(501, '版块ID不存在'); } $today = Db::table('tb_thread') ->where(['category_id' => $categoryId]) ->where('created_at', '>', strtotime(date('Y-m-d') . ' 00:00:00')) ->count(); $topic = Db::table('tb_thread') ->where(['category_id' => $categoryId]) ->count(); $admin = Db::table('tb_user') ->field('id,username,avatar') ->where(['id' => $category['admin_id']]) ->find(); //解析获取头像url if (str_starts_with($admin['avatar'], 'system://')) { $data = explode('//', $admin['avatar']); $avatarId = intval($data[1]); $avatar = Db::table('tb_system_avatar') ->where(['id' => $avatarId]) ->find(); $admin['avatar'] = $avatar['image_url']; } $this->success('success', [ 'name' => $category['name'], //版块名称 'today' => $today, //今日 'topic' => $topic, //主题 'admin' => $admin, //版主信息 ]); } //获取帖子分类带数据 public function getForumCategoryData() { $categoryId = $this->request->post('categoryId'); //版块ID if (!is_numeric($categoryId)) { $this->fail(500, 'categoryId参数校验错误'); } $threadCategory = Db::table('tb_forum_thread_category') ->where(['category_id' => $categoryId]) ->select(); $data = []; foreach ($threadCategory as $categoryData) { $threadsCount = Db::table('tb_thread') ->where(['category_id' => $categoryId]) ->where(['thread_category' => $categoryData['id']]) ->count(); $data[] = [ 'threadCategoryId' => $categoryData['id'], //分类ID 'threadCategoryTitle' => $categoryData['title'], //帖子分类标题 'threadsCount' => $threadsCount, //帖子数量 ]; } $this->success('success', $data); } //仅获取帖子分类 public function getForumCategoryOnly() { $categoryId = $this->request->post('categoryId'); //版块ID if (!is_numeric($categoryId)) { $this->fail(500, 'categoryId参数校验错误'); } $threadCategory = Db::table('tb_forum_thread_category') ->where(['category_id' => $categoryId]) ->select(); $this->success('success', $threadCategory); } //获取版块下帖子列表 public function getThreads() { $page = $this->request->post('page'); // 页码 $list = $this->request->post('list'); // 每页记录数 $categoryId = $this->request->post('categoryId'); //版块ID 999代表全部 $thread_category = $this->request->post('thread_category'); // 板块下分类ID if (!is_numeric($page)|| !is_numeric($list)|| !is_numeric($categoryId)) { $this->fail(500, '参数校验错误'); } $query = Db::table('tb_thread') ->alias('tt') ->order('tt.created_at DESC') ->join('tb_user tu', 'tt.user_id = tu.id') ->field('tt.*, tu.username, tu.avatar'); if($categoryId == 999) { // 999的情况下不做任何筛选 } else { $query->where(['tt.category_id' => $categoryId]); } if ($thread_category) { $query->where(['tt.thread_category' => $thread_category]); } $data = $query->paginate([ 'page' => $page, 'list_rows' => $list ]); // // 处理头像数据 // $items = $data->getCollection()->toArray(); // foreach($items as &$item) { // if (isset($item['avatar'])) { // $item['avatar'] = $this->getUserAvatar($item['avatar']); // } // } // unset($item); // 释放引用 // // 重新设置处理后的数据 // $data->setCollection(collect($items)); $this->success('success', $data); } //获取帖子内容,前端需要做下判断:如果登录了header需要传token,没登录就不传 public function getThread() { $token = $this->request->header('token'); $threadId = $this->request->post('threadId'); if (!is_numeric($threadId)) { $this->fail(500, 'threadId参数校验错误'); } $thread = Db::table('tb_thread') ->alias('tt') ->where(['tt.id' => $threadId]) ->join('tb_user tu', 'tt.user_id = tu.id') ->field('tt.*, tu.username, tu.avatar') ->find(); if (!$thread) { $this->fail(501, '找不到threadId对应的帖子'); } $thread['avatar'] = $this->getUserAvatar($thread['avatar']); $userId = 0; $isUser = 0; if (!empty($token)) { $userToken = Db::table('tb_user_token') ->where(['token' => $token]) ->find(); if ($userToken && $userToken['expired_at'] > time()) { $userId = $userToken['user_id']; $isUser = 1; } } Db::table('tb_thread_visit') ->insert([ 'user_id' => $userId, 'is_user' => $isUser, 'created_at' => time(), ]); Db::table('tb_thread') ->where(['id' => $threadId]) ->update([ 'browse' => $thread['browse'] + 1, ]); $thread['theme_count'] = Db::table('tb_thread')->where('user_id', $userId)->count(); $thread['theme_count'] = Db::table('tb_thread_reply')->where('user_id', $userId)->count(); $thread['user_score'] = Db::table('tb_user')->where('id', $userId)->value('score'); $this->success('success', $thread); } //获取自己的帖子内容 public function getSelfThread() { $threadId = $this->request->post('threadId'); if (!is_numeric($threadId)) { $this->fail(500, 'threadId参数校验错误'); } $thread = Db::table('tb_thread') ->where(['id' => $threadId]) ->where(['user_id' => $this->getUser()->user_id]) ->find(); if (!$thread) { $this->fail(501, '找不到threadId对应的帖子'); } $this->success('success', $thread); } //获取自己的回复内容 public function getSelfReply() { $replyId = $this->request->post('replyId'); if (!is_numeric($replyId)) { $this->fail(500, 'replyId参数校验错误'); } $reply = Db::table('tb_thread_reply') ->where(['id' => $replyId]) ->where(['user_id' => $this->getUser()->user_id]) ->find(); if (!$reply) { $this->fail(501, '找不到replyId对应的帖子'); } $this->success('success', $reply); } //发表帖子 public function postThread() { $categoryId = $this->request->post('categoryId'); //版块ID $title = $this->request->post('title'); //帖子标题 $threadCategoryId = $this->request->post('threadCategoryId'); //帖子分类ID $content = $this->request->post('content'); //帖子内容 if (!is_numeric($categoryId) || empty($title) || empty($content) || !is_numeric($threadCategoryId)) { $this->fail(500, '参数校验错误'); } //版块ID是否存在 $forumCategory = Db::table('tb_forum_category') ->where(['id' => $categoryId]) ->find(); if (!$forumCategory) { $this->fail(501, '对应的版块ID不存在'); } //帖子分类ID是否存在 $threadCategory = Db::table('tb_forum_thread_category') ->where(['id' => $threadCategoryId]) ->find(); if (!$threadCategory) { $this->fail(502, '帖子分类ID不存在'); } Db::table('tb_thread') ->insert([ 'category_id' => $categoryId, 'thread_category' => $threadCategoryId, 'user_id' => $this->getUser()->user_id, 'title' => $title, 'content' => $content, 'browse' => 0, 'created_at' => time(), 'updated_at' => time(), ]); $this->success('success', null); } //编辑帖子 public function editThread() { $threadId = $this->request->post('threadId'); //帖子ID $title = $this->request->post('title'); //帖子标题 $threadCategoryId = $this->request->post('threadCategoryId'); //帖子分类ID $content = $this->request->post('content'); //帖子内容 if (!is_numeric($threadId) || !is_numeric($categoryId) || empty($title) || empty($content) || !is_numeric($threadCategory)) { $this->fail(500, '参数校验错误'); } $selfThread = Db::table('tb_thread') ->where(['user_id' => $this->getUser()->user_id]) ->where(['id' => $threadId]) ->find(); if (!$selfThread) { $this->fail(501, '只能编辑自己的帖子'); } Db::table('tb_thread') ->where(['id' => $threadId]) ->where(['user_id' => $this->getUser()->user_id]) ->update([ 'thread_category' => $threadCategoryId, 'title' => $title, 'content' => $content, 'updated_at' => time(), ]); $this->success('success', null); } //发表回复 public function postReply() { $threadId = $this->request->post('threadId'); //帖子ID $content = $this->request->post('content'); //回复内容 if (!is_numeric($threadId) || empty($content)) { $this->fail(500, '参数校验错误'); } $thread = Db::table('tb_thread') ->where(['id' => $threadId]) ->find(); if (!$thread) { $this->fail(501, '帖子ID不存在'); } Db::table('tb_thread_reply') ->insert([ 'user_id' => $this->getUser()->user_id, 'thread_id' => $threadId, 'content' => $content, 'created_at' => time(), 'updated_at' => time(), ]); $this->success('success', null); } //编辑回复 public function editReply() { $replyId = $this->request->post('replyId'); $content = $this->request->post('content'); if (!is_numeric($replyId)) { $this->fail(500, '参数校验错误'); } $reply = Db::table('tb_thread_reply') ->where(['id' => $replyId]) ->where(['user_id' => $this->getUser()->user_id]) ->find(); if (!$reply) { $this->fail(501, '只能编辑自己的帖子回复'); } Db::table('tb_thread_reply') ->where(['id' => $replyId]) ->where(['user_id' => $this->getUser()->user_id]) ->update([ 'content' => $content, 'updated_at' => time(), ]); $this->success('success', null); } //获取帖子回复,需要支持分页 public function getThreadReply() { $page = $this->request->post('page'); // 当前页数 $list = $this->request->post('list'); // 每页记录数 $threadId = $this->request->post('threadId'); //帖子ID if (!is_numeric($threadId)) { $this->fail(500, 'threadId参数校验错误'); } $data = Db::table('tb_thread_reply') ->where(['thread_id' => $threadId]) ->order('created_at ASC') ->paginate([ 'page' => $page, 'list_rows' => $list ]); $this->success('success', $data); } //上传图片,图片文件不能超过2MB public function uploadImage() { $file = $this->request->file('file'); if (empty($file)) { $this->fail(500, '上传的图片文件不能为空'); } // 使用验证器验证上传的文件 validate(['file' => [ // 限制文件大小(单位b),这里限制为2M 'fileSize' => 2 * 1024 * 1024, // 限制文件后缀,多个后缀以英文逗号分割 'fileExt' => 'jpg,jpeg,png,gif,bmp,webp', ]])->check(['file' => $file]); $saveName = \think\facade\Filesystem::disk('public')->putFile('images', $file); $this->success('success', [ 'filePath' => $this->request->scheme() . '://' . $this->request->host() . '/storage/' . $saveName, ]); } //上传附件,单个附件不得超过50MB public function uploadAttachment() { $file = $this->request->file('file'); if (empty($file)) { $this->fail(500, '上传的图片文件不能为空'); } // 使用验证器验证上传的文件 validate(['file' => [ // 限制文件大小(单位b),这里限制为20M 'fileSize' => 50 * 1024 * 1024, // 限制文件后缀,多个后缀以英文逗号分割 'fileExt' => 'rar,zip,tar.gz,gz,7z,txt', ]])->check(['file' => $file]); $saveName = \think\facade\Filesystem::disk('public')->putFile('attachments', $file); $this->success('success', [ 'filePath' => $this->request->scheme() . '://' . $this->request->host() . '/storage/' . $saveName, ]); } }