71 lines
2.6 KiB
PHP
71 lines
2.6 KiB
PHP
<?php
|
||
/**
|
||
* 企业新增接口 POST /api/company/add.php
|
||
* 必填:display_name;其余字段见 COMPANY_FIELDS 白名单
|
||
*/
|
||
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';
|
||
|
||
checkAjax();
|
||
checkPermission('company');
|
||
|
||
$data = extractFields(COMPANY_FIELDS);
|
||
// 显示名称不再单独录入:取中文名称(数据库 display_name 为 NOT NULL)
|
||
if (empty($data['display_name'])) {
|
||
$data['display_name'] = $data['name_zh'] ?? '';
|
||
}
|
||
if (empty($data['display_name'])) {
|
||
Response::error('企业名称(中文名称)为必填项', 400);
|
||
}
|
||
|
||
$pdo = DB::getInstance()->getPdo();
|
||
[$sql, $params] = buildInsert($data);
|
||
$pdo->prepare("INSERT INTO companies $sql")->execute($params);
|
||
$newId = (int)$pdo->lastInsertId();
|
||
|
||
// 财务信息 / 资质认证 / 联系方式(可选,整表替换)
|
||
if (array_key_exists('financials', $_POST) || array_key_exists('certifications', $_POST) || array_key_exists('accounts', $_POST)) {
|
||
$pdo->beginTransaction();
|
||
try {
|
||
if (array_key_exists('financials', $_POST)) {
|
||
$financials = json_decode($_POST['financials'], true);
|
||
if (!is_array($financials)) {
|
||
$pdo->rollBack();
|
||
Response::error('财务信息格式错误', 400);
|
||
}
|
||
saveCompanyFinancials($pdo, $newId, $financials);
|
||
}
|
||
if (array_key_exists('certifications', $_POST)) {
|
||
$certs = json_decode($_POST['certifications'], true);
|
||
if (!is_array($certs)) {
|
||
$pdo->rollBack();
|
||
Response::error('资质认证格式错误', 400);
|
||
}
|
||
saveCompanyCertifications($pdo, $newId, $certs);
|
||
}
|
||
if (array_key_exists('accounts', $_POST)) {
|
||
$accounts = json_decode($_POST['accounts'], true);
|
||
if (!is_array($accounts)) {
|
||
$pdo->rollBack();
|
||
Response::error('联系方式格式错误', 400);
|
||
}
|
||
saveCompanyAccounts($pdo, $newId, $accounts);
|
||
}
|
||
$pdo->commit();
|
||
} catch (Exception $e) {
|
||
$pdo->rollBack();
|
||
Response::error('关联信息保存失败:' . $e->getMessage());
|
||
}
|
||
}
|
||
|
||
// 完整度检查:企业主表 + 其 social_accounts
|
||
updateIncomplete($pdo, 'companies', $newId);
|
||
updateAccountsIncomplete($pdo, 'company', $newId);
|
||
|
||
logCurrent('add', 'company', 'companies', $newId, $data);
|
||
Response::success(['id' => $newId], '新增成功');
|