feat(output_view): 导出页面重构 - 长图卡片化展示、缩略图增大、预览优化、纯图片页原图保存

- output_view.html: 长图改为固定高度卡片(70px),Blob URL查看,缩略图minmax(280px,1fr),
  竖图预览优先填充视口高度,下载功能完整保留
- phone-image.js: renderPureImageToCanvas()使用naturalWidth/naturalHeight保持原图分辨率,
  新增长图生成和保存功能
- Post.php: 新增outputView()方法提供导出页面渲染数据
- PhoneImage.php: 图片数据改为DB存储,新增saveLongImage()方法
- phone_image.html: 添加导出页面入口按钮
- 新增数据库迁移: post_output_file表添加image_data字段
This commit is contained in:
augushong
2026-05-14 23:22:19 +08:00
parent f60d1abff6
commit 10879a8037
6 changed files with 784 additions and 83 deletions

View File

@@ -379,6 +379,7 @@ class Post extends Common
View::assign('post', $model_post);
View::assign('layoutContentHtml', $layoutContentHtml);
View::assign('layoutConfig', $layoutConfig);
View::assign('lastOutputId', $postOutput ? $postOutput->id : 0);
return View::fetch();
}
@@ -431,6 +432,12 @@ class Post extends Common
$phoneImage->savePageImage($output->id, $index + 1, $pageData);
}
// 保存长图(如果有)
$longImage = $data['long_image'] ?? '';
if (!empty($longImage)) {
$phoneImage->saveLongImage($output->id, $longImage);
}
$phoneImage->completeOutput($output->id, count($pages));
return json(['code' => 0, 'msg' => '保存成功', 'data' => ['output_id' => $output->id]]);
@@ -580,6 +587,60 @@ class Post extends Common
}
}
/**
* 导出查看页面
*/
public function outputView($id)
{
$output = \app\model\PostOutput::find((int) $id);
if (empty($output)) {
$this->error('输出记录不存在');
}
$post = ModelPost::find($output->post_id);
if (empty($post)) {
$this->error('文章不存在');
}
$allFiles = \app\model\PostOutputFile::where('output_id', (int) $id)
->order('page', 'asc')
->select();
// 分离长图和分页
$longImage = null;
$pages = [];
foreach ($allFiles as $file) {
$item = [
'id' => $file->id,
'page' => $file->page,
'file_url' => $file->file_url,
'width' => $file->width,
'height' => $file->height,
'image_src' => '',
];
// 优先使用 image_data (data URI)
if (!empty($file->image_data)) {
$item['image_src'] = 'data:image/jpeg;base64,' . $file->image_data;
} elseif (!empty($file->file_url)) {
$item['image_src'] = $file->file_url;
}
if ($file->page == 0) {
$longImage = $item;
} else {
$pages[] = $item;
}
}
View::assign('output', $output);
View::assign('post', $post);
View::assign('longImage', $longImage);
View::assign('pages', $pages);
View::assign('downloadZipUrl', url('post/downloadPostOutputZip', ['id' => $id]));
return View::fetch('post/output_view');
}
/**
* 获取输出文件列表(AJAX)
*/
@@ -607,6 +668,7 @@ class Post extends Common
'file_size' => $file->file_size,
'width' => $file->width,
'height' => $file->height,
'image_data' => $file->image_data ?: null,
];
}

View File

