Files
superlink/api/person/import.php
T
nanguaboss 71c6e8d9c1 v1.0.10
2026-08-03 00:07:01 +08:00

83 lines
2.9 KiB
PHP

<?php
/**
* 人员 CSV 导入接口 POST /api/person/import.php (multipart/form-data, 字段名 file)
* CSV 表头:union_id,full_name,gender,nationality,id_type,id_number,education,
* graduated_from,hometown,work_location,phone,email
* 仅 full_name 必填;union_id 留空自动生成;phone/email 写入 social_accounts。
*/
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('person');
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();
$insPerson = $pdo->prepare(
"INSERT INTO persons (union_id, full_name, gender, nationality, id_type, id_number, education, graduated_from, hometown, work_location)
VALUES (?,?,?,?,?,?,?,?,?,?)"
);
$insAccount = $pdo->prepare(
"INSERT INTO social_accounts (owner_type, owner_id, platform, account_id, is_primary)
VALUES ('person', ?, ?, ?, ?)"
);
$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['full_name'])) {
$failed++;
continue;
}
try {
$unionId = !empty($rec['union_id']) ? $rec['union_id'] : ('P' . date('YmdHis') . substr(uniqid(), -6));
// 跳过重复 union_id
$chk = $pdo->prepare("SELECT COUNT(*) FROM persons WHERE union_id = ?");
$chk->execute([$unionId]);
if ((int)$chk->fetchColumn() > 0) {
$failed++;
continue;
}
$insPerson->execute([
$unionId, $rec['full_name'], $rec['gender'] ?? '保密', $rec['nationality'] ?? null,
$rec['id_type'] ?? null, $rec['id_number'] ?? null, $rec['education'] ?? null,
$rec['graduated_from'] ?? null, $rec['hometown'] ?? null, $rec['work_location'] ?? null,
]);
$newId = (int)$pdo->lastInsertId();
if (!empty($rec['phone'])) {
$insAccount->execute([$newId, 'phone', $rec['phone'], 1]);
}
if (!empty($rec['email'])) {
$insAccount->execute([$newId, 'email', $rec['email'], 0]);
}
$inserted++;
} catch (Exception $e) {
$failed++;
}
}
fclose($handle);
logCurrent('import', 'person', 'persons', null, ['inserted' => $inserted, 'failed' => $failed]);
Response::success(['inserted' => $inserted, 'failed' => $failed], "导入完成:成功 $inserted 条,失败 $failed 条");