74 lines
2.3 KiB
PHP
74 lines
2.3 KiB
PHP
<?php
|
|
/**
|
|
* 企业编辑接口 POST /api/company/update.php
|
|
* 入参:id + 需要更新的白名单字段
|
|
*/
|
|
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';
|
|
|
|
checkAjax();
|
|
checkPermission('company');
|
|
|
|
$id = (int)($_POST['id'] ?? 0);
|
|
if ($id <= 0) {
|
|
Response::error('参数错误', 400);
|
|
}
|
|
|
|
$data = extractFields(COMPANY_FIELDS);
|
|
unset($data['display_name']); // 显示名称不允许通过编辑接口置空/改名,如需改名请走完整字段
|
|
$hasFinancials = array_key_exists('financials', $_POST);
|
|
$hasCertifications = array_key_exists('certifications', $_POST);
|
|
if (empty($data) && !$hasFinancials && !$hasCertifications) {
|
|
Response::error('没有需要更新的字段', 400);
|
|
}
|
|
|
|
$pdo = DB::getInstance()->getPdo();
|
|
|
|
$check = $pdo->prepare("SELECT id, display_name FROM companies WHERE id = ?");
|
|
$check->execute([$id]);
|
|
$old = $check->fetch();
|
|
if (!$old) {
|
|
Response::error('企业不存在');
|
|
}
|
|
|
|
if (!empty($data)) {
|
|
[$sets, $params] = buildUpdate($data);
|
|
$params[] = $id;
|
|
$pdo->prepare("UPDATE companies SET $sets WHERE id = ?")->execute($params);
|
|
}
|
|
|
|
// 财务信息 / 资质认证(可选,整表替换)
|
|
if ($hasFinancials || $hasCertifications) {
|
|
$pdo->beginTransaction();
|
|
try {
|
|
if ($hasFinancials) {
|
|
$financials = json_decode($_POST['financials'], true);
|
|
if (!is_array($financials)) {
|
|
$pdo->rollBack();
|
|
Response::error('财务信息格式错误', 400);
|
|
}
|
|
saveCompanyFinancials($pdo, $id, $financials);
|
|
}
|
|
if ($hasCertifications) {
|
|
$certs = json_decode($_POST['certifications'], true);
|
|
if (!is_array($certs)) {
|
|
$pdo->rollBack();
|
|
Response::error('资质认证格式错误', 400);
|
|
}
|
|
saveCompanyCertifications($pdo, $id, $certs);
|
|
}
|
|
$pdo->commit();
|
|
} catch (Exception $e) {
|
|
$pdo->rollBack();
|
|
Response::error('关联信息保存失败:' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
if (!empty($data)) {
|
|
logCurrent('update', 'company', 'companies', $id, ['before' => $old, 'after' => $data]);
|
|
}
|
|
Response::success(null, '更新成功');
|