1, 'msg' => '目录不存在']); } $version = $this->generateFlexibleDirectoryHash($baseDir); $cacheKey = 'preview_flexible_dirs_' . $version; $dirList = cache($cacheKey); if (!$dirList) { $dirList = $this->scanFlexibleDirectories($baseDir, $baseRelativePath); cache($cacheKey, $dirList); } return json([ 'code' => 0, 'msg' => '获取成功', 'data' => $dirList ]); } private function scanFlexibleDirectories($baseDir, $baseRelativePath) { $dirs = []; $index = 1; $firstLevelDirs = glob($baseDir . '/*', GLOB_ONLYDIR); foreach ($firstLevelDirs as $level1Path) { $secondLevelDirs = glob($level1Path . '/*', GLOB_ONLYDIR); if ($secondLevelDirs) { foreach ($secondLevelDirs as $level2Path) { $dirs = array_merge($dirs, $this->processDir($level2Path, $baseDir, $baseRelativePath, $index)); } } else { $dirs = array_merge($dirs, $this->processDir($level1Path, $baseDir, $baseRelativePath, $index)); } } usort($dirs, function ($a, $b) { return $b['id'] - $a['id']; }); return $dirs; } private function processDir($fullPath, $baseDir, $baseRelativePath, &$index) { $result = []; $relativeDir = ltrim(str_replace($baseDir, '', $fullPath), '/'); $ctime = @filectime($fullPath) ?: time(); $imageFiles = glob($fullPath . '/*.{jpg,jpeg,png}', GLOB_BRACE); $originalImageCount = $imageFiles ? count($imageFiles) : 0; $img_count = Db::name('text_to_image') ->where('status', 1) ->where('custom_image_url', '<>', '') ->where('img_name', '<>', '') ->whereLike('old_image_url', $baseRelativePath . '/' . $relativeDir . '/%') ->count(); $queueLog = Db::name('image_task_log') ->whereLike('file_name', $baseRelativePath . '/' . $relativeDir . '/%') ->whereLike('log', '%处理中%') ->order('id', 'desc') ->find(); if ($img_count === 0 && !$queueLog && $originalImageCount === 0) { return []; } $result[] = [ 'id' => $index++, 'name' => basename($fullPath), 'ctime' => $ctime, 'ctime_text' => date('Y-m-d H:i:s', $ctime), 'old_img_count' => $originalImageCount, 'new_image_count' => $img_count, 'old_image_url' => $baseRelativePath . '/' . $relativeDir, 'new_image_url' => '/uploads/operate/ai/dall-e/', 'queueLog_id' => $queueLog['id'] ?? '', 'queueLog_task_id' => $queueLog['task_id'] ?? '', 'queueLog_model_name' => $queueLog['model_name'] ?? '', 'queueLog_model_name_status' => $queueLog ? 1 : 0, ]; return $result; } private function generateFlexibleDirectoryHash($baseDir) { $hash = ''; $dirPaths = []; $firstDirs = glob($baseDir . '/*', GLOB_ONLYDIR); foreach ($firstDirs as $dir1) { $subDirs = glob($dir1 . '/*', GLOB_ONLYDIR); if ($subDirs) { foreach ($subDirs as $sub) { $dirPaths[] = $sub; $hash .= basename($dir1) . '/' . basename($sub) . filemtime($sub); } } else { $dirPaths[] = $dir1; $hash .= basename($dir1) . filemtime($dir1); } } $baseRelativePath = 'uploads/operate/ai/Preview'; $queueStatusBits = []; foreach ($dirPaths as $fullPath) { $relativeDir = ltrim(str_replace($baseDir, '', $fullPath), '/'); $fileNameLike = $baseRelativePath . '/' . $relativeDir . '/%'; // 查询是否存在任何“处理中”的记录 $logs = Db::name('image_task_log') ->whereLike('file_name', $fileNameLike) ->whereLike('log', '%处理中%') ->select(); // 转换为布尔状态再转成位标记(0 或 1) $queueStatusBits[] = count($logs) > 0 ? '1' : '0'; // 可选:调试打印 // echo "
路径:{$fileNameLike} => 状态:" . (count($logs) > 0 ? '有处理中' : '无') . "";
}
// 队列状态位图拼接
$queueStatusHash = implode('', $queueStatusBits); // 如:'01001'
$hash .= '_QS_' . md5($queueStatusHash); // 状态稳定扰动,无需 time()
return md5($hash);
}
// private function generateFlexibleDirectoryHash($baseDir)
// {
// $hash = '';
// $firstDirs = glob($baseDir . '/*', GLOB_ONLYDIR);
// foreach ($firstDirs as $dir1) {
// $subDirs = glob($dir1 . '/*', GLOB_ONLYDIR);
// if ($subDirs) {
// foreach ($subDirs as $sub) {
// $hash .= basename($dir1) . '/' . basename($sub) . filemtime($sub);
// }
// } else {
// $hash .= basename($dir1) . filemtime($dir1);
// }
// }
// return md5($hash);
// }
// /**
// * 采用缓存机制
// * 获取原图目录及每个目录下的图片数量(优化版)
// */
// public function getPreviewSubDirs()
// {
// // 1. 设置基础路径
// $baseDir = rtrim(str_replace('\\', '/', ROOT_PATH), '/') . '/public/uploads/operate/ai/Preview';
// $baseRelativePath = 'uploads/operate/ai/Preview';
//
// // 2. 检查目录是否存在
// if (!is_dir($baseDir)) {
// return json(['code' => 1, 'msg' => '目录不存在']);
// }
//
// // 3. 获取目录最后修改时间作为缓存标识
// $cacheKey = 'preview_dirs_' . md5($baseDir);
// $lastModified = filemtime($baseDir);
// $cacheVersionKey = $cacheKey . '_version';
//
// // 4. 检查缓存版本是否匹配
// if (cache($cacheVersionKey) != $lastModified) {
// cache($cacheKey, null);
// cache($cacheVersionKey, $lastModified, 86400);
// }
//
// // 5. 尝试从缓存获取
// if (!$dirList = cache($cacheKey)) {
// // 6. 重新扫描目录
// $dirList = $this->scanDirectories($baseDir, $baseRelativePath);
// cache($cacheKey, $dirList, 86400); // 缓存1天
// }
//
//
// return json([
// 'code' => 0,
// 'msg' => '获取成功',
// 'data' => $dirList
// ]);
// }
//
// /**
// * 扫描目录结构
// */
// private function scanDirectories($baseDir, $baseRelativePath)
// {
//
// $dirs = [];
// $index = 1;
// $processedDirs = [];
//
// $scanDir = function ($dirPath, $relativePath) use (&$scanDir, &$dirs, &$index, &$processedDirs) {
// $items = @scandir($dirPath) ?: [];
// foreach ($items as $item) {
// if ($item === '.' || $item === '..') continue;
//
// $fullPath = $dirPath . '/' . $item;
// $relPath = $relativePath . '/' . $item;
//
// if (is_dir($fullPath)) {
// $dirKey = md5($fullPath);
// if (!isset($processedDirs[$dirKey])) {
// $processedDirs[$dirKey] = true;
// $scanDir($fullPath, $relPath);
// }
// } elseif (preg_match('/\.(jpg|jpeg|png)$/i', $item)) {
// $parentDir = dirname($fullPath);
// $relativeDir = dirname($relPath);
// $key = md5($parentDir);
//
// if (!isset($dirs[$key])) {
// $ctime = @filectime($parentDir) ?: time();
//
// // 数据库查询
// $hasData = Db::name('text_to_image')
// ->where('custom_image_url', '<>', '')
// ->where('img_name', '<>', '')
// ->whereLike('old_image_url', $relativeDir . '/%')
// ->where('status',1)
// ->cache(true, 300)
// ->count();
//
// $queue_logs = Db::name('queue_logs')
// ->whereLike('file_name', $relativeDir . '/%')
// ->group('file_name')
// ->order('id desc')
// ->find();
//
//
//
// $imageFiles = @glob($parentDir . '/*.{jpg,jpeg,png}', GLOB_BRACE);
// $imageCount = $imageFiles ? count($imageFiles) : 0;
//
// $dirs[$key] = [
// 'model_name'=> $queue_logs['model_name'],
// 'id' => $index++,
// 'name' => basename($parentDir),
// 'count' => $hasData,
// 'ctime' => $ctime,
// 'ctime_text' => date('Y-m-d H:i:s', $ctime),
// 'image_count' => $imageCount,
// 'new_image_url' => "/uploads/operate/ai/dall-e/",
// 'old_image_url' => $relativeDir
// ];
// }
// }
// }
// };
//
// $scanDir($baseDir, $baseRelativePath);
//
// // 按ID降序排序
// $dirList = array_values($dirs);
// usort($dirList, function ($a, $b) {
// return $b['id'] - $a['id'];
// });
//
// return $dirList;
// }
/**
* 获取指定目录所有图片(完全实时版本)
*/
public function getPreviewimg()
{
$page = (int)$this->request->param('page', 1);
$limit = (int)$this->request->param('limit', 50);
$status = $this->request->param('status', '');
$status_name = $this->request->param('status_name', '');
$relativePath = $this->request->param('path', '');
$basePath = ROOT_PATH . 'public/';
$fullPath = $basePath . $relativePath;
if (!is_dir($fullPath)) {
return json(['code' => 1, 'msg' => '原图目录不存在']);
}
// 构建缓存键与构建锁键(仅缓存文件系统信息)
$hash = md5($relativePath);
$cacheKey = "previewimg_fileinfo_{$hash}";
$lockKey = "previewimg_building_{$hash}";
$cacheExpire = 600; // 10分钟
$cachedFileInfo = cache($cacheKey);
if (!$cachedFileInfo) {
// 防止缓存"惊群效应"
if (!cache($lockKey)) {
cache($lockKey, 1, 60); // 1分钟构建锁
// 获取所有图片文件信息
$allImages = glob($fullPath . '/*.{jpg,jpeg,png}', GLOB_BRACE);
$fileInfoMap = [];
foreach ($allImages as $imgPath) {
$relative = str_replace('\\', '/', trim(str_replace($basePath, '', $imgPath), '/'));
$info = @getimagesize($imgPath);
$fileInfoMap[$relative] = [
'width' => $info[0] ?? 0,
'height' => $info[1] ?? 0,
'size_kb' => round(filesize($imgPath) / 1024, 2),
'created_time' => date('Y-m-d H:i:s', filectime($imgPath))
];
}
// 构建缓存数据(仅文件系统信息)
$cachedFileInfo = [];
foreach (array_keys($fileInfoMap) as $path) {
$cachedFileInfo[] = [
'path' => $path,
'info' => $fileInfoMap[$path]
];
}
// 设置缓存 + 删除构建锁
cache($cacheKey, $cachedFileInfo, $cacheExpire);
cache($lockKey, null);
} else {
// 等待缓存生成
$waitTime = 0;
while (!$cachedFileInfo && $waitTime < 10) {
sleep(1);
$waitTime++;
$cachedFileInfo = cache($cacheKey);
}
if (!$cachedFileInfo) {
return json(['code' => 2, 'msg' => '系统正忙,请稍后重试']);
}
}
}
// 获取所有需要实时查询的路径
$paths = array_column($cachedFileInfo, 'path');
// 实时查询数据库状态信息(单次批量查询)
$dbRecords = Db::name('text_to_image')
->whereIn('old_image_url', $paths)
->field('id as img_id, old_image_url, new_image_url, custom_image_url, chinese_description, english_description, img_name, status, status_name')
->select();
// 实时查询队列状态(单次批量查询)
$queueRecords = Db::name('image_task_log')
->where('mod_rq', null)
->whereIn('file_name', $paths)
->field('file_name, log')
->select();
// 实时查询same_count(稍后按需查询)
// 构建映射关系
$processedMap = [];
foreach ($dbRecords as $item) {
$key = str_replace('\\', '/', trim($item['old_image_url'], '/'));
$processedMap[$key] = $item;
}
$queueMap = [];
foreach ($queueRecords as $q) {
$key = str_replace('\\', '/', trim($q['file_name'], '/'));
$queueMap[$key] = $q['log'];
}
// 合并数据
$mergedData = [];
foreach ($cachedFileInfo as $data) {
$path = $data['path'];
$item = $processedMap[$path] ?? [];
$mergedData[] = [
'path' => $path,
'item' => $item,
'info' => $data['info'],
'dbStatus' => isset($item['status']) ? (int)$item['status'] : 0,
'dbStatusName' => $item['status_name'] ?? '',
'isProcessed' => !empty($item['img_name']) && !empty($item['custom_image_url']),
'queueStatus' => $queueMap[$path] ?? ''
];
}
// 筛选状态字段
$filtered = array_filter($mergedData, function ($data) use ($status, $status_name) {
if ($status !== '' && (int)$status !== $data['dbStatus']) return false;
if ($status_name !== '' && $status_name !== $data['dbStatusName']) return false;
return true;
});
// 分页处理
$total = count($filtered);
$paged = array_slice(array_values($filtered), ($page - 1) * $limit, $limit);
// 实时查询当前页的same_count(优化性能)
$pagedPaths = array_column($paged, 'path');
$sameCountMap = [];
if ($pagedPaths) {
$sameCountMap = Db::name('text_to_image')
->whereIn('old_image_url', $pagedPaths)
->where('new_image_url', '<>', '')
->group('old_image_url')
->column('count(*) as cnt', 'old_image_url');
}
// 构建最终结果
$result = [];
foreach ($paged as $i => $data) {
$path = $data['path'];
$item = $data['item'];
$info = $data['info'];
$result[] = [
'id' => ($page - 1) * $limit + $i + 1,
'path' => $path,
// 实时数据
'status' => $data['dbStatus'],
'status_name' => $data['dbStatusName'],
'same_count' => $sameCountMap[$path] ?? 0,
'is_processed' => $data['isProcessed'] ? 1 : 0,
'queue_status' => $data['queueStatus'],
'new_image_url' => $item['new_image_url'] ?? '',
'custom_image_url' => $item['custom_image_url'] ?? '',
'chinese_description' => $item['chinese_description'] ?? '',
'english_description' => $item['english_description'] ?? '',
'img_name' => $item['img_name'] ?? '',
// 来自缓存
'width' => $info['width'],
'height' => $info['height'],
'created_time' => $info['created_time']
];
}
return json([
'code' => 0,
'msg' => '获取成功',
'data' => $result,
'total' => $total,
'page' => $page,
'limit' => $limit
]);
}
/**
* 通过服务器中目录获取对应数据
*/
public function getlsit()
{
// 获取前端传入的图片路径参数
$params = $this->request->param('path', '');
// 查询数据库
$res = Db::name('text_to_image')
->field('id,chinese_description,english_description,new_image_url,custom_image_url,size,old_image_url,img_name,model,imgtoimg_url')
->where('old_image_url', $params)
->where('img_name', '<>', '')
->order('id desc')
->select();
return json(['code' => 0, 'msg' => '查询成功', 'data' => $res,'count'=>count($res)]);
}
/**
* 图片上传
*/
public function ImgUpload()
{
// 处理 CORS OPTIONS 预检请求
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
header('Access-Control-Max-Age: 86400');
exit(204);
}
// 实际请求必须返回 CORS 头
header('Access-Control-Allow-Origin: *');
// 获取上传的文件
$file = request()->file('image');
if ($file) {
// 指定目标目录(你想上传到的目录)
$targetPath = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'operate' . DS . 'ai' . DS . 'Preview';
// 若目录不存在则创建
if (!is_dir($targetPath)) {
mkdir($targetPath, 0755, true);
}
// 移动文件到指定目录,并验证大小/格式
$info = $file->validate([
'size' => 10485760, // 最大10MB
'ext' => 'jpg,png'
])->move($targetPath);
if ($info) {
$fileName = $info->getSaveName();
$imageUrl = '/uploads/operate/ai/Preview/' . str_replace('\\', '/', $fileName);
return json(['code' => 0, 'msg' => '成功', 'data' => ['url' => $imageUrl]]);
} else {
$res = $file->getError();
return json(['code' => 1, 'msg' => '失败', 'data' => $res]);
}
}
return json(['code' => 1, 'msg' => '没有文件上传', 'data' => null]);
}
/**
* 查询模版
*/
public function Template(){
$Template = Db::name("template")->where('ids',1)->find();
return json([
'code' => 0,
'msg' => '模版',
'data' => $Template
]);
}
/**
* 更新模版
*/
public function updatetemplate(){
if (Request::instance()->isPost() == false){
$this->error('非法请求');
}
$params = Request::instance()->post();
if (empty($params['textareaContent']) || empty($params['width']) || empty($params['height'])) {
return json(['code' => 1, 'msg' => '参数缺失']);
}
$Template = Db::name("template")
->where('ids', 1)
->update([
'english_content' => $params['english_content'], // 更新文生文模版内容
'content' => $params['textareaContent'], // 更新图生文模版内容
'width' => $params['width'], // 更新宽度
'height' => $params['height'], // 更新宽度
]);
if ($Template){
return json(['code' => 0, 'msg' => '成功']);
}else{
return json(['code' => 1, 'msg' => '失败']);
}
}
/**
* 打包图片
*/
public function packImagess()
{
try {
$params = $this->request->post();
$paths = $params['paths'] ?? [];
if (empty($paths) || !is_array($paths)) {
return json(['code' => 1, 'msg' => '路径参数不能为空或格式不正确']);
}
// 设置基础路径和压缩目录路径
$basePath = ROOT_PATH . 'public/';
$zipDir = $basePath . 'uploads/operate/ai/zip/';
if (!is_dir($zipDir)) {
mkdir($zipDir, 0755, true);
}
// 压缩包文件名及完整路径
$fileName = 'images_' . date('Ymd_His') . '.zip';
$zipPath = $zipDir . $fileName;
// 创建 Zip 文件
$zip = new \ZipArchive();
if ($zip->open($zipPath, \ZipArchive::CREATE) !== TRUE) {
return json(['code' => 1, 'msg' => '无法创建压缩包']);
}
// 添加文件到压缩包
$addCount = 0;
foreach ($paths as $relativePath) {
$relativePath = ltrim($relativePath, '/');
$fullPath = $basePath . $relativePath;
if (file_exists($fullPath)) {
$zip->addFile($fullPath, basename($fullPath)); // 仅保存文件名
$addCount++;
}
}
$zip->close();
if ($addCount === 0) {
return json(['code' => 1, 'msg' => '未找到有效图片,未生成压缩包']);
}
// 返回下载地址(注意路径与保存路径一致)
$downloadUrl = request()->domain() . '/uploads/operate/ai/zip/' . $fileName;
return json([
'code' => 0,
'msg' => '打包成功',
'download_url' => $downloadUrl
]);
} catch (\Exception $e) {
return json([
'code' => 1,
'msg' => '异常错误:' . $e->getMessage()
]);
}
}
}