66 lines
2.5 KiB
PHP
66 lines
2.5 KiB
PHP
<?php
|
||
/**
|
||
* 媒体数据导出接口(CSV) GET/POST /api/media/export.php
|
||
* 参数同 list.php
|
||
*/
|
||
require_once __DIR__ . '/../common/db.php';
|
||
require_once __DIR__ . '/../common/response.php';
|
||
require_once __DIR__ . '/../common/auth.php';
|
||
require_once __DIR__ . '/../common/logger.php';
|
||
|
||
checkPermission('media');
|
||
|
||
// 仅导出勾选的媒体账号
|
||
$idsRaw = trim($_REQUEST['ids'] ?? '');
|
||
$idList = [];
|
||
if ($idsRaw !== '') {
|
||
foreach (explode(',', $idsRaw) as $v) {
|
||
$v = (int)trim($v);
|
||
if ($v > 0) {
|
||
$idList[] = $v;
|
||
}
|
||
}
|
||
}
|
||
$idList = array_unique($idList);
|
||
if (!$idList) {
|
||
Response::error('请先选择需要导出的媒体数据', 1);
|
||
}
|
||
|
||
$where = ['sa.id IN (' . implode(',', array_fill(0, count($idList), '?')) . ')', 'sa.is_active = 1'];
|
||
$params = $idList;
|
||
$whereSql = implode(' AND ', $where);
|
||
|
||
$pdo = DB::getInstance()->getPdo();
|
||
$stmt = $pdo->prepare(
|
||
"SELECT sa.id, sa.owner_type, sa.owner_id, sa.platform, sa.account_id, sa.profile_url, sa.remark, sa.is_defult,
|
||
mca.account_level, mca.content_categories, mca.follower_count, mca.avg_read_count,
|
||
mca.certification_type, sa.created_at,
|
||
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"
|
||
);
|
||
$stmt->execute($params);
|
||
$list = $stmt->fetchAll();
|
||
|
||
logCurrent('export', 'media', 'social_accounts', null, ['count' => count($list)]);
|
||
|
||
header('Content-Type: text/csv; charset=utf-8');
|
||
header('Content-Disposition: attachment; filename="media_' . date('Ymd_His') . '.csv"');
|
||
echo "\xEF\xBB\xBF";
|
||
$out = fopen('php://output', 'w');
|
||
fputcsv($out, ['ID', '归属类型', '归属ID', '归属名称', '平台', '账号', '主页链接', '备注', '是否常用', '等级', '内容领域', '粉丝数', '平均阅读', '认证类型', '创建时间']);
|
||
foreach ($list as $r) {
|
||
fputcsv($out, [
|
||
$r['id'], $r['owner_type'], $r['owner_id'], $r['owner_name'], $r['platform'],
|
||
$r['account_id'], $r['profile_url'], $r['remark'], $r['is_defult'] ? '常用' : '备用', $r['account_level'],
|
||
$r['content_categories'], $r['follower_count'], $r['avg_read_count'],
|
||
$r['certification_type'], $r['created_at'],
|
||
]);
|
||
}
|
||
fclose($out);
|
||
exit;
|