Files
superlink/api/media/add.php
T
nanguaboss 71c6e8d9c1 v1.0.10
2026-08-03 00:07:01 +08:00

58 lines
2.0 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';
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);
}
[$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);
}
logCurrent('add', 'media', 'social_accounts', $newId, ['data' => $data, 'attr' => $attr]);
Response::success(['id' => $newId], '新增成功');