67 lines
2.5 KiB
PHP
67 lines
2.5 KiB
PHP
<?php
|
|
/**
|
|
* 媒体数据新增接口 POST /api/media/add.php
|
|
* 必填:owner_type / owner_id / platform / account_id
|
|
* 可选:profile_url / remark / is_primary + 商业属性(account_level/certification_type/follower_count 等)
|
|
*/
|
|
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');
|
|
|
|
$data = extractFields(MEDIA_FIELDS);
|
|
if (!in_array($data['owner_type'] ?? '', ['person', 'company'], true)) {
|
|
Response::error('owner_type 必须为 person 或 company', 400);
|
|
}
|
|
if (empty($data['platform']) || empty($data['account_id'])) {
|
|
Response::error('平台(platform)和账号(account_id)为必填项', 400);
|
|
}
|
|
|
|
$pdo = DB::getInstance()->getPdo();
|
|
|
|
// owner 存在性校验
|
|
if ($data['owner_type'] === 'person') {
|
|
$chk = $pdo->prepare("SELECT COUNT(*) FROM persons WHERE id = ?");
|
|
} else {
|
|
$chk = $pdo->prepare("SELECT COUNT(*) FROM companies WHERE id = ?");
|
|
}
|
|
$chk->execute([$data['owner_id']]);
|
|
if ((int)$chk->fetchColumn() === 0) {
|
|
Response::error('归属对象不存在', 400);
|
|
}
|
|
|
|
// platform + account_id 唯一
|
|
$chk = $pdo->prepare("SELECT COUNT(*) FROM social_accounts WHERE platform = ? AND account_id = ?");
|
|
$chk->execute([$data['platform'], $data['account_id']]);
|
|
if ((int)$chk->fetchColumn() > 0) {
|
|
Response::error('该平台账号已存在', 400);
|
|
}
|
|
|
|
// 唯一性校验:社媒账号ID(同平台)/ 手机 / 邮箱 / 主页链接
|
|
$dup = checkSingleAccountDuplicate($pdo, $data['platform'] ?? '', $data['account_id'] ?? '', $data['profile_url'] ?? '');
|
|
if ($dup) Response::duplicate($dup);
|
|
|
|
[$sql, $params] = buildInsert($data);
|
|
$pdo->prepare("INSERT INTO social_accounts $sql")->execute($params);
|
|
$newId = (int)$pdo->lastInsertId();
|
|
|
|
// 写入商业属性
|
|
$attr = extractFields(MEDIA_ATTR_FIELDS);
|
|
if (!empty($attr)) {
|
|
$attr['social_account_id'] = $newId;
|
|
[$attrSql, $attrParams] = buildInsert($attr);
|
|
$pdo->prepare("INSERT INTO media_commercial_attributes $attrSql")->execute($attrParams);
|
|
}
|
|
|
|
// 完整度检查:媒体账号 + 商业属性
|
|
updateMediaIncomplete($pdo, $newId);
|
|
|
|
logCurrent('add', 'media', 'social_accounts', $newId, ['data' => $data, 'attr' => $attr]);
|
|
Response::success(['id' => $newId], '新增成功');
|