feat: 完善电影记录系统源码与项目文档
功能模块: - 观影记录:列表(瀑布流)/ 添加 / 编辑 / 详情 / 删除,删除时同步清理图片文件 - 首页快捷搜索 + 高级搜索(片名、影院、观影人、日期区间组合筛选) - 影院管理:三段式信息 + 50 种预设配色,删除已引用影院时二次确认 - 观影人管理:头像裁剪上传 + 专属配色,已引用者禁止删除 - 图片处理:Cropper.js 裁剪票根/海报/观影照片/头像,多图上传,首字母 SVG 头像兜底 - OMDb 自动获取影片信息(fetch_movie.php 代理,返回 JSON) - CSV 批量导入:UTF-8/GBK 自动识别,影院去重,事务保护,逐行错误报告 - 统计仪表板:总览 + 按影院/观影人/年份/近 12 个月排行 - 年度报告:按账号关联的观影人视角生成,含 Chart.js 月度与星期分布图 - 多用户:账号增删改、账号与观影人绑定、修改自己的用户名与密码 - 登录鉴权:Session + password_hash,全站页面登录校验 文档: - 重写 README:功能特性、技术栈、目录结构、数据库结构、部署与使用指南、接口说明、CSV 格式、安全注意事项、已知限制 - 新增 .gitignore,排除 db/ 与 uploads/ 等运行时数据
This commit is contained in:
+333
@@ -0,0 +1,333 @@
|
||||
<?php
|
||||
// ============================================
|
||||
// 文件: import.php (修复版:观影人以空格分割,允许空日期)
|
||||
// ============================================
|
||||
|
||||
require_once 'config.php';
|
||||
checkLogin();
|
||||
|
||||
if (!function_exists('extractFileName')) {
|
||||
function extractFileName($input) {
|
||||
if (preg_match('/\(([^)]+)\)/', $input, $matches)) {
|
||||
$input = $matches[1];
|
||||
}
|
||||
$parts = explode('/', str_replace('\\', '/', $input));
|
||||
return trim(end($parts));
|
||||
}
|
||||
}
|
||||
|
||||
$admin = getCurrentAdmin();
|
||||
$result = [];
|
||||
$error = '';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
|
||||
$file = $_FILES['csv_file'];
|
||||
if ($file['error'] !== UPLOAD_ERR_OK) {
|
||||
$error = '文件上传失败,错误代码:' . $file['error'];
|
||||
} else {
|
||||
$csv_data = file_get_contents($file['tmp_name']);
|
||||
$encoding = mb_detect_encoding($csv_data, ['UTF-8', 'GBK', 'GB2312'], true);
|
||||
if ($encoding !== 'UTF-8') {
|
||||
$csv_data = mb_convert_encoding($csv_data, 'UTF-8', $encoding);
|
||||
}
|
||||
|
||||
$lines = explode("\n", $csv_data);
|
||||
array_shift($lines); // 跳过标题行
|
||||
$import_count = 0;
|
||||
$skip_count = 0;
|
||||
$errors = [];
|
||||
$db = getDB();
|
||||
$cinema_cache = [];
|
||||
|
||||
$db->beginTransaction();
|
||||
try {
|
||||
foreach ($lines as $line_num => $line) {
|
||||
$line = trim($line);
|
||||
if (empty($line)) continue;
|
||||
|
||||
$row = str_getcsv($line);
|
||||
if (count($row) < 9) {
|
||||
$errors[] = "第 " . ($line_num + 2) . " 行列数不足,跳过";
|
||||
$skip_count++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$title = trim($row[0]);
|
||||
$cinema_name = trim($row[2]);
|
||||
$watch_date = trim($row[3]);
|
||||
$viewer_names = trim($row[4]);
|
||||
$ticket_file = trim($row[5]);
|
||||
$poster_file = trim($row[6]);
|
||||
$remark = trim($row[7]);
|
||||
$photo_file = trim($row[8]);
|
||||
|
||||
if (empty($title)) {
|
||||
$errors[] = "第 " . ($line_num + 2) . " 行缺少电影名称,跳过";
|
||||
$skip_count++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// 处理日期(允许为空)
|
||||
$formatted_date = null;
|
||||
if (!empty($watch_date)) {
|
||||
$timestamp = strtotime($watch_date);
|
||||
if ($timestamp === false) {
|
||||
$errors[] = "第 " . ($line_num + 2) . " 行日期格式无效: $watch_date,跳过";
|
||||
$skip_count++;
|
||||
continue;
|
||||
}
|
||||
$formatted_date = date('Y-m-d', $timestamp);
|
||||
}
|
||||
|
||||
// 电影院去重
|
||||
$cinema_id = null;
|
||||
if (!empty($cinema_name)) {
|
||||
if (!isset($cinema_cache[$cinema_name])) {
|
||||
$stmt = $db->prepare("SELECT id FROM cinemas WHERE name = ?");
|
||||
$stmt->execute([$cinema_name]);
|
||||
$existing = $stmt->fetch();
|
||||
if ($existing) {
|
||||
$cinema_cache[$cinema_name] = $existing['id'];
|
||||
} else {
|
||||
$stmt = $db->prepare("INSERT INTO cinemas (place, name, address) VALUES (?, ?, ?)");
|
||||
$stmt->execute(['', $cinema_name, '']);
|
||||
$cinema_id = $db->lastInsertId();
|
||||
$cinema_cache[$cinema_name] = $cinema_id;
|
||||
}
|
||||
}
|
||||
$cinema_id = $cinema_cache[$cinema_name];
|
||||
}
|
||||
|
||||
$ticket_filename = extractFileName($ticket_file);
|
||||
$poster_filename = extractFileName($poster_file);
|
||||
$photo_filename = extractFileName($photo_file);
|
||||
|
||||
$ticket_path = !empty($ticket_filename) ? 'uploads/tickets/' . $ticket_filename : null;
|
||||
$poster_path = !empty($poster_filename) ? 'uploads/posters/' . $poster_filename : null;
|
||||
$photo_path = !empty($photo_filename) ? 'uploads/photos/' . $photo_filename : null;
|
||||
|
||||
$stmt = $db->prepare("
|
||||
INSERT INTO movies (title, info, remark, poster_path, ticket_path, photo_path, cinema_id, watch_date)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
");
|
||||
$stmt->execute([
|
||||
$title,
|
||||
'',
|
||||
$remark,
|
||||
$poster_path,
|
||||
$ticket_path,
|
||||
$photo_path,
|
||||
$cinema_id,
|
||||
$formatted_date
|
||||
]);
|
||||
$movie_id = $db->lastInsertId();
|
||||
|
||||
// 处理观影人:按空白字符分割(空格、制表符等)
|
||||
if (!empty($viewer_names)) {
|
||||
$viewers = preg_split('/\s+/', trim($viewer_names));
|
||||
$viewers = array_filter($viewers, function($v) { return !empty($v); });
|
||||
if (!empty($viewers)) {
|
||||
$stmt = $db->prepare("INSERT INTO viewers (movie_id, name) VALUES (?, ?)");
|
||||
foreach ($viewers as $name) {
|
||||
$stmt->execute([$movie_id, $name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$import_count++;
|
||||
}
|
||||
|
||||
$db->commit();
|
||||
$result = [
|
||||
'success' => true,
|
||||
'imported' => $import_count,
|
||||
'skipped' => $skip_count,
|
||||
'errors' => $errors
|
||||
];
|
||||
} catch (Exception $e) {
|
||||
$db->rollBack();
|
||||
$error = '导入过程中发生错误:' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>CSV导入 - 电影日志</title>
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
<style>
|
||||
.form-container {
|
||||
max-width: 720px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
}
|
||||
.card {
|
||||
background: var(--card-bg);
|
||||
border-radius: 16px;
|
||||
padding: 28px 32px;
|
||||
box-shadow: var(--shadow);
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
.card h2 {
|
||||
font-size: 22px;
|
||||
font-weight: 700;
|
||||
margin: 0 0 8px;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
.card .sub {
|
||||
color: var(--text-secondary);
|
||||
font-size: 14px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
.form-group {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
.form-group label {
|
||||
display: block;
|
||||
font-weight: 600;
|
||||
font-size: 14px;
|
||||
color: var(--text-primary);
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
.form-group input[type="file"] {
|
||||
padding: 8px;
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 8px;
|
||||
width: 100%;
|
||||
background: var(--bg-secondary);
|
||||
}
|
||||
.btn {
|
||||
padding: 10px 20px;
|
||||
border-radius: 8px;
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
transition: var(--transition);
|
||||
text-decoration: none;
|
||||
display: inline-block;
|
||||
}
|
||||
.btn-primary {
|
||||
background: linear-gradient(135deg, var(--primary), var(--primary-light));
|
||||
color: #fff;
|
||||
}
|
||||
.btn-primary:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 8px 28px rgba(230,126,34,0.35);
|
||||
}
|
||||
.btn-outline {
|
||||
background: transparent;
|
||||
color: var(--text-secondary);
|
||||
border: 1px solid var(--border-color);
|
||||
}
|
||||
.btn-outline:hover {
|
||||
background: var(--bg-secondary);
|
||||
}
|
||||
.error-box {
|
||||
background: rgba(231,76,60,0.08);
|
||||
border: 1px solid rgba(231,76,60,0.2);
|
||||
color: #e74c3c;
|
||||
padding: 10px 14px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 16px;
|
||||
font-size: 14px;
|
||||
}
|
||||
.success-box {
|
||||
background: rgba(39,174,96,0.08);
|
||||
border: 1px solid rgba(39,174,96,0.2);
|
||||
color: #27ae60;
|
||||
padding: 10px 14px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 16px;
|
||||
font-size: 14px;
|
||||
}
|
||||
.result-details {
|
||||
background: var(--bg-secondary);
|
||||
border-radius: 8px;
|
||||
padding: 12px 16px;
|
||||
margin-top: 12px;
|
||||
font-size: 14px;
|
||||
max-height: 300px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
.result-details .error-item {
|
||||
color: #e74c3c;
|
||||
}
|
||||
.format-hint {
|
||||
background: var(--bg-secondary);
|
||||
border-radius: 8px;
|
||||
padding: 12px 16px;
|
||||
font-size: 13px;
|
||||
color: var(--text-secondary);
|
||||
margin-top: 12px;
|
||||
}
|
||||
.format-hint code {
|
||||
background: var(--bg-primary);
|
||||
padding: 2px 6px;
|
||||
border-radius: 4px;
|
||||
}
|
||||
@media (max-width: 600px) {
|
||||
.card {
|
||||
padding: 20px 16px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<?php include 'nav.php'; ?>
|
||||
|
||||
<main class="main-content">
|
||||
<div class="form-container">
|
||||
<div class="card">
|
||||
<h2>📥 CSV批量导入</h2>
|
||||
<p class="sub">将整理好的电影数据一次性导入系统</p>
|
||||
|
||||
<?php if ($error): ?>
|
||||
<div class="error-box"><?= h($error) ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (isset($result['success']) && $result['success']): ?>
|
||||
<div class="success-box">
|
||||
✅ 导入完成!成功 <strong><?= $result['imported'] ?></strong> 条,跳过 <strong><?= $result['skipped'] ?></strong> 条。
|
||||
<?php if (!empty($result['errors'])): ?>
|
||||
<div class="result-details">
|
||||
<?php foreach ($result['errors'] as $err): ?>
|
||||
<div class="error-item">⚠️ <?= h($err) ?></div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<form method="POST" enctype="multipart/form-data">
|
||||
<div class="form-group">
|
||||
<label>选择CSV文件</label>
|
||||
<input type="file" name="csv_file" accept=".csv" required>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">开始导入</button>
|
||||
</form>
|
||||
|
||||
<div class="format-hint">
|
||||
<strong>📋 CSV格式要求:</strong><br>
|
||||
列顺序(共9列):<br>
|
||||
<code>电影名称, 地点(忽略), 电影院名称, 观影日期, 观影人(以空格分隔), 电影票图片路径, 海报图片路径, 备注, 合影图片路径</code><br>
|
||||
<br>
|
||||
<strong>观影人格式:</strong> 多个姓名之间用空格分隔,例如 <code>张三 李四 王五</code><br>
|
||||
<br>
|
||||
<strong>图片路径格式:</strong> 可以包含路径,系统会自动提取最后一个 <code>/</code> 后的文件名。<br>
|
||||
如果路径用括号括起来,如 <code>(电影票扫描件/电影票_阿甘正传.jpg)</code>,系统会自动提取括号内的内容再取文件名。<br>
|
||||
<br>
|
||||
<strong>示例行:</strong><br>
|
||||
<code>阿甘正传, , 万达影城, 2024-01-15, 张三 李四, (电影票/阿甘正传.jpg), (海报/阿甘正传.jpg), 经典励志片, (合影/阿甘正传.jpg)</code><br>
|
||||
<br>
|
||||
<strong>📌 注意:</strong> 导入前请将图片文件放入对应的 <code>uploads/tickets/</code>、<code>uploads/posters/</code>、<code>uploads/photos/</code> 目录,并确保文件名与CSV中提取的一致。<br>
|
||||
<strong>观影日期可为空</strong>,空日期的电影将排在最后。
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user