Files

117 lines
5.0 KiB
PHP
Raw Permalink 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/fragment/hard_add.php
* 入参:source_type(company|person|product|need|mixed,必填)/
* company_name / person_name / contact_phone / contact_email /
* target_product_category / description / source_channel
* 写入 preliminary_data,recorded_by=当前登录用户(v1.0.50 起无 status 字段)
*/
require_once __DIR__ . '/../common/Api.php';
$pdo = Api::boot(['module' => 'preliminary']);
$sourceType = trim($_POST['source_type'] ?? '');
if (!in_array($sourceType, ['company', 'person', 'product', 'need', 'mixed'], true)) {
Response::error('碎片类型(source_type)为必填项', 400);
}
$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'] ?? ''); // 重要-主页
$sourceChannel = trim($_POST['source_channel'] ?? '');
// 硬数据标准校验:
// 企业/产品/需求 → 企业名称必填;人员 → 手机、邮箱至少填一个;混合 → 至少有一项内容
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);
}
// 唯一性校验(收集全部重复):企业名称 / 手机 / 邮箱 / 身份证 / 账号ID(同平台)/ 主页链接
$pdo = DB::getInstance()->getPdo();
$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);
// 至少有一项内容才能录为碎片
if ($companyName === '' && $personName === '' && $phone === '' && $email === '' && $idNumber === '' && $accountId === '') {
Response::error('请至少填写一项内容', 400);
}
$pdo->prepare(
"INSERT INTO preliminary_data
(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,
source_channel, recorded_by)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
)->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,
$sourceChannel !== '' ? $sourceChannel : null, // 未选择来源保持空白
$_SESSION['username'] ?? null,
]);
$newId = (int)$pdo->lastInsertId();
logCurrent('add', 'fragment', 'preliminary_data', $newId, [
'source_type' => $sourceType,
'company_name' => $companyName,
'person_name' => $personName,
'phone' => $phone,
'email' => $email,
'id_number' => $idNumber,
'platform' => $platform,
'account_id' => $accountId,
]);
Response::success(['id' => $newId], '新增成功');