Files
superlink/api/company/official_media_add.php
T

59 lines
2.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 企业官媒新增接口 POST /api/company/official_media_add.php
* 为指定公司快速新增一条官媒记录(social_accounts owner_type=company)
* 入参:company_id(必填)/ platform(平台)/ account_id(账号名称)/ profile_url(主页链接)/
* remark / is_active(1启用 0停用)/ is_defult(1常用)
*/
require_once __DIR__ . '/../common/db.php';
require_once __DIR__ . '/../common/response.php';
require_once __DIR__ . '/../common/auth.php';
require_once __DIR__ . '/../common/helpers.php';
require_once __DIR__ . '/../common/logger.php';
require_once __DIR__ . '/../common/completeness.php';
require_once __DIR__ . '/../common/duplicate_check.php';
checkAjax();
checkPermission('company');
$companyId = (int)($_POST['company_id'] ?? 0);
$platform = trim($_POST['platform'] ?? '');
$accountId = trim($_POST['account_id'] ?? '');
if ($companyId <= 0 || $platform === '' || $accountId === '') {
Response::error('平台与账号名称为必填项', 400);
}
$pdo = DB::getInstance()->getPdo();
$chk = $pdo->prepare("SELECT COUNT(*) FROM companies WHERE id = ?");
$chk->execute([$companyId]);
if ((int)$chk->fetchColumn() === 0) {
Response::error('企业不存在');
}
// 唯一性校验:社媒账号ID(同平台)/ 手机 / 邮箱 / 主页链接
$dup = checkSingleAccountDuplicate($pdo, $platform, $accountId, $_POST['profile_url'] ?? '');
if ($dup) Response::duplicate($dup);
$ins = $pdo->prepare(
"INSERT INTO social_accounts (owner_type, owner_id, platform, account_id, profile_url, remark, is_primary, is_defult, is_active)
VALUES ('company', ?, ?, ?, ?, ?, 0, ?, ?)"
);
$ins->execute([
$companyId,
$platform,
$accountId,
strOrNull($_POST['profile_url'] ?? null),
strOrNull($_POST['remark'] ?? null),
!empty($_POST['is_defult']) ? 1 : 0,
isset($_POST['is_active']) && (int)$_POST['is_active'] === 0 ? 0 : 1,
]);
$newId = (int)$pdo->lastInsertId();
// 完整度检查:新官媒账号 + 所属企业全部联系方式
updateIncomplete($pdo, 'social_accounts', $newId);
updateAccountsIncomplete($pdo, 'company', $companyId);
logCurrent('add', 'official_media', 'social_accounts', $newId, ['company_id' => $companyId, 'platform' => $platform, 'account_id' => $accountId]);
Response::success(['id' => $newId], '新增成功');