feat: add community post categories and channels

This commit is contained in:
hajimi
2026-08-02 17:39:52 +08:00
parent 95f03af60e
commit b0b278d11f
18 changed files with 1132 additions and 12 deletions
@@ -0,0 +1,170 @@
# Community Post Channels and Categories Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** 为社区帖子补齐推荐、热门、话题频道管理,并新增独立帖子分类表、管理后台和前台频道筛选。
**Architecture:** 复用 `la_community_post.is_recommend``is_hot`,只新增 `is_topic` 和单选 `category_id`。分类通过独立 `la_community_post_category` 管理,初始化时复制标签并回填历史帖子;标签多选关系继续保留。后台使用 likeadmin Controller/Logic/Validate/Lists 分层,用户端列表通过现有 `/api/community/postLists` 参数筛选。
**Tech Stack:** PHP 8、ThinkPHP/likeadmin、MySQL 8、Vue 3、Element Plus、uni-app、TypeScript
---
### Task 1: 数据库结构、初始化数据与菜单权限
**Files:**
- Create: `docs/sql/create_community_post_categories.sql`
- Modify: `sbnews.sql`
- [x] **Step 1: 创建独立分类表和帖子字段**
SQL 必须幂等创建 `la_community_post_category`,字段为 `id/name/icon/sort/post_count/status/create_time/update_time`,并在 `la_community_post` 增加:
```sql
`category_id` int unsigned NOT NULL DEFAULT 0 COMMENT '帖子主分类ID',
`is_topic` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '是否话题 0否 1是'
```
同时添加 `category_id``status + is_recommend``status + is_hot``status + is_topic` 查询索引。
- [x] **Step 2: 从标签初始化分类并回填历史帖子**
分类初始化使用 `INSERT IGNORE ... SELECT` 保留标签 ID,不覆盖后续独立维护的分类。历史帖子只在 `category_id=0` 时回填,按标签 `sort DESC, community_post_tag.id ASC` 选取一个主分类;已有标签的帖子初始化为 `is_topic=1` 以保持原话题流可见性,最后按实际帖子数重算 `post_count`
- [x] **Step 3: 初始化管理菜单与按钮权限**
新增 `community.communityCategory/lists` 页面及 `detail/add/edit/delete` 按钮权限;补充帖子 `setRecommend/setTopic/setCategory` 权限。所有菜单通过已有社区帖子或标签菜单反查父级,使用 `NOT EXISTS` 幂等插入,并授权角色 `role_id=1`
### Task 2: 后端分类管理和帖子管理接口
**Files:**
- Create: `server/app/common/model/community/CommunityCategory.php`
- Modify: `server/app/common/model/community/CommunityPost.php`
- Create: `server/app/adminapi/controller/community/CommunityCategoryController.php`
- Create: `server/app/adminapi/logic/community/CommunityCategoryLogic.php`
- Create: `server/app/adminapi/validate/community/CommunityCategoryValidate.php`
- Create: `server/app/adminapi/lists/community/CommunityCategoryLists.php`
- Modify: `server/app/adminapi/controller/community/CommunityPostController.php`
- Modify: `server/app/adminapi/logic/community/CommunityPostLogic.php`
- Modify: `server/app/adminapi/validate/community/CommunityPostValidate.php`
- Modify: `server/app/adminapi/lists/community/CommunityPostLists.php`
- [x] **Step 1: 实现分类 CRUD**
分类接口沿用标签管理风格:
```text
GET /adminapi/community.communityCategory/lists
GET /adminapi/community.communityCategory/all
GET /adminapi/community.communityCategory/detail
POST /adminapi/community.communityCategory/add
POST /adminapi/community.communityCategory/edit
POST /adminapi/community.communityCategory/delete
```
分类图标使用 `FileService::getFileUrl/setFileUrl`;删除前查询 `CommunityPost::where('category_id', id)->count()`,存在引用时返回“该分类下存在帖子,不能删除”。
- [x] **Step 2: 扩展帖子管理字段**
帖子列表筛选增加:
```php
'=' => ['status', 'post_type', 'category_id', 'is_top', 'is_hot', 'is_recommend', 'is_topic', 'user_id']
```
列表和详情批量补充 `category_name`。新增 `setTopic()``setCategory()`;复用已有 `setRecommend()`。设置分类和删除帖子后重算受影响分类的 `post_count`
- [x] **Step 3: 补齐参数校验**
`is_topic``is_hot``is_recommend` 只允许 `0/1``category_id` 允许 `0`,大于 `0` 时必须存在于分类表。控制器继续使用 likeadmin 统一成功/失败响应。
### Task 3: 前台帖子频道筛选
**Files:**
- Modify: `server/app/api/lists/community/CommunityPostLists.php`
- Modify: `uniapp/src/pages/community/community.vue`
- [x] **Step 1: 后端列表接收频道参数**
`queryWhere()` 对明确传入的值进行过滤:
```php
foreach (['is_recommend', 'is_hot', 'is_topic', 'category_id'] as $field) {
if (isset($this->params[$field]) && $this->params[$field] !== '') {
$where[] = [$field, '=', (int) $this->params[$field]];
}
}
```
列表字段返回 `category_id/is_topic`,并批量补充 `category_name`;排序保持 `is_top DESC, is_hot DESC, create_time DESC`
- [x] **Step 2: 用户端频道发送真实筛选条件**
社区页请求参数调整为:
```ts
if (activeFeed.value === 'recommend') params.is_recommend = 1
if (activeFeed.value === 'follow') params.follow = 1
if (activeFeed.value === 'hot') params.is_hot = 1
if (activeFeed.value === 'topic') params.is_topic = 1
if (activeTagId.value > 0) params.tag_id = activeTagId.value
```
移除仅对当前页进行热门分数排序的 `rankHotPosts()`,防止分页结果与后台热门开关不一致。
### Task 4: 管理端分类页面和帖子字段管理
**Files:**
- Create: `admin/src/views/community/category/index.vue`
- Modify: `admin/src/api/community.ts`
- Modify: `admin/src/views/community/post/index.vue`
- [x] **Step 1: 新增分类 API 和管理页**
API 增加分类 `lists/all/detail/add/edit/delete`。分类页复用标签页布局,支持名称搜索、图标、排序、状态、新增、编辑和受保护删除。
- [x] **Step 2: 补齐帖子 API**
新增调用:
```ts
communityPostSetRecommend({ id, is_recommend })
communityPostSetTopic({ id, is_topic })
communityPostSetCategory({ id, category_id })
```
- [x] **Step 3: 扩展帖子列表管理**
搜索区增加分类、推荐、热门、话题筛选;表格增加分类单选框,以及推荐、热门、话题三个开关。接口失败时重新获取列表恢复服务端真实值,详情弹窗展示分类和三个频道状态。
### Task 5: Review、静态验证、提交与测试服同步
**Files:**
- Modify: `docs/业务进度管理.md`
- Create: `docs/superpowers/plans/2026-08-02-community-post-channels-and-categories.md`
- [x] **Step 1: 静态检查**
运行所有修改 PHP 文件的 `php -l``git diff --check`、SQL 未完成标记扫描,并检查 Controller/Logic/Validate/Lists/Model/Admin API 字段名称一致。项目未授权执行完整构建或自动化测试。
- [x] **Step 2: 前端代码 review**
检查管理端请求失败回滚、权限标识、选择器数值类型、分页筛选参数和用户端频道切换请求,确认不存在客户端分页内伪排序或未使用导入。
- [ ] **Step 3: 提交源码**
只暂存本任务文件,排除工作区原有 `AGENTS.md` 和共享文档混合改动,提交信息:
```text
feat: add community post categories and channels
```
- [ ] **Step 4: 同步测试服并应用数据库初始化**
先执行项目规定部署命令:
```powershell
Set-Location D:\www\gs-sport-era; powershell -ExecutionPolicy Bypass -File .\scripts\deploy-server.ps1 -Sudo -AutoIncremental -AutoIncrementalCommits 3 -RemoteDir /www/wwwroot/test-server.sbnews.net
```
随后在测试服运行幂等 SQL `docs/sql/create_community_post_categories.sql`,复核分类表、帖子新字段和社区分类菜单已经存在。