60 lines
2.3 KiB
PHP
60 lines
2.3 KiB
PHP
<?php
|
||
/**
|
||
* 人员导出接口(CSV) GET/POST /api/person/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('person');
|
||
|
||
// 仅导出勾选的人员
|
||
$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 = ['p.id IN (' . implode(',', array_fill(0, count($idList), '?')) . ')', 'p.is_active = 1'];
|
||
$params = $idList;
|
||
$whereSql = implode(' AND ', $where);
|
||
|
||
$pdo = DB::getInstance()->getPdo();
|
||
$stmt = $pdo->prepare(
|
||
"SELECT p.id, p.union_id, p.full_name, p.gender, p.nationality, p.id_type, p.id_number,
|
||
p.education, p.graduated_from, p.hometown, p.work_location, p.created_at,
|
||
(SELECT sa.account_id FROM social_accounts sa WHERE sa.owner_type='person' AND sa.owner_id=p.id AND sa.platform='phone' AND sa.is_active=1 LIMIT 1) AS phone,
|
||
(SELECT sa.account_id FROM social_accounts sa WHERE sa.owner_type='person' AND sa.owner_id=p.id AND sa.platform='email' AND sa.is_active=1 LIMIT 1) AS email
|
||
FROM persons p WHERE $whereSql ORDER BY p.id DESC"
|
||
);
|
||
$stmt->execute($params);
|
||
$list = $stmt->fetchAll();
|
||
|
||
logCurrent('export', 'person', 'persons', null, ['count' => count($list)]);
|
||
|
||
header('Content-Type: text/csv; charset=utf-8');
|
||
header('Content-Disposition: attachment; filename="persons_' . date('Ymd_His') . '.csv"');
|
||
echo "\xEF\xBB\xBF";
|
||
$out = fopen('php://output', 'w');
|
||
fputcsv($out, ['ID', 'union_id', '姓名', '性别', '国籍', '证件类型', '证件号', '学历', '毕业院校', '家乡', '工作所在地', '手机', '邮箱', '创建时间']);
|
||
foreach ($list as $r) {
|
||
fputcsv($out, [
|
||
$r['id'], $r['union_id'], $r['full_name'], $r['gender'], $r['nationality'],
|
||
$r['id_type'], $r['id_number'], $r['education'], $r['graduated_from'],
|
||
$r['hometown'], $r['work_location'], $r['phone'], $r['email'], $r['created_at'],
|
||
]);
|
||
}
|
||
fclose($out);
|
||
exit;
|