Facility.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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')->alias('b')
  66. ->field('b.chinese_description,b.english_description,b.new_image_url,b.custom_image_url,b.size,b.old_image_url,b.img_name')
  67. ->where('old_image_url', $params)
  68. ->where('custom_image_url', '<>', '')
  69. ->where('status', 1)
  70. ->order('b.id desc')
  71. ->select();
  72. return json(['code' => 0, 'msg' => '查询成功', 'data' => $res,'count'=>count($res)]);
  73. }
  74. //获取指定目录所有图片
  75. public function getPreviewimg()
  76. {
  77. $page = (int)$this->request->param('page', 1);
  78. $limit = (int)$this->request->param('limit', 50);
  79. $status = $this->request->param('status', '');
  80. $relativePath = $this->request->param('path', '');
  81. $basePath = ROOT_PATH . 'public/';
  82. $fullPath = $basePath . $relativePath;
  83. if (!is_dir($fullPath)) {
  84. return json(['code' => 1, 'msg' => '目录不存在']);
  85. }
  86. // 1. 获取所有图片路径(不再全部加载到内存)
  87. $allImages = glob($fullPath . '/*.{jpg,jpeg,png}', GLOB_BRACE);
  88. if (empty($allImages)) {
  89. return json(['code' => 0, 'msg' => '暂无图片', 'data' => [], 'total' => 0]);
  90. }
  91. // ✅ 加入排序:按照创建时间从新到旧
  92. usort($allImages, function ($a, $b) {
  93. return filectime($b) - filectime($a);
  94. });
  95. // 构建相对路径数组
  96. $relativeImages = array_map(function ($imgPath) use ($basePath) {
  97. return str_replace($basePath, '', $imgPath);
  98. }, $allImages);
  99. // 2. 提前构建是否已出图map
  100. $dbRecords = Db::name('text_to_image')
  101. ->whereIn('old_image_url', $relativeImages)
  102. ->where('custom_image_url', '<>', '')
  103. ->field('old_image_url,new_image_url,custom_image_url,chinese_description,english_description,img_name')
  104. ->select();
  105. $processedMap = [];
  106. foreach ($dbRecords as $item) {
  107. $processedMap[$item['old_image_url']] = $item;
  108. }
  109. // 3. 提前获取 same_count 的统计
  110. $sameCountMap = Db::name('text_to_image')
  111. ->whereIn('old_image_url', $relativeImages)
  112. ->where('custom_image_url', '<>', '')
  113. ->group('old_image_url')
  114. ->column('count(*) as cnt', 'old_image_url');
  115. // 4. 构造最终筛选数据(分页前进行状态筛选)
  116. $filtered = [];
  117. foreach ($allImages as $imgPath) {
  118. $relative = str_replace($basePath, '', $imgPath);
  119. $processed = $processedMap[$relative] ?? null;
  120. $isProcessed = $processed ? 1 : 0;
  121. // 状态过滤
  122. if ($status === 'processed' && !$isProcessed) continue;
  123. if ($status === 'unprocessed' && $isProcessed) continue;
  124. $info = @getimagesize($imgPath); // 加@防止报错
  125. $sizeKB = round(filesize($imgPath) / 1024, 2);
  126. $ctime = date('Y-m-d H:i:s', filectime($imgPath));
  127. $filtered[] = [
  128. 'path' => $relative,
  129. 'width' => $info[0] ?? 0,
  130. 'height' => $info[1] ?? 0,
  131. 'size_kb' => $sizeKB,
  132. 'created_time' => $ctime,
  133. 'img_name' => $processed['img_name'] ?? '',
  134. 'is_processed' => $isProcessed,
  135. 'new_image_url' => $processed['new_image_url'] ?? '',
  136. 'custom_image_url' => $processed['custom_image_url'] ?? '',
  137. 'chinese_description' => ($processed['chinese_description'] ?? '') . ($processed['english_description'] ?? ''),
  138. 'same_count' => $sameCountMap[$relative] ?? 0
  139. ];
  140. }
  141. // 5. 手动分页(对少量已筛选后的数据)
  142. $total = count($filtered);
  143. $pagedData = array_slice($filtered, ($page - 1) * $limit, $limit);
  144. foreach ($pagedData as $index => &$item) {
  145. $item['id'] = ($page - 1) * $limit + $index + 1;
  146. }
  147. return json([
  148. 'code' => 0,
  149. 'msg' => '获取成功',
  150. 'data' => $pagedData,
  151. 'total' => $total,
  152. 'page' => $page,
  153. 'limit' => $limit
  154. ]);
  155. }
  156. /**
  157. * 获取原图目录及每个目录下的图片数量
  158. */
  159. public function getPreviewSubDirs()
  160. {
  161. $baseDir = rtrim(str_replace('\\', '/', ROOT_PATH), '/') . '/public/uploads/operate/ai/Preview';
  162. $baseRelativePath = 'uploads/operate/ai/Preview';
  163. if (!is_dir($baseDir)) {
  164. return json(['code' => 1, 'msg' => '目录不存在']);
  165. }
  166. $dirs = [];
  167. $index = 1;
  168. /**
  169. * 递归扫描目录,提取含图片的子目录信息
  170. */
  171. $scanDir = function ($dirPath, $relativePath) use (&$scanDir, &$dirs, &$index) {
  172. $items = scandir($dirPath);
  173. foreach ($items as $item) {
  174. if ($item === '.' || $item === '..') continue;
  175. $fullPath = $dirPath . '/' . $item;
  176. $relPath = $relativePath . '/' . $item;
  177. if (is_dir($fullPath)) {
  178. // 递归子目录
  179. $scanDir($fullPath, $relPath);
  180. } else {
  181. // 匹配图片文件
  182. if (preg_match('/\.(jpg|jpeg|png)$/i', $item)) {
  183. $parentDir = dirname($fullPath);
  184. $relativeDir = dirname($relPath);
  185. $key = md5($parentDir);
  186. if (!isset($dirs[$key])) {
  187. $ctime = filectime($parentDir);
  188. // 数据库统计:已处理图片数量
  189. $hasData = Db::name('text_to_image')
  190. ->where('custom_image_url', '<>', '')
  191. ->whereLike('old_image_url', $relativeDir . '/%')
  192. ->whereNotNull('custom_image_url')
  193. ->count();
  194. // 当前目录下图片数量
  195. $imageFiles = glob($parentDir . '/*.{jpg,jpeg,png}', GLOB_BRACE);
  196. $imageCount = is_array($imageFiles) ? count($imageFiles) : 0;
  197. $dirs[$key] = [
  198. 'id' => $index++,
  199. 'name' => basename($parentDir),
  200. 'count' => $hasData,
  201. 'ctime' => $ctime, // 时间戳,用于排序
  202. 'ctime_text' => date('Y-m-d H:i:s', $ctime), // 格式化日期,用于显示
  203. 'image_count' => $imageCount,
  204. 'new_image_url' => "/uploads/operate/ai/dall-e/",
  205. 'old_image_url' => $relativeDir
  206. ];
  207. }
  208. }
  209. }
  210. }
  211. };
  212. // 执行目录扫描
  213. $scanDir($baseDir, $baseRelativePath);
  214. // 排序:按照创建时间(从新到旧)
  215. $dirList = array_values($dirs);
  216. usort($dirList, function ($a, $b) {
  217. return $b['ctime'] - $a['ctime'];
  218. });
  219. return json([
  220. 'code' => 0,
  221. 'msg' => '获取成功',
  222. 'data' => $dirList
  223. ]);
  224. }
  225. /**
  226. * 图片上传
  227. * @return void
  228. */
  229. // public function getUploadPath()
  230. // {
  231. // // 处理 CORS OPTIONS 预检请求
  232. // if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  233. // header('Access-Control-Allow-Origin: *');
  234. // header('Access-Control-Allow-Methods: POST, OPTIONS');
  235. // header('Access-Control-Allow-Headers: Content-Type, Authorization');
  236. // header('Access-Control-Max-Age: 86400');
  237. // exit(204);
  238. // }
  239. //
  240. //// 实际请求必须返回 CORS 头
  241. // header('Access-Control-Allow-Origin: *');
  242. // $today = date('Ymd');
  243. // $basePath = 'uploads/operate/ai/Preview/' . $today;
  244. // $rootBasePath = ROOT_PATH . 'public/' . $basePath;
  245. //
  246. // // 创建当天目录
  247. // if (!is_dir($rootBasePath)) {
  248. // mkdir($rootBasePath, 0755, true);
  249. // }
  250. //
  251. // // 获取子目录索引
  252. // $dirs = array_filter(glob($rootBasePath . '/*'), 'is_dir');
  253. // $usedIndexes = [];
  254. //
  255. // foreach ($dirs as $dirPath) {
  256. // $dirName = basename($dirPath);
  257. // if (preg_match('/^\d{2}$/', $dirName)) {
  258. // $usedIndexes[] = intval($dirName);
  259. // }
  260. // }
  261. //
  262. // $nextIndex = empty($usedIndexes) ? 1 : max($usedIndexes) + 1;
  263. // $subDir = str_pad($nextIndex, 2, '0', STR_PAD_LEFT);
  264. // $relativePath = $basePath . '/' . $subDir;
  265. // $targetPath = ROOT_PATH . 'public/' . $relativePath;
  266. //
  267. // // 创建该批次目录
  268. // if (!is_dir($targetPath)) {
  269. // mkdir($targetPath, 0755, true);
  270. // }
  271. //
  272. // return json([
  273. // 'code' => 0,
  274. // 'msg' => '获取上传路径成功',
  275. // 'data' => [
  276. // 'upload_path' => $relativePath
  277. // ]
  278. // ]);
  279. // }
  280. // public function ImgUpload()
  281. // {
  282. // if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  283. // header('Access-Control-Allow-Origin: *');
  284. // header('Access-Control-Allow-Methods: POST, OPTIONS');
  285. // header('Access-Control-Allow-Headers: Content-Type, Authorization');
  286. // header('Access-Control-Max-Age: 86400');
  287. // exit(204);
  288. // }
  289. // header('Access-Control-Allow-Origin: *');
  290. //
  291. // $file = request()->file('image');
  292. // $relativePath = input('post.upload_path');
  293. //
  294. // if (!$file || !$relativePath) {
  295. // return json(['code' => 1, 'msg' => '缺少上传文件或路径参数']);
  296. // }
  297. //
  298. // $targetPath = ROOT_PATH . 'public/' . $relativePath;
  299. //
  300. // if (!is_dir($targetPath)) {
  301. // mkdir($targetPath, 0755, true);
  302. // }
  303. //
  304. // $tmpFilePath = $file->getPathname();
  305. // $extension = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
  306. // $hashName = hash_file('md5', $tmpFilePath);
  307. // $newFileName = $hashName . '.' . $extension;
  308. //
  309. // $info = $file->validate(['size' => 10 * 1024 * 1024, 'ext' => 'jpg,jpeg,png'])
  310. // ->move($targetPath, $newFileName);
  311. //
  312. // if ($info) {
  313. // $imageUrl = $relativePath . '/' . str_replace('\\', '/', $newFileName);
  314. // return json(['code' => 0, 'msg' => '上传成功', 'data' => ['url' => $imageUrl]]);
  315. // } else {
  316. // return json(['code' => 1, 'msg' => '上传失败', 'data' => $file->getError()]);
  317. // }
  318. // }
  319. public function ImgUpload()
  320. {
  321. // 处理 CORS OPTIONS 预检请求
  322. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  323. header('Access-Control-Allow-Origin: *');
  324. header('Access-Control-Allow-Methods: POST, OPTIONS');
  325. header('Access-Control-Allow-Headers: Content-Type, Authorization');
  326. header('Access-Control-Max-Age: 86400');
  327. exit(204);
  328. }
  329. // 实际请求必须返回 CORS 头
  330. header('Access-Control-Allow-Origin: *');
  331. // 获取上传的文件
  332. $file = request()->file('image');
  333. if ($file) {
  334. // 指定目标目录(你想上传到的目录)
  335. $targetPath = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'operate' . DS . 'ai' . DS . 'Preview';
  336. // 若目录不存在则创建
  337. if (!is_dir($targetPath)) {
  338. mkdir($targetPath, 0755, true);
  339. }
  340. // 移动文件到指定目录,并验证大小/格式
  341. $info = $file->validate([
  342. 'size' => 10485760, // 最大10MB
  343. 'ext' => 'jpg,png'
  344. ])->move($targetPath);
  345. if ($info) {
  346. $fileName = $info->getSaveName();
  347. $imageUrl = '/uploads/operate/ai/Preview/' . str_replace('\\', '/', $fileName);
  348. return json(['code' => 0, 'msg' => '成功', 'data' => ['url' => $imageUrl]]);
  349. } else {
  350. $res = $file->getError();
  351. return json(['code' => 1, 'msg' => '失败', 'data' => $res]);
  352. }
  353. }
  354. return json(['code' => 1, 'msg' => '没有文件上传', 'data' => null]);
  355. }
  356. /**
  357. * 模版
  358. */
  359. public function Template(){
  360. $Template = Db::name("template")->find();
  361. return json([
  362. 'code' => 0,
  363. 'msg' => '模版',
  364. 'data' => $Template
  365. ]);
  366. }
  367. /**
  368. * 更新模版
  369. */
  370. public function updatetemplate(){
  371. if (Request::instance()->isPost() == false){
  372. $this->error('非法请求');
  373. }
  374. $params = Request::instance()->post();
  375. // 验证传入的参数是否存在,避免空值
  376. if (empty($params['textareaContent']) || empty($params['width']) || empty($params['height'])) {
  377. return json(['code' => 1, 'msg' => '参数缺失']);
  378. }
  379. // 更新模板数据
  380. $Template = Db::name("template")
  381. ->where('id', 1) // 假设模板 ID 是 1,需根据实际情况修改
  382. ->update([
  383. 'content' => $params['textareaContent'], // 更新图生文模版内容
  384. 'width' => $params['width'], // 更新宽度
  385. 'height' => $params['height'], // 更新宽度
  386. ]);
  387. // 判断数据库更新是否成功
  388. if ($Template){
  389. return json(['code' => 0, 'msg' => '成功']);
  390. }else{
  391. return json(['code' => 1, 'msg' => '失败']);
  392. }
  393. }
  394. }