Facility.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Request;
  6. use RecursiveIteratorIterator;
  7. use RecursiveDirectoryIterator;
  8. class Facility extends Api
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. /**
  13. * 获取一张原目录图片对应明细数据
  14. */
  15. public function getlsit()
  16. {
  17. // 获取前端传入的图片路径参数
  18. $params = $this->request->param('path', '');
  19. // 查询数据库
  20. $res = Db::name('text_to_image')
  21. ->field('id,chinese_description,english_description,new_image_url,custom_image_url,size,old_image_url,img_name,model,imgtoimg_url')
  22. ->where('old_image_url', $params)
  23. ->where('img_name', '<>', '')
  24. ->order('id desc')
  25. ->select();
  26. return json(['code' => 0, 'msg' => '查询成功', 'data' => $res,'count'=>count($res)]);
  27. }
  28. /**
  29. * 缓存及时
  30. * 获取指定目录所有图片
  31. */
  32. public function getPreviewimg()
  33. {
  34. $page = (int)$this->request->param('page', 1);
  35. $limit = (int)$this->request->param('limit', 50);
  36. $status = $this->request->param('status', '');
  37. $status_name = $this->request->param('status_name', '');
  38. $relativePath = $this->request->param('path', '');
  39. $basePath = ROOT_PATH . 'public/';
  40. $fullPath = $basePath . $relativePath;
  41. if (!is_dir($fullPath)) {
  42. return json(['code' => 1, 'msg' => '原图目录不存在']);
  43. }
  44. // 构建缓存键与构建锁键
  45. $hash = md5($relativePath);
  46. $cacheKey = "previewimg_full_{$hash}";
  47. $lockKey = "previewimg_building_{$hash}";
  48. $cacheExpire = 600; // 10分钟
  49. $fullData = cache($cacheKey);
  50. if (!$fullData) {
  51. // 防止缓存“惊群效应”:只允许一个任务生成缓存,其它等待
  52. if (!cache($lockKey)) {
  53. cache($lockKey, 1, 60); // 1分钟构建锁
  54. $allImages = glob($fullPath . '/*.{jpg,jpeg,png}', GLOB_BRACE);
  55. $imageInfoMap = [];
  56. foreach ($allImages as $imgPath) {
  57. $relative = str_replace('\\', '/', trim(str_replace($basePath, '', $imgPath), '/'));
  58. $info = @getimagesize($imgPath);
  59. $imageInfoMap[$relative] = [
  60. 'width' => $info[0] ?? 0,
  61. 'height' => $info[1] ?? 0,
  62. 'size_kb' => round(filesize($imgPath) / 1024, 2),
  63. 'created_time' => date('Y-m-d H:i:s', filectime($imgPath))
  64. ];
  65. }
  66. $relativeImages = array_keys($imageInfoMap);
  67. // 批量查库
  68. $dbRecords = Db::name('text_to_image')
  69. ->whereIn('old_image_url', $relativeImages)
  70. ->field('id as img_id, old_image_url, new_image_url, custom_image_url, chinese_description, english_description, img_name, status, status_name')
  71. ->select();
  72. $queueRecords = Db::name('image_task_log')
  73. ->where('mod_rq', null)
  74. ->field('file_name, log')
  75. ->select();
  76. $queueMap = [];
  77. foreach ($queueRecords as $q) {
  78. $key = str_replace('\\', '/', trim($q['file_name'], '/'));
  79. $queueMap[$key] = $q['log'];
  80. }
  81. $processedMap = [];
  82. foreach ($dbRecords as $item) {
  83. $key = str_replace('\\', '/', trim($item['old_image_url'], '/'));
  84. $processedMap[$key] = $item;
  85. }
  86. $fullData = [];
  87. foreach ($relativeImages as $path) {
  88. $item = $processedMap[$path] ?? [];
  89. $info = $imageInfoMap[$path];
  90. $fullData[] = [
  91. 'path' => $path,
  92. 'item' => $item,
  93. 'info' => $info,
  94. 'dbStatus' => isset($item['status']) ? (int)$item['status'] : 0,
  95. 'dbStatusName' => $item['status_name'] ?? '',
  96. 'isProcessed' => !empty($item['img_name']) && !empty($item['custom_image_url']),
  97. 'queueStatus' => $queueMap[$path] ?? ''
  98. ];
  99. }
  100. // 设置缓存 + 删除构建锁
  101. cache($cacheKey, $fullData, $cacheExpire);
  102. cache($lockKey, null);
  103. } else {
  104. // 如果有构建锁,等待缓存生成后再读
  105. $waitTime = 0;
  106. while (!$fullData && $waitTime < 10) {
  107. sleep(1);
  108. $waitTime++;
  109. $fullData = cache($cacheKey);
  110. }
  111. if (!$fullData) {
  112. return json(['code' => 2, 'msg' => '系统正忙,请稍后重试']);
  113. }
  114. }
  115. }
  116. // 筛选状态字段
  117. $filtered = array_filter($fullData, function ($data) use ($status, $status_name) {
  118. if ($status !== '' && (int)$status !== $data['dbStatus']) return false;
  119. if ($status_name !== '' && $status_name !== $data['dbStatusName']) return false;
  120. return true;
  121. });
  122. // 分页处理
  123. $total = count($filtered);
  124. $paged = array_slice(array_values($filtered), ($page - 1) * $limit, $limit);
  125. // 统计 same_count
  126. $paths = array_column($paged, 'path');
  127. $sameCountMap = [];
  128. if ($paths) {
  129. $sameCountMap = Db::name('text_to_image')
  130. ->whereIn('old_image_url', $paths)
  131. ->where('new_image_url', '<>', '')
  132. ->group('old_image_url')
  133. ->column('count(*) as cnt', 'old_image_url');
  134. }
  135. // 构建返回数据
  136. $result = [];
  137. foreach ($paged as $i => $data) {
  138. $path = $data['path'];
  139. $item = $data['item'];
  140. $info = $data['info'];
  141. $result[] = [
  142. 'id' => ($page - 1) * $limit + $i + 1,
  143. 'path' => $path,
  144. 'status' => $data['dbStatus'],
  145. 'status_name' => $data['dbStatusName'],
  146. 'same_count' => $sameCountMap[$path] ?? 0,
  147. 'is_processed' => $data['isProcessed'] ? 1 : 0,
  148. 'queue_status' => $data['queueStatus'],
  149. 'new_image_url' => $item['new_image_url'] ?? '',
  150. 'custom_image_url' => $item['custom_image_url'] ?? '',
  151. 'chinese_description' => $item['chinese_description'] ?? '',
  152. 'english_description' => $item['english_description'] ?? '',
  153. 'img_name' => $item['img_name'] ?? '',
  154. 'width' => $info['width'],
  155. 'height' => $info['height'],
  156. 'size_kb' => $info['size_kb'],
  157. 'created_time' => $info['created_time']
  158. ];
  159. }
  160. return json([
  161. 'code' => 0,
  162. 'msg' => '获取成功',
  163. 'data' => $result,
  164. 'total' => $total,
  165. 'page' => $page,
  166. 'limit' => $limit
  167. ]);
  168. }
  169. // public function getPreviewimg()
  170. // {
  171. // $page = (int)$this->request->param('page', 1);
  172. // $limit = (int)$this->request->param('limit', 50);
  173. // $status = $this->request->param('status', '');
  174. // $status_name = $this->request->param('status_name', '');
  175. // $relativePath = $this->request->param('path', '');
  176. //
  177. // $basePath = ROOT_PATH . 'public/';
  178. // $fullPath = $basePath . $relativePath;
  179. //
  180. // if (!is_dir($fullPath)) {
  181. // return json(['code' => 1, 'msg' => '原图目录不存在']);
  182. // }
  183. //
  184. // $baseCacheKey = 'previewimg_full_' . md5($relativePath);
  185. // $fullData = cache($baseCacheKey);
  186. //
  187. // if (!$fullData) {
  188. // $allImages = glob($fullPath . '/*.{jpg,jpeg,png}', GLOB_BRACE);
  189. // $imageInfoMap = [];
  190. //
  191. // foreach ($allImages as $imgPath) {
  192. // $relative = str_replace('\\', '/', trim(str_replace($basePath, '', $imgPath), '/'));
  193. // $info = @getimagesize($imgPath);
  194. // $imageInfoMap[$relative] = [
  195. // 'width' => $info[0] ?? 0,
  196. // 'height' => $info[1] ?? 0,
  197. // 'size_kb' => round(filesize($imgPath) / 1024, 2),
  198. // 'created_time' => date('Y-m-d H:i:s', filectime($imgPath))
  199. // ];
  200. // }
  201. //
  202. // $relativeImages = array_keys($imageInfoMap);
  203. //
  204. // $dbRecords = Db::name('text_to_image')
  205. // ->whereIn('old_image_url', $relativeImages)
  206. // ->field('id as img_id, old_image_url, new_image_url, custom_image_url, chinese_description, english_description, img_name, status, status_name')
  207. // ->select();
  208. //
  209. // $queueRecords = Db::name('image_task_log')
  210. // ->field('file_name, log')
  211. // ->where('mod_rq', null)
  212. // ->select();
  213. //
  214. // $queueMap = [];
  215. // foreach ($queueRecords as $q) {
  216. // $k = str_replace('\\', '/', trim($q['file_name'], '/'));
  217. // $queueMap[$k] = $q['log'];
  218. // }
  219. //
  220. // $processedMap = [];
  221. // foreach ($dbRecords as $item) {
  222. // $key = str_replace('\\', '/', trim($item['old_image_url'], '/'));
  223. // $processedMap[$key] = $item;
  224. // }
  225. //
  226. // $fullData = [];
  227. // foreach ($relativeImages as $path) {
  228. // $item = $processedMap[$path] ?? [];
  229. // $info = $imageInfoMap[$path];
  230. //
  231. // $fullData[] = [
  232. // 'path' => $path,
  233. // 'item' => $item,
  234. // 'info' => $info,
  235. // 'dbStatus' => isset($item['status']) ? (int)$item['status'] : 0,
  236. // 'dbStatusName' => $item['status_name'] ?? '',
  237. // 'isProcessed' => !empty($item['img_name']) && !empty($item['custom_image_url']),
  238. // 'queueStatus' => $queueMap[$path] ?? ''
  239. // ];
  240. // }
  241. //
  242. // // 缓存整个数据,不包含分页
  243. // cache($baseCacheKey, $fullData, 600);
  244. // }
  245. //
  246. // // 筛选状态(如 status, status_name)
  247. // $filteredData = array_filter($fullData, function ($data) use ($status, $status_name) {
  248. // if ($status !== '' && (int)$status !== $data['dbStatus']) return false;
  249. // if ($status_name !== '' && $status_name !== $data['dbStatusName']) return false;
  250. // return true;
  251. // });
  252. //
  253. // // 分页处理
  254. // $total = count($filteredData);
  255. // $pagedData = array_slice(array_values($filteredData), ($page - 1) * $limit, $limit);
  256. //
  257. // // 统计 same_count
  258. // $paths = array_column($pagedData, 'path');
  259. // $sameCountMap = [];
  260. // if (!empty($paths)) {
  261. // $sameCountMap = Db::name('text_to_image')
  262. // ->whereIn('old_image_url', $paths)
  263. // ->where('new_image_url', '<>', '')
  264. // ->group('old_image_url')
  265. // ->column('count(*) as cnt', 'old_image_url');
  266. // }
  267. //
  268. // // 构建返回结构
  269. // $resultData = [];
  270. // foreach ($pagedData as $i => $data) {
  271. // $path = $data['path'];
  272. // $item = $data['item'];
  273. // $info = $data['info'];
  274. //
  275. // $resultData[] = [
  276. // 'id' => ($page - 1) * $limit + $i + 1,
  277. // 'path' => $path,
  278. // 'status' => $data['dbStatus'],
  279. // 'status_name' => $data['dbStatusName'],
  280. // 'same_count' => $sameCountMap[$path] ?? 0,
  281. // 'is_processed' => $data['isProcessed'] ? 1 : 0,
  282. // 'queue_status' => $data['queueStatus'],
  283. // 'new_image_url' => $item['new_image_url'] ?? '',
  284. // 'custom_image_url' => $item['custom_image_url'] ?? '',
  285. // 'chinese_description' => $item['chinese_description'] ?? '',
  286. // 'english_description' => $item['english_description'] ?? '',
  287. // 'img_name' => $item['img_name'] ?? '',
  288. // 'width' => $info['width'],
  289. // 'height' => $info['height'],
  290. // 'size_kb' => $info['size_kb'],
  291. // 'created_time' => $info['created_time']
  292. // ];
  293. // }
  294. //
  295. // return json([
  296. // 'code' => 0,
  297. // 'msg' => '获取成功',
  298. // 'data' => $resultData,
  299. // 'total' => $total,
  300. // 'page' => $page,
  301. // 'limit' => $limit
  302. // ]);
  303. // }
  304. /**
  305. * 获取指定目录所有图片
  306. */
  307. // public function getPreviewimg()
  308. // {
  309. // $page = (int)$this->request->param('page', 1);
  310. // $limit = (int)$this->request->param('limit', 50);
  311. // $status = $this->request->param('status', '');
  312. // $status_name = $this->request->param('status_name', '');
  313. // $relativePath = $this->request->param('path', '');
  314. //
  315. // $basePath = ROOT_PATH . 'public/';
  316. // $fullPath = $basePath . $relativePath;
  317. //
  318. // if (!is_dir($fullPath)) {
  319. // return json(['code' => 1, 'msg' => '原图目录不存在']);
  320. // }
  321. //
  322. // // 设置缓存路径
  323. // $cacheDir = RUNTIME_PATH . 'image_cache/';
  324. // if (!is_dir($cacheDir)) {
  325. // mkdir($cacheDir, 0755, true);
  326. // }
  327. // $cacheFile = $cacheDir . md5($relativePath) . '.json';
  328. //
  329. // // 判断缓存文件是否存在,并且最后修改时间在1小时(3600秒)以内
  330. // if (file_exists($cacheFile) && time() - filemtime($cacheFile) < 3600) {
  331. // $imageInfoMap = json_decode(file_get_contents($cacheFile), true);
  332. // } else {
  333. // // 没有缓存或缓存过期,重新扫描目录
  334. // $allImages = glob($fullPath . '/*.{jpg,jpeg,png}', GLOB_BRACE);
  335. // $imageInfoMap = [];
  336. // foreach ($allImages as $imgPath) {
  337. // $relative = str_replace('\\', '/', trim(str_replace($basePath, '', $imgPath), '/'));
  338. // $info = @getimagesize($imgPath);
  339. // $imageInfoMap[$relative] = [
  340. // 'width' => $info[0] ?? 0,
  341. // 'height' => $info[1] ?? 0,
  342. // 'size_kb' => round(filesize($imgPath) / 1024, 2),
  343. // 'created_time' => date('Y-m-d H:i:s', filectime($imgPath))
  344. // ];
  345. // }
  346. // file_put_contents($cacheFile, json_encode($imageInfoMap));
  347. // }
  348. //
  349. // // 1. 获取所有图片路径
  350. // $relativeImages = array_keys($imageInfoMap);
  351. //
  352. // // 2. 查询数据库记录(一次性查询所有相关记录)
  353. // $dbRecords = Db::name('text_to_image')
  354. // ->whereIn('old_image_url', $relativeImages)
  355. // ->field('id as img_id, old_image_url, new_image_url, custom_image_url, chinese_description, english_description, img_name, status, status_name')
  356. // ->select();
  357. //
  358. // // 3. 查询队列表中的记录(获取队列状态信息)
  359. // $queueRecords = Db::name('image_task_log')
  360. // // ->where('status', 0)
  361. // // ->where('log', '队列中')
  362. // ->field('file_name, log')
  363. // ->select();
  364. //
  365. // // 4. 创建队列信息映射
  366. // $queueMap = [];
  367. // foreach ($queueRecords as $queueItem) {
  368. // $key = str_replace('\\', '/', trim($queueItem['file_name'], '/'));
  369. // $queueMap[$key] = $queueItem['log'];
  370. // }
  371. //
  372. // // 5. 映射记录
  373. // $processedMap = [];
  374. // foreach ($dbRecords as $item) {
  375. // $key = str_replace('\\', '/', trim($item['old_image_url'], '/'));
  376. // $processedMap[$key] = $item;
  377. // }
  378. //
  379. // // 6. 构建完整数据并进行筛选
  380. // $filteredData = [];
  381. // foreach ($relativeImages as $path) {
  382. // $item = $processedMap[$path] ?? [];
  383. // $info = $imageInfoMap[$path];
  384. //
  385. // $dbStatus = isset($item['status']) ? (int)$item['status'] : 0;
  386. // $dbStatusName = isset($item['status_name']) ? trim($item['status_name']) : '';
  387. //
  388. // // 状态筛选条件
  389. // if ($status !== '' && (int)$status !== $dbStatus) {
  390. // continue;
  391. // }
  392. // if ($status_name !== '' && $dbStatusName !== $status_name) {
  393. // continue;
  394. // }
  395. //
  396. // $isProcessed = !empty($item['img_name']) && !empty($item['custom_image_url']);
  397. // $queueStatus = $queueMap[$path] ?? '';
  398. //
  399. // $filteredData[] = [
  400. // 'path' => $path,
  401. // 'item' => $item,
  402. // 'info' => $info,
  403. // 'dbStatus' => $dbStatus,
  404. // 'dbStatusName' => $dbStatusName,
  405. // 'isProcessed' => $isProcessed,
  406. // 'queueStatus' => $queueStatus
  407. // ];
  408. // }
  409. //
  410. // // 7. 获取相同图片数量统计(基于筛选后的结果,只统计有new_image_url的记录)
  411. // $filteredPaths = array_column($filteredData, 'path');
  412. // $sameCountMap = [];
  413. // if (!empty($filteredPaths)) {
  414. // $sameCountMap = Db::name('text_to_image')
  415. // ->whereIn('old_image_url', $filteredPaths)
  416. // ->where('new_image_url', '<>', '') // 只统计有new_image_url的记录
  417. // ->group('old_image_url')
  418. // ->column('count(*) as cnt', 'old_image_url');
  419. // }
  420. //
  421. // // 8. 分页处理
  422. // $total = count($filteredData);
  423. // $pagedData = array_slice($filteredData, ($page - 1) * $limit, $limit);
  424. //
  425. // // 9. 构建最终响应数据
  426. // $resultData = [];
  427. // foreach ($pagedData as $i => $data) {
  428. // $path = $data['path'];
  429. // $item = $data['item'];
  430. // $info = $data['info'];
  431. //
  432. // $resultData[] = [
  433. // 'id' => ($page - 1) * $limit + $i + 1,
  434. // 'path' => $path,
  435. // 'status' => $data['dbStatus'],
  436. // 'status_name' => $data['dbStatusName'],
  437. // 'same_count' => $sameCountMap[$path] ?? 0,
  438. // 'is_processed' => $data['isProcessed'] ? 1 : 0,
  439. // 'queue_status' => $data['queueStatus'], // 新增队列状态字段
  440. // 'new_image_url' => $item['new_image_url'] ?? '',
  441. // 'custom_image_url' => $item['custom_image_url'] ?? '',
  442. // 'chinese_description' => $item['chinese_description'] ?? '',
  443. // 'english_description' => $item['english_description'] ?? '',
  444. // 'img_name' => $item['img_name'] ?? '',
  445. // 'width' => $info['width'],
  446. // 'height' => $info['height'],
  447. // 'size_kb' => $info['size_kb'],
  448. // 'created_time' => $info['created_time']
  449. // ];
  450. // }
  451. //
  452. // return json([
  453. // 'code' => 0,
  454. // 'msg' => '获取成功',
  455. // 'data' => $resultData,
  456. // 'total' => $total,
  457. // 'page' => $page,
  458. // 'limit' => $limit
  459. // ]);
  460. // }
  461. /**
  462. * 采用缓存机制
  463. * 获取原图目录及每个目录下的图片数量(优化版)
  464. */
  465. public function getPreviewSubDirs()
  466. {
  467. // 1. 设置基础路径
  468. $baseDir = rtrim(str_replace('\\', '/', ROOT_PATH), '/') . '/public/uploads/operate/ai/Preview';
  469. $baseRelativePath = 'uploads/operate/ai/Preview';
  470. // 2. 检查目录是否存在
  471. if (!is_dir($baseDir)) {
  472. return json(['code' => 1, 'msg' => '目录不存在']);
  473. }
  474. // 3. 获取目录最后修改时间作为缓存标识
  475. $cacheKey = 'preview_dirs_' . md5($baseDir);
  476. $lastModified = filemtime($baseDir);
  477. $cacheVersionKey = $cacheKey . '_version';
  478. // 4. 检查缓存版本是否匹配
  479. if (cache($cacheVersionKey) != $lastModified) {
  480. cache($cacheKey, null);
  481. cache($cacheVersionKey, $lastModified, 86400);
  482. }
  483. // 5. 尝试从缓存获取
  484. if (!$dirList = cache($cacheKey)) {
  485. // 6. 重新扫描目录
  486. $dirList = $this->scanDirectories($baseDir, $baseRelativePath);
  487. cache($cacheKey, $dirList, 86400); // 缓存1天
  488. }
  489. return json([
  490. 'code' => 0,
  491. 'msg' => '获取成功',
  492. 'data' => $dirList
  493. ]);
  494. }
  495. /**
  496. * 扫描目录结构
  497. */
  498. private function scanDirectories($baseDir, $baseRelativePath)
  499. {
  500. $dirs = [];
  501. $index = 1;
  502. $processedDirs = [];
  503. $scanDir = function ($dirPath, $relativePath) use (&$scanDir, &$dirs, &$index, &$processedDirs) {
  504. $items = @scandir($dirPath) ?: [];
  505. foreach ($items as $item) {
  506. if ($item === '.' || $item === '..') continue;
  507. $fullPath = $dirPath . '/' . $item;
  508. $relPath = $relativePath . '/' . $item;
  509. if (is_dir($fullPath)) {
  510. $dirKey = md5($fullPath);
  511. if (!isset($processedDirs[$dirKey])) {
  512. $processedDirs[$dirKey] = true;
  513. $scanDir($fullPath, $relPath);
  514. }
  515. } elseif (preg_match('/\.(jpg|jpeg|png)$/i', $item)) {
  516. $parentDir = dirname($fullPath);
  517. $relativeDir = dirname($relPath);
  518. $key = md5($parentDir);
  519. if (!isset($dirs[$key])) {
  520. $ctime = @filectime($parentDir) ?: time();
  521. // 数据库查询
  522. $hasData = Db::name('text_to_image')
  523. ->where('custom_image_url', '<>', '')
  524. ->where('img_name', '<>', '')
  525. ->whereLike('old_image_url', $relativeDir . '/%')
  526. ->where('status',1)
  527. ->cache(true, 300)
  528. ->count();
  529. $imageFiles = @glob($parentDir . '/*.{jpg,jpeg,png}', GLOB_BRACE);
  530. $imageCount = $imageFiles ? count($imageFiles) : 0;
  531. $dirs[$key] = [
  532. 'id' => $index++,
  533. 'name' => basename($parentDir),
  534. 'count' => $hasData,
  535. 'ctime' => $ctime,
  536. 'ctime_text' => date('Y-m-d H:i:s', $ctime),
  537. 'image_count' => $imageCount,
  538. 'new_image_url' => "/uploads/operate/ai/dall-e/",
  539. 'old_image_url' => $relativeDir
  540. ];
  541. }
  542. }
  543. }
  544. };
  545. $scanDir($baseDir, $baseRelativePath);
  546. // 按ID降序排序
  547. $dirList = array_values($dirs);
  548. usort($dirList, function ($a, $b) {
  549. return $b['id'] - $a['id'];
  550. });
  551. return $dirList;
  552. }
  553. /**
  554. * 手动清除缓存(在目录变更后调用)
  555. */
  556. public function clearCache()
  557. {
  558. $baseDir = rtrim(str_replace('\\', '/', ROOT_PATH), '/') . '/public/uploads/operate/ai/Preview';
  559. $cacheKey = 'preview_dirs_' . md5($baseDir);
  560. cache($cacheKey, null);
  561. cache($cacheKey . '_version', null);
  562. return json(['code' => 0, 'msg' => '缓存已清除']);
  563. }
  564. /**
  565. * 不使用缓存机制(查询速度较慢)
  566. * 获取原图目录及每个目录下的图片数量
  567. */
  568. // public function getPreviewSubDirs()
  569. // {
  570. // $baseDir = rtrim(str_replace('\\', '/', ROOT_PATH), '/') . '/public/uploads/operate/ai/Preview';
  571. // $baseRelativePath = 'uploads/operate/ai/Preview';
  572. //
  573. // if (!is_dir($baseDir)) {
  574. // return json(['code' => 1, 'msg' => '目录不存在']);
  575. // }
  576. //
  577. // $dirs = [];
  578. // $index = 1;
  579. //
  580. // /**
  581. // * 递归扫描目录,提取含图片的子目录信息
  582. // */
  583. // $scanDir = function ($dirPath, $relativePath) use (&$scanDir, &$dirs, &$index) {
  584. // $items = scandir($dirPath);
  585. // foreach ($items as $item) {
  586. // if ($item === '.' || $item === '..') continue;
  587. //
  588. // $fullPath = $dirPath . '/' . $item;
  589. // $relPath = $relativePath . '/' . $item;
  590. //
  591. // if (is_dir($fullPath)) {
  592. // // 递归子目录
  593. // $scanDir($fullPath, $relPath);
  594. // } else {
  595. // // 匹配图片文件
  596. // if (preg_match('/\.(jpg|jpeg|png)$/i', $item)) {
  597. // $parentDir = dirname($fullPath);
  598. // $relativeDir = dirname($relPath);
  599. // $key = md5($parentDir);
  600. //
  601. // if (!isset($dirs[$key])) {
  602. // $ctime = filectime($parentDir);
  603. //
  604. // // 数据库统计:已处理图片数量
  605. // $hasData = Db::name('text_to_image')
  606. // ->where('custom_image_url', '<>', '')
  607. // ->where('img_name', '<>', '')
  608. // ->whereLike('old_image_url', $relativeDir . '/%')
  609. // ->where('status',1)
  610. // ->whereNotNull('custom_image_url')
  611. // ->count();
  612. //
  613. // // 当前目录下图片数量
  614. // $imageFiles = glob($parentDir . '/*.{jpg,jpeg,png}', GLOB_BRACE);
  615. // $imageCount = is_array($imageFiles) ? count($imageFiles) : 0;
  616. //
  617. // $dirs[$key] = [
  618. // 'id' => $index++,
  619. // 'name' => basename($parentDir),
  620. // 'count' => $hasData,
  621. // 'ctime' => $ctime, // 时间戳,用于排序
  622. // 'ctime_text' => date('Y-m-d H:i:s', $ctime), // 格式化日期,用于显示
  623. // 'image_count' => $imageCount,
  624. // 'new_image_url' => "/uploads/operate/ai/dall-e/",
  625. // 'old_image_url' => $relativeDir
  626. // ];
  627. // }
  628. // }
  629. // }
  630. // }
  631. // };
  632. //
  633. // // 执行目录扫描
  634. // $scanDir($baseDir, $baseRelativePath);
  635. //
  636. // // 排序:按照创建时间(从新到旧)
  637. // $dirList = array_values($dirs);
  638. //// usort($dirList, function ($a, $b) {
  639. //// return $b['ctime'] - $a['ctime'];
  640. //// });
  641. //
  642. // usort($dirList, function ($a, $b) {
  643. // return $b['id'] - $a['id'];
  644. // });
  645. //
  646. //
  647. // return json([
  648. // 'code' => 0,
  649. // 'msg' => '获取成功',
  650. // 'data' => $dirList
  651. // ]);
  652. // }
  653. /**
  654. * 图片上传
  655. */
  656. public function ImgUpload()
  657. {
  658. // 处理 CORS OPTIONS 预检请求
  659. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  660. header('Access-Control-Allow-Origin: *');
  661. header('Access-Control-Allow-Methods: POST, OPTIONS');
  662. header('Access-Control-Allow-Headers: Content-Type, Authorization');
  663. header('Access-Control-Max-Age: 86400');
  664. exit(204);
  665. }
  666. // 实际请求必须返回 CORS 头
  667. header('Access-Control-Allow-Origin: *');
  668. // 获取上传的文件
  669. $file = request()->file('image');
  670. if ($file) {
  671. // 指定目标目录(你想上传到的目录)
  672. $targetPath = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'operate' . DS . 'ai' . DS . 'Preview';
  673. // 若目录不存在则创建
  674. if (!is_dir($targetPath)) {
  675. mkdir($targetPath, 0755, true);
  676. }
  677. // 移动文件到指定目录,并验证大小/格式
  678. $info = $file->validate([
  679. 'size' => 10485760, // 最大10MB
  680. 'ext' => 'jpg,png'
  681. ])->move($targetPath);
  682. if ($info) {
  683. $fileName = $info->getSaveName();
  684. $imageUrl = '/uploads/operate/ai/Preview/' . str_replace('\\', '/', $fileName);
  685. return json(['code' => 0, 'msg' => '成功', 'data' => ['url' => $imageUrl]]);
  686. } else {
  687. $res = $file->getError();
  688. return json(['code' => 1, 'msg' => '失败', 'data' => $res]);
  689. }
  690. }
  691. return json(['code' => 1, 'msg' => '没有文件上传', 'data' => null]);
  692. }
  693. /**
  694. * 查询模版
  695. */
  696. public function Template(){
  697. $Template = Db::name("template")->where('ids',1)->find();
  698. return json([
  699. 'code' => 0,
  700. 'msg' => '模版',
  701. 'data' => $Template
  702. ]);
  703. }
  704. /**
  705. * 更新模版
  706. */
  707. public function updatetemplate(){
  708. if (Request::instance()->isPost() == false){
  709. $this->error('非法请求');
  710. }
  711. $params = Request::instance()->post();
  712. if (empty($params['textareaContent']) || empty($params['width']) || empty($params['height'])) {
  713. return json(['code' => 1, 'msg' => '参数缺失']);
  714. }
  715. $Template = Db::name("template")
  716. ->where('ids', 1)
  717. ->update([
  718. 'english_content' => $params['english_content'], // 更新文生文模版内容
  719. 'content' => $params['textareaContent'], // 更新图生文模版内容
  720. 'width' => $params['width'], // 更新宽度
  721. 'height' => $params['height'], // 更新宽度
  722. ]);
  723. if ($Template){
  724. return json(['code' => 0, 'msg' => '成功']);
  725. }else{
  726. return json(['code' => 1, 'msg' => '失败']);
  727. }
  728. }
  729. /**
  730. * 打包图片
  731. */
  732. public function packImagess()
  733. {
  734. try {
  735. $params = $this->request->post();
  736. $paths = $params['paths'] ?? [];
  737. if (empty($paths) || !is_array($paths)) {
  738. return json(['code' => 1, 'msg' => '路径参数不能为空或格式不正确']);
  739. }
  740. // 设置基础路径和压缩目录路径
  741. $basePath = ROOT_PATH . 'public/';
  742. $zipDir = $basePath . 'uploads/operate/ai/zip/';
  743. if (!is_dir($zipDir)) {
  744. mkdir($zipDir, 0755, true);
  745. }
  746. // 压缩包文件名及完整路径
  747. $fileName = 'images_' . date('Ymd_His') . '.zip';
  748. $zipPath = $zipDir . $fileName;
  749. // 创建 Zip 文件
  750. $zip = new \ZipArchive();
  751. if ($zip->open($zipPath, \ZipArchive::CREATE) !== TRUE) {
  752. return json(['code' => 1, 'msg' => '无法创建压缩包']);
  753. }
  754. // 添加文件到压缩包
  755. $addCount = 0;
  756. foreach ($paths as $relativePath) {
  757. $relativePath = ltrim($relativePath, '/');
  758. $fullPath = $basePath . $relativePath;
  759. if (file_exists($fullPath)) {
  760. $zip->addFile($fullPath, basename($fullPath)); // 仅保存文件名
  761. $addCount++;
  762. }
  763. }
  764. $zip->close();
  765. if ($addCount === 0) {
  766. return json(['code' => 1, 'msg' => '未找到有效图片,未生成压缩包']);
  767. }
  768. // 返回下载地址(注意路径与保存路径一致)
  769. $downloadUrl = request()->domain() . '/uploads/operate/ai/zip/' . $fileName;
  770. return json([
  771. 'code' => 0,
  772. 'msg' => '打包成功',
  773. 'download_url' => $downloadUrl
  774. ]);
  775. } catch (\Exception $e) {
  776. return json([
  777. 'code' => 1,
  778. 'msg' => '异常错误:' . $e->getMessage()
  779. ]);
  780. }
  781. }
  782. }