120 lines
5.0 KiB
PHP
120 lines
5.0 KiB
PHP
<?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,status=待处理,recorded_by=当前登录用户
|
||
*/
|
||
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');
|
||
|
||
$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'] ?? '');
|
||
$personName = trim($_POST['person_name'] ?? '');
|
||
$phone = trim($_POST['contact_phone'] ?? '');
|
||
$email = trim($_POST['contact_email'] ?? '');
|
||
$idNumber = trim($_POST['id_number'] ?? '');
|
||
$platform = trim($_POST['platform'] ?? '');
|
||
$accountId = trim($_POST['account_id'] ?? '');
|
||
$profileUrl = trim($_POST['profile_url'] ?? '');
|
||
$industry = trim($_POST['industry'] ?? ''); // 企业:行业
|
||
$registrationNum = trim($_POST['registration_number'] ?? ''); // 企业:注册号
|
||
$category = trim($_POST['target_product_category'] ?? ''); // 产品:品类(存 extra)
|
||
$needCategory = trim($_POST['need_category'] ?? ''); // 需求:需求类别(存 extra)
|
||
$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);
|
||
|
||
// 额外信息(身份证/社媒平台/账号ID/主页链接/产品品类/需求类别)存 extra_info
|
||
$extra = [];
|
||
if ($idNumber !== '') $extra['id_number'] = $idNumber;
|
||
if ($platform !== '') $extra['platform'] = $platform;
|
||
if ($accountId !== '') $extra['account_id'] = $accountId;
|
||
if ($profileUrl !== '') $extra['profile_url'] = $profileUrl;
|
||
if ($category !== '') $extra['category'] = $category;
|
||
if ($needCategory !== '') $extra['need_category'] = $needCategory;
|
||
|
||
// 至少有一项内容才能录为碎片
|
||
if ($companyName === '' && $personName === '' && $phone === '' && $email === '' && !$extra) {
|
||
Response::error('请至少填写一项内容', 400);
|
||
}
|
||
|
||
$pdo->prepare(
|
||
"INSERT INTO preliminary_data
|
||
(source_type, company_name, industry, registration_number, person_name, contact_phone, contact_email,
|
||
source_channel, recorded_by, status, extra_info)
|
||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, '待处理', ?)"
|
||
)->execute([
|
||
$sourceType,
|
||
$companyName !== '' ? $companyName : null,
|
||
$industry !== '' ? $industry : null,
|
||
$registrationNum !== '' ? $registrationNum : null,
|
||
$personName !== '' ? $personName : null,
|
||
$phone !== '' ? $phone : null,
|
||
$email !== '' ? $email : null,
|
||
$sourceChannel !== '' ? $sourceChannel : null, // 未选择来源保持空白
|
||
$_SESSION['username'] ?? null,
|
||
$extra ? json_encode($extra, JSON_UNESCAPED_UNICODE) : null,
|
||
]);
|
||
$newId = (int)$pdo->lastInsertId();
|
||
|
||
logCurrent('add', 'fragment', 'preliminary_data', $newId, [
|
||
'source_type' => $sourceType,
|
||
'company_name' => $companyName,
|
||
'person_name' => $personName,
|
||
'phone' => $phone,
|
||
'email' => $email,
|
||
'extra' => $extra,
|
||
]);
|
||
|
||
Response::success(['id' => $newId], '新增成功');
|