Facility.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\File;
  6. use think\Request;
  7. use RecursiveIteratorIterator;
  8. use RecursiveDirectoryIterator;
  9. class Facility extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. public function getPreviewSubDirs()
  14. {
  15. $baseDir = rtrim(str_replace('\\', '/', ROOT_PATH), '/') . '/public/uploads/operate/ai/Preview';
  16. $baseRelativePath = 'uploads/operate/ai/Preview';
  17. if (!is_dir($baseDir)) {
  18. return json(['code' => 1, 'msg' => '目录不存在']);
  19. }
  20. // 包含 DB 时间戳扰动的缓存键
  21. $version = $this->generateFlexibleDirectoryHash($baseDir);
  22. $cacheKey = 'preview_flexible_dirs_' . $version;
  23. $dirList = cache($cacheKey);
  24. if (!$dirList) {
  25. $dirList = $this->scanFlexibleDirectories($baseDir, $baseRelativePath);
  26. cache($cacheKey, $dirList, 300); // 缓存 5 分钟(可调)
  27. } else {
  28. // 实时刷新 new_image_count(避免缓存值过期)
  29. foreach ($dirList as &$dir) {
  30. $dir['new_image_count'] = Db::name('text_to_image')
  31. ->where('status', 1)
  32. ->where('custom_image_url', '<>', '')
  33. ->where('img_name', '<>', '')
  34. ->whereLike('old_image_url', $dir['old_image_url'] . '/%')
  35. ->count();
  36. }
  37. }
  38. return json([
  39. 'code' => 0,
  40. 'msg' => '获取成功',
  41. 'data' => $dirList
  42. ]);
  43. }
  44. private function scanFlexibleDirectories($baseDir, $baseRelativePath)
  45. {
  46. $dirs = [];
  47. $index = 1;
  48. $firstLevelDirs = glob($baseDir . '/*', GLOB_ONLYDIR);
  49. foreach ($firstLevelDirs as $level1Path) {
  50. $secondLevelDirs = glob($level1Path . '/*', GLOB_ONLYDIR);
  51. if ($secondLevelDirs) {
  52. foreach ($secondLevelDirs as $level2Path) {
  53. $dirs = array_merge($dirs, $this->processDir($level2Path, $baseDir, $baseRelativePath, $index));
  54. }
  55. } else {
  56. $dirs = array_merge($dirs, $this->processDir($level1Path, $baseDir, $baseRelativePath, $index));
  57. }
  58. }
  59. usort($dirs, function ($a, $b) {
  60. return $b['id'] - $a['id'];
  61. });
  62. return $dirs;
  63. }
  64. private function processDir($fullPath, $baseDir, $baseRelativePath, &$index)
  65. {
  66. $result = [];
  67. $relativeDir = ltrim(str_replace($baseDir, '', $fullPath), '/');
  68. $ctime = @filectime($fullPath) ?: time();
  69. $imageFiles = glob($fullPath . '/*.{jpg,jpeg,png}', GLOB_BRACE);
  70. $originalImageCount = $imageFiles ? count($imageFiles) : 0;
  71. $img_count = Db::name('text_to_image')
  72. ->where('status', 1)
  73. ->where('custom_image_url', '<>', '')
  74. ->where('img_name', '<>', '')
  75. ->whereLike('old_image_url', $baseRelativePath . '/' . $relativeDir . '/%')
  76. ->count();
  77. $queueLog = Db::name('image_task_log')
  78. ->whereLike('file_name', $baseRelativePath . '/' . $relativeDir . '/%')
  79. ->whereLike('log', '%处理中%')
  80. ->order('id', 'desc')
  81. ->find();
  82. if ($img_count === 0 && !$queueLog && $originalImageCount === 0) {
  83. return [];
  84. }
  85. $result[] = [
  86. 'id' => $index++,
  87. 'name' => basename($fullPath),
  88. 'ctime' => $ctime,
  89. 'ctime_text' => date('Y-m-d H:i:s', $ctime),
  90. 'old_img_count' => $originalImageCount,
  91. 'new_image_count' => $img_count,
  92. 'old_image_url' => $baseRelativePath . '/' . $relativeDir,
  93. 'new_image_url' => '/uploads/operate/ai/dall-e/',
  94. 'queueLog_id' => $queueLog['id'] ?? '',
  95. 'queueLog_task_id' => $queueLog['task_id'] ?? '',
  96. 'queueLog_model_name' => $queueLog['model_name'] ?? '',
  97. 'queueLog_model_name_status' => $queueLog ? 1 : 0,
  98. ];
  99. return $result;
  100. }
  101. private function generateFlexibleDirectoryHash($baseDir)
  102. {
  103. $hash = '';
  104. $dirPaths = [];
  105. $firstDirs = glob($baseDir . '/*', GLOB_ONLYDIR);
  106. foreach ($firstDirs as $dir1) {
  107. $subDirs = glob($dir1 . '/*', GLOB_ONLYDIR);
  108. if ($subDirs) {
  109. foreach ($subDirs as $sub) {
  110. $dirPaths[] = $sub;
  111. $hash .= basename($dir1) . '/' . basename($sub) . filemtime($sub);
  112. }
  113. } else {
  114. $dirPaths[] = $dir1;
  115. $hash .= basename($dir1) . filemtime($dir1);
  116. }
  117. }
  118. $baseRelativePath = 'uploads/operate/ai/Preview';
  119. $queueStatusBits = [];
  120. foreach ($dirPaths as $fullPath) {
  121. $relativeDir = ltrim(str_replace($baseDir, '', $fullPath), '/');
  122. $fileNameLike = $baseRelativePath . '/' . $relativeDir . '/%';
  123. // 查询是否存在任何“处理中”的记录
  124. $logs = Db::name('image_task_log')
  125. ->whereLike('file_name', $fileNameLike)
  126. ->whereLike('log', '%处理中%')
  127. ->select();
  128. // 转换为布尔状态再转成位标记(0 或 1)
  129. $queueStatusBits[] = count($logs) > 0 ? '1' : '0';
  130. // 可选:调试打印
  131. // echo "<pre>路径:{$fileNameLike} => 状态:" . (count($logs) > 0 ? '有处理中' : '无') . "</pre>";
  132. }
  133. // 队列状态位图拼接
  134. $queueStatusHash = implode('', $queueStatusBits); // 如:'01001'
  135. $hash .= '_QS_' . md5($queueStatusHash); // 状态稳定扰动,无需 time()
  136. return md5($hash);
  137. }
  138. /**
  139. * 获取指定目录所有图片(完全实时版本)
  140. */
  141. public function getPreviewimg()
  142. {
  143. $page = (int)$this->request->param('page', 1);
  144. $limit = (int)$this->request->param('limit', 50);
  145. $status = $this->request->param('status', '');
  146. $status_name = $this->request->param('status_name', '');
  147. $relativePath = $this->request->param('path', '');
  148. $basePath = ROOT_PATH . 'public/';
  149. $fullPath = $basePath . $relativePath;
  150. if (!is_dir($fullPath)) {
  151. return json(['code' => 1, 'msg' => '原图目录不存在']);
  152. }
  153. // 构建缓存键与构建锁键(仅缓存文件系统信息)
  154. $hash = md5($relativePath);
  155. $cacheKey = "previewimg_fileinfo_{$hash}";
  156. $lockKey = "previewimg_building_{$hash}";
  157. $cacheExpire = 600; // 10分钟
  158. $cachedFileInfo = cache($cacheKey);
  159. if (!$cachedFileInfo) {
  160. // 防止缓存"惊群效应"
  161. if (!cache($lockKey)) {
  162. cache($lockKey, 1, 60); // 1分钟构建锁
  163. // 获取所有图片文件信息
  164. $allImages = glob($fullPath . '/*.{jpg,jpeg,png}', GLOB_BRACE);
  165. $fileInfoMap = [];
  166. foreach ($allImages as $imgPath) {
  167. $relative = str_replace('\\', '/', trim(str_replace($basePath, '', $imgPath), '/'));
  168. $info = @getimagesize($imgPath);
  169. $fileInfoMap[$relative] = [
  170. 'width' => $info[0] ?? 0,
  171. 'height' => $info[1] ?? 0,
  172. 'size_kb' => round(filesize($imgPath) / 1024, 2),
  173. 'created_time' => date('Y-m-d H:i:s', filectime($imgPath))
  174. ];
  175. }
  176. // 构建缓存数据(仅文件系统信息)
  177. $cachedFileInfo = [];
  178. foreach (array_keys($fileInfoMap) as $path) {
  179. $cachedFileInfo[] = [
  180. 'path' => $path,
  181. 'info' => $fileInfoMap[$path]
  182. ];
  183. }
  184. // 设置缓存 + 删除构建锁
  185. cache($cacheKey, $cachedFileInfo, $cacheExpire);
  186. cache($lockKey, null);
  187. } else {
  188. // 等待缓存生成
  189. $waitTime = 0;
  190. while (!$cachedFileInfo && $waitTime < 10) {
  191. sleep(1);
  192. $waitTime++;
  193. $cachedFileInfo = cache($cacheKey);
  194. }
  195. if (!$cachedFileInfo) {
  196. return json(['code' => 2, 'msg' => '系统正忙,请稍后重试']);
  197. }
  198. }
  199. }
  200. // 获取所有需要实时查询的路径
  201. $paths = array_column($cachedFileInfo, 'path');
  202. // 实时查询数据库状态信息(单次批量查询)
  203. $dbRecords = Db::name('text_to_image')
  204. ->whereIn('old_image_url', $paths)
  205. ->field('id as img_id, old_image_url, new_image_url, custom_image_url, chinese_description, english_description, img_name, status, status_name')
  206. ->select();
  207. // 实时查询队列状态(单次批量查询)
  208. $queueRecords = Db::name('image_task_log')
  209. ->where('mod_rq', null)
  210. ->whereIn('file_name', $paths)
  211. ->field('file_name, log')
  212. ->select();
  213. // 实时查询same_count(稍后按需查询)
  214. // 构建映射关系
  215. $processedMap = [];
  216. foreach ($dbRecords as $item) {
  217. $key = str_replace('\\', '/', trim($item['old_image_url'], '/'));
  218. $processedMap[$key] = $item;
  219. }
  220. $queueMap = [];
  221. foreach ($queueRecords as $q) {
  222. $key = str_replace('\\', '/', trim($q['file_name'], '/'));
  223. $queueMap[$key] = $q['log'];
  224. }
  225. // 合并数据
  226. $mergedData = [];
  227. foreach ($cachedFileInfo as $data) {
  228. $path = $data['path'];
  229. $item = $processedMap[$path] ?? [];
  230. $mergedData[] = [
  231. 'path' => $path,
  232. 'item' => $item,
  233. 'info' => $data['info'],
  234. 'dbStatus' => isset($item['status']) ? (int)$item['status'] : 0,
  235. 'dbStatusName' => $item['status_name'] ?? '',
  236. 'isProcessed' => !empty($item['img_name']) && !empty($item['custom_image_url']),
  237. 'queueStatus' => $queueMap[$path] ?? ''
  238. ];
  239. }
  240. // 筛选状态字段
  241. $filtered = array_filter($mergedData, function ($data) use ($status, $status_name) {
  242. if ($status !== '' && (int)$status !== $data['dbStatus']) return false;
  243. if ($status_name !== '' && $status_name !== $data['dbStatusName']) return false;
  244. return true;
  245. });
  246. // 分页处理
  247. $total = count($filtered);
  248. $paged = array_slice(array_values($filtered), ($page - 1) * $limit, $limit);
  249. // 实时查询当前页的same_count(优化性能)
  250. $pagedPaths = array_column($paged, 'path');
  251. $sameCountMap = [];
  252. if ($pagedPaths) {
  253. $sameCountMap = Db::name('text_to_image')
  254. ->whereIn('old_image_url', $pagedPaths)
  255. ->where('new_image_url', '<>', '')
  256. ->group('old_image_url')
  257. ->column('count(*) as cnt', 'old_image_url');
  258. }
  259. // 构建最终结果
  260. $result = [];
  261. foreach ($paged as $i => $data) {
  262. $path = $data['path'];
  263. $item = $data['item'];
  264. $info = $data['info'];
  265. $result[] = [
  266. 'id' => ($page - 1) * $limit + $i + 1,
  267. 'path' => $path,
  268. // 实时数据
  269. 'status' => $data['dbStatus'],
  270. 'status_name' => $data['dbStatusName'],
  271. 'same_count' => $sameCountMap[$path] ?? 0,
  272. 'is_processed' => $data['isProcessed'] ? 1 : 0,
  273. 'queue_status' => $data['queueStatus'],
  274. 'new_image_url' => $item['new_image_url'] ?? '',
  275. 'custom_image_url' => $item['custom_image_url'] ?? '',
  276. 'chinese_description' => $item['chinese_description'] ?? '',
  277. 'english_description' => $item['english_description'] ?? '',
  278. 'img_name' => $item['img_name'] ?? '',
  279. // 来自缓存
  280. 'width' => $info['width'],
  281. 'height' => $info['height'],
  282. 'created_time' => $info['created_time']
  283. ];
  284. }
  285. return json([
  286. 'code' => 0,
  287. 'msg' => '获取成功',
  288. 'data' => $result,
  289. 'total' => $total,
  290. 'page' => $page,
  291. 'limit' => $limit
  292. ]);
  293. }
  294. /**
  295. * 通过服务器中目录获取对应数据
  296. */
  297. public function getlsit()
  298. {
  299. // 获取前端传入的图片路径参数
  300. $params = $this->request->param('path', '');
  301. // 查询数据库
  302. $res = Db::name('text_to_image')
  303. ->field('id,chinese_description,english_description,new_image_url,custom_image_url,size,old_image_url,img_name,model,imgtoimg_url')
  304. ->where('old_image_url', $params)
  305. ->where('img_name', '<>', '')
  306. ->order('id desc')
  307. ->select();
  308. return json(['code' => 0, 'msg' => '查询成功', 'data' => $res,'count'=>count($res)]);
  309. }
  310. /**
  311. * 图片上传
  312. */
  313. public function ImgUpload()
  314. {
  315. // 处理 CORS OPTIONS 预检请求
  316. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  317. header('Access-Control-Allow-Origin: *');
  318. header('Access-Control-Allow-Methods: POST, OPTIONS');
  319. header('Access-Control-Allow-Headers: Content-Type, Authorization');
  320. header('Access-Control-Max-Age: 86400');
  321. exit(204);
  322. }
  323. // 实际请求必须返回 CORS 头
  324. header('Access-Control-Allow-Origin: *');
  325. // 获取上传的文件
  326. $file = request()->file('image');
  327. if ($file) {
  328. // 指定目标目录(你想上传到的目录)
  329. $targetPath = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'operate' . DS . 'ai' . DS . 'Preview';
  330. // 若目录不存在则创建
  331. if (!is_dir($targetPath)) {
  332. mkdir($targetPath, 0755, true);
  333. }
  334. // 移动文件到指定目录,并验证大小/格式
  335. $info = $file->validate([
  336. 'size' => 10485760, // 最大10MB
  337. 'ext' => 'jpg,png'
  338. ])->move($targetPath);
  339. if ($info) {
  340. $fileName = $info->getSaveName();
  341. $imageUrl = '/uploads/operate/ai/Preview/' . str_replace('\\', '/', $fileName);
  342. return json(['code' => 0, 'msg' => '成功', 'data' => ['url' => $imageUrl]]);
  343. } else {
  344. $res = $file->getError();
  345. return json(['code' => 1, 'msg' => '失败', 'data' => $res]);
  346. }
  347. }
  348. return json(['code' => 1, 'msg' => '没有文件上传', 'data' => null]);
  349. }
  350. /**
  351. * 查询模版
  352. */
  353. public function Template(){
  354. $Template = Db::name("template")->where('ids',1)->find();
  355. return json([
  356. 'code' => 0,
  357. 'msg' => '模版',
  358. 'data' => $Template
  359. ]);
  360. }
  361. /**
  362. * 更新模版
  363. */
  364. public function updatetemplate(){
  365. if (Request::instance()->isPost() == false){
  366. $this->error('非法请求');
  367. }
  368. $params = Request::instance()->post();
  369. if (empty($params['textareaContent']) || empty($params['width']) || empty($params['height'])) {
  370. return json(['code' => 1, 'msg' => '参数缺失']);
  371. }
  372. $Template = Db::name("template")
  373. ->where('ids', 1)
  374. ->update([
  375. 'english_content' => $params['english_content'], // 更新文生文模版内容
  376. 'content' => $params['textareaContent'], // 更新图生文模版内容
  377. 'width' => $params['width'], // 更新宽度
  378. 'height' => $params['height'], // 更新宽度
  379. ]);
  380. if ($Template){
  381. return json(['code' => 0, 'msg' => '成功']);
  382. }else{
  383. return json(['code' => 1, 'msg' => '失败']);
  384. }
  385. }
  386. /**
  387. * 打包图片(支持对象结构中的 path 字段)
  388. */
  389. public function packImagess()
  390. {
  391. try {
  392. $params = $this->request->post();
  393. $paths = $params['paths'] ?? [];
  394. if (empty($paths) || !is_array($paths)) {
  395. return json(['code' => 1, 'msg' => '路径参数不能为空或格式不正确']);
  396. }
  397. // 提取所有合法的路径(支持字符串或对象中带 path 字段)
  398. $validPaths = [];
  399. foreach ($paths as $item) {
  400. if (is_string($item)) {
  401. $validPaths[] = $item;
  402. } elseif (is_array($item) && isset($item['path']) && is_string($item['path'])) {
  403. $validPaths[] = $item['path'];
  404. }
  405. }
  406. if (empty($validPaths)) {
  407. return json(['code' => 1, 'msg' => '没有有效的图片路径']);
  408. }
  409. // 设置基本路径和 zip 目录
  410. $basePath = ROOT_PATH . 'public/';
  411. $zipDir = $basePath . 'uploads/operate/ai/zip/';
  412. if (!is_dir($zipDir)) {
  413. mkdir($zipDir, 0755, true);
  414. }
  415. // 生成压缩文件路径
  416. $fileName = 'images_' . date('Ymd_His') . '.zip';
  417. $zipPath = $zipDir . $fileName;
  418. $zip = new \ZipArchive();
  419. if ($zip->open($zipPath, \ZipArchive::CREATE) !== TRUE) {
  420. return json(['code' => 1, 'msg' => '无法创建压缩包']);
  421. }
  422. $addCount = 0;
  423. foreach ($validPaths as $relativePath) {
  424. $relativePath = ltrim($relativePath, '/'); // 去除前导斜杠
  425. $fullPath = $basePath . $relativePath;
  426. if (file_exists($fullPath)) {
  427. // 使用 basename 作为压缩包内的文件名(不保留路径结构)
  428. $zip->addFile($fullPath, basename($fullPath));
  429. $addCount++;
  430. }
  431. }
  432. $zip->close();
  433. if ($addCount === 0) {
  434. @unlink($zipPath);
  435. return json(['code' => 1, 'msg' => '未找到有效图片文件,未生成压缩包']);
  436. }
  437. $downloadUrl = request()->domain() . '/uploads/operate/ai/zip/' . $fileName;
  438. return json([
  439. 'code' => 0,
  440. 'msg' => '打包成功',
  441. 'download_url' => $downloadUrl
  442. ]);
  443. } catch (\Exception $e) {
  444. return json([
  445. 'code' => 1,
  446. 'msg' => '异常错误:' . $e->getMessage()
  447. ]);
  448. }
  449. }
  450. }