1, 'msg' => '目录不存在']); } // 包含 DB 时间戳扰动的缓存键 $version = $this->generateFlexibleDirectoryHash($baseDir); $cacheKey = 'preview_flexible_dirs_' . $version; $dirList = cache($cacheKey); if (!$dirList) { $dirList = $this->scanFlexibleDirectories($baseDir, $baseRelativePath); cache($cacheKey, $dirList, 300); // 缓存 5 分钟(可调) } else { // 实时刷新 new_image_count(避免缓存值过期) foreach ($dirList as &$dir) { $dir['new_image_count'] = Db::name('text_to_image') ->where('status', 1) ->where('custom_image_url', '<>', '') ->where('img_name', '<>', '') ->whereLike('old_image_url', $dir['old_image_url'] . '/%') ->count(); } } 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); } /** * 获取指定目录所有图片(完全实时版本) */ 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' => '失败']); } } /** * 打包图片(支持对象结构中的 path 字段) */ public function packImagess() { try { $params = $this->request->post(); $paths = $params['paths'] ?? []; if (empty($paths) || !is_array($paths)) { return json(['code' => 1, 'msg' => '路径参数不能为空或格式不正确']); } // 提取所有合法的路径(支持字符串或对象中带 path 字段) $validPaths = []; foreach ($paths as $item) { if (is_string($item)) { $validPaths[] = $item; } elseif (is_array($item) && isset($item['path']) && is_string($item['path'])) { $validPaths[] = $item['path']; } } if (empty($validPaths)) { return json(['code' => 1, 'msg' => '没有有效的图片路径']); } // 设置基本路径和 zip 目录 $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 = new \ZipArchive(); if ($zip->open($zipPath, \ZipArchive::CREATE) !== TRUE) { return json(['code' => 1, 'msg' => '无法创建压缩包']); } $addCount = 0; foreach ($validPaths as $relativePath) { $relativePath = ltrim($relativePath, '/'); // 去除前导斜杠 $fullPath = $basePath . $relativePath; if (file_exists($fullPath)) { // 使用 basename 作为压缩包内的文件名(不保留路径结构) $zip->addFile($fullPath, basename($fullPath)); $addCount++; } } $zip->close(); if ($addCount === 0) { @unlink($zipPath); 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() ]); } } }