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,72 @@
<?php
namespace app\adminapi\logic\community;
use app\common\logic\BaseLogic;
use app\common\model\community\CommunityCategory;
use app\common\model\community\CommunityPost;
class CommunityCategoryLogic extends BaseLogic
{
public static function add(array $params): bool
{
try {
CommunityCategory::create([
'name' => trim($params['name']),
'icon' => $params['icon'] ?? '',
'sort' => $params['sort'] ?? 0,
'status' => $params['status'] ?? 1,
]);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
public static function edit(array $params): bool
{
try {
CommunityCategory::update([
'id' => $params['id'],
'name' => trim($params['name']),
'icon' => $params['icon'] ?? '',
'sort' => $params['sort'] ?? 0,
'status' => $params['status'] ?? 1,
]);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
public static function delete(array $params): bool
{
if (CommunityPost::where('category_id', $params['id'])->count() > 0) {
self::setError('该分类下存在帖子,不能删除');
return false;
}
try {
CommunityCategory::destroy($params['id']);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
public static function detail(array $params): array
{
return CommunityCategory::findOrEmpty($params['id'])->toArray();
}
public static function all(): array
{
return CommunityCategory::order('sort', 'desc')
->order('id', 'desc')
->select()
->toArray();
}
}
@@ -3,9 +3,11 @@
namespace app\adminapi\logic\community;
use app\common\logic\BaseLogic;
use app\common\model\community\CommunityCategory;
use app\common\model\community\CommunityPost;
use app\common\service\ai\KbSyncService;
use app\common\model\user\User;
use app\common\service\ai\KbSyncService;
use think\facade\Db;
class CommunityPostLogic extends BaseLogic
{
@@ -21,6 +23,9 @@ class CommunityPostLogic extends BaseLogic
$user = User::field('nickname,avatar')->findOrEmpty($post['user_id'] ?? 0)->toArray();
$post['nickname'] = $user['nickname'] ?? '-';
$post['avatar'] = $user['avatar'] ?? '';
$post['category_name'] = (int) ($post['category_id'] ?? 0) > 0
? (string) CommunityCategory::where('id', $post['category_id'])->value('name')
: '';
return $post;
}
@@ -87,9 +92,65 @@ class CommunityPostLogic extends BaseLogic
}
}
public static function setTopic(array $params): bool
{
try {
CommunityPost::update([
'id' => $params['id'],
'is_topic' => $params['is_topic'],
]);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
public static function setCategory(array $params): bool
{
$post = CommunityPost::findOrEmpty($params['id']);
if ($post->isEmpty()) {
self::setError('帖子不存在');
return false;
}
$oldCategoryId = (int) $post->category_id;
$newCategoryId = (int) $params['category_id'];
if ($oldCategoryId === $newCategoryId) {
return true;
}
Db::startTrans();
try {
$post->category_id = $newCategoryId;
$post->save();
self::refreshCategoryCount($oldCategoryId);
self::refreshCategoryCount($newCategoryId);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
public static function delete(array $params)
{
$categoryId = (int) CommunityPost::where('id', $params['id'])->value('category_id');
CommunityPost::destroy($params['id']);
self::refreshCategoryCount($categoryId);
KbSyncService::enqueue('post', 'post', (int) $params['id'], 'delete', 20);
}
private static function refreshCategoryCount(int $categoryId): void
{
if ($categoryId <= 0) {
return;
}
CommunityCategory::where('id', $categoryId)->update([
'post_count' => CommunityPost::where('category_id', $categoryId)->count(),
'update_time' => time(),
]);
}
}