Facility.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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')
  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. public function getPreviewimg()
  32. {
  33. $page = (int)$this->request->param('page', 1);
  34. $limit = (int)$this->request->param('limit', 50);
  35. $status = $this->request->param('status', '');
  36. $status_name = $this->request->param('status_name', '');
  37. $relativePath = $this->request->param('path', '');
  38. $basePath = ROOT_PATH . 'public/';
  39. $fullPath = $basePath . $relativePath;
  40. if (!is_dir($fullPath)) {
  41. return json(['code' => 1, 'msg' => '原图目录不存在']);
  42. }
  43. // 设置缓存路径
  44. $cacheDir = RUNTIME_PATH . 'image_cache/';
  45. if (!is_dir($cacheDir)) {
  46. mkdir($cacheDir, 0755, true);
  47. }
  48. $cacheFile = $cacheDir . md5($relativePath) . '.json';
  49. // 判断缓存文件是否存在,并且最后修改时间在1小时(3600秒)以内
  50. if (file_exists($cacheFile) && time() - filemtime($cacheFile) < 3600) {
  51. $imageInfoMap = json_decode(file_get_contents($cacheFile), true);
  52. } else {
  53. // 没有缓存或缓存过期,重新扫描目录
  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. file_put_contents($cacheFile, json_encode($imageInfoMap));
  67. }
  68. // 1. 获取所有图片路径
  69. $relativeImages = array_keys($imageInfoMap);
  70. // 2. 查询数据库记录(一次性查询所有相关记录)
  71. $dbRecords = Db::name('text_to_image')
  72. ->whereIn('old_image_url', $relativeImages)
  73. ->field('id as img_id, old_image_url, new_image_url, custom_image_url, chinese_description, english_description, img_name, status, status_name')
  74. ->select();
  75. // 3. 查询队列表中的记录(获取队列状态信息)
  76. $queueRecords = Db::name('image_task_log')
  77. // ->where('status', 0)
  78. // ->where('log', '队列中')
  79. ->field('file_name, log')
  80. ->select();
  81. // 4. 创建队列信息映射
  82. $queueMap = [];
  83. foreach ($queueRecords as $queueItem) {
  84. $key = str_replace('\\', '/', trim($queueItem['file_name'], '/'));
  85. $queueMap[$key] = $queueItem['log'];
  86. }
  87. // 5. 映射记录
  88. $processedMap = [];
  89. foreach ($dbRecords as $item) {
  90. $key = str_replace('\\', '/', trim($item['old_image_url'], '/'));
  91. $processedMap[$key] = $item;
  92. }
  93. // 6. 构建完整数据并进行筛选
  94. $filteredData = [];
  95. foreach ($relativeImages as $path) {
  96. $item = $processedMap[$path] ?? [];
  97. $info = $imageInfoMap[$path];
  98. $dbStatus = isset($item['status']) ? (int)$item['status'] : 0;
  99. $dbStatusName = isset($item['status_name']) ? trim($item['status_name']) : '';
  100. // 状态筛选条件
  101. if ($status !== '' && (int)$status !== $dbStatus) {
  102. continue;
  103. }
  104. if ($status_name !== '' && $dbStatusName !== $status_name) {
  105. continue;
  106. }
  107. $isProcessed = !empty($item['img_name']) && !empty($item['custom_image_url']);
  108. $queueStatus = $queueMap[$path] ?? '';
  109. $filteredData[] = [
  110. 'path' => $path,
  111. 'item' => $item,
  112. 'info' => $info,
  113. 'dbStatus' => $dbStatus,
  114. 'dbStatusName' => $dbStatusName,
  115. 'isProcessed' => $isProcessed,
  116. 'queueStatus' => $queueStatus
  117. ];
  118. }
  119. // 7. 获取相同图片数量统计(基于筛选后的结果,只统计有new_image_url的记录)
  120. $filteredPaths = array_column($filteredData, 'path');
  121. $sameCountMap = [];
  122. if (!empty($filteredPaths)) {
  123. $sameCountMap = Db::name('text_to_image')
  124. ->whereIn('old_image_url', $filteredPaths)
  125. ->where('new_image_url', '<>', '') // 只统计有new_image_url的记录
  126. ->group('old_image_url')
  127. ->column('count(*) as cnt', 'old_image_url');
  128. }
  129. // 8. 分页处理
  130. $total = count($filteredData);
  131. $pagedData = array_slice($filteredData, ($page - 1) * $limit, $limit);
  132. // 9. 构建最终响应数据
  133. $resultData = [];
  134. foreach ($pagedData as $i => $data) {
  135. $path = $data['path'];
  136. $item = $data['item'];
  137. $info = $data['info'];
  138. $resultData[] = [
  139. 'id' => ($page - 1) * $limit + $i + 1,
  140. 'path' => $path,
  141. 'status' => $data['dbStatus'],
  142. 'status_name' => $data['dbStatusName'],
  143. 'same_count' => $sameCountMap[$path] ?? 0,
  144. 'is_processed' => $data['isProcessed'] ? 1 : 0,
  145. 'queue_status' => $data['queueStatus'], // 新增队列状态字段
  146. 'new_image_url' => $item['new_image_url'] ?? '',
  147. 'custom_image_url' => $item['custom_image_url'] ?? '',
  148. 'chinese_description' => $item['chinese_description'] ?? '',
  149. 'english_description' => $item['english_description'] ?? '',
  150. 'img_name' => $item['img_name'] ?? '',
  151. 'width' => $info['width'],
  152. 'height' => $info['height'],
  153. 'size_kb' => $info['size_kb'],
  154. 'created_time' => $info['created_time']
  155. ];
  156. }
  157. return json([
  158. 'code' => 0,
  159. 'msg' => '获取成功',
  160. 'data' => $resultData,
  161. 'total' => $total,
  162. 'page' => $page,
  163. 'limit' => $limit
  164. ]);
  165. }
  166. /**
  167. * 采用缓存机制
  168. * 获取原图目录及每个目录下的图片数量(优化版)
  169. */
  170. public function getPreviewSubDirs()
  171. {
  172. // 1. 设置基础路径
  173. $baseDir = rtrim(str_replace('\\', '/', ROOT_PATH), '/') . '/public/uploads/operate/ai/Preview';
  174. $baseRelativePath = 'uploads/operate/ai/Preview';
  175. // 2. 检查目录是否存在
  176. if (!is_dir($baseDir)) {
  177. return json(['code' => 1, 'msg' => '目录不存在']);
  178. }
  179. // 3. 获取目录最后修改时间作为缓存标识
  180. $cacheKey = 'preview_dirs_' . md5($baseDir);
  181. $lastModified = filemtime($baseDir);
  182. $cacheVersionKey = $cacheKey . '_version';
  183. // 4. 检查缓存版本是否匹配
  184. if (cache($cacheVersionKey) != $lastModified) {
  185. cache($cacheKey, null);
  186. cache($cacheVersionKey, $lastModified, 86400);
  187. }
  188. // 5. 尝试从缓存获取
  189. if (!$dirList = cache($cacheKey)) {
  190. // 6. 重新扫描目录
  191. $dirList = $this->scanDirectories($baseDir, $baseRelativePath);
  192. cache($cacheKey, $dirList, 86400); // 缓存1天
  193. }
  194. return json([
  195. 'code' => 0,
  196. 'msg' => '获取成功',
  197. 'data' => $dirList
  198. ]);
  199. }
  200. /**
  201. * 扫描目录结构
  202. */
  203. private function scanDirectories($baseDir, $baseRelativePath)
  204. {
  205. $dirs = [];
  206. $index = 1;
  207. $processedDirs = [];
  208. $scanDir = function ($dirPath, $relativePath) use (&$scanDir, &$dirs, &$index, &$processedDirs) {
  209. $items = @scandir($dirPath) ?: [];
  210. foreach ($items as $item) {
  211. if ($item === '.' || $item === '..') continue;
  212. $fullPath = $dirPath . '/' . $item;
  213. $relPath = $relativePath . '/' . $item;
  214. if (is_dir($fullPath)) {
  215. $dirKey = md5($fullPath);
  216. if (!isset($processedDirs[$dirKey])) {
  217. $processedDirs[$dirKey] = true;
  218. $scanDir($fullPath, $relPath);
  219. }
  220. } elseif (preg_match('/\.(jpg|jpeg|png)$/i', $item)) {
  221. $parentDir = dirname($fullPath);
  222. $relativeDir = dirname($relPath);
  223. $key = md5($parentDir);
  224. if (!isset($dirs[$key])) {
  225. $ctime = @filectime($parentDir) ?: time();
  226. // 数据库查询
  227. $hasData = Db::name('text_to_image')
  228. ->where('custom_image_url', '<>', '')
  229. ->where('img_name', '<>', '')
  230. ->whereLike('old_image_url', $relativeDir . '/%')
  231. ->where('status',1)
  232. ->cache(true, 300)
  233. ->count();
  234. $imageFiles = @glob($parentDir . '/*.{jpg,jpeg,png}', GLOB_BRACE);
  235. $imageCount = $imageFiles ? count($imageFiles) : 0;
  236. $dirs[$key] = [
  237. 'id' => $index++,
  238. 'name' => basename($parentDir),
  239. 'count' => $hasData,
  240. 'ctime' => $ctime,
  241. 'ctime_text' => date('Y-m-d H:i:s', $ctime),
  242. 'image_count' => $imageCount,
  243. 'new_image_url' => "/uploads/operate/ai/dall-e/",
  244. 'old_image_url' => $relativeDir
  245. ];
  246. }
  247. }
  248. }
  249. };
  250. $scanDir($baseDir, $baseRelativePath);
  251. // 按ID降序排序
  252. $dirList = array_values($dirs);
  253. usort($dirList, function ($a, $b) {
  254. return $b['id'] - $a['id'];
  255. });
  256. return $dirList;
  257. }
  258. /**
  259. * 手动清除缓存(在目录变更后调用)
  260. */
  261. public function clearCache()
  262. {
  263. $baseDir = rtrim(str_replace('\\', '/', ROOT_PATH), '/') . '/public/uploads/operate/ai/Preview';
  264. $cacheKey = 'preview_dirs_' . md5($baseDir);
  265. cache($cacheKey, null);
  266. cache($cacheKey . '_version', null);
  267. return json(['code' => 0, 'msg' => '缓存已清除']);
  268. }
  269. /**
  270. * 不使用缓存机制(查询速度较慢)
  271. * 获取原图目录及每个目录下的图片数量
  272. */
  273. // public function getPreviewSubDirs()
  274. // {
  275. // $baseDir = rtrim(str_replace('\\', '/', ROOT_PATH), '/') . '/public/uploads/operate/ai/Preview';
  276. // $baseRelativePath = 'uploads/operate/ai/Preview';
  277. //
  278. // if (!is_dir($baseDir)) {
  279. // return json(['code' => 1, 'msg' => '目录不存在']);
  280. // }
  281. //
  282. // $dirs = [];
  283. // $index = 1;
  284. //
  285. // /**
  286. // * 递归扫描目录,提取含图片的子目录信息
  287. // */
  288. // $scanDir = function ($dirPath, $relativePath) use (&$scanDir, &$dirs, &$index) {
  289. // $items = scandir($dirPath);
  290. // foreach ($items as $item) {
  291. // if ($item === '.' || $item === '..') continue;
  292. //
  293. // $fullPath = $dirPath . '/' . $item;
  294. // $relPath = $relativePath . '/' . $item;
  295. //
  296. // if (is_dir($fullPath)) {
  297. // // 递归子目录
  298. // $scanDir($fullPath, $relPath);
  299. // } else {
  300. // // 匹配图片文件
  301. // if (preg_match('/\.(jpg|jpeg|png)$/i', $item)) {
  302. // $parentDir = dirname($fullPath);
  303. // $relativeDir = dirname($relPath);
  304. // $key = md5($parentDir);
  305. //
  306. // if (!isset($dirs[$key])) {
  307. // $ctime = filectime($parentDir);
  308. //
  309. // // 数据库统计:已处理图片数量
  310. // $hasData = Db::name('text_to_image')
  311. // ->where('custom_image_url', '<>', '')
  312. // ->where('img_name', '<>', '')
  313. // ->whereLike('old_image_url', $relativeDir . '/%')
  314. // ->where('status',1)
  315. // ->whereNotNull('custom_image_url')
  316. // ->count();
  317. //
  318. // // 当前目录下图片数量
  319. // $imageFiles = glob($parentDir . '/*.{jpg,jpeg,png}', GLOB_BRACE);
  320. // $imageCount = is_array($imageFiles) ? count($imageFiles) : 0;
  321. //
  322. // $dirs[$key] = [
  323. // 'id' => $index++,
  324. // 'name' => basename($parentDir),
  325. // 'count' => $hasData,
  326. // 'ctime' => $ctime, // 时间戳,用于排序
  327. // 'ctime_text' => date('Y-m-d H:i:s', $ctime), // 格式化日期,用于显示
  328. // 'image_count' => $imageCount,
  329. // 'new_image_url' => "/uploads/operate/ai/dall-e/",
  330. // 'old_image_url' => $relativeDir
  331. // ];
  332. // }
  333. // }
  334. // }
  335. // }
  336. // };
  337. //
  338. // // 执行目录扫描
  339. // $scanDir($baseDir, $baseRelativePath);
  340. //
  341. // // 排序:按照创建时间(从新到旧)
  342. // $dirList = array_values($dirs);
  343. //// usort($dirList, function ($a, $b) {
  344. //// return $b['ctime'] - $a['ctime'];
  345. //// });
  346. //
  347. // usort($dirList, function ($a, $b) {
  348. // return $b['id'] - $a['id'];
  349. // });
  350. //
  351. //
  352. // return json([
  353. // 'code' => 0,
  354. // 'msg' => '获取成功',
  355. // 'data' => $dirList
  356. // ]);
  357. // }
  358. /**
  359. * 图片上传
  360. */
  361. public function ImgUpload()
  362. {
  363. // 处理 CORS OPTIONS 预检请求
  364. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  365. header('Access-Control-Allow-Origin: *');
  366. header('Access-Control-Allow-Methods: POST, OPTIONS');
  367. header('Access-Control-Allow-Headers: Content-Type, Authorization');
  368. header('Access-Control-Max-Age: 86400');
  369. exit(204);
  370. }
  371. // 实际请求必须返回 CORS 头
  372. header('Access-Control-Allow-Origin: *');
  373. // 获取上传的文件
  374. $file = request()->file('image');
  375. if ($file) {
  376. // 指定目标目录(你想上传到的目录)
  377. $targetPath = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'operate' . DS . 'ai' . DS . 'Preview';
  378. // 若目录不存在则创建
  379. if (!is_dir($targetPath)) {
  380. mkdir($targetPath, 0755, true);
  381. }
  382. // 移动文件到指定目录,并验证大小/格式
  383. $info = $file->validate([
  384. 'size' => 10485760, // 最大10MB
  385. 'ext' => 'jpg,png'
  386. ])->move($targetPath);
  387. if ($info) {
  388. $fileName = $info->getSaveName();
  389. $imageUrl = '/uploads/operate/ai/Preview/' . str_replace('\\', '/', $fileName);
  390. return json(['code' => 0, 'msg' => '成功', 'data' => ['url' => $imageUrl]]);
  391. } else {
  392. $res = $file->getError();
  393. return json(['code' => 1, 'msg' => '失败', 'data' => $res]);
  394. }
  395. }
  396. return json(['code' => 1, 'msg' => '没有文件上传', 'data' => null]);
  397. }
  398. /**
  399. * 查询模版
  400. */
  401. public function Template(){
  402. $Template = Db::name("template")->where('ids',1)->find();
  403. return json([
  404. 'code' => 0,
  405. 'msg' => '模版',
  406. 'data' => $Template
  407. ]);
  408. }
  409. /**
  410. * 更新模版
  411. */
  412. public function updatetemplate(){
  413. if (Request::instance()->isPost() == false){
  414. $this->error('非法请求');
  415. }
  416. $params = Request::instance()->post();
  417. if (empty($params['textareaContent']) || empty($params['width']) || empty($params['height'])) {
  418. return json(['code' => 1, 'msg' => '参数缺失']);
  419. }
  420. $Template = Db::name("template")
  421. ->where('ids', 1)
  422. ->update([
  423. 'english_content' => $params['english_content'], // 更新文生文模版内容
  424. 'content' => $params['textareaContent'], // 更新图生文模版内容
  425. 'width' => $params['width'], // 更新宽度
  426. 'height' => $params['height'], // 更新宽度
  427. ]);
  428. if ($Template){
  429. return json(['code' => 0, 'msg' => '成功']);
  430. }else{
  431. return json(['code' => 1, 'msg' => '失败']);
  432. }
  433. }
  434. /**
  435. * 打包图片
  436. */
  437. public function packImagess()
  438. {
  439. try {
  440. $params = $this->request->post();
  441. $paths = $params['paths'] ?? [];
  442. if (empty($paths) || !is_array($paths)) {
  443. return json(['code' => 1, 'msg' => '路径参数不能为空或格式不正确']);
  444. }
  445. // 设置基础路径和压缩目录路径
  446. $basePath = ROOT_PATH . 'public/';
  447. $zipDir = $basePath . 'uploads/operate/ai/zip/';
  448. if (!is_dir($zipDir)) {
  449. mkdir($zipDir, 0755, true);
  450. }
  451. // 压缩包文件名及完整路径
  452. $fileName = 'images_' . date('Ymd_His') . '.zip';
  453. $zipPath = $zipDir . $fileName;
  454. // 创建 Zip 文件
  455. $zip = new \ZipArchive();
  456. if ($zip->open($zipPath, \ZipArchive::CREATE) !== TRUE) {
  457. return json(['code' => 1, 'msg' => '无法创建压缩包']);
  458. }
  459. // 添加文件到压缩包
  460. $addCount = 0;
  461. foreach ($paths as $relativePath) {
  462. $relativePath = ltrim($relativePath, '/');
  463. $fullPath = $basePath . $relativePath;
  464. if (file_exists($fullPath)) {
  465. $zip->addFile($fullPath, basename($fullPath)); // 仅保存文件名
  466. $addCount++;
  467. }
  468. }
  469. $zip->close();
  470. if ($addCount === 0) {
  471. return json(['code' => 1, 'msg' => '未找到有效图片,未生成压缩包']);
  472. }
  473. // 返回下载地址(注意路径与保存路径一致)
  474. $downloadUrl = request()->domain() . '/uploads/operate/ai/zip/' . $fileName;
  475. return json([
  476. 'code' => 0,
  477. 'msg' => '打包成功',
  478. 'download_url' => $downloadUrl
  479. ]);
  480. } catch (\Exception $e) {
  481. return json([
  482. 'code' => 1,
  483. 'msg' => '异常错误:' . $e->getMessage()
  484. ]);
  485. }
  486. }
  487. }