Files
superlink/api/marketing/edm_export.php
T

54 lines
1.7 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
/**
* EDM 邮箱列表导出接口(CSV) GET/POST /api/marketing/edm_export.php
* 入参同 edm.php filter:industry / province / city / keyword
*/
require_once __DIR__ . '/../common/Api.php';
$pdo = Api::boot(['module' => 'marketing']);
// 仅导出勾选的邮箱(emails 逗号分隔)
$emailsRaw = trim($_REQUEST['emails'] ?? '');
$emailList = [];
if ($emailsRaw !== '') {
foreach (explode(',', $emailsRaw) as $v) {
$v = trim($v);
if ($v !== '') {
$emailList[] = $v;
}
}
}
$emailList = array_unique($emailList);
if (!$emailList) {
Response::error('请先选择需要导出的邮箱', 1);
}
$in = implode(',', array_fill(0, count($emailList), '?'));
$where = ["sa.platform = 'email'", 'sa.is_active = 1', 'p.is_active = 1', "sa.account_id IN ($in)"];
$params = $emailList;
$whereSql = implode(' AND ', $where);
$pdo = DB::getInstance()->getPdo();
$stmt = $pdo->prepare(
"SELECT sa.account_id AS email, p.full_name, p.work_location, p.hometown
FROM social_accounts sa
LEFT JOIN persons p ON p.id = sa.owner_id
WHERE $whereSql
ORDER BY sa.id DESC"
);
$stmt->execute($params);
$list = $stmt->fetchAll();
logCurrent('export', 'marketing', 'social_accounts', null, ['count' => count($list), 'emails' => $emailList]);
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="edm_emails_' . date('Ymd_His') . '.csv"');
echo "\xEF\xBB\xBF";
$out = fopen('php://output', 'w');
fputcsv($out, ['邮箱', '姓名', '工作所在地', '家乡']);
foreach ($list as $r) {
fputcsv($out, [$r['email'], $r['full_name'], $r['work_location'], $r['hometown']]);
}
fclose($out);
exit;