Facility.php 21 KB

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