feat: support full community post editing

This commit is contained in:
hajimi
2026-08-02 20:38:47 +08:00
parent f78580b2b9
commit 2358603eda
9 changed files with 638 additions and 72 deletions
@@ -5,6 +5,7 @@ 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\model\community\CommunityTag;
use app\common\model\user\User;
use app\common\service\ai\KbSyncService;
use think\facade\Db;
@@ -26,6 +27,19 @@ class CommunityPostLogic extends BaseLogic
$post['category_name'] = (int) ($post['category_id'] ?? 0) > 0
? (string) CommunityCategory::where('id', $post['category_id'])->value('name')
: '';
$post['tag_ids'] = array_map('intval', Db::name('community_post_tag')
->where('post_id', $params['id'])
->column('tag_id'));
if (is_array($post['ext'] ?? null)) {
$post['ext'] = json_encode($post['ext'], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
} elseif (is_string($post['ext'] ?? null) && trim($post['ext']) !== '') {
$decodedExt = json_decode($post['ext'], true);
$post['ext'] = json_last_error() === JSON_ERROR_NONE
? json_encode($decodedExt, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
: $post['ext'];
} else {
$post['ext'] = '';
}
return $post;
}
@@ -50,7 +64,7 @@ class CommunityPostLogic extends BaseLogic
}
}
public static function editContent(array $params): bool
public static function edit(array $params): bool
{
$post = CommunityPost::findOrEmpty($params['id']);
if ($post->isEmpty()) {
@@ -58,17 +72,79 @@ class CommunityPostLogic extends BaseLogic
return false;
}
$oldCategoryId = (int) $post->category_id;
$oldTagIds = array_map('intval', Db::name('community_post_tag')
->where('post_id', $params['id'])
->column('tag_id'));
$newCategoryId = (int) $params['category_id'];
$newTagIds = array_values(array_unique(array_filter(
array_map('intval', $params['tag_ids'] ?? []),
static fn (int $tagId): bool => $tagId > 0
)));
$isPaid = (int) $params['is_paid'] === 1;
$extValue = $params['ext'] ?? '';
$ext = is_array($extValue)
? json_encode($extValue, JSON_UNESCAPED_UNICODE)
: trim((string) $extValue);
Db::startTrans();
try {
$post->content = (string) $params['content'];
$post->save();
if ((int) $post->status === 1) {
KbSyncService::enqueue('post', 'post', (int) $post->id, 'upsert', 30);
$post->save([
'origin_id' => trim((string) ($params['origin_id'] ?? '')) ?: null,
'user_id' => (int) $params['user_id'],
'content' => (string) ($params['content'] ?? ''),
'images' => array_values($params['images'] ?? []),
'post_type' => (int) $params['post_type'],
'category_id' => $newCategoryId,
'match_id' => (int) $params['match_id'],
'is_paid' => $isPaid ? 1 : 0,
'price_points' => $isPaid ? (int) $params['price_points'] : 0,
'free_content_len' => $isPaid ? (int) $params['free_content_len'] : 100,
'paid_count' => (int) $params['paid_count'],
'view_count' => (int) $params['view_count'],
'like_count' => (int) $params['like_count'],
'comment_count' => (int) $params['comment_count'],
'share_count' => (int) $params['share_count'],
'ext' => $ext === '' ? null : $ext,
'is_top' => (int) $params['is_top'],
'is_hot' => (int) $params['is_hot'],
'is_recommend' => (int) $params['is_recommend'],
'is_topic' => (int) $params['is_topic'],
'sort' => (int) $params['sort'],
'status' => (int) $params['status'],
'create_time' => strtotime((string) $params['create_time']),
'update_time' => time(),
]);
Db::name('community_post_tag')->where('post_id', $post->id)->delete();
if ($newTagIds) {
Db::name('community_post_tag')->insertAll(array_map(
static fn (int $tagId): array => [
'post_id' => (int) $post->id,
'tag_id' => $tagId,
],
$newTagIds
));
}
return true;
self::refreshCategoryCount($oldCategoryId);
self::refreshCategoryCount($newCategoryId);
self::refreshTagCounts(array_merge($oldTagIds, $newTagIds));
Db::commit();
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
KbSyncService::enqueue(
'post',
'post',
(int) $post->id,
(int) $post->status === 1 ? 'upsert' : 'delete',
30
);
return true;
}
public static function setTop(array $params): bool
@@ -188,4 +264,24 @@ class CommunityPostLogic extends BaseLogic
'update_time' => time(),
]);
}
private static function refreshTagCounts(array $tagIds): void
{
$tagIds = array_values(array_unique(array_filter(
array_map('intval', $tagIds),
static fn (int $tagId): bool => $tagId > 0
)));
foreach ($tagIds as $tagId) {
$postCount = Db::name('community_post_tag')
->alias('post_tag')
->join('community_post post', 'post.id = post_tag.post_id')
->where('post_tag.tag_id', $tagId)
->whereNull('post.delete_time')
->count();
CommunityTag::where('id', $tagId)->update([
'post_count' => $postCount,
'update_time' => time(),
]);
}
}
}