v1.0.10
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* 人员编辑接口 POST /api/person/update.php
|
||||
* 入参:id + 白名单字段;可选 contacts(JSON 数组,整体替换联系方式)
|
||||
*/
|
||||
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('person');
|
||||
|
||||
$id = (int)($_POST['id'] ?? 0);
|
||||
if ($id <= 0) {
|
||||
Response::error('参数错误', 400);
|
||||
}
|
||||
|
||||
$data = extractFields(PERSON_FIELDS);
|
||||
unset($data['union_id']); // 不允许修改 union_id
|
||||
|
||||
$pdo = DB::getInstance()->getPdo();
|
||||
$check = $pdo->prepare("SELECT * FROM persons WHERE id = ?");
|
||||
$check->execute([$id]);
|
||||
$old = $check->fetch();
|
||||
if (!$old) {
|
||||
Response::error('人员不存在');
|
||||
}
|
||||
|
||||
$contactsChanged = false;
|
||||
if (isset($_POST['contacts'])) {
|
||||
$contactsChanged = true;
|
||||
// 整体替换联系方式:先删旧(保留非联系人性质?此处直接删除全部后重建)
|
||||
$pdo->prepare("DELETE FROM social_accounts WHERE owner_type = 'person' AND owner_id = ?")->execute([$id]);
|
||||
$contacts = json_decode($_POST['contacts'], true);
|
||||
if (is_array($contacts)) {
|
||||
$ins = $pdo->prepare(
|
||||
"INSERT INTO social_accounts (owner_type, owner_id, platform, account_id, profile_url, remark, is_primary)
|
||||
VALUES ('person', ?, ?, ?, ?, ?, ?)"
|
||||
);
|
||||
foreach ($contacts as $c) {
|
||||
$platform = trim($c['platform'] ?? '');
|
||||
$accountId = trim($c['account_id'] ?? '');
|
||||
if ($platform === '' || $accountId === '') continue;
|
||||
$ins->execute([$id, $platform, $accountId, $c['profile_url'] ?? null, $c['remark'] ?? null, !empty($c['is_primary']) ? 1 : 0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($data)) {
|
||||
[$sets, $params] = buildUpdate($data);
|
||||
$params[] = $id;
|
||||
$pdo->prepare("UPDATE persons SET $sets WHERE id = ?")->execute($params);
|
||||
}
|
||||
|
||||
logCurrent('update', 'person', 'persons', $id, ['before' => $old, 'after' => $data, 'contacts_replaced' => $contactsChanged]);
|
||||
Response::success(null, '更新成功');
|
||||
Reference in New Issue
Block a user