Files
superlink/api/fragment/hard_add.php
T

71 lines
2.5 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/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';
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'] ?? '');
$category = trim($_POST['target_product_category'] ?? '');
$description = trim($_POST['description'] ?? '');
$sourceChannel = trim($_POST['source_channel'] ?? '');
if ($sourceChannel === '') {
$sourceChannel = '手动录入';
}
// 格式校验:手机 / 邮箱
checkFieldFormat('phone', $phone);
checkFieldFormat('email', $email);
// 至少有一项内容才能录为碎片
if ($companyName === '' && $personName === '' && $phone === '' && $email === '' && $category === '' && $description === '') {
Response::error('请至少填写一项内容', 400);
}
$pdo = DB::getInstance()->getPdo();
$pdo->prepare(
"INSERT INTO preliminary_data
(source_type, company_name, person_name, contact_phone, contact_email,
target_product_category, description, source_channel, recorded_by, status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, '待处理')"
)->execute([
$sourceType,
$companyName !== '' ? $companyName : null,
$personName !== '' ? $personName : null,
$phone !== '' ? $phone : null,
$email !== '' ? $email : null,
$category !== '' ? $category : null,
$description !== '' ? $description : null,
$sourceChannel,
$_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,
]);
Response::success(['id' => $newId], '新增成功');