Facility.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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, 3600); // 默认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('new_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('new_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. $sys_id = $this->request->param('sys_id', '');
  149. $basePath = ROOT_PATH . 'public/';
  150. $fullPath = $basePath . $relativePath;
  151. if (!is_dir($fullPath)) {
  152. return json(['code' => 1, 'msg' => '原图目录不存在']);
  153. }
  154. // 构建缓存键与构建锁键(仅缓存文件系统信息)
  155. $hash = md5($relativePath);
  156. $cacheKey = "previewimg_fileinfo_{$hash}";
  157. $lockKey = "previewimg_building_{$hash}";
  158. // $cacheExpire = 600; // 10分钟
  159. $cacheExpire = 3600; // 60分钟
  160. $cachedFileInfo = cache($cacheKey);
  161. if (!$cachedFileInfo) {
  162. // 防止缓存"惊群效应"
  163. if (!cache($lockKey)) {
  164. cache($lockKey, 1, 60); // 1分钟构建锁
  165. // 获取所有图片文件信息
  166. $allImages = glob($fullPath . '/*.{jpg,jpeg,png}', GLOB_BRACE);
  167. $fileInfoMap = [];
  168. foreach ($allImages as $imgPath) {
  169. $relative = str_replace('\\', '/', trim(str_replace($basePath, '', $imgPath), '/'));
  170. $info = @getimagesize($imgPath);
  171. $fileInfoMap[$relative] = [
  172. 'width' => $info[0] ?? 0,
  173. 'height' => $info[1] ?? 0,
  174. 'size_kb' => round(filesize($imgPath) / 1024, 2),
  175. 'created_time' => date('Y-m-d H:i:s', filectime($imgPath))
  176. ];
  177. }
  178. // 构建缓存数据(仅文件系统信息)
  179. $cachedFileInfo = [];
  180. foreach (array_keys($fileInfoMap) as $path) {
  181. $cachedFileInfo[] = [
  182. 'path' => $path,
  183. 'info' => $fileInfoMap[$path]
  184. ];
  185. }
  186. // 设置缓存 + 删除构建锁
  187. cache($cacheKey, $cachedFileInfo, $cacheExpire);
  188. cache($lockKey, null);
  189. } else {
  190. // 等待缓存生成
  191. $waitTime = 0;
  192. while (!$cachedFileInfo && $waitTime < 10) {
  193. sleep(1);
  194. $waitTime++;
  195. $cachedFileInfo = cache($cacheKey);
  196. }
  197. if (!$cachedFileInfo) {
  198. return json(['code' => 2, 'msg' => '系统正忙,请稍后重试']);
  199. }
  200. }
  201. }
  202. // 获取所有需要实时查询的路径
  203. $paths = array_column($cachedFileInfo, 'path');
  204. // 实时查询数据库状态信息(单次批量查询)
  205. $dbRecords = Db::name('text_to_image')
  206. ->whereIn('old_image_url', $paths)
  207. ->field('id as img_id, old_image_url, new_image_url, custom_image_url,imgtoimg_url,taskId,chinese_description, english_description, img_name, status, status_name,sys_id')
  208. ->where('sys_id',$sys_id)
  209. ->select();
  210. // 实时查询队列状态(单次批量查询)
  211. $queueRecords = Db::name('image_task_log')
  212. ->where('mod_rq', null)
  213. ->whereIn('file_name', $paths)
  214. ->field('file_name, log')
  215. ->select();
  216. // 实时查询same_count(稍后按需查询)
  217. // 构建映射关系
  218. $processedMap = [];
  219. foreach ($dbRecords as $item) {
  220. $key = str_replace('\\', '/', trim($item['old_image_url'], '/'));
  221. $processedMap[$key] = $item;
  222. }
  223. $queueMap = [];
  224. foreach ($queueRecords as $q) {
  225. $key = str_replace('\\', '/', trim($q['file_name'], '/'));
  226. $queueMap[$key] = $q['log'];
  227. }
  228. // 合并数据
  229. $mergedData = [];
  230. foreach ($cachedFileInfo as $data) {
  231. $path = $data['path'];
  232. $item = $processedMap[$path] ?? [];
  233. $mergedData[] = [
  234. 'path' => $path,
  235. 'item' => $item,
  236. 'info' => $data['info'],
  237. 'dbStatus' => isset($item['status']) ? (int)$item['status'] : 0,
  238. 'dbStatusName' => $item['status_name'] ?? '',
  239. 'isProcessed' => !empty($item['img_name']) && !empty($item['custom_image_url']),
  240. 'queueStatus' => $queueMap[$path] ?? ''
  241. ];
  242. }
  243. // 筛选状态字段
  244. $filtered = array_filter($mergedData, function ($data) use ($status, $status_name) {
  245. // 状态码筛选
  246. if ($status !== '' && (int)$status !== $data['dbStatus']) return false;
  247. // 状态名称筛选
  248. if ($status_name !== '') {
  249. if ($status_name === '未图生文') {
  250. // 当状态名为"未图生文"时,匹配空值或null
  251. if (!empty($data['dbStatusName'])) return false;
  252. } elseif ($status_name === '未文生文') {
  253. if ($data['dbStatusName'] !== '图生文') return false;
  254. } elseif ($status_name === '未文生图') {
  255. if ($data['dbStatusName'] !== '图生文') return false;
  256. } elseif ($status_name === '未图生图') {
  257. if ($data['dbStatusName'] !== '文生图') return false;
  258. } else {
  259. // 其他状态名需要精确匹配
  260. if ($status_name !== $data['dbStatusName']) return false;
  261. }
  262. }
  263. return true;
  264. });
  265. // 分页处理
  266. $total = count($filtered);
  267. $paged = array_slice(array_values($filtered), ($page - 1) * $limit, $limit);
  268. // 实时查询当前页的same_count(优化性能)
  269. $pagedPaths = array_column($paged, 'path');
  270. $sameCountMap = [];
  271. if ($pagedPaths) {
  272. $sameCountMap = Db::name('text_to_image')
  273. ->whereIn('old_image_url', $pagedPaths)
  274. // ->where('new_image_url', '<>', '')
  275. ->where('new_image_url', '<>', '')
  276. ->where('taskId', '<>', '')
  277. ->group('old_image_url')
  278. ->column('count(*) as cnt', 'old_image_url');
  279. }
  280. // 构建最终结果
  281. $result = [];
  282. foreach ($paged as $i => $data) {
  283. $path = $data['path'];
  284. $item = $data['item'];
  285. $info = $data['info'];
  286. $result[] = [
  287. 'id' => ($page - 1) * $limit + $i + 1,
  288. 'ids' => $item['img_id'] ?? '',
  289. 'path' => $path,
  290. // 实时数据
  291. 'status' => $data['dbStatus'],
  292. 'status_name' => $data['dbStatusName'],
  293. 'same_count' => $sameCountMap[$path] ?? 0,
  294. 'is_processed' => $data['isProcessed'] ? 1 : 0,
  295. 'queue_status' => $data['queueStatus'],
  296. 'taskId' => $item['taskId'] ?? '',
  297. 'sys_id' => $item['sys_id'] ?? '',
  298. 'new_image_url' => $item['new_image_url'] ?? '',
  299. 'custom_image_url' => $item['custom_image_url'] ?? '',
  300. 'imgtoimg_url' => $item['imgtoimg_url'] ?? '',
  301. 'chinese_description' => $item['chinese_description'] ?? '',
  302. 'english_description' => $item['english_description'] ?? '',
  303. 'img_name' => $item['img_name'] ?? '',
  304. // 来自缓存
  305. 'width' => $info['width'],
  306. 'height' => $info['height'],
  307. 'created_time' => $info['created_time']
  308. ];
  309. }
  310. return json([
  311. 'code' => 0,
  312. 'msg' => '获取成功',
  313. 'data' => $result,
  314. 'total' => $total,
  315. 'page' => $page,
  316. 'limit' => $limit
  317. ]);
  318. }
  319. /**
  320. * 通过服务器中获取对应目录
  321. */
  322. public function getlsit()
  323. {
  324. // 获取前端传入的图片路径参数
  325. $params = $this->request->param('path', '');
  326. $sys_id = $this->request->param('sys_id', '');
  327. // 查询数据库
  328. $res = Db::name('text_to_image')
  329. ->field('id,chinese_description,english_description,new_image_url,custom_image_url,size,old_image_url,img_name,model,imgtoimg_url')
  330. ->where('old_image_url', $params)
  331. ->where('img_name', '<>', '')
  332. ->where('sys_id',$sys_id)
  333. ->order('id desc')
  334. ->select();
  335. if($res){
  336. return json(['code' => 0, 'msg' => '查询成功', 'data' => $res,'count'=>count($res)]);
  337. }else{
  338. return json(['code' => 0, 'msg' => '查询成功', 'data' => [],'count'=>0]);
  339. }
  340. }
  341. /**
  342. * 图片上传
  343. */
  344. public function ImgUpload()
  345. {
  346. // 处理 CORS OPTIONS 预检请求
  347. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  348. header('Access-Control-Allow-Origin: *');
  349. header('Access-Control-Allow-Methods: POST, OPTIONS');
  350. header('Access-Control-Allow-Headers: Content-Type, Authorization');
  351. header('Access-Control-Max-Age: 86400');
  352. exit(204);
  353. }
  354. // 实际请求必须返回 CORS 头
  355. header('Access-Control-Allow-Origin: *');
  356. // 获取上传的文件
  357. $file = request()->file('image');
  358. if ($file) {
  359. // 生成日期格式的文件夹名 image_YYYYMMDD
  360. $dateFolder = 'image_' . date('Ymd');
  361. // 指定目标目录(包含日期文件夹)
  362. $targetPath = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'operate' . DS . 'ai' . DS . 'Preview' . DS . $dateFolder;
  363. // 若目录不存在则创建
  364. if (!is_dir($targetPath)) {
  365. mkdir($targetPath, 0755, true);
  366. }
  367. // 获取原始文件名(或自定义新文件名)
  368. $originalName = $file->getInfo('name'); // 原始文件名
  369. $extension = pathinfo($originalName, PATHINFO_EXTENSION); // 文件扩展名
  370. $newFileName = uniqid() . '.' . $extension; // 生成唯一文件名(避免冲突)
  371. // 移动文件到指定目录,并验证大小/格式,同时指定自定义文件名
  372. $info = $file->validate([
  373. 'size' => 10485760, // 最大10MB
  374. 'ext' => 'jpg,png'
  375. ])->move($targetPath, $newFileName); // 关键:手动指定文件名,避免自动生成日期目录
  376. if ($info) {
  377. // 直接拼接路径,不依赖 getSaveName() 的返回值
  378. $imageUrl = '/uploads/operate/ai/Preview/' . $dateFolder . '/' . $newFileName;
  379. return json(['code' => 0, 'msg' => '成功', 'data' => ['url' => $imageUrl]]);
  380. } else {
  381. $res = $file->getError();
  382. return json(['code' => 1, 'msg' => '失败', 'data' => $res]);
  383. }
  384. }
  385. return json(['code' => 1, 'msg' => '没有文件上传', 'data' => null]);
  386. }
  387. /**
  388. * 打包图片(支持对象结构中的 path 字段)
  389. */
  390. public function packImagess()
  391. {
  392. try {
  393. $params = $this->request->post();
  394. $paths = $params['paths'] ?? [];
  395. if (empty($paths) || !is_array($paths)) {
  396. return json(['code' => 1, 'msg' => '路径参数不能为空或格式不正确']);
  397. }
  398. // 提取所有合法的路径(支持字符串或对象中带 path 字段)
  399. $validPaths = [];
  400. foreach ($paths as $item) {
  401. if (is_string($item)) {
  402. $validPaths[] = $item;
  403. } elseif (is_array($item) && isset($item['path']) && is_string($item['path'])) {
  404. $validPaths[] = $item['path'];
  405. }
  406. }
  407. if (empty($validPaths)) {
  408. return json(['code' => 1, 'msg' => '没有有效的图片路径']);
  409. }
  410. // 设置基本路径和 zip 目录
  411. $basePath = ROOT_PATH . 'public/';
  412. $zipDir = $basePath . 'uploads/operate/ai/zip/';
  413. if (!is_dir($zipDir)) {
  414. mkdir($zipDir, 0755, true);
  415. }
  416. // 生成压缩文件路径
  417. $fileName = 'images_' . date('Ymd_His') . '.zip';
  418. $zipPath = $zipDir . $fileName;
  419. $zip = new \ZipArchive();
  420. if ($zip->open($zipPath, \ZipArchive::CREATE) !== TRUE) {
  421. return json(['code' => 1, 'msg' => '无法创建压缩包']);
  422. }
  423. $addCount = 0;
  424. foreach ($validPaths as $relativePath) {
  425. $relativePath = ltrim($relativePath, '/'); // 去除前导斜杠
  426. $fullPath = $basePath . $relativePath;
  427. if (file_exists($fullPath)) {
  428. // 使用 basename 作为压缩包内的文件名(不保留路径结构)
  429. $zip->addFile($fullPath, basename($fullPath));
  430. $addCount++;
  431. }
  432. }
  433. $zip->close();
  434. if ($addCount === 0) {
  435. @unlink($zipPath);
  436. return json(['code' => 1, 'msg' => '未找到有效图片文件,未生成压缩包']);
  437. }
  438. $downloadUrl = request()->domain() . '/uploads/operate/ai/zip/' . $fileName;
  439. return json([
  440. 'code' => 0,
  441. 'msg' => '打包成功',
  442. 'download_url' => $downloadUrl
  443. ]);
  444. } catch (\Exception $e) {
  445. return json([
  446. 'code' => 1,
  447. 'msg' => '异常错误:' . $e->getMessage()
  448. ]);
  449. }
  450. }
  451. /**
  452. * 获取所有模版列表,并返回当前使用模版 ID
  453. */
  454. public function TemplateList()
  455. {
  456. $params = Request::instance()->param();
  457. $path = $params['path'] ?? '';
  458. if (empty($path)) {
  459. return json([
  460. 'code' => 400,
  461. 'msg' => '缺少 path 参数',
  462. 'data' => []
  463. ]);
  464. }
  465. // 先查询是否已有数据
  466. $list = Db::name("template")
  467. ->where('path', $path)
  468. ->order('ids', 'desc')
  469. ->select();
  470. // 如果有记录,直接返回
  471. if ($list) {
  472. return json([
  473. 'code' => 0,
  474. 'msg' => '模版列表',
  475. 'data' => [
  476. 'list' => $list,
  477. 'usedId' => Db::name("template")->where('path', $path)->where('ids', 1)->value('id')
  478. ]
  479. ]);
  480. }
  481. $res_ids = Db::name("template")->where('ids', 99)->find();
  482. // 否则插入默认模板
  483. $default = [
  484. 'path' => $path,
  485. 'english_content' => '',
  486. 'content' => $res_ids['content'],
  487. 'width' => '1024',
  488. 'height' => '1303',
  489. 'ids' => 1
  490. ];
  491. $insertId = Db::name("template")->insertGetId($default);
  492. if ($insertId) {
  493. $list = Db::name("template")
  494. ->where('path', $path)
  495. ->order('ids', 'desc')
  496. ->select();
  497. return json([
  498. 'code' => 0,
  499. 'msg' => '模版列表(已创建默认)',
  500. 'data' => [
  501. 'list' => $list,
  502. 'usedId' => Db::name("template")->where('path', $path)->where('ids', 1)->value('id')
  503. ]
  504. ]);
  505. }
  506. // 插入失败
  507. return json([
  508. 'code' => 500,
  509. 'msg' => '创建默认模版失败',
  510. 'data' => []
  511. ]);
  512. }
  513. /**
  514. * 查询使用模版
  515. */
  516. public function Template_ids(){
  517. $params = Request::instance()->param();
  518. $Template = Db::name("template")->where('path',$params['path'])->where('ids',1)->find();
  519. return json([
  520. 'code' => 0,
  521. 'msg' => '模版',
  522. 'data' => $Template
  523. ]);
  524. }
  525. /**
  526. * 查询模版
  527. */
  528. public function Template()
  529. {
  530. $id = Request::instance()->param('id');
  531. if (!$id) {
  532. return json(['code' => 1, 'msg' => '参数错误']);
  533. }
  534. $template = Db::name("template")->where('id', $id)->find();
  535. return json([
  536. 'code' => 0,
  537. 'msg' => '模版详情',
  538. 'data' => $template
  539. ]);
  540. }
  541. /**
  542. * 更新模版
  543. */
  544. public function updatetemplate()
  545. {
  546. if (!Request::instance()->isPost()) {
  547. return json(['code' => 1, 'msg' => '非法请求']);
  548. }
  549. $params = Request::instance()->post();
  550. if (empty($params['textareaContent']) || empty($params['width']) || empty($params['height'])) {
  551. return json(['code' => 1, 'msg' => '参数缺失']);
  552. }
  553. $data = [
  554. 'path' => $params['path'],
  555. 'english_content' => $params['english_content'],
  556. 'content' => $params['textareaContent'],
  557. 'width' => $params['width'],
  558. 'height' => $params['height'],
  559. ];
  560. if (!empty($params['id'])) {
  561. // 更新已有模版
  562. Db::name("template")->where('id', $params['id'])->update($data);
  563. } else {
  564. // 新增模版,默认设为备用
  565. $data['ids'] = 0;
  566. Db::name("template")->insert($data);
  567. }
  568. return json(['code' => 0, 'msg' => '保存成功']);
  569. }
  570. /**
  571. * 设置默认使用模版
  572. */
  573. public function setActiveTemplate()
  574. {
  575. $id = Request::instance()->param('id');
  576. $params = Request::instance()->param();
  577. if (!$id) {
  578. return json(['code' => 1, 'msg' => '参数错误']);
  579. }
  580. Db::name("template")->where('path',$params['path'])->where('ids', 1)->update(['ids' => 0]); // 清除当前使用
  581. Db::name("template")->where('path',$params['path'])->where('id', $id)->update(['ids' => 1]); // 设置新的使用模版
  582. return json(['code' => 0, 'msg' => '模版已设为当前使用']);
  583. }
  584. public function txttoimg_moxing()
  585. {
  586. // 获取所有模型数据
  587. $list = Db::name("moxing")->order('id asc')->select();
  588. // 初始化分类数组
  589. $classifiedData = [
  590. 'wenshengwen' => [], // 文生文模型 (txttotxt)
  591. 'tushengwen' => [], // 图生文模型 (imgtotxt)
  592. 'wenshengtu' => [] // 文生图模型 (txttoimg)
  593. ];
  594. // 当前使用的模型ID
  595. $usedIds = [
  596. 'wenshengwen' => null,
  597. 'tushengwen' => null,
  598. 'wenshengtu' => null
  599. ];
  600. // 分类处理数据
  601. foreach ($list as $model) {
  602. // 文生文模型 (txttotxt)
  603. if (!empty($model['txttotxt'])) {
  604. $classifiedData['wenshengwen'][] = [
  605. 'id' => $model['id'],
  606. 'txttotxt' => $model['txttotxt'],
  607. 'txttotxt_val' => $model['txttotxt_val']
  608. ];
  609. if ($model['txttotxt_val'] == 1) {
  610. $usedIds['wenshengwen'] = $model['id'];
  611. }
  612. }
  613. // 图生文模型 (imgtotxt)
  614. if (!empty($model['imgtotxt'])) {
  615. $classifiedData['tushengwen'][] = [
  616. 'id' => $model['id'],
  617. 'imgtotxt' => $model['imgtotxt'],
  618. 'imgtotxt_val' => $model['imgtotxt_val']
  619. ];
  620. if ($model['imgtotxt_val'] == 1) {
  621. $usedIds['tushengwen'] = $model['id'];
  622. }
  623. }
  624. // 文生图模型 (txttoimg)
  625. if (!empty($model['txttoimg'])) {
  626. $classifiedData['wenshengtu'][] = [
  627. 'id' => $model['id'],
  628. 'txttoimg' => $model['txttoimg'],
  629. 'txttoimg_val' => $model['txttoimg_val']
  630. ];
  631. if ($model['txttoimg_val'] == 1) {
  632. $usedIds['wenshengtu'] = $model['id'];
  633. }
  634. }
  635. }
  636. return json([
  637. 'code' => 0,
  638. 'msg' => '模型分类列表',
  639. 'data' => [
  640. 'models' => $classifiedData,
  641. 'used_ids' => $usedIds
  642. ]
  643. ]);
  644. }
  645. public function txttoimg_update()
  646. {
  647. // 获取并验证参数
  648. $id = input('id/d', 0); // 强制转换为整数
  649. $type = input('type/s', ''); // 强制转换为字符串
  650. if ($id <= 0) {
  651. return json(['code' => 1, 'msg' => '无效的模型ID']);
  652. }
  653. if (!in_array($type, ['tushengwen', 'wenshengwen', 'wenshengtu'])) {
  654. return json(['code' => 1, 'msg' => '无效的模型类型']);
  655. }
  656. // 定义模型类型与字段的映射关系
  657. $fieldMap = [
  658. 'tushengwen' => 'imgtotxt_val',
  659. 'wenshengwen' => 'txttotxt_val',
  660. 'wenshengtu' => 'txttoimg_val'
  661. ];
  662. $field = $fieldMap[$type];
  663. try {
  664. // 开启事务确保数据一致性
  665. Db::startTrans();
  666. // 1. 重置该类型下所有模型的激活状态
  667. Db::name("moxing")
  668. ->where($field, 1)
  669. ->update([$field => 0]);
  670. // 2. 设置指定模型为激活状态
  671. $result = Db::name("moxing")
  672. ->where('id', $id)
  673. ->update([$field => 1]);
  674. Db::commit();
  675. if ($result) {
  676. return json([
  677. 'code' => 0,
  678. 'msg' => '设置成功',
  679. 'data' => [
  680. 'id' => $id,
  681. 'type' => $type
  682. ]
  683. ]);
  684. }
  685. return json(['code' => 1, 'msg' => '设置失败,模型不存在或未变更']);
  686. } catch (\Exception $e) {
  687. Db::rollback();
  688. return json([
  689. 'code' => 2,
  690. 'msg' => '设置失败: ' . $e->getMessage()
  691. ]);
  692. }
  693. }
  694. //获取原文件夹数据
  695. public function getPreviewFolders()
  696. {
  697. $rootPath = app()->getRootPath(); // 更标准
  698. $baseDir = rtrim(str_replace('\\', '/', $rootPath), '/') . '/public/uploads/operate/ai/Preview';
  699. $cacheDir = $rootPath . 'runtime/cache/';
  700. $cacheFile = $cacheDir . 'folder_list.json';
  701. $cacheTTL = 86400; // 缓存 1 天
  702. $forceRefresh = input('refresh/d', 0); // 更安全,强制转 int
  703. try {
  704. if (!is_dir($baseDir)) {
  705. return json([
  706. 'code' => 404,
  707. 'msg' => '预览目录不存在',
  708. 'data' => []
  709. ]);
  710. }
  711. $useCache = file_exists($cacheFile) && (time() - filemtime($cacheFile)) < $cacheTTL;
  712. $folders = [];
  713. if ($useCache && !$forceRefresh) {
  714. $folders = json_decode(file_get_contents($cacheFile), true) ?: [];
  715. } else {
  716. // 重新扫描目录
  717. $directory = new \RecursiveDirectoryIterator($baseDir, \RecursiveDirectoryIterator::SKIP_DOTS);
  718. $iterator = new \RecursiveIteratorIterator($directory, \RecursiveIteratorIterator::SELF_FIRST);
  719. foreach ($iterator as $file) {
  720. if ($file->isDir()) {
  721. $fullPath = str_replace('\\', '/', $file->getPathname());
  722. // 去掉 public/ 之前的路径(使其相对路径用于前端)
  723. $relativePath = ltrim(str_replace(str_replace('\\', '/', $rootPath . 'public/'), '', $fullPath), '/');
  724. $folders[] = [
  725. 'name' => basename($fullPath),
  726. 'path' => $relativePath,
  727. 'full_path' => $fullPath
  728. ];
  729. }
  730. }
  731. // 写入缓存(带锁)
  732. if (!is_dir($cacheDir)) {
  733. mkdir($cacheDir, 0777, true);
  734. }
  735. file_put_contents($cacheFile, json_encode($folders, JSON_UNESCAPED_UNICODE), LOCK_EX);
  736. }
  737. return json([
  738. 'code' => 0,
  739. 'msg' => '获取预览文件夹成功',
  740. 'data' => [
  741. 'folders' => $folders,
  742. 'total' => count($folders),
  743. 'from_cache' => $useCache && !$forceRefresh
  744. ]
  745. ]);
  746. } catch (\Exception $e) {
  747. return json([
  748. 'code' => 500,
  749. 'msg' => '服务器错误: ' . $e->getMessage(),
  750. 'data' => []
  751. ]);
  752. }
  753. }
  754. }