系统设置里多一块「🎨 我的界面主题」,点卡片即时预览、保存后全站生效,不用重新登录。 - lib/theme.php(新增):主题注册表 ticket/indigo/forest/wine/midnight, 分别是「复古票根(暖橙,默认)/ 靛蓝 / 松林 / 绛红 / 暗夜(深色)」。 currentThemeKey() 以 session 为准,session 里没有就从 admins.theme 补读一次 —— 升级前就已经登录着的会话也能立刻用上自己账号存的主题。库里是脏值时回落默认主题。 - css/themes.css(新增):5 套调色板,全部以 [data-theme="<key>"] 规则块给出, 每套 16 个变量一个不少。**必须排在 css/style.css 之后加载**:两者优先级相同 (都是 0,1,0),靠后出现者胜。另加 --primary-rgb("R, G, B" 三元组), 页面里的淡色底/光晕一律 rgba(var(--primary-rgb), α),所以加一套主题只需动 CSS, 页面与 JS 一行都不用改;每套还要声明 color-scheme,否则暗色主题下原生控件 (日期选择器、下拉、滚动条)仍是刺眼的白底。 - css/style.css::root 保留默认主题那一份作兜底;导航底色改走 --nav-bg, 滚动条改走变量,按钮光晕改走 --primary-rgb。 - lib/db.php / lib/auth.php:admins 增列 theme(幂等 ALTER,老库自动补,空值 = 默认主题); setAdminTheme() 校验 key、写库、同步 session。 - settings.php:主题卡片区。每张预览卡自己套一层 data-theme,用的就是那套主题真实的 变量,因此「预览所见」与「换上去之后」必然一致,不必再抄一份色值;另有一小段 JS 在点选时改 <html data-theme> 做即时预览(没脚本时选中后提交同样生效)。 - 14 个页面:<html> 上输出 data-theme,并在 style.css 之后引入 themes.css; 内联样式里写死的橙色 rgba(230,126,34,…) 全部改走 rgba(var(--primary-rgb),…); 影院/观影人色板选中态的描边由 #333 改为 var(--text-primary)(暗色主题下才看得见)。 - annual_report.php:报告本身仍是深色「海报」版式,但点缀色跟随主题; Chart.js 画在 canvas 上读不到 CSS 变量,改为运行时从 <html> 的计算样式取色。 顺手修的两个坑: 1. 注释里**不能**出现 PHP 起止标签。PHP 的结束标签在 // 注释里同样会关闭 PHP 模式, 之后若再出现短标签就会被真的执行 —— lib/theme.php 的用法说明里写了完整标签示例, 导致该文件被 require 时直接 Fatal error、整站白屏,而 php -l 查不出来(只看语法)。 现在部署脚本里加了一道静态断言专门挡这个。 2. themesCssLink() 的返回值末尾补一个换行:PHP 会吃掉结束标签后面那一个换行, 不补的话渲染结果里这一行会和下一行黏成一行。 回归:theme_e2e 111/111(含老库自动补列、脏值回落、5 个选项渲染、切换后落库+落 session+ 立刻生效、跨账号隔离、非法 key 与注入被拒、themes.css 完整性、:root 与 ticket 一致、 无残留写死主色、无悬空 CSS 变量);reviews_e2e 84/84、fetch_movie 76/76、storage 196/196。 A/B 48 个响应:把「新增 head 行 / data-theme 属性 / 橙色→变量」三类等价替换归一化、 并把设置页新增区块整块删掉之后,**其余部分逐字节一致,零残留差异**。 线上 PHP 7.4.30 / 8.1.7 双版本冒烟全通过(迁移、5 套主题切换、跨账号隔离均验证, 写测试后已把 admin #1 的主题原样还原、其它账号一字未动)。
334 lines
13 KiB
PHP
334 lines
13 KiB
PHP
<?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"<?= themeAttr() ?>>
|
|
<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">
|
|
<?= themesCssLink() ?>
|
|
<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(var(--primary-rgb),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>
|