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();
}
}