123 lines
4.7 KiB
PHP
123 lines
4.7 KiB
PHP
<?php
|
||
/**
|
||
* 硬碎片补全保存接口 POST /api/fragment/hard_update.php
|
||
* 仅更新硬碎片字段(企业名/联系人/手机/邮箱/身份证/社媒账号/主页链接),不执行转换。
|
||
* 转换请走 hard_convert.php(前端「转软碎片」按钮)。
|
||
*/
|
||
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/validate.php';
|
||
require_once __DIR__ . '/../common/duplicate_check.php';
|
||
|
||
checkAjax();
|
||
checkPermission('preliminary');
|
||
|
||
$id = (int)($_POST['id'] ?? 0);
|
||
if ($id <= 0) {
|
||
Response::error('参数错误', 400);
|
||
}
|
||
|
||
$pdo = DB::getInstance()->getPdo();
|
||
$stmt = $pdo->prepare("SELECT * FROM preliminary_data WHERE id = ? AND is_active = 1");
|
||
$stmt->execute([$id]);
|
||
$frag = $stmt->fetch();
|
||
if (!$frag) {
|
||
Response::error('碎片不存在');
|
||
}
|
||
if (in_array($frag['status'], ['已转换', '已废弃'], true)) {
|
||
Response::error('该碎片已处理(' . $frag['status'] . ')');
|
||
}
|
||
|
||
$sourceType = trim($_POST['source_type'] ?? ($frag['source_type'] ?? ''));
|
||
$companyName = trim($_POST['company_name'] ?? '');
|
||
$industry = trim($_POST['industry'] ?? '');
|
||
$registrationNum = trim($_POST['registration_number'] ?? '');
|
||
$businessRole = trim($_POST['business_role'] ?? '');
|
||
$country = trim($_POST['country'] ?? '');
|
||
$address = trim($_POST['address'] ?? '');
|
||
$personName = trim($_POST['person_name'] ?? '');
|
||
$phone = trim($_POST['contact_phone'] ?? '');
|
||
$email = trim($_POST['contact_email'] ?? '');
|
||
$workLocation = trim($_POST['work_location'] ?? '');
|
||
$idNumber = trim($_POST['id_number'] ?? '');
|
||
$platform = trim($_POST['platform'] ?? '');
|
||
$accountId = trim($_POST['account_id'] ?? '');
|
||
$profileUrl = trim($_POST['profile_url'] ?? '');
|
||
if (!in_array($sourceType, ['company', 'person', 'product', 'need', 'mixed'], true)) {
|
||
Response::error('碎片类型不合法', 400);
|
||
}
|
||
|
||
// 硬数据标准校验:企业/产品/需求 → 企业名称必填;人员 → 手机、邮箱至少一项
|
||
if (in_array($sourceType, ['company', 'product', 'need'], true) && $companyName === '') {
|
||
Response::error('企业类型硬数据:企业名称为必填项', 400);
|
||
}
|
||
if ($sourceType === 'person' && $phone === '' && $email === '') {
|
||
Response::error('人员类型硬数据:手机号码、邮箱至少填写一项', 400);
|
||
}
|
||
|
||
// 格式校验
|
||
checkFieldFormat('phone', $phone);
|
||
checkFieldFormat('email', $email);
|
||
checkFieldFormat('id_number', $idNumber);
|
||
if ($profileUrl !== '' && !preg_match('/^https?:\/\//i', $profileUrl)) {
|
||
Response::error('主页链接格式不正确(应以 http:// 或 https:// 开头)', 400);
|
||
}
|
||
|
||
// 唯一性校验(收集全部重复)
|
||
$dups = [];
|
||
if ($companyName !== '') {
|
||
$dup = findDuplicate($pdo, 'company_name', $companyName);
|
||
if ($dup) $dups[] = $dup;
|
||
}
|
||
$dups = array_merge($dups, checkSingleAccountDuplicate($pdo, $phone !== '' ? 'phone' : '', $phone, $profileUrl));
|
||
if ($email !== '') {
|
||
$dup = findDuplicate($pdo, 'email', $email);
|
||
if ($dup) $dups[] = $dup;
|
||
}
|
||
if ($idNumber !== '') {
|
||
$dup = findDuplicate($pdo, 'id_number', $idNumber);
|
||
if ($dup) $dups[] = $dup;
|
||
}
|
||
if ($accountId !== '' && $platform !== '') {
|
||
$dup = findDuplicate($pdo, 'account_id', $accountId, [], $platform);
|
||
if ($dup) $dups[] = $dup;
|
||
}
|
||
if (!empty($dups)) Response::duplicates($dups);
|
||
|
||
$pdo->prepare(
|
||
"UPDATE preliminary_data
|
||
SET source_type = ?, company_name = ?, industry = ?, registration_number = ?, business_role = ?, country = ?, address = ?,
|
||
person_name = ?, contact_phone = ?, contact_email = ?, work_location = ?, id_number = ?, platform = ?, account_id = ?, profile_url = ?
|
||
WHERE id = ?"
|
||
)->execute([
|
||
$sourceType,
|
||
$companyName !== '' ? $companyName : null,
|
||
$industry !== '' ? $industry : null,
|
||
$registrationNum !== '' ? $registrationNum : null,
|
||
$businessRole !== '' ? $businessRole : null,
|
||
$country !== '' ? $country : null,
|
||
$address !== '' ? $address : null,
|
||
$personName !== '' ? $personName : null,
|
||
$phone !== '' ? $phone : null,
|
||
$email !== '' ? $email : null,
|
||
$workLocation !== '' ? $workLocation : null,
|
||
$idNumber !== '' ? $idNumber : null,
|
||
$platform !== '' ? $platform : null,
|
||
$accountId !== '' ? $accountId : null,
|
||
$profileUrl !== '' ? $profileUrl : null,
|
||
$id,
|
||
]);
|
||
|
||
logCurrent('update', 'fragment', 'preliminary_data', $id, [
|
||
'source_type' => $sourceType,
|
||
'company_name' => $companyName,
|
||
'person_name' => $personName,
|
||
'phone' => $phone,
|
||
'email' => $email,
|
||
'id_number' => $idNumber,
|
||
]);
|
||
|
||
Response::success(null, '已保存,点击「转软碎片」完成转换');
|