feat: add community post category selection

This commit is contained in:
hajimi
2026-08-03 17:27:17 +08:00
parent d86b1b2cad
commit d5160e0ba3
3 changed files with 207 additions and 15 deletions
@@ -168,8 +168,9 @@ class CommunityController extends BaseApiController
$content = $this->request->post('content/s', '');
$images = $this->request->post('images/a', []);
$postType = $this->request->post('post_type/d', 0);
$categoryId = $this->request->post('category_id/d', 0);
$matchId = $this->request->post('match_id/d', 0);
$tagIds = $this->request->post('tag_ids/a', []);
$tagIds = array_values(array_unique(array_filter(array_map('intval', $this->request->post('tag_ids/a', [])))));
$isPaid = $this->request->post('is_paid/d', 0);
$pricePoints = $this->request->post('price_points/d', 0);
$freeContentLen = $this->request->post('free_content_len/d', 100);
@@ -178,6 +179,24 @@ class CommunityController extends BaseApiController
return $this->fail('请输入内容或上传图片');
}
if ($categoryId <= 0) {
return $this->fail('请选择帖子分类');
}
$category = CommunityCategory::where('id', $categoryId)->where('status', 1)->findOrEmpty();
if ($category->isEmpty()) {
return $this->fail('所选分类不可用');
}
if (count($tagIds) > 3) {
return $this->fail('最多选择3个标签');
}
if ($tagIds) {
$validTagCount = CommunityTag::whereIn('id', $tagIds)->where('status', 1)->count();
if ($validTagCount !== count($tagIds)) {
return $this->fail('存在不可用标签');
}
}
if ($isPaid && $pricePoints <= 0) {
return $this->fail('付费帖子必须设置积分价格');
}
@@ -192,6 +211,7 @@ class CommunityController extends BaseApiController
'content' => $content,
'images' => $images,
'post_type' => $postType,
'category_id' => $categoryId,
'match_id' => $matchId,
'is_paid' => $isPaid ? 1 : 0,
'price_points' => $isPaid ? $pricePoints : 0,
@@ -211,6 +231,7 @@ class CommunityController extends BaseApiController
// 更新标签帖子数
CommunityTag::whereIn('id', $tagIds)->inc('post_count')->update();
}
CommunityCategory::where('id', $categoryId)->inc('post_count')->update();
Db::commit();
KbSyncService::enqueue('post', 'post', (int) $post->id, 'upsert', 30);