info 的 id) */ protected function uid(): int { $info = $this->auth->info ?? []; return (int) ($info['id'] ?? 0); } /** 发帖页 */ public function post() { if ($this->request->isPost()) { return $this->doPost(); } $boards = ForumBoard::enabledList(); $cfg = \ywxapp\service\AddonService::config('forum'); $this->view->assign('boards', $boards); $this->view->assign('editorHeight', (int) ($cfg['editor_height'] ?? 360)); return $this->view->fetch('forum/post'); } /** 提交发帖 */ protected function doPost() { $uid = $this->uid(); if ($uid < 1) { return $this->result->error('请先登录'); } $data = $this->request->post(); $validate = Validate::rule([ 'board_id' => 'require|integer', 'title' => 'require|length:2,120', 'content' => 'require|length:5,65535', ]); if (!$validate->check($data)) { return $this->result->error($validate->getError()); } $board = ForumBoard::find($data['board_id']); if (!$board || $board->status != 1) { return $this->result->error('版块不存在或已禁用'); } $topicId = ForumTopic::insertGetId([ 'user_id' => $uid, 'board_id' => (int) $data['board_id'], 'board_slug' => $board->slug ?? '', 'title' => trim($data['title']), 'content' => $data['content'], 'type' => (int) ($data['type'] ?? 99), 'reply_count' => 0, 'view_count' => 0, 'digest' => 0, 'top' => 0, 'status' => 1, 'create_at' => date('Y-m-d H:i:s'), 'update_at' => date('Y-m-d H:i:s'), ]); // 版块帖子数 +1 ForumBoard::incr((int) $data['board_id'], 'topic_count', 1); if ($topicId) { return $this->result->success(['id' => $topicId], '发布成功'); } return $this->result->error('发布失败,请重试'); } /** 我的帖子管理 */ public function mytopics() { $uid = $this->uid(); if ($uid < 1) { return $this->result->error('请先登录'); } $page = (int) $this->request->get('page', 1); $limit = 15; $query = ForumTopic::where('user_id', $uid)->where('status', '>=', 0); $total = (clone $query)->count(); $topics = $query->order('id desc')->page($page, $limit)->select(); $boardIds = $topics->column('board_id'); $boards = ForumBoard::whereIn('id', $boardIds ?: [0])->column('title', 'id'); $list = []; foreach ($topics as $t) { $list[] = [ 'id' => $t->id, 'title' => $t->title, 'board_title' => $boards[$t->board_id] ?? '未知版块', 'reply_count' => $t->reply_count, 'view_count' => $t->view_count, 'status' => $t->status, 'create_at' => date('Y-m-d H:i', strtotime($t->create_at)), ]; } if ($this->request->isAjax()) { return $this->result->success([ 'list' => $list, 'total' => $total, 'page' => $page, ], 'ok'); } $this->view->assign('list', $list); $this->view->assign('total', $total); $this->view->assign('page', $page); return $this->view->fetch('forum/mytopics'); } /** 删除我的帖子 */ public function del() { if (!$this->request->isPost()) { return $this->result->error('请求方式错误'); } $uid = $this->uid(); if ($uid < 1) { return $this->result->error('请先登录'); } $id = (int) $this->request->post('id', 0); if ($id < 1) { return $this->result->error('参数错误'); } $topic = ForumTopic::where('id', $id)->where('user_id', $uid)->find(); if (!$topic) { return $this->result->error('帖子不存在'); } $topic->status = -1; $topic->save(); if ($topic->board_id > 0) { ForumBoard::where('id', $topic->board_id)->where('topic_count', '>', 0) ->dec('topic_count', 1)->update(); } return $this->result->success([], '已删除'); } }