Files
superlink/api/media/import.php
T
nanguaboss 2cb688ed46 0806
2026-08-06 00:50:23 +08:00

97 lines
3.7 KiB
PHP

<?php
/**
* 媒体数据 CSV 导入接口 POST /api/media/import.php (multipart/form-data, 字段名 file)
* CSV 表头:owner_type,owner_id,platform,account_id,profile_url,remark,is_primary,is_defult,
* account_level,content_categories,follower_count,avg_read_count,certification_type
* owner_type 为 person/company 时 owner_id 也可填对应名称(display_name/full_name),自动解析为ID。
*/
require_once __DIR__ . '/../common/db.php';
require_once __DIR__ . '/../common/response.php';
require_once __DIR__ . '/../common/auth.php';
require_once __DIR__ . '/../common/logger.php';
checkAjax();
checkPermission('media');
if (empty($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
Response::error('请选择要上传的CSV文件', 400);
}
$handle = fopen($_FILES['file']['tmp_name'], 'r');
if (!$handle) {
Response::error('无法读取文件', 400);
}
$first = preg_replace('/^\xEF\xBB\xBF/', '', fgets($handle));
$header = str_getcsv(trim($first));
$pdo = DB::getInstance()->getPdo();
$insAccount = $pdo->prepare(
"INSERT INTO social_accounts (owner_type, owner_id, platform, account_id, profile_url, remark, is_primary, is_defult, is_active)
VALUES (?,?,?,?,?,?,?,?,1)"
);
$insAttr = $pdo->prepare(
"INSERT INTO media_commercial_attributes
(social_account_id, account_level, content_categories, follower_count, avg_read_count, certification_type)
VALUES (?,?,?,?,?,?)"
);
$inserted = 0;
$failed = 0;
while (($row = fgetcsv($handle)) !== false) {
$row = array_map('trim', $row);
$rec = [];
foreach ($header as $idx => $col) {
$col = trim($col);
if (isset($row[$idx])) {
$rec[$col] = $row[$idx];
}
}
if (empty($rec['platform']) || empty($rec['account_id']) || empty($rec['owner_type'])) {
$failed++;
continue;
}
$ownerType = $rec['owner_type'] === 'company' ? 'company' : 'person';
$ownerId = (int)($rec['owner_id'] ?? 0);
if ($ownerId <= 0) {
// 按名称解析
$nameCol = $ownerType === 'company' ? 'display_name' : 'full_name';
$tbl = $ownerType === 'company' ? 'companies' : 'persons';
$find = $pdo->prepare("SELECT id FROM $tbl WHERE $nameCol = ? LIMIT 1");
$find->execute([$rec['owner_name'] ?? $rec['owner_id'] ?? '']);
$ownerId = (int)$find->fetchColumn();
}
if ($ownerId <= 0) {
$failed++;
continue;
}
try {
$chk = $pdo->prepare("SELECT COUNT(*) FROM social_accounts WHERE platform = ? AND account_id = ?");
$chk->execute([$rec['platform'], $rec['account_id']]);
if ((int)$chk->fetchColumn() > 0) {
$failed++;
continue;
}
$insAccount->execute([
$ownerType, $ownerId, $rec['platform'], $rec['account_id'],
$rec['profile_url'] ?? null, $rec['remark'] ?? null, !empty($rec['is_primary']) ? 1 : 0,
!empty($rec['is_defult']) ? 1 : 0,
]);
$newId = (int)$pdo->lastInsertId();
if (!empty($rec['account_level']) || !empty($rec['certification_type'])) {
$insAttr->execute([
$newId, $rec['account_level'] ?? null, $rec['content_categories'] ?? null,
(int)($rec['follower_count'] ?? 0), (int)($rec['avg_read_count'] ?? 0),
$rec['certification_type'] ?? null,
]);
}
$inserted++;
} catch (Exception $e) {
$failed++;
}
}
fclose($handle);
logCurrent('import', 'media', 'social_accounts', null, ['inserted' => $inserted, 'failed' => $failed]);
Response::success(['inserted' => $inserted, 'failed' => $failed], "导入完成:成功 $inserted 条,失败 $failed 条");