@@ -70,40 +70,69 @@ class PhoneImage implements PostOutputManagerInterface
*/
public function savePageImage(int $outputId, int $page, string $imageData)
{
$imageData = str_replace('data:image/jpeg;base64,', '', $imageData);
$imageData = str_replace('data:image/png;base64,', '', $imageData);
$imageData = str_replace(' ', '+', $imageData);
$decoded = base64_decode($imageData);
// 保留原始base64用于存储
$rawBase64 = str_replace('data:image/jpeg;base64,', '', $imageData);
$rawBase64 = str_replace('data:image/png;base64,', '', $rawBase64);
$rawBase64 = str_replace(' ', '+', $rawBase64);
$decoded = base64_decode($rawBase64);
if ($decoded === false) {
return false;
}
$dateDir = date('Ymd');
$relativeDir = '/upload/post_output/' . $dateDir;
$fileName = $outputId . '_' . $page . '.jpg';
$relativePath = $relativeDir . '/' . $fileName;
$fullDir = App::getRootPath() . '/public' . $relativeDir;
$fullPath = $fullDir . '/' . $fileName;
if (!is_dir($fullDir)) {
mkdir($fullDir, 0777, true);
}
file_put_contents($fullPath, $decoded);
$imageInfo = getimagesize($fullPath);
$width = $imageInfo[0] ?? 0;
$height = $imageInfo[1] ?? 0;
$imageInfo = @getimagesizefromstring($decoded);
$width = is_array($imageInfo) ? $imageInfo[0] : 0;
$height = is_array($imageInfo) ? $imageInfo[1] : 0;
$fileRecord = PostOutputFile::create([
'output_id' => $outputId,
'page' => $page,
'file_path' => $relativePath,
'file_url' => $relativePath,
'file_path' => '',
'file_url' => '',
'file_size' => strlen($decoded),
'width' => $width,
'height' => $height,
'image_data' => $rawBase64,
]);
return $fileRecord;
}
/**
* 保存长图
* @param int $outputId 输出记录ID
* @param string $imageData base64编码的长图数据
* @return PostOutputFile|false
*/
public function saveLongImage(int $outputId, string $imageData)
{
$rawBase64 = str_replace('data:image/jpeg;base64,', '', $imageData);
$rawBase64 = str_replace('data:image/png;base64,', '', $rawBase64);
$rawBase64 = str_replace(' ', '+', $rawBase64);
$decoded = base64_decode($rawBase64);
if ($decoded === false) {
return false;
}
$imageInfo = @getimagesizefromstring($decoded);
$width = is_array($imageInfo) ? $imageInfo[0] : 0;
$height = is_array($imageInfo) ? $imageInfo[1] : 0;
// 先删除该output_id下已有的长图(page=0)
PostOutputFile::where('output_id', $outputId)
->where('page', 0)
->delete();
$fileRecord = PostOutputFile::create([
'output_id' => $outputId,
'page' => 0,
'file_path' => '',
'file_url' => '',
'file_size' => strlen($decoded),
'width' => $width,
'height' => $height,
'image_data' => $rawBase64,
]);
return $fileRecord;
@@ -137,21 +166,41 @@ class PhoneImage implements PostOutputManagerInterface
$zipFileName = 'post_output_' . $outputId . '.zip';
$zipPath = $tempDir . '/' . $zipFileName;
$tempFiles = [];
try {
$zip = new \ZipArchive();
if ($zip->open($zipPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) !== true) {
throw new \Exception('无法创建ZIP文件');
}
foreach ($files as $file) {
$pageName = str_pad((string) $file->page, 2, '0', STR_PAD_LEFT) . '.jpg';
if (!empty($file->image_data)) {
$decoded = base64_decode($file->image_data);
if ($decoded !== false) {
$tempFilePath = $tempDir . '/page_' . $file->page . '_' . $outputId . '.jpg';
file_put_contents($tempFilePath, $decoded);
$tempFiles[] = $tempFilePath;
$zip->addFile($tempFilePath, $pageName);
}
} else {
$filePath = App::getRootPath() . '/public' . $file->file_path;
if (file_exists($filePath)) {
$pageName = str_pad((string) $file->page, 2, '0', STR_PAD_LEFT) . '.jpg';
$zip->addFile($filePath, $pageName);
}
}
}
$zip->close();
} finally {
foreach ($tempFiles as $tempFile) {
if (file_exists($tempFile)) {
@unlink($tempFile);
}
}
}
return $zipPath;
}

View File

