纯结构性重构,不改变任何功能。 - config.php 由 739 行缩减为 42 行,改为纯引导入口;新增 ROOT_DIR 常量 统一拼接绝对路径(lib/ 内模块的 __DIR__ 会指向 lib/,必须替换) - 新增 lib/ 五个模块 db.php / helpers.php / auth.php / movies.php / stats.php, 由 config.php 按 db → helpers → auth → movies → stats 顺序加载 - add.php、edit.php 各自约 80 行重复的 POST 处理改为调用 resolveCinemaId / resolvePersonIds / linkViewers / uploadImageFile / saveBase64Image / uploadMultiplePhotos / deleteStoredFile - cinemas.php、persons.php 中重复 4 次的颜色校验改用 normalizeHexColor() - toggleNewViewer / removeViewerRow / toggleNewCinema 下沉至 js/main.js, add.php 中的 removeViewer 统一更名为 removeViewerRow - 删除死代码:imageUrl()(全项目无调用);cinemas.php 配色数组中本就会被 array_slice 丢弃的 extra 颜色逻辑,简化为单条扁平 50 元素数组 - README 目录结构补充 lib/ 说明 验证方式:以便携 PHP 8.4 搭建 A/B 回归环境,播种固定数据后遍历 47 个 GET/POST 端点录制「状态码 + 响应体」。重构前后 42 个逐字节相同、2 个仅 上传文件名时间戳不同、3 个差异均为内联脚本下沉(已单独校验函数体逐字符 等价,剥离脚本后 HTML 标记完全一致)。24 个 PHP 文件语法检查全部通过。
646 lines
26 KiB
PHP
646 lines
26 KiB
PHP
<?php
|
||
// ============================================
|
||
// 文件: add.php (支持 OMDb 自动获取 + 票根裁剪)
|
||
// ============================================
|
||
|
||
require_once 'config.php';
|
||
checkLogin();
|
||
|
||
$admin = getCurrentAdmin();
|
||
$cinemas = getCinemas();
|
||
|
||
// 获取所有观影人
|
||
$db = getDB();
|
||
$stmt = $db->query("SELECT * FROM persons ORDER BY name");
|
||
$all_persons = $stmt->fetchAll();
|
||
|
||
$error = '';
|
||
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
$title = trim($_POST['title'] ?? '');
|
||
$info = trim($_POST['info'] ?? '');
|
||
$watch_date = $_POST['watch_date'] ?? '';
|
||
$cinema_id = $_POST['cinema_id'] ?? '';
|
||
$viewer_selects = $_POST['viewer_select'] ?? [];
|
||
$viewer_news = $_POST['viewers_new'] ?? [];
|
||
|
||
// 票根裁剪数据
|
||
$ticket_crop = $_POST['ticket_crop'] ?? '';
|
||
|
||
// 电影院:选择已有影院、新建影院或留空
|
||
$cinema_id = resolveCinemaId(
|
||
$cinema_id,
|
||
trim($_POST['new_cinema_place'] ?? ''),
|
||
trim($_POST['new_cinema_name'] ?? ''),
|
||
trim($_POST['new_cinema_address'] ?? '')
|
||
);
|
||
|
||
if (empty($title)) {
|
||
$error = '请输入电影名称';
|
||
} elseif (empty($watch_date)) {
|
||
$error = '请选择观影日期';
|
||
} else {
|
||
// ----- 海报:直接上传 -----
|
||
$poster_path = uploadImageFile('poster', POSTER_DIR, 'uploads/posters/') ?? '';
|
||
|
||
// ----- 票根:裁剪结果优先,其次原始上传 -----
|
||
$ticket_path = '';
|
||
if (!empty($ticket_crop)) {
|
||
$new_path = saveBase64Image($ticket_crop, TICKET_DIR, 'ticket');
|
||
if ($new_path) {
|
||
$ticket_path = $new_path;
|
||
} else {
|
||
$error = '票根裁剪保存失败,请重试';
|
||
}
|
||
} else {
|
||
$ticket_path = uploadImageFile('ticket', TICKET_DIR, 'uploads/tickets/') ?? '';
|
||
}
|
||
|
||
// 没有错误才继续
|
||
if (empty($error)) {
|
||
// 插入电影
|
||
$stmt = $db->prepare("
|
||
INSERT INTO movies (title, info, poster_path, ticket_path, cinema_id, watch_date)
|
||
VALUES (?, ?, ?, ?, ?, ?)
|
||
");
|
||
$stmt->execute([$title, $info, $poster_path, $ticket_path, $cinema_id, $watch_date]);
|
||
$movie_id = $db->lastInsertId();
|
||
|
||
// ---- 观影照片(多张)----
|
||
uploadMultiplePhotos('photos', $movie_id);
|
||
|
||
// ---- 一起看的人 ----
|
||
linkViewers($movie_id, resolvePersonIds($viewer_selects, $viewer_news));
|
||
|
||
header('Location: detail.php?id=' . $movie_id);
|
||
exit;
|
||
}
|
||
}
|
||
}
|
||
|
||
?>
|
||
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>添加电影 - 电影日志</title>
|
||
<link rel="stylesheet" href="css/style.css">
|
||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.13/cropper.min.css">
|
||
<style>
|
||
.form-container {
|
||
max-width: 720px;
|
||
margin: 0 auto;
|
||
padding: 20px;
|
||
}
|
||
.form-card {
|
||
background: var(--card-bg);
|
||
border-radius: 16px;
|
||
padding: 32px 36px;
|
||
box-shadow: var(--shadow);
|
||
}
|
||
.form-card h2 {
|
||
font-size: 24px;
|
||
font-weight: 700;
|
||
margin: 0 0 8px;
|
||
color: var(--text-primary);
|
||
}
|
||
.form-card .sub {
|
||
color: var(--text-secondary);
|
||
font-size: 14px;
|
||
margin-bottom: 24px;
|
||
}
|
||
.form-group {
|
||
margin-bottom: 20px;
|
||
}
|
||
.form-group label {
|
||
display: block;
|
||
font-weight: 600;
|
||
font-size: 14px;
|
||
color: var(--text-primary);
|
||
margin-bottom: 6px;
|
||
}
|
||
.form-group label .hint {
|
||
font-weight: 400;
|
||
color: var(--text-secondary);
|
||
font-size: 13px;
|
||
}
|
||
.form-group input, .form-group textarea, .form-group select {
|
||
width: 100%;
|
||
padding: 12px 16px;
|
||
border: 1px solid var(--border-color);
|
||
border-radius: 10px;
|
||
font-size: 15px;
|
||
font-family: inherit;
|
||
background: var(--input-bg);
|
||
color: var(--text-primary);
|
||
transition: border-color 0.2s, box-shadow 0.2s;
|
||
box-sizing: border-box;
|
||
}
|
||
.form-group input:focus, .form-group textarea:focus, .form-group select:focus {
|
||
outline: none;
|
||
border-color: var(--primary);
|
||
box-shadow: 0 0 0 4px rgba(230,126,34,0.12);
|
||
}
|
||
.form-group textarea {
|
||
min-height: 100px;
|
||
resize: vertical;
|
||
}
|
||
.form-group input[type="file"] {
|
||
padding: 10px 12px;
|
||
background: var(--bg-secondary);
|
||
border: 1px dashed var(--border-color);
|
||
}
|
||
.form-group input[type="file"]:hover {
|
||
border-color: var(--primary);
|
||
}
|
||
.form-row {
|
||
display: grid;
|
||
grid-template-columns: 1fr 1fr;
|
||
gap: 16px;
|
||
}
|
||
.form-hint {
|
||
font-size: 13px;
|
||
color: var(--text-secondary);
|
||
margin-top: 4px;
|
||
}
|
||
.viewer-input-group {
|
||
display: flex;
|
||
gap: 10px;
|
||
align-items: center;
|
||
margin-bottom: 8px;
|
||
}
|
||
.viewer-input-group select, .viewer-input-group input {
|
||
flex: 1;
|
||
}
|
||
.viewer-input-group select {
|
||
width: 60%;
|
||
}
|
||
.viewer-input-group input[type="text"] {
|
||
width: 40%;
|
||
}
|
||
.btn-remove-viewer {
|
||
background: none;
|
||
border: none;
|
||
color: #e74c3c;
|
||
font-size: 20px;
|
||
cursor: pointer;
|
||
padding: 0 8px;
|
||
line-height: 1;
|
||
transition: transform 0.2s;
|
||
}
|
||
.btn-remove-viewer:hover {
|
||
transform: scale(1.2);
|
||
}
|
||
.btn-add-viewer {
|
||
background: var(--bg-secondary);
|
||
border: 1px dashed var(--border-color);
|
||
border-radius: 8px;
|
||
padding: 8px 16px;
|
||
color: var(--text-secondary);
|
||
cursor: pointer;
|
||
font-size: 14px;
|
||
transition: all 0.2s;
|
||
width: 100%;
|
||
}
|
||
.btn-add-viewer:hover {
|
||
border-color: var(--primary);
|
||
color: var(--primary);
|
||
background: rgba(230,126,34,0.05);
|
||
}
|
||
.form-actions {
|
||
display: flex;
|
||
gap: 12px;
|
||
margin-top: 28px;
|
||
padding-top: 20px;
|
||
border-top: 1px solid var(--border-color);
|
||
}
|
||
.form-actions .btn {
|
||
padding: 12px 32px;
|
||
font-size: 16px;
|
||
}
|
||
.btn-secondary {
|
||
background: var(--bg-secondary);
|
||
color: var(--text-primary);
|
||
border: 1px solid var(--border-color);
|
||
}
|
||
.btn-secondary:hover {
|
||
background: var(--border-color);
|
||
}
|
||
.error-box {
|
||
background: rgba(231,76,60,0.08);
|
||
border: 1px solid rgba(231,76,60,0.2);
|
||
color: #e74c3c;
|
||
padding: 12px 16px;
|
||
border-radius: 10px;
|
||
margin-bottom: 20px;
|
||
font-size: 14px;
|
||
}
|
||
.cinema-new-group {
|
||
margin-top: 12px;
|
||
padding: 16px 18px;
|
||
background: var(--bg-secondary);
|
||
border-radius: 10px;
|
||
border: 1px solid var(--border-color);
|
||
}
|
||
.cinema-new-group .form-group:last-child {
|
||
margin-bottom: 0;
|
||
}
|
||
/* 裁剪预览 */
|
||
.image-preview {
|
||
max-width: 120px;
|
||
max-height: 120px;
|
||
border-radius: 4px;
|
||
border: 1px solid var(--border-color);
|
||
margin-top: 4px;
|
||
}
|
||
.crop-btn {
|
||
display: inline-block;
|
||
padding: 4px 12px;
|
||
background: #3498db;
|
||
color: #fff;
|
||
border: none;
|
||
border-radius: 4px;
|
||
font-size: 12px;
|
||
cursor: pointer;
|
||
margin-top: 4px;
|
||
}
|
||
.crop-btn:hover { background: #2980b9; }
|
||
.modal-overlay {
|
||
display: none;
|
||
position: fixed;
|
||
top: 0; left: 0; right: 0; bottom: 0;
|
||
background: rgba(0,0,0,0.6);
|
||
z-index: 999;
|
||
justify-content: center;
|
||
align-items: center;
|
||
}
|
||
.modal-overlay.active { display: flex; }
|
||
.modal-content {
|
||
background: var(--card-bg);
|
||
max-width: 600px;
|
||
width: 90%;
|
||
padding: 24px;
|
||
border-radius: 16px;
|
||
box-shadow: 0 20px 60px rgba(0,0,0,0.5);
|
||
max-height: 90vh;
|
||
overflow-y: auto;
|
||
}
|
||
.modal-header {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
margin-bottom: 16px;
|
||
}
|
||
.modal-header h3 { margin: 0; }
|
||
.modal-header .close-btn {
|
||
background: none;
|
||
border: none;
|
||
font-size: 28px;
|
||
cursor: pointer;
|
||
color: var(--text-secondary);
|
||
}
|
||
.modal-body img { max-width: 100%; display: block; }
|
||
.modal-footer {
|
||
margin-top: 16px;
|
||
display: flex;
|
||
gap: 12px;
|
||
justify-content: flex-end;
|
||
}
|
||
@media (max-width: 600px) {
|
||
.form-card { padding: 20px 16px; }
|
||
.form-row { grid-template-columns: 1fr; }
|
||
.form-actions { flex-direction: column; }
|
||
.form-actions .btn { width: 100%; text-align: center; }
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<?php include 'nav.php'; ?>
|
||
|
||
<main class="main-content">
|
||
<div class="form-container">
|
||
<div class="form-card">
|
||
<h2>🎬 添加新电影</h2>
|
||
<p class="sub">记录一部你曾看过的电影</p>
|
||
|
||
<?php if ($error): ?>
|
||
<div class="error-box"><?= h($error) ?></div>
|
||
<?php endif; ?>
|
||
|
||
<form method="POST" action="" enctype="multipart/form-data" id="addForm">
|
||
<!-- 电影名称 + 自动获取按钮 -->
|
||
<div class="form-group">
|
||
<label>电影名称 <span class="hint">*必填</span></label>
|
||
<div style="display: flex; gap: 10px;">
|
||
<input type="text" name="title" id="movieTitle" value="<?= h($_POST['title'] ?? '') ?>" placeholder="请输入电影名称" required style="flex:1;">
|
||
<button type="button" class="btn btn-primary" id="fetchBtn" style="white-space:nowrap;">📡 自动获取</button>
|
||
</div>
|
||
<div id="fetchStatus" style="font-size:13px;color:var(--text-secondary);margin-top:4px;"></div>
|
||
</div>
|
||
|
||
<!-- 电影信息 -->
|
||
<div class="form-group">
|
||
<label>电影信息</label>
|
||
<textarea name="info" id="movieInfo" placeholder="剧情简介、观影感受..."><?= h($_POST['info'] ?? '') ?></textarea>
|
||
</div>
|
||
|
||
<!-- 观影日期 -->
|
||
<div class="form-group">
|
||
<label>观影日期 <span class="hint">*必填</span></label>
|
||
<input type="date" name="watch_date" value="<?= h($_POST['watch_date'] ?? date('Y-m-d')) ?>" required>
|
||
</div>
|
||
|
||
<!-- 电影院 -->
|
||
<div class="form-group">
|
||
<label>电影院</label>
|
||
<select name="cinema_id" id="cinemaSelect" onchange="toggleNewCinema()">
|
||
<option value="">-- 不选择 --</option>
|
||
<?php foreach ($cinemas as $c): ?>
|
||
<option value="<?= $c['id'] ?>" <?= (isset($_POST['cinema_id']) && $_POST['cinema_id'] == $c['id']) ? 'selected' : '' ?>>
|
||
<?= h($c['place']) ?> · <?= h($c['name']) ?>
|
||
</option>
|
||
<?php endforeach; ?>
|
||
<option value="new" <?= (isset($_POST['cinema_id']) && $_POST['cinema_id'] === 'new') ? 'selected' : '' ?>>➕ 新建电影院</option>
|
||
</select>
|
||
</div>
|
||
|
||
<div class="cinema-new-group" id="newCinemaGroup" style="display: <?= (isset($_POST['cinema_id']) && $_POST['cinema_id'] === 'new') ? 'block' : 'none' ?>;">
|
||
<div class="form-group">
|
||
<label>所在地方 <span class="hint">如: 万达广场</span></label>
|
||
<input type="text" name="new_cinema_place" value="<?= h($_POST['new_cinema_place'] ?? '') ?>" placeholder="如: 万达广场">
|
||
</div>
|
||
<div class="form-group">
|
||
<label>电影院名称 <span class="hint">*必填</span></label>
|
||
<input type="text" name="new_cinema_name" value="<?= h($_POST['new_cinema_name'] ?? '') ?>" placeholder="如: 万达影城(IMAX店)">
|
||
</div>
|
||
<div class="form-group">
|
||
<label>详细地址</label>
|
||
<input type="text" name="new_cinema_address" value="<?= h($_POST['new_cinema_address'] ?? '') ?>" placeholder="详细地址">
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 观影人 -->
|
||
<div class="form-group">
|
||
<label>一起看的人</label>
|
||
<div id="viewerContainer">
|
||
<?php
|
||
$viewer_selects = $_POST['viewer_select'] ?? [];
|
||
$viewer_news = $_POST['viewers_new'] ?? [];
|
||
if (empty($viewer_selects)) {
|
||
$viewer_selects = [''];
|
||
}
|
||
foreach ($viewer_selects as $idx => $val):
|
||
$new_name = $viewer_news[$idx] ?? '';
|
||
?>
|
||
<div class="viewer-input-group">
|
||
<select name="viewer_select[]" onchange="toggleNewViewer(this)" class="viewer-select" style="flex:1;padding:8px;border:1px solid #ddd;border-radius:6px;">
|
||
<option value="">-- 选择 --</option>
|
||
<?php foreach ($all_persons as $p): ?>
|
||
<option value="<?= $p['id'] ?>" <?= ($val == $p['id']) ? 'selected' : '' ?>><?= h($p['name']) ?></option>
|
||
<?php endforeach; ?>
|
||
<option value="new" <?= ($val === 'new') ? 'selected' : '' ?>>➕ 新增</option>
|
||
</select>
|
||
<input type="text" name="viewers_new[]" placeholder="输入新姓名" style="flex:1;display:<?= ($val === 'new') ? 'block' : 'none' ?>;padding:8px;border:1px solid #ddd;border-radius:6px;" value="<?= h($new_name) ?>">
|
||
<?php if ($idx > 0): ?>
|
||
<button type="button" class="btn-remove-viewer" onclick="removeViewerRow(this)">×</button>
|
||
<?php endif; ?>
|
||
</div>
|
||
<?php endforeach; ?>
|
||
</div>
|
||
<button type="button" class="btn-add-viewer" onclick="addViewerRow()">➕ 添加一位</button>
|
||
<div class="form-hint">选择已有观影人,或选择“新增”后输入姓名</div>
|
||
</div>
|
||
|
||
<!-- 海报(直接上传 + 预览) -->
|
||
<div class="form-row">
|
||
<div class="form-group">
|
||
<label>电影海报</label>
|
||
<input type="file" name="poster" accept="image/*" id="posterInput">
|
||
<div id="posterPreviewWrap" style="display:none;margin-top:6px;">
|
||
<img id="posterPreview" style="max-width:150px;max-height:200px;border-radius:4px;border:1px solid var(--border-color);">
|
||
<br>
|
||
<span style="font-size:12px;color:var(--text-secondary);">预览(仅供参考,仍需上传图片文件)</span>
|
||
</div>
|
||
<div class="form-hint">推荐比例 2:3</div>
|
||
</div>
|
||
<!-- 票根(带裁剪) -->
|
||
<div class="form-group">
|
||
<label>电影票扫描件</label>
|
||
<input type="file" name="ticket" id="ticketInput" accept="image/*" onchange="handleTicketSelect(this)">
|
||
<img id="ticketPreview" class="image-preview" style="display:none;">
|
||
<input type="hidden" name="ticket_crop" id="ticket_crop">
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 观影照片(多张) -->
|
||
<div class="form-group">
|
||
<label>观影照片 <span class="hint">可多选</span></label>
|
||
<input type="file" name="photos[]" accept="image/*" multiple>
|
||
<div class="form-hint">按住 Ctrl(或 ⌘)可一次选择多张</div>
|
||
</div>
|
||
|
||
<div class="form-actions">
|
||
<button type="submit" class="btn btn-primary">✅ 保存电影</button>
|
||
<a href="index.php" class="btn btn-secondary">取消</a>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
</main>
|
||
|
||
<!-- 裁剪模态框 -->
|
||
<div class="modal-overlay" id="cropModal">
|
||
<div class="modal-content">
|
||
<div class="modal-header">
|
||
<h3>✂️ 裁剪票根</h3>
|
||
<button class="close-btn" onclick="closeCrop()">×</button>
|
||
</div>
|
||
<div class="modal-body">
|
||
<img id="cropImage" src="">
|
||
</div>
|
||
<div class="modal-footer">
|
||
<button class="btn btn-outline" onclick="closeCrop()">取消</button>
|
||
<button class="btn btn-primary" onclick="cropSave()">✅ 确认裁剪</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<script src="js/main.js"></script>
|
||
<script src="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.13/cropper.min.js"></script>
|
||
<script>
|
||
// ---------- 观影人 ----------
|
||
function addViewerRow() {
|
||
var container = document.getElementById('viewerContainer');
|
||
var div = document.createElement('div');
|
||
div.className = 'viewer-input-group';
|
||
var selectHtml = '<select name="viewer_select[]" onchange="toggleNewViewer(this)" class="viewer-select" style="flex:1;padding:8px;border:1px solid #ddd;border-radius:6px;">';
|
||
selectHtml += '<option value="">-- 选择 --</option>';
|
||
<?php foreach ($all_persons as $p): ?>
|
||
selectHtml += '<option value="<?= $p['id'] ?>"><?= h($p['name']) ?></option>';
|
||
<?php endforeach; ?>
|
||
selectHtml += '<option value="new">➕ 新增</option>';
|
||
selectHtml += '</select>';
|
||
var inputHtml = '<input type="text" name="viewers_new[]" placeholder="输入新姓名" style="flex:1;display:none;padding:8px;border:1px solid #ddd;border-radius:6px;">';
|
||
var removeBtn = '<button type="button" class="btn-remove-viewer" onclick="removeViewerRow(this)">×</button>';
|
||
div.innerHTML = selectHtml + inputHtml + removeBtn;
|
||
container.appendChild(div);
|
||
}
|
||
|
||
document.addEventListener('DOMContentLoaded', function() {
|
||
toggleNewCinema();
|
||
document.querySelectorAll('.viewer-input-group').forEach(function(group) {
|
||
var select = group.querySelector('select');
|
||
if (select) toggleNewViewer(select);
|
||
});
|
||
});
|
||
|
||
// ---------- 票根裁剪 ----------
|
||
let cropper = null;
|
||
let currentFileInput = null;
|
||
|
||
function handleTicketSelect(input) {
|
||
const file = input.files[0];
|
||
if (!file) return;
|
||
if (!file.type.startsWith('image/')) {
|
||
alert('请选择图片文件');
|
||
input.value = '';
|
||
return;
|
||
}
|
||
currentFileInput = input;
|
||
const reader = new FileReader();
|
||
reader.onload = function(e) {
|
||
const img = document.getElementById('cropImage');
|
||
img.src = e.target.result;
|
||
document.getElementById('cropModal').classList.add('active');
|
||
if (cropper) cropper.destroy();
|
||
setTimeout(function() {
|
||
cropper = new Cropper(img, {
|
||
aspectRatio: NaN,
|
||
viewMode: 1,
|
||
dragMode: 'move',
|
||
autoCropArea: 0.8,
|
||
responsive: true,
|
||
restore: false,
|
||
});
|
||
}, 150);
|
||
};
|
||
reader.readAsDataURL(file);
|
||
}
|
||
|
||
function closeCrop() {
|
||
document.getElementById('cropModal').classList.remove('active');
|
||
if (cropper) {
|
||
cropper.destroy();
|
||
cropper = null;
|
||
}
|
||
}
|
||
|
||
function cropSave() {
|
||
if (!cropper) {
|
||
alert('请先加载图片');
|
||
return;
|
||
}
|
||
try {
|
||
const canvas = cropper.getCroppedCanvas({
|
||
width: 600,
|
||
height: 600,
|
||
imageSmoothingQuality: 'high',
|
||
});
|
||
if (!canvas) {
|
||
alert('裁剪失败,请重试');
|
||
return;
|
||
}
|
||
const base64 = canvas.toDataURL('image/jpeg', 0.9);
|
||
document.getElementById('ticket_crop').value = base64;
|
||
const preview = document.getElementById('ticketPreview');
|
||
preview.src = base64;
|
||
preview.style.display = 'block';
|
||
// 清空文件输入,避免同时提交原始文件
|
||
document.getElementById('ticketInput').value = '';
|
||
closeCrop();
|
||
} catch (e) {
|
||
alert('裁剪失败:' + e.message);
|
||
}
|
||
}
|
||
|
||
// 点击模态框外部关闭
|
||
document.getElementById('cropModal').addEventListener('click', function(e) {
|
||
if (e.target === this) {
|
||
closeCrop();
|
||
}
|
||
});
|
||
|
||
// ---------- OMDb 自动获取 ----------
|
||
document.getElementById('fetchBtn').addEventListener('click', function() {
|
||
const title = document.getElementById('movieTitle').value.trim();
|
||
const status = document.getElementById('fetchStatus');
|
||
const btn = this;
|
||
|
||
if (!title) {
|
||
status.style.color = '#e74c3c';
|
||
status.textContent = '⚠️ 请先输入电影名称';
|
||
return;
|
||
}
|
||
|
||
btn.disabled = true;
|
||
status.style.color = 'var(--text-secondary)';
|
||
status.textContent = '⏳ 正在获取...';
|
||
|
||
fetch('fetch_movie.php?q=' + encodeURIComponent(title))
|
||
.then(res => res.json())
|
||
.then(data => {
|
||
if (data.error) {
|
||
status.style.color = '#e74c3c';
|
||
status.textContent = '❌ ' + data.error;
|
||
btn.disabled = false;
|
||
return;
|
||
}
|
||
|
||
// 填充电影名称(自动补全)
|
||
document.getElementById('movieTitle').value = data.title;
|
||
|
||
// 组装信息文本
|
||
let infoText = '';
|
||
if (data.year) infoText += '📅 年份:' + data.year + '\n';
|
||
if (data.genre) infoText += '🏷️ 类型:' + data.genre + '\n';
|
||
if (data.runtime) infoText += '⏱️ 片长:' + data.runtime + '\n';
|
||
if (data.director) infoText += '🎬 导演:' + data.director + '\n';
|
||
if (data.actors) infoText += '👤 主演:' + data.actors + '\n';
|
||
if (data.imdbRating && data.imdbRating !== 'N/A') infoText += '⭐ IMDb评分:' + data.imdbRating + '\n';
|
||
if (data.plot && data.plot !== 'N/A') infoText += '\n📝 ' + data.plot;
|
||
|
||
document.getElementById('movieInfo').value = infoText;
|
||
|
||
// 显示海报预览
|
||
if (data.poster && data.poster !== 'N/A') {
|
||
const preview = document.getElementById('posterPreview');
|
||
const wrap = document.getElementById('posterPreviewWrap');
|
||
preview.src = data.poster;
|
||
wrap.style.display = 'block';
|
||
status.textContent = '✅ 已获取信息,海报仅供参考,仍需上传图片文件';
|
||
} else {
|
||
document.getElementById('posterPreviewWrap').style.display = 'none';
|
||
status.textContent = '✅ 已获取基本信息(无海报)';
|
||
}
|
||
status.style.color = '#27ae60';
|
||
btn.disabled = false;
|
||
})
|
||
.catch(err => {
|
||
status.style.color = '#e74c3c';
|
||
status.textContent = '❌ 网络错误,请稍后重试';
|
||
btn.disabled = false;
|
||
console.error(err);
|
||
});
|
||
});
|
||
|
||
// 回车触发获取
|
||
document.getElementById('movieTitle').addEventListener('keypress', function(e) {
|
||
if (e.key === 'Enter') {
|
||
e.preventDefault();
|
||
document.getElementById('fetchBtn').click();
|
||
}
|
||
});
|
||
</script>
|
||
</body>
|
||
</html>
|