72 lines
2.5 KiB
PHP
72 lines
2.5 KiB
PHP
<?php
|
|
/**
|
|
* 媒体数据编辑接口 POST /api/media/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';
|
|
require_once __DIR__ . '/../common/completeness.php';
|
|
require_once __DIR__ . '/../common/duplicate_check.php';
|
|
|
|
checkAjax();
|
|
checkPermission('media');
|
|
|
|
$id = (int)($_POST['id'] ?? 0);
|
|
if ($id <= 0) {
|
|
Response::error('参数错误', 400);
|
|
}
|
|
|
|
$pdo = DB::getInstance()->getPdo();
|
|
$check = $pdo->prepare("SELECT * FROM social_accounts WHERE id = ?");
|
|
$check->execute([$id]);
|
|
$old = $check->fetch();
|
|
if (!$old) {
|
|
Response::error('媒体账号不存在');
|
|
}
|
|
|
|
$data = extractFields(MEDIA_FIELDS);
|
|
unset($data['owner_type'], $data['owner_id']); // 归属关系不允许改
|
|
|
|
// 唯一性校验(排除自身):社媒账号ID(同平台)/ 手机 / 邮箱 / 主页链接
|
|
if (isset($_POST['platform']) || isset($_POST['account_id']) || isset($_POST['profile_url'])) {
|
|
$dup = checkSingleAccountDuplicate(
|
|
$pdo,
|
|
$_POST['platform'] ?? ($old['platform'] ?? ''),
|
|
$_POST['account_id'] ?? ($old['account_id'] ?? ''),
|
|
$_POST['profile_url'] ?? ($old['profile_url'] ?? ''),
|
|
[$id]
|
|
);
|
|
if ($dup) Response::duplicate($dup);
|
|
}
|
|
|
|
if (!empty($data)) {
|
|
[$sets, $params] = buildUpdate($data);
|
|
$params[] = $id;
|
|
$pdo->prepare("UPDATE social_accounts SET $sets WHERE id = ?")->execute($params);
|
|
}
|
|
|
|
// 更新商业属性(存在则更新,不存在则插入)
|
|
$attr = extractFields(MEDIA_ATTR_FIELDS);
|
|
if (!empty($attr)) {
|
|
$exists = $pdo->prepare("SELECT COUNT(*) FROM media_commercial_attributes WHERE social_account_id = ?");
|
|
$exists->execute([$id]);
|
|
if ((int)$exists->fetchColumn() > 0) {
|
|
[$attrSets, $attrParams] = buildUpdate($attr);
|
|
$attrParams[] = $id;
|
|
$pdo->prepare("UPDATE media_commercial_attributes SET $attrSets WHERE social_account_id = ?")->execute($attrParams);
|
|
} else {
|
|
$attr['social_account_id'] = $id;
|
|
[$attrSql, $attrParams] = buildInsert($attr);
|
|
$pdo->prepare("INSERT INTO media_commercial_attributes $attrSql")->execute($attrParams);
|
|
}
|
|
}
|
|
|
|
// 完整度检查:媒体账号 + 商业属性
|
|
updateMediaIncomplete($pdo, $id);
|
|
|
|
logCurrent('update', 'media', 'social_accounts', $id, ['before' => $old, 'after' => $data, 'attr' => $attr]);
|
|
Response::success(null, '更新成功');
|