@@ -0,0 +1,14 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class AddImageDataToPostOutputFile extends Migrator
{
public function change()
{
$table = $this->table('post_output_file');
$table->addColumn(Column::make('image_data', 'text', ['limit' => \Phinx\Db\Adapter\MysqlAdapter::TEXT_LONG])->setComment('base64图片数据')->setNull(true));
$table->update();
}
}

View File

@@ -912,14 +912,15 @@ var PhoneImageEngine = (function () {
return;
}
var canvas = document.createElement('canvas');
canvas.width = width * 2; // scale:2 高质量
canvas.height = height * 2;
// 使用原图尺寸,保持原始清晰度
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
var ctx = canvas.getContext('2d');
// 白色背景
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 缩放绘制
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
// 按原图尺寸绘制,不缩放
ctx.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight);
deferred.resolve(canvas);
};
img.onerror = function () {
@@ -1179,6 +1180,7 @@ var PhoneImageEngine = (function () {
function saveImages(postId, saveConfig, onProgress) {
var deferred = $.Deferred();
// 第一步:生成分页图片和长图
capturePagesFromStaging().then(function (canvases) {
if (onProgress) onProgress(canvases.length, canvases.length, null);
var pagesData = [];
@@ -1186,19 +1188,27 @@ var PhoneImageEngine = (function () {
pagesData.push(canvases[i].toDataURL('image/jpeg', 0.92));
}
// 第二步:生成长图
return generateLongImageBase64().then(function(longBase64) {
return { pages: pagesData, longImage: longBase64 };
});
}).then(function(result) {
var pagesData = result.pages;
var longImageData = result.longImage;
// 检查数据总大小,防止超大文章导致请求失败
var totalSize = 0;
for (var i = 0; i < pagesData.length; i++) {
if (pagesData[i]) {
totalSize += pagesData[i].length;
}
if (pagesData[i]) totalSize += pagesData[i].length;
}
if (longImageData) totalSize += longImageData.length;
if (totalSize > 16 * 1024 * 1024) {
layer.msg('数据量过大超过16MB请减少内容或联系管理员');
deferred.reject('数据量过大超过16MB');
return;
}
// 第三步:发送到后端
$.ajax({
url: '/index.php/admin/post/savePostOutput',
type: 'POST',
@@ -1207,7 +1217,8 @@ var PhoneImageEngine = (function () {
output_type: 'phone_image',
config: saveConfig || config,
content_html: postData.content_html,
pages: pagesData
pages: pagesData,
long_image: longImageData
}),
contentType: 'application/json',
success: function (result) {
@@ -1218,6 +1229,20 @@ var PhoneImageEngine = (function () {
}
},
error: function (xhr) {
// 容错后端可能返回200 JSON + 500错误页面的混合响应
// 尝试从响应文本开头提取有效JSON
try {
var text = xhr.responseText || '';
var jsonEnd = text.indexOf('}');
if (jsonEnd > 0) {
var jsonStr = text.substring(0, jsonEnd + 1);
var parsed = JSON.parse(jsonStr);
if (parsed && parsed.code === 0) {
deferred.resolve(parsed.data || {});
return;
}
}
} catch (e) { /* 解析失败,走正常错误流程 */ }
deferred.reject('网络错误: ' + xhr.statusText);
}
});
@@ -1262,6 +1287,19 @@ var PhoneImageEngine = (function () {
}
},
error: function (xhr) {
// 容错后端可能返回200 JSON + 500错误页面的混合响应
try {
var text = xhr.responseText || '';
var jsonEnd = text.indexOf('}');
if (jsonEnd > 0) {
var jsonStr = text.substring(0, jsonEnd + 1);
var parsed = JSON.parse(jsonStr);
if (parsed && parsed.code === 0) {
deferred.resolve(parsed.data || {});
return;
}
}
} catch (e) { /* 解析失败,走正常错误流程 */ }
deferred.reject('网络错误: ' + xhr.statusText);
}
});
@@ -1283,6 +1321,68 @@ var PhoneImageEngine = (function () {
config.pageAlignments[pageNum] = align;
}
// ===== 长图base64生成不触发下载 =====
/**
* 生成长图base64不触发下载
* @returns {jQuery Deferred} resolves with base64 string or null
*/
function generateLongImageBase64() {
var deferred = $.Deferred();
var editorChildren = getEditorChildren();
if (editorChildren.length === 0) {
deferred.resolve(null);
return deferred.promise();
}
var $container = $('<div>').css({
width: '540px',
position: 'fixed',
left: '-9999px',
top: '0',
visibility: 'visible',
background: '#ffffff',
padding: '20px',
boxSizing: 'border-box',
fontSize: '14px',
lineHeight: '1.8'
});
for (var i = 0; i < editorChildren.length; i++) {
var el = editorChildren[i];
var isDivider = el.getAttribute('data-w-e-type') === 'divider' ||
el.tagName.toLowerCase() === 'hr';
if (!isDivider) {
$container.append(el.cloneNode(true));
}
}
$('body').append($container);
requestAnimationFrame(function () {
html2canvas($container[0], {
scale: 2,
useCORS: true,
backgroundColor: '#ffffff',
width: $container.outerWidth(),
height: $container[0].scrollHeight,
logging: false
}).then(function (canvas) {
$container.remove();
// 使用JPEG压缩减少体积
deferred.resolve(canvas.toDataURL('image/jpeg', 0.85));
}).catch(function (err) {
$container.remove();
// 长图生成失败不阻断主流程
console.warn('长图生成失败:', err);
deferred.resolve(null);
});
});
return deferred.promise();
}
// ===== 导出长图 =====
/**

View File

@@ -0,0 +1,498 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{$post.title} - 排版导出</title>
<link rel="stylesheet" href="/static/lib/layui/css/layui.css">
<style>
body { background: #f2f2f2; margin: 0; padding: 20px; }
.export-header {
background: #fff;
padding: 15px 20px;
border-radius: 4px;
margin-bottom: 20px;
display: flex;
align-items: center;
justify-content: space-between;
box-shadow: 0 1px 4px rgba(0,0,0,0.08);
}
.export-header h2 { margin: 0; font-size: 18px; color: #333; }
.export-header .header-info { color: #999; font-size: 13px; margin-top: 4px; }
.export-actions { display: flex; gap: 10px; align-items: center; }
.image-section {
background: #fff;
border-radius: 4px;
padding: 20px;
margin-bottom: 20px;
box-shadow: 0 1px 4px rgba(0,0,0,0.08);
}
.image-section h3 {
margin: 0 0 15px 0;
font-size: 16px;
color: #333;
border-bottom: 1px solid #eee;
padding-bottom: 10px;
}
/* 长图卡片 */
.long-image-card {
height: 70px;
display: flex;
align-items: center;
background: #fff;
border: 1px solid #e8e8e8;
border-radius: 4px;
padding: 0 16px;
box-sizing: border-box;
}
.long-image-card .card-info {
flex: 1;
display: flex;
align-items: center;
gap: 12px;
}
.long-image-card .card-icon {
width: 32px;
height: 32px;
background: #f0f0f0;
border-radius: 4px;
display: flex;
align-items: center;
justify-content: center;
font-size: 16px;
color: #999;
}
.long-image-card .card-label {
font-size: 14px;
font-weight: 600;
color: #333;
}
.long-image-card .card-dims {
font-size: 12px;
color: #999;
}
.long-image-card .card-actions {
display: flex;
gap: 8px;
}
/* 缩略图网格 */
.thumb-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 16px;
}
.thumb-item {
background: #fafafa;
border: 1px solid #e8e8e8;
border-radius: 4px;
overflow: hidden;
cursor: pointer;
transition: box-shadow 0.2s, transform 0.15s;
position: relative;
}
.thumb-item:hover {
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
transform: translateY(-2px);
}
.thumb-item img {
width: 100%;
height: auto;
display: block;
}
.thumb-bar {
display: flex;
align-items: center;
justify-content: space-between;
padding: 6px 10px;
background: #fff;
border-top: 1px solid #e8e8e8;
}
.thumb-bar .thumb-label {
font-size: 12px;
color: #666;
}
.thumb-bar .thumb-dl {
background: none;
border: none;
cursor: pointer;
padding: 2px 6px;
color: #1e9fff;
font-size: 12px;
border-radius: 2px;
}
.thumb-bar .thumb-dl:hover {
background: #e8f4ff;
}
/* 全屏预览 */
.preview-overlay {
display: none;
position: fixed;
top: 0; left: 0; right: 0; bottom: 0;
background: rgba(0,0,0,0.85);
z-index: 19999;
justify-content: center;
align-items: center;
flex-direction: column;
}
.preview-overlay.active { display: flex; }
.preview-topbar {
position: absolute;
top: 0; left: 0; right: 0;
height: 48px;
background: rgba(0,0,0,0.6);
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 20px;
color: #fff;
font-size: 14px;
z-index: 20001;
}
.preview-topbar button {
background: none;
border: 1px solid rgba(255,255,255,0.4);
color: #fff;
cursor: pointer;
padding: 4px 12px;
border-radius: 3px;
font-size: 13px;
}
.preview-topbar button:hover { background: rgba(255,255,255,0.15); }
.preview-topbar-actions { display: flex; gap: 8px; align-items: center; }
.preview-body {
max-width: 90vw;
max-height: calc(85vh - 48px);
height: calc(85vh - 48px);
margin-top: 48px;
display: flex;
justify-content: center;
align-items: center;
}
.preview-body img {
max-height: calc(85vh - 48px);
max-width: 90vw;
height: auto;
width: auto;
object-fit: contain;
border-radius: 2px;
box-shadow: 0 4px 24px rgba(0,0,0,0.4);
user-select: none;
}
.preview-body img.portrait-fill {
height: calc(85vh - 48px);
width: auto;
max-width: 90vw;
}
/* 底部缩略图导航 */
.preview-nav {
position: absolute;
bottom: 0; left: 0; right: 0;
background: rgba(0,0,0,0.6);
padding: 10px 20px;
display: flex;
gap: 8px;
overflow-x: auto;
justify-content: center;
z-index: 20001;
}
.preview-nav-item {
width: 48px;
height: 64px;
flex-shrink: 0;
border: 2px solid transparent;
border-radius: 3px;
overflow: hidden;
cursor: pointer;
opacity: 0.5;
transition: opacity 0.15s, border-color 0.15s;
}
.preview-nav-item.active {
border-color: #1e9fff;
opacity: 1;
}
.preview-nav-item:hover { opacity: 0.85; }
.preview-nav-item img {
width: 100%;
height: 100%;
object-fit: cover;
}
/* 左右切换箭头 */
.preview-arrow {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 44px;
height: 44px;
background: rgba(255,255,255,0.1);
border: 1px solid rgba(255,255,255,0.2);
border-radius: 50%;
color: #fff;
font-size: 22px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
z-index: 20001;
user-select: none;
transition: background 0.15s;
}
.preview-arrow:hover { background: rgba(255,255,255,0.25); }
.preview-arrow.prev { left: 20px; }
.preview-arrow.next { right: 20px; }
.export-footer {
text-align: center;
padding: 20px;
color: #999;
font-size: 13px;
}
</style>
</head>
<body>
<div class="export-header">
<div>
<h2>{$post.title} - 排版导出</h2>
<div class="header-info">
共 {$output.page_count} 页
| 生成时间:{$output.create_time|date='Y-m-d H:i:s'}
</div>
</div>
<div class="export-actions">
<a href="{:url('post/phoneImage', ['id' => $post.id])}" class="layui-btn layui-btn-sm layui-btn-primary">
<i class="layui-icon layui-icon-return"></i> 返回排版
</a>
<a href="{$downloadZipUrl|raw}" class="layui-btn layui-btn-sm layui-btn-normal">
<i class="layui-icon layui-icon-download-circle"></i> 下载全部 ZIP
</a>
</div>
</div>
{if condition="$longImage"}
<div class="image-section">
<h3>长图</h3>
<div class="long-image-card">
<div class="card-info">
<span class="card-icon"><i class="layui-icon layui-icon-picture"></i></span>
<span class="card-label">长图</span>
<span class="card-dims">{$longImage.width} x {$longImage.height} px</span>
</div>
<div class="card-actions">
<button class="layui-btn layui-btn-sm layui-btn-primary btn-view-long-image" data-src="{$longImage.image_src|raw}">查看原图</button>
<button class="layui-btn layui-btn-sm layui-btn-normal btn-download-image" data-src="{$longImage.image_src|raw}" data-filename="long_image.jpg">下载</button>
</div>
</div>
</div>
{/if}
{if condition="count($pages) > 0"}
<div class="image-section">
<h3>分页图片({$output.page_count} 页)</h3>
<div class="thumb-grid">
{volist name="pages" id="page"}
<div class="thumb-item" data-index="{$i}">
<img src="{$page.image_src|raw}" alt="第{$page.page}页">
<div class="thumb-bar">
<span class="thumb-label">第 {$page.page} 页</span>
<button class="thumb-dl btn-download-image"
data-src="{$page.image_src|raw}" data-filename="page_{$page.page}.jpg">
下载
</button>
</div>
</div>
{/volist}
</div>
</div>
{/if}
<div class="export-footer">
排版导出 - 共 {$output.page_count} 页
</div>
<!-- 全屏预览浮层 -->
<div class="preview-overlay" id="previewOverlay">
<div class="preview-topbar">
<span id="previewTitle">第 1 页</span>
<div class="preview-topbar-actions">
<button id="previewDownload"><i class="layui-icon layui-icon-download-circle"></i> 下载此页</button>
<button id="previewClose">关闭</button>
</div>
</div>
<div class="preview-arrow prev" id="previewPrev">&#8249;</div>
<div class="preview-arrow next" id="previewNext">&#8250;</div>
<div class="preview-body">
<img id="previewImg" src="" alt="">
</div>
<div class="preview-nav" id="previewNav"></div>
</div>
<script src="/static/lib/jquery/jquery-3.4.1.min.js"></script>
<script src="/static/lib/layui/layui.js"></script>
<script>
layui.use(['layer'], function () {
var layer = layui.layer;
// 收集所有图片数据(仅分页,不含长图)
var allImages = [];
{volist name="pages" id="p"}
allImages.push({ src: '{$p.image_src|raw}', label: '第 {$p.page} 页', filename: 'page_{$p.page}.jpg' });
{/volist}
var currentIndex = 0;
// ===== 查看长图(通过 Blob URL 在新标签打开) =====
function viewLongImage(src) {
try {
var parts = src.split(',');
var byteString = atob(parts[1]);
var mimeString = parts[0].split(':')[1].split(';')[0];
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
var blob = new Blob([ab], {type: mimeString});
var url = URL.createObjectURL(blob);
window.open(url, '_blank');
} catch(e) {
layer.msg('打开长图失败');
}
}
// 长图查看按钮
$(document).on('click', '.btn-view-long-image', function () {
viewLongImage($(this).data('src'));
});
// ===== 下载单张图片 =====
function downloadImage(src, filename) {
if (!src) { layer.msg('图片地址无效'); return; }
if (src.indexOf('data:') === 0) {
var link = document.createElement('a');
link.download = filename || 'image.jpg';
link.href = src;
link.click();
return;
}
var img = new Image();
img.crossOrigin = 'anonymous';
img.onload = function () {
var canvas = document.createElement('canvas');
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
canvas.getContext('2d').drawImage(img, 0, 0);
var link = document.createElement('a');
link.download = filename || 'image.jpg';
link.href = canvas.toDataURL('image/jpeg', 0.92);
link.click();
};
img.onerror = function () { window.open(src); };
img.src = src;
}
// 缩略图上的下载按钮
$(document).on('click', '.btn-download-image', function (e) {
e.stopPropagation();
downloadImage($(this).data('src'), $(this).data('filename'));
});
// ===== 全屏预览 =====
function showPreview(index) {
if (index < 0 || index >= allImages.length) return;
currentIndex = index;
var item = allImages[index];
var $img = $('#previewImg');
$img.attr('src', item.src);
// 重置样式
$img.removeClass('portrait-fill').css({ 'height': '', 'width': '' });
// 根据图片方向动态调整预览尺寸
var tempImg = new Image();
tempImg.onload = function () {
var isPortrait = tempImg.naturalHeight > tempImg.naturalWidth;
if (isPortrait) {
$img.addClass('portrait-fill');
}
};
tempImg.src = item.src;
$('#previewTitle').text(item.label + ' (' + (index + 1) + '/' + allImages.length + ')');
$('#previewDownload').data('src', item.src).data('filename', item.filename);
// 底部导航高亮
$('#previewNav .preview-nav-item').removeClass('active');
$('#previewNav .preview-nav-item').eq(index).addClass('active');
$('#previewOverlay').addClass('active');
document.body.style.overflow = 'hidden';
}
function closePreview() {
$('#previewOverlay').removeClass('active');
document.body.style.overflow = '';
}
// 点击缩略图打开预览
$(document).on('click', '.thumb-item', function (e) {
if ($(e.target).closest('.btn-download-image').length) return;
var idx = parseInt($(this).data('index'), 10);
showPreview(idx);
});
// 构建底部导航缩略图
var navHtml = '';
for (var i = 0; i < allImages.length; i++) {
navHtml += '<div class="preview-nav-item" data-nav-index="' + i + '"><img src="' + allImages[i].src + '"></div>';
}
$('#previewNav').html(navHtml);
// 底部导航点击
$(document).on('click', '.preview-nav-item', function () {
showPreview(parseInt($(this).data('nav-index'), 10));
});
// 关闭
$('#previewClose').on('click', closePreview);
$('#previewOverlay').on('click', function (e) {
if (e.target === this) closePreview();
});
// 上一页 / 下一页
$('#previewPrev').on('click', function (e) {
e.stopPropagation();
if (currentIndex > 0) showPreview(currentIndex - 1);
});
$('#previewNext').on('click', function (e) {
e.stopPropagation();
if (currentIndex < allImages.length - 1) showPreview(currentIndex + 1);
});
// 键盘快捷键
$(document).on('keydown.preview', function (e) {
if (!$('#previewOverlay').hasClass('active')) return;
if (e.key === 'Escape') closePreview();
if (e.key === 'ArrowLeft' || e.key === 'Left') { e.preventDefault(); $('#previewPrev').click(); }
if (e.key === 'ArrowRight' || e.key === 'Right') { e.preventDefault(); $('#previewNext').click(); }
});
// 预览中的下载
$('#previewDownload').on('click', function () {
downloadImage($(this).data('src'), $(this).data('filename'));
});
});
</script>
</body>
</html>

View File

@@ -138,18 +138,19 @@
<div class="page-header-right">
<button type="button" class="layui-btn layui-btn-sm layui-btn-primary" id="btn-settings"><i
class="layui-icon layui-icon-set"></i> 设置</button>
<button type="button" class="layui-btn layui-btn-sm layui-btn-warm" id="btn-render"><i
class="layui-icon layui-icon-refresh"></i> 生成</button>
<button type="button" class="layui-btn layui-btn-sm layui-btn-normal" id="btn-save"><i
class="layui-icon layui-icon-ok"></i> 保存</button>
<button type="button" class="layui-btn layui-btn-sm" id="btn-generate"><i
class="layui-icon layui-icon-picture"></i> 生成并保存</button>
<button type="button" class="layui-btn layui-btn-sm layui-btn-primary" id="btn-export-page"><i
class="layui-icon layui-icon-upload-circle"></i> 导出页面</button>
<div class="more-dropdown-wrapper" style="position:relative;display:inline-block;">
<button type="button" class="layui-btn layui-btn-sm layui-btn-primary" id="btn-more"><i
class="layui-icon layui-icon-more"></i></button>
<ul class="more-dropdown-menu" id="more-dropdown-menu" style="display:none;position:absolute;right:0;top:100%;margin-top:4px;background:#fff;border:1px solid #e8e8e8;border-radius:4px;box-shadow:0 2px 8px rgba(0,0,0,0.12);z-index:9999;min-width:130px;padding:4px 0;list-style:none;">
<li data-action="history" style="padding:8px 16px;cursor:pointer;font-size:13px;white-space:nowrap;">历史记录</li>
<li data-action="rerender" style="padding:8px 16px;cursor:pointer;font-size:13px;white-space:nowrap;">重新生成</li>
<li data-action="download" style="padding:8px 16px;cursor:pointer;font-size:13px;white-space:nowrap;">打包下载</li>
<li data-action="exportLong" style="padding:8px 16px;cursor:pointer;font-size:13px;white-space:nowrap;">导出长图</li>
</ul>
</div>
</div>
@@ -184,8 +185,9 @@
var form = layui.form;
var layer = layui.layer;
var lastOutputId = null;
var lastOutputId = <?php echo $lastOutputId ? (int) $lastOutputId : 'null'; ?>;
var downloadBaseUrl = '{:url("post/downloadPostOutputZip", ["id" => 0])}';
var outputViewUrl = '{:url("post/outputView", ["id" => 0])}';
var saveConfigUrl = '{:url("post/savePostOutputConfig")}';
var historyListUrl = '{:url("post/getOutputListJson", ["id" => $post->id])}';
var loadConfigUrl = '{:url("post/loadPostOutputConfig")}';
@@ -395,6 +397,21 @@
}, 300);
}
// ========== 生成按钮 ==========
$('#btn-render').click(function () {
doRender();
});
// ========== 导出页面按钮 ==========
$('#btn-export-page').click(function () {
if (!lastOutputId) {
layer.msg('请先生成并保存');
return;
}
var url = outputViewUrl.replace('id=0', 'id=' + lastOutputId);
window.open(url);
});
// ========== 更多下拉菜单纯JS实现 ==========
var $menu = $('#more-dropdown-menu');
$('#btn-more').on('click', function(e) {
@@ -408,15 +425,6 @@
case 'history':
$('#btn-history').trigger('click');
break;
case 'rerender':
doRender();
break;
case 'download':
$('#btn-download-hidden').trigger('click');
break;
case 'exportLong':
$('#btn-export-long-hidden').trigger('click');
break;
}
});
// 点击外部关闭
@@ -505,40 +513,10 @@
});
});
// ========== 打包下载(隐藏按钮,由更多菜单触发) ==========
var downloadBtn = $('<button type="button" id="btn-download-hidden" style="display:none;"></button>');
$('body').append(downloadBtn);
downloadBtn.on('click', function () {
if (!lastOutputId) {
layer.msg('请先生成并保存图片');
return;
}
var url = downloadBaseUrl.replace('/0/', '/' + lastOutputId + '/');
window.open(url);
});
// ========== 导出长图(隐藏按钮,由更多菜单触发) ==========
var exportLongBtn = $('<button type="button" id="btn-export-long-hidden" style="display:none;"></button>');
$('body').append(exportLongBtn);
exportLongBtn.on('click', function () {
PhoneImageEngine.exportLongImage().then(function () {
layer.msg('长图已导出');
}).catch(function (err) {
layer.msg('导出失败: ' + err);
});
});
// ========== 历史记录(隐藏按钮,由更多菜单和设置旁入口触发) ==========
var historyBtn = $('<button type="button" id="btn-history" style="display:none;"></button>');
$('body').append(historyBtn);
// 重新生成(隐藏按钮,由更多菜单触发)
var rerenderBtn = $('<button type="button" id="btn-rerender" style="display:none;"></button>');
$('body').append(rerenderBtn);
rerenderBtn.on('click', function () {
doRender();
});
historyBtn.on('click', function () {
var loadIdx = layer.load();
$.get(historyListUrl, function (res) {