仅支持 CSV、XLS、XLSX 格式'; } else { $imported = 0; $errors = []; if ($ext === 'csv') { $handle = fopen($_FILES['file']['tmp_name'], 'r'); if ($handle) { $lineNo = 0; while (($row = fgetcsv($handle)) !== false) { $lineNo++; if ($lineNo === 1) continue; // 跳过表头 if (count($row) < 5) { $errors[] = "第{$lineNo}行: 列数不足(至少需要5列)"; continue; } $name = trim($row[0]); $batchNo = trim($row[1]); $type = (int)trim($row[2]); $code = trim($row[3]); $value = trim($row[4]); $expiredAt = isset($row[5]) ? trim($row[5]) : ''; $price1 = isset($row[6]) && $row[6] !== '' ? (float)trim($row[6]) : null; $price2 = isset($row[7]) && $row[7] !== '' ? (float)trim($row[7]) : null; if (!$name || !$batchNo || !$code) { $errors[] = "第{$lineNo}行: 兑换码名称、批次号、兑换码为必填项"; continue; } if ($type !== 1) { $errors[] = "第{$lineNo}行: 兑换码类型必须为1,当前为{$type}"; continue; } $stmt = $pdo->prepare('INSERT INTO redemption_codes (product_id, created_at, user_id, name, batch_no, type, code, value, expired_at, price_tier1, price_tier2) VALUES (?, NOW(), ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE name = VALUES(name), batch_no = VALUES(batch_no), type = VALUES(type), value = VALUES(value), expired_at = VALUES(expired_at), price_tier1 = VALUES(price_tier1), price_tier2 = VALUES(price_tier2)'); $stmt->execute([$pid, getCurrentUserId(), $name, $batchNo, $type, $code, $value, $expiredAt ?: null, $price1, $price2]); $imported++; } fclose($handle); } } elseif ($ext === 'xlsx') { $rows = readXlsx($_FILES['file']['tmp_name']); if ($rows === false) { $errors[] = '无法解析 XLSX 文件,请确认文件未损坏'; } else { foreach ($rows as $lineNo => $row) { if ($lineNo === 0) continue; // 跳过表头 $ln = $lineNo + 1; if (count($row) < 5) { $errors[] = "第{$ln}行: 列数不足(至少需要5列)"; continue; } $name = trim($row[0] ?? ''); $batchNo = trim($row[1] ?? ''); $type = (int)($row[2] ?? 0); $code = trim($row[3] ?? ''); $value = trim($row[4] ?? ''); $expiredAt = isset($row[5]) ? trim($row[5]) : ''; $price1 = isset($row[6]) && $row[6] !== '' ? (float)$row[6] : null; $price2 = isset($row[7]) && $row[7] !== '' ? (float)$row[7] : null; if (!$name || !$batchNo || !$code) { $errors[] = "第{$ln}行: 兑换码名称、批次号、兑换码为必填项"; continue; } if ($type !== 1) { $errors[] = "第{$ln}行: 兑换码类型必须为1,当前为{$type}"; continue; } $stmt = $pdo->prepare('INSERT INTO redemption_codes (product_id, created_at, user_id, name, batch_no, type, code, value, expired_at, price_tier1, price_tier2) VALUES (?, NOW(), ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE name = VALUES(name), batch_no = VALUES(batch_no), type = VALUES(type), value = VALUES(value), expired_at = VALUES(expired_at), price_tier1 = VALUES(price_tier1), price_tier2 = VALUES(price_tier2)'); $stmt->execute([$pid, getCurrentUserId(), $name, $batchNo, $type, $code, $value, $expiredAt ?: null, $price1, $price2]); $imported++; } } } else { $errors[] = 'XLS(旧版Excel格式)不支持,请另存为 XLSX 或 CSV 格式后导入'; } $flashMsg = ''; if ($imported > 0) { $flashMsg = '成功导入 ' . $imported . ' 条记录'; if (!empty($errors)) $flashMsg .= ',' . count($errors) . ' 条失败'; } if (!empty($errors) && $imported > 0) { $flashMsg .= "\n" . implode("\n", array_slice($errors, 0, 20)); $_SESSION['flash_msg'] = $flashMsg; $_SESSION['flash_type'] = 'success'; header('Location: code_manage.php'); exit; } elseif ($imported > 0) { $_SESSION['flash_msg'] = $flashMsg; $_SESSION['flash_type'] = 'success'; header('Location: code_manage.php'); exit; } elseif (!empty($errors)) { $msg = '
' . h(implode("\n", array_slice($errors, 0, 20))) . '| ID | 创建时间 | 名称 | 批次号 | 类型 | 兑换码 | 面值 | 状态 | 过期时间 | 用户 | 领取时间 | 出货价一档 | 出货价二挡 | 操作 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| = $c['id'] ?> | = formatDateTime($c['created_at']) ?> | = h($c['name']) ?> | = h($c['batch_no']) ?> | = h($c['type']) ?> | = h($c['code']) ?> | = h($c['value']) ?> | = claimStatusBadge((int)$c['status']) ?> | = formatDateTime($c['expired_at']) ?> | = h($c['claim_username'] ?? ($c['claim_user_id'] ? 'ID:' . $c['claim_user_id'] : '-')) ?> | = formatDateTime($c['claimed_at']) ?> | = $c['price_tier1'] !== null ? h($c['price_tier1']) : '-' ?> | = $c['price_tier2'] !== null ? h($c['price_tier2']) : '-' ?> | 删除 |
| 暂无数据 | |||||||||||||