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

78 lines
3.5 KiB
PHP

<?php
/**
* 媒体数据列表接口 GET/POST /api/media/list.php
* 参数:page / limit / keyword / platform / certification_type / account_level / owner_type / is_active
* 数据源:social_accounts JOIN media_commercial_attributes
*/
require_once __DIR__ . '/../common/db.php';
require_once __DIR__ . '/../common/response.php';
require_once __DIR__ . '/../common/auth.php';
checkPermission('media');
[$page, $limit] = pageParams();
$keyword = trim($_REQUEST['keyword'] ?? '');
$platform = trim($_REQUEST['platform'] ?? '');
$certType = trim($_REQUEST['certification_type'] ?? '');
$level = trim($_REQUEST['account_level'] ?? '');
$ownerType = trim($_REQUEST['owner_type'] ?? '');
$active = isset($_REQUEST['is_active']) && $_REQUEST['is_active'] !== '' ? (int)$_REQUEST['is_active'] : null;
$where = ['sa.is_active = 1'];
$params = [];
if ($active !== null) {
$where = ['sa.is_active = ?'];
$params = [$active];
}
if ($keyword !== '') {
$where[] = "(sa.account_id LIKE ? OR sa.profile_url LIKE ? OR sa.owner_id IN (
SELECT p.id FROM persons p WHERE p.full_name LIKE ?
UNION SELECT c.id FROM companies c WHERE c.display_name LIKE ?
))";
$like = "%$keyword%";
array_push($params, $like, $like, $like, $like);
}
if ($platform !== '') { $where[] = 'sa.platform = ?'; $params[] = $platform; }
if ($certType !== '') { $where[] = 'mca.certification_type = ?'; $params[] = $certType; }
if ($level !== '') { $where[] = 'mca.account_level = ?'; $params[] = $level; }
if ($ownerType !== '') { $where[] = 'sa.owner_type = ?'; $params[] = $ownerType; }
$whereSql = implode(' AND ', $where);
$pdo = DB::getInstance()->getPdo();
$stmt = $pdo->prepare(
"SELECT COUNT(*) FROM social_accounts sa
LEFT JOIN media_commercial_attributes mca ON mca.social_account_id = sa.id
WHERE $whereSql"
);
$stmt->execute($params);
$total = (int)$stmt->fetchColumn();
$offset = ($page - 1) * $limit;
$stmt = $pdo->prepare(
"SELECT sa.id, sa.owner_type, sa.owner_id, sa.platform, sa.account_id, sa.profile_url,
sa.remark, sa.is_primary, sa.is_active, sa.created_at,
mca.account_level, mca.content_categories, mca.follower_count, mca.avg_read_count,
mca.certification_type, mca.special_requirements, mca.media_remark,
CASE sa.owner_type
WHEN 'person' THEN (SELECT p.full_name FROM persons p WHERE p.id = sa.owner_id)
WHEN 'company' THEN (SELECT c.display_name FROM companies c WHERE c.id = sa.owner_id)
ELSE NULL END AS owner_name
FROM social_accounts sa
LEFT JOIN media_commercial_attributes mca ON mca.social_account_id = sa.id
WHERE $whereSql
ORDER BY sa.id DESC
LIMIT $limit OFFSET $offset"
);
$stmt->execute($params);
$list = $stmt->fetchAll();
// 供前端筛选下拉使用:平台/认证类型/等级 字典
$dict = [
'platforms' => $pdo->query("SELECT DISTINCT platform FROM social_accounts WHERE is_active = 1 ORDER BY platform")->fetchAll(PDO::FETCH_COLUMN),
'certification_types' => $pdo->query("SELECT DISTINCT certification_type FROM media_commercial_attributes WHERE certification_type IS NOT NULL AND certification_type <> '' ORDER BY certification_type")->fetchAll(PDO::FETCH_COLUMN),
'account_levels' => $pdo->query("SELECT DISTINCT account_level FROM media_commercial_attributes ORDER BY account_level")->fetchAll(PDO::FETCH_COLUMN),
];
Response::success(['list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit, 'dict' => $dict]);