feat: 在账号资源库中展示导入的标签字段

KOC 资源导入模板新增「标签」列,导入后写入 accounts.tags 并在
账号资源库面板以 badge 形式展示,导出时也带回标签列以保证往返一致。

- lib/resource-import.ts: 新增 tags 字段与 HEADER 别名(标签/账号标签/人设标签),
  新增 normalizeTags/mergeTags 工具,按顿号规整与合并
- 修复 xlsx 解析器对自闭合空 cell (<c r="G6"/>) 的错位 bug,否则
  行内出现空 cell 会让后续 cell 的列引用读到错误 body(如老茄子行标签丢失)
- db/schema.ts + lib/mvp-db.ts: accounts 表新增 tags VARCHAR(500)
- app/api/resources-import/route.ts: SELECT/INSERT/UPDATE/预览全部带上 tags
- app/api/resources-export/route.ts: 导出表加「标签」列
- app/admin-app.tsx + app/globals.css: 资源卡片新增「账号标签」区,
  导入预览行展示标签
- public/KOC资源导入模板.xlsx: 模板补「标签(选填)」列与填写说明
- mysql/0005_account_tags.sql: 线上 MySQL 迁移
- tests/resource-import.test.mjs: 补 tags 解析/合并用例
This commit is contained in:
ABAPPLO
2026-08-18 16:39:38 +08:00
parent ad3dbdcc86
commit 582c26e57e
10 changed files with 145 additions and 8 deletions

View File

@@ -9,6 +9,7 @@ import {
import { getRuntimeEnv } from "../../../lib/runtime-env";
import {
mergeCooperationSources,
mergeTags,
normalizeProfileUrl,
parseResourceImportFile,
RESOURCE_IMPORT_MAX_BYTES,
@@ -27,6 +28,7 @@ type AccountRow = {
ip_location: string;
followers: number;
cooperation_source: string;
tags: string;
};
type AnalyzedRow = ResourceImportRow & {
@@ -34,6 +36,7 @@ type AnalyzedRow = ResourceImportRow & {
accountId: string;
platformUid: string;
cooperationSource: string;
tags: string;
};
function identityKey(platform: string, value: string) {
@@ -46,7 +49,7 @@ async function loadAccounts() {
return getRawDb()
.prepare(
`SELECT id, platform, platform_uid, public_account_id, nickname,
profile_url, ip_location, followers, cooperation_source
profile_url, ip_location, followers, cooperation_source, tags
FROM accounts`,
)
.all<AccountRow>();
@@ -187,6 +190,7 @@ function analyzeRows(rows: ResourceImportRow[], accountRows: AccountRow[]) {
existing?.cooperation_source ?? "",
row.cooperationSource,
),
tags: mergeTags(existing?.tags ?? "", row.tags),
};
if (analyzed.action !== "error") {
const virtual: AccountRow = {
@@ -199,6 +203,7 @@ function analyzeRows(rows: ResourceImportRow[], accountRows: AccountRow[]) {
ip_location: row.ipLocation || existing?.ip_location || "待识别",
followers: row.followers || existing?.followers || 0,
cooperation_source: analyzed.cooperationSource,
tags: analyzed.tags,
};
if (row.profileUrl) profileMap.set(identityKey(row.platform, row.profileUrl), virtual);
if (row.publicAccountId) {
@@ -249,6 +254,7 @@ export async function POST(request: Request) {
ipLocation: row.ipLocation,
followers: row.followers,
cooperationSource: row.cooperationSource,
tags: row.tags,
action: row.action,
errors: row.errors,
})),
@@ -275,6 +281,7 @@ export async function POST(request: Request) {
WHEN ? != '' AND ? != '待识别' THEN ? ELSE ip_location END,
followers = CASE WHEN ? = 1 THEN ? ELSE followers END,
cooperation_source = ?,
tags = ?,
last_seen_at = CURRENT_TIMESTAMP
WHERE id = ?`,
)
@@ -290,6 +297,7 @@ export async function POST(request: Request) {
row.followersResolved ? 1 : 0,
row.followers,
row.cooperationSource,
row.tags,
row.accountId,
)
: db
@@ -297,8 +305,8 @@ export async function POST(request: Request) {
`INSERT INTO accounts
(id, platform, platform_uid, public_account_id, nickname,
profile_url, ip_location, followers, post_count, avg_views,
cooperation_source)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, 0, ?)`,
cooperation_source, tags)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, 0, ?, ?)`,
)
.bind(
row.accountId,
@@ -310,6 +318,7 @@ export async function POST(request: Request) {
row.ipLocation,
row.followers,
row.cooperationSource,
row.tags,
),
);
if (statements.length > 0) await db.batch(statements);