Files
superlink/api/person/add.php
T

78 lines
2.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 人员新增接口 POST /api/person/add.php
* 必填:full_name;union_id 为空时自动生成。
* 可选:contacts(JSON 数组,如 [{"platform":"email","account_id":"a@b.com","is_primary":1}])
*/
require_once __DIR__ . '/../common/db.php';
require_once __DIR__ . '/../common/response.php';
require_once __DIR__ . '/../common/auth.php';
require_once __DIR__ . '/../common/logger.php';
require_once __DIR__ . '/../common/helpers.php';
require_once __DIR__ . '/../common/completeness.php';
require_once __DIR__ . '/../common/duplicate_check.php';
checkAjax();
checkPermission('person');
$data = extractFields(PERSON_FIELDS);
if (empty($data['full_name'])) {
Response::error('姓名(full_name)为必填项', 400);
}
if (empty($data['union_id'])) {
$data['union_id'] = 'P' . date('YmdHis') . substr(uniqid(), -6);
}
$pdo = DB::getInstance()->getPdo();
// union_id 唯一性
$chk = $pdo->prepare("SELECT COUNT(*) FROM persons WHERE union_id = ?");
$chk->execute([$data['union_id']]);
if ((int)$chk->fetchColumn() > 0) {
Response::error('union_id 已存在,请更换');
}
// 唯一性校验:身份证号码 / 联系方式(手机/邮箱/社媒账号/主页链接),收集全部重复
$dups = [];
if (!empty($data['id_number'])) {
$dup = findDuplicate($pdo, 'id_number', $data['id_number']);
if ($dup) $dups[] = $dup;
}
$contacts = json_decode($_POST['contacts'] ?? '[]', true);
if (!is_array($contacts)) {
$contacts = [];
}
$dups = array_merge($dups, checkAccountsDuplicates($pdo, $contacts));
if (!empty($dups)) Response::duplicates($dups);
[$sql, $params] = buildInsert($data);
$pdo->prepare("INSERT INTO persons $sql")->execute($params);
$newId = (int)$pdo->lastInsertId();
// 写入联系方式(v1.0.16:支持 is_defult 字段,1常用/0备用)
if (is_array($contacts) && count($contacts) > 0) {
$ins = $pdo->prepare(
"INSERT INTO social_accounts (owner_type, owner_id, platform, account_id, profile_url, remark, is_primary, is_defult)
VALUES ('person', ?, ?, ?, ?, ?, ?, ?)"
);
foreach ($contacts as $c) {
$platform = trim($c['platform'] ?? '');
$accountId = trim($c['account_id'] ?? '');
if ($platform === '' || $accountId === '') continue;
$ins->execute([$newId, $platform, $accountId, $c['profile_url'] ?? null, $c['remark'] ?? null, !empty($c['is_primary']) ? 1 : 0, !empty($c['is_defult']) ? 1 : 0]);
}
}
// 工作履历(v1.0.18:整表替换)
$experiences = json_decode($_POST['experiences'] ?? '[]', true);
if (is_array($experiences)) {
savePersonExperiences($pdo, $newId, $experiences);
}
// 完整度检查:人员主表 + 其 social_accounts
updateIncomplete($pdo, 'persons', $newId);
updateAccountsIncomplete($pdo, 'person', $newId);
logCurrent('add', 'person', 'persons', $newId, ['data' => $data, 'contacts' => $contacts]);
Response::success(['id' => $newId], '新增成功');