AiNew.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\service\ImageService;
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. use PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf;
  7. use think\Config;
  8. use think\Db;
  9. use think\Exception;
  10. use think\Log;
  11. use think\Request;
  12. use PhpOffice\PhpSpreadsheet\IOFactory;
  13. use function EasyWeChat\Kernel\Support\rsa_public_encrypt;
  14. use function fast\e;
  15. use RecursiveDirectoryIterator;
  16. use RecursiveIteratorIterator;
  17. /**
  18. * AI出图
  19. */
  20. class AiNew extends Api
  21. {
  22. protected $noNeedLogin = ['*'];
  23. protected $noNeedRight = ['*'];
  24. /**
  25. * 出图接口
  26. * 此方法处理图像转换为文本的请求,将图像信息存入队列以供后续处理。
  27. */
  28. public function imageToText()
  29. {
  30. $params = $this->request->param();
  31. $service = new ImageService();
  32. $service->handleImage($params);
  33. $this->success('任务成功提交至队列');
  34. }
  35. /**
  36. * 查询队列列表
  37. * 统计文件对应的队列情况
  38. */
  39. public function get_queue_logs()
  40. {
  41. $params = $this->request->param('old_image_file', '');
  42. $queue_logs = Db::name('queue_logs')
  43. ->where('old_image_file', $params)
  44. ->order('id desc')
  45. ->select();
  46. $result = []; //初始化变量,避免未定义错误
  47. foreach ($queue_logs as &$log) {
  48. $taskId = $log['id'];
  49. $statusCount = Db::name('image_task_log')
  50. ->field('status, COUNT(*) as count')
  51. ->where('task_id', $taskId)
  52. ->where('mod_rq', null)
  53. ->group('status')
  54. ->select();
  55. $log['已完成数量'] = 0;
  56. $log['处理中数量'] = 0;
  57. $log['排队中的数量'] = 0;
  58. $log['失败数量'] = 0;
  59. foreach ($statusCount as $item) {
  60. switch ($item['status']) {
  61. case 0:
  62. $log['排队中的数量'] = $item['count'];
  63. break;
  64. case 1:
  65. $log['处理中数量'] = $item['count'];
  66. break;
  67. case 2:
  68. $log['已完成数量'] = $item['count'];
  69. break;
  70. case -1:
  71. $log['失败数量'] = $item['count'];
  72. break;
  73. }
  74. }
  75. // if ($log['排队中的数量'] >$log['已完成数量']) {
  76. // $result[] = $log;
  77. // }
  78. if ($log['排队中的数量']) {
  79. $result[] = $log;
  80. }
  81. // if ($log['处理中数量'] >= 0) {
  82. // $result[] = $log;
  83. // }
  84. }
  85. return json([
  86. 'code' => 0,
  87. 'msg' => '查询成功',
  88. 'data' => $result,
  89. 'count' => count($result)
  90. ]);
  91. }
  92. /**
  93. * 查询总队列状态(统计当前处理的数据量)
  94. */
  95. public function queueStats()
  96. {
  97. $statusList = Db::name('image_task_log')
  98. ->field('status, COUNT(*) as total')
  99. ->where('mod_rq', null)
  100. ->where('create_time', '>=', date('Y-m-d 00:00:00'))
  101. ->group('status')
  102. ->select();
  103. $statusCount = [];
  104. foreach ($statusList as $item) {
  105. $statusCount[$item['status']] = $item['total'];
  106. }
  107. // 总数为所有状态和
  108. $total = array_sum($statusCount);
  109. //获取队列当前状态
  110. $statusText = Db::name('queue_logs')->order('id desc')->value('status');
  111. return json([
  112. 'code' => 0,
  113. 'msg' => '获取成功',
  114. 'data' => [
  115. '总任务数' => $total,
  116. '待处理' => $statusCount[0] ?? 0,
  117. '处理中' => $statusCount[1] ?? 0,
  118. '成功' => $statusCount[2] ?? 0,
  119. '失败' => $statusCount[-1] ?? 0,
  120. '当前状态' => $statusText
  121. ]
  122. ]);
  123. }
  124. /**
  125. * 显示当前运行中的队列监听进程
  126. */
  127. public function viewQueueStatus()
  128. {
  129. $redis = new \Redis();
  130. $redis->connect('127.0.0.1', 6379);
  131. $redis->auth('123456');
  132. $redis->select(15);
  133. $key = 'queues:imgtotxt';
  134. // 判断 key 是否存在,避免报错
  135. if (!$redis->exists($key)) {
  136. return json([
  137. 'code' => 0,
  138. 'msg' => '查询成功,队列为空',
  139. 'count' => 0,
  140. 'tasks_preview' => []
  141. ]);
  142. }
  143. $count = $redis->lLen($key);
  144. $list = $redis->lRange($key, 0, 9);
  145. // 解码 JSON 内容,确保每一项都有效
  146. $parsed = array_filter(array_map(function ($item) {
  147. return json_decode($item, true);
  148. }, $list), function ($item) {
  149. return !is_null($item);
  150. });
  151. return json([
  152. 'code' => 0,
  153. 'msg' => '查询成功',
  154. 'count' => $count,
  155. 'tasks_preview' => $parsed
  156. ]);
  157. }
  158. /**
  159. * 清空队列并删除队列日志记录
  160. */
  161. public function stopQueueProcesses()
  162. {
  163. Db::name('image_task_log')
  164. ->where('log', '队列中')
  165. ->whereOr('status', 1)
  166. ->where('create_time', '>=', date('Y-m-d 00:00:00'))
  167. ->update([
  168. 'status' => "-1",
  169. 'log' => '清空取消队列',
  170. 'mod_rq' => date('Y-m-d H:i:s')
  171. ]);
  172. Db::name('image_task_log')
  173. ->whereLike('log', '%处理中%')
  174. ->where('create_time', '>=', date('Y-m-d 00:00:00'))
  175. ->update([
  176. 'status' => "-1",
  177. 'log' => '清空取消队列',
  178. 'mod_rq' => date('Y-m-d H:i:s')
  179. ]);
  180. $redis = new \Redis();
  181. $redis->connect('127.0.0.1', 6379);
  182. $redis->auth('123456');
  183. $redis->select(15);
  184. $key_txttoimg = 'queues:txttoimg:reserved';
  185. $key_txttotxt = 'queues:txttotxt:reserved';
  186. $key_imgtotxt = 'queues:imgtotxt:reserved';
  187. $key_imgtoimg = 'queues:imgtoimg:reserved';
  188. // 清空 Redis 队列
  189. $redis->del($key_txttoimg);
  190. $redis->del($key_txttotxt);
  191. $redis->del($key_imgtotxt);
  192. $redis->del($key_imgtoimg);
  193. return json([
  194. 'code' => 0,
  195. 'msg' => '成功停止队列任务'
  196. ]);
  197. }
  198. public function getPreviewSubDirs()
  199. {
  200. $baseDir = rtrim(str_replace('\\', '/', ROOT_PATH), '/') . '/public/uploads/operate/ai/Preview';
  201. $baseRelativePath = 'uploads/operate/ai/Preview';
  202. if (!is_dir($baseDir)) {
  203. return json(['code' => 1, 'msg' => '目录不存在']);
  204. }
  205. // 包含 DB 时间戳扰动的缓存键
  206. $version = $this->generateFlexibleDirectoryHash($baseDir);
  207. $cacheKey = 'preview_flexible_dirs_' . $version;
  208. $dirList = cache($cacheKey);
  209. if (!$dirList) {
  210. $dirList = $this->scanFlexibleDirectories($baseDir, $baseRelativePath);
  211. cache($cacheKey, $dirList, 3600); // 默认300 缓存 5 分钟(可调)
  212. } else {
  213. // 实时刷新 new_image_count(避免缓存值过期)
  214. foreach ($dirList as &$dir) {
  215. $dir['new_image_count'] = Db::name('text_to_image')
  216. ->where('status', 1)
  217. ->where('new_image_url', '<>', '')
  218. ->where('img_name', '<>', '')
  219. ->whereLike('old_image_url', $dir['old_image_url'] . '/%')
  220. ->count();
  221. }
  222. }
  223. return json([
  224. 'code' => 0,
  225. 'msg' => '获取成功',
  226. 'data' => $dirList
  227. ]);
  228. }
  229. private function scanFlexibleDirectories($baseDir, $baseRelativePath)
  230. {
  231. $dirs = [];
  232. $index = 1;
  233. $firstLevelDirs = glob($baseDir . '/*', GLOB_ONLYDIR);
  234. foreach ($firstLevelDirs as $level1Path) {
  235. $secondLevelDirs = glob($level1Path . '/*', GLOB_ONLYDIR);
  236. if ($secondLevelDirs) {
  237. foreach ($secondLevelDirs as $level2Path) {
  238. $dirs = array_merge($dirs, $this->processDir($level2Path, $baseDir, $baseRelativePath, $index));
  239. }
  240. } else {
  241. $dirs = array_merge($dirs, $this->processDir($level1Path, $baseDir, $baseRelativePath, $index));
  242. }
  243. }
  244. usort($dirs, function ($a, $b) {
  245. return $b['id'] - $a['id'];
  246. });
  247. return $dirs;
  248. }
  249. private function processDir($fullPath, $baseDir, $baseRelativePath, &$index)
  250. {
  251. $result = [];
  252. $relativeDir = ltrim(str_replace($baseDir, '', $fullPath), '/');
  253. $ctime = @filectime($fullPath) ?: time();
  254. $imageFiles = glob($fullPath . '/*.{jpg,jpeg,png}', GLOB_BRACE);
  255. $originalImageCount = $imageFiles ? count($imageFiles) : 0;
  256. $img_count = Db::name('text_to_image')
  257. ->where('status', 1)
  258. ->where('new_image_url', '<>', '')
  259. ->where('img_name', '<>', '')
  260. ->whereLike('old_image_url', $baseRelativePath . '/' . $relativeDir . '/%')
  261. ->count();
  262. $queueLog = Db::name('image_task_log')
  263. ->whereLike('file_name', $baseRelativePath . '/' . $relativeDir . '/%')
  264. ->whereLike('log', '%处理中%')
  265. ->order('id', 'desc')
  266. ->find();
  267. if ($img_count === 0 && !$queueLog && $originalImageCount === 0) {
  268. return [];
  269. }
  270. $result[] = [
  271. 'id' => $index++,
  272. 'name' => basename($fullPath),
  273. 'ctime' => $ctime,
  274. 'ctime_text' => date('Y-m-d H:i:s', $ctime),
  275. 'old_img_count' => $originalImageCount,
  276. 'new_image_count' => $img_count,
  277. 'old_image_url' => $baseRelativePath . '/' . $relativeDir,
  278. 'new_image_url' => '/uploads/operate/ai/dall-e/',
  279. 'queueLog_id' => $queueLog['id'] ?? '',
  280. 'queueLog_task_id' => $queueLog['task_id'] ?? '',
  281. 'queueLog_model_name' => $queueLog['model_name'] ?? '',
  282. 'queueLog_model_name_status' => $queueLog ? 1 : 0,
  283. ];
  284. return $result;
  285. }
  286. private function generateFlexibleDirectoryHash($baseDir)
  287. {
  288. $hash = '';
  289. $dirPaths = [];
  290. $firstDirs = glob($baseDir . '/*', GLOB_ONLYDIR);
  291. foreach ($firstDirs as $dir1) {
  292. $subDirs = glob($dir1 . '/*', GLOB_ONLYDIR);
  293. if ($subDirs) {
  294. foreach ($subDirs as $sub) {
  295. $dirPaths[] = $sub;
  296. $hash .= basename($dir1) . '/' . basename($sub) . filemtime($sub);
  297. }
  298. } else {
  299. $dirPaths[] = $dir1;
  300. $hash .= basename($dir1) . filemtime($dir1);
  301. }
  302. }
  303. $baseRelativePath = 'uploads/operate/ai/Preview';
  304. $queueStatusBits = [];
  305. foreach ($dirPaths as $fullPath) {
  306. $relativeDir = ltrim(str_replace($baseDir, '', $fullPath), '/');
  307. $fileNameLike = $baseRelativePath . '/' . $relativeDir . '/%';
  308. // 查询是否存在任何“处理中”的记录
  309. $logs = Db::name('image_task_log')
  310. ->whereLike('file_name', $fileNameLike)
  311. ->whereLike('log', '%处理中%')
  312. ->select();
  313. // 转换为布尔状态再转成位标记(0 或 1)
  314. $queueStatusBits[] = count($logs) > 0 ? '1' : '0';
  315. // 可选:调试打印
  316. // echo "<pre>路径:{$fileNameLike} => 状态:" . (count($logs) > 0 ? '有处理中' : '无') . "</pre>";
  317. }
  318. // 队列状态位图拼接
  319. $queueStatusHash = implode('', $queueStatusBits); // 如:'01001'
  320. $hash .= '_QS_' . md5($queueStatusHash); // 状态稳定扰动,无需 time()
  321. return md5($hash);
  322. }
  323. /**
  324. * 获取指定目录所有图片(完全实时版本)
  325. */
  326. public function getPreviewimg()
  327. {
  328. $page = (int)$this->request->param('page', 1);
  329. $limit = (int)$this->request->param('limit', 50);
  330. $status = $this->request->param('status', '');
  331. $status_name = $this->request->param('status_name', '');
  332. $relativePath = $this->request->param('path', '');
  333. $sys_id = $this->request->param('sys_id', '');
  334. $basePath = ROOT_PATH . 'public/';
  335. $fullPath = $basePath . $relativePath;
  336. if (!is_dir($fullPath)) {
  337. return json(['code' => 1, 'msg' => '原图目录不存在']);
  338. }
  339. // 构建缓存键与构建锁键(仅缓存文件系统信息),包含sys_id以确保不同sys_id的数据有各自的缓存
  340. // 更新缓存键前缀,确保旧缓存失效
  341. $hash = md5($relativePath . $sys_id);
  342. $cacheKey = "previewimg_fileinfo_v2_{$hash}";
  343. $lockKey = "previewimg_building_v2_{$hash}";
  344. // $cacheExpire = 600; // 10分钟
  345. $cacheExpire = 3600; // 60分钟
  346. $cachedFileInfo = cache($cacheKey);
  347. if (!$cachedFileInfo) {
  348. // 防止缓存"惊群效应"
  349. if (!cache($lockKey)) {
  350. cache($lockKey, 1, 60); // 1分钟构建锁
  351. // 获取所有图片文件信息
  352. $allImages = glob($fullPath . '/*.{jpg,jpeg,png}', GLOB_BRACE);
  353. $fileInfoMap = [];
  354. foreach ($allImages as $imgPath) {
  355. $relative = str_replace('\\', '/', trim(str_replace($basePath, '', $imgPath), '/'));
  356. $info = @getimagesize($imgPath);
  357. $fileInfoMap[$relative] = [
  358. 'width' => $info[0] ?? 0,
  359. 'height' => $info[1] ?? 0,
  360. 'size_kb' => round(filesize($imgPath) / 1024, 2),
  361. 'created_time' => date('Y-m-d H:i:s', filectime($imgPath))
  362. ];
  363. }
  364. // 构建缓存数据(仅文件系统信息)
  365. $cachedFileInfo = [];
  366. foreach (array_keys($fileInfoMap) as $path) {
  367. $cachedFileInfo[] = [
  368. 'path' => $path,
  369. 'info' => $fileInfoMap[$path]
  370. ];
  371. }
  372. // 设置缓存 + 删除构建锁
  373. cache($cacheKey, $cachedFileInfo, $cacheExpire);
  374. cache($lockKey, null);
  375. } else {
  376. // 等待缓存生成
  377. $waitTime = 0;
  378. while (!$cachedFileInfo && $waitTime < 10) {
  379. sleep(1);
  380. $waitTime++;
  381. $cachedFileInfo = cache($cacheKey);
  382. }
  383. if (!$cachedFileInfo) {
  384. return json(['code' => 2, 'msg' => '系统正忙,请稍后重试']);
  385. }
  386. }
  387. }
  388. // 获取所有需要实时查询的路径
  389. $paths = array_column($cachedFileInfo, 'path');
  390. // 实时查询数据库状态信息(单次批量查询)
  391. $dbQuery = Db::name('text_to_image')
  392. ->whereIn('old_image_url', $paths)
  393. ->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');
  394. // 仅在sys_id不为空时添加sys_id条件
  395. if (!empty($sys_id)) {
  396. $dbQuery->where('sys_id', $sys_id);
  397. }
  398. $dbRecords = $dbQuery->select();
  399. // 实时查询队列状态(单次批量查询)
  400. $queueRecords = Db::name('image_task_log')
  401. ->where('mod_rq', null)
  402. ->whereIn('file_name', $paths)
  403. ->field('file_name, log')
  404. ->select();
  405. // 实时查询same_count(稍后按需查询)
  406. // 构建映射关系
  407. $processedMap = [];
  408. foreach ($dbRecords as $item) {
  409. $key = str_replace('\\', '/', trim($item['old_image_url'], '/'));
  410. $processedMap[$key] = $item;
  411. }
  412. $queueMap = [];
  413. foreach ($queueRecords as $q) {
  414. $key = str_replace('\\', '/', trim($q['file_name'], '/'));
  415. $queueMap[$key] = $q['log'];
  416. }
  417. // 合并数据
  418. $mergedData = [];
  419. foreach ($cachedFileInfo as $data) {
  420. $path = $data['path'];
  421. $item = $processedMap[$path] ?? [];
  422. $mergedData[] = [
  423. 'path' => $path,
  424. 'item' => $item,
  425. 'info' => $data['info'],
  426. 'dbStatus' => isset($item['status']) ? (int)$item['status'] : 0,
  427. 'dbStatusName' => $item['status_name'] ?? '',
  428. 'isProcessed' => !empty($item['img_name']) && !empty($item['custom_image_url']),
  429. 'queueStatus' => $queueMap[$path] ?? ''
  430. ];
  431. }
  432. // 筛选状态字段
  433. $filtered = array_filter($mergedData, function ($data) use ($status, $status_name) {
  434. // 状态码筛选
  435. if ($status !== '' && (int)$status !== $data['dbStatus']) return false;
  436. // 状态名称筛选
  437. if ($status_name !== '') {
  438. if ($status_name === '未图生文') {
  439. // 当状态名为"未图生文"时,匹配空值或null
  440. if (!empty($data['dbStatusName'])) return false;
  441. } elseif ($status_name === '未文生文') {
  442. if ($data['dbStatusName'] !== '图生文') return false;
  443. } elseif ($status_name === '未文生图') {
  444. if ($data['dbStatusName'] !== '图生文') return false;
  445. } elseif ($status_name === '未图生图') {
  446. if ($data['dbStatusName'] !== '文生图') return false;
  447. } else {
  448. // 其他状态名需要精确匹配
  449. if ($status_name !== $data['dbStatusName']) return false;
  450. }
  451. }
  452. return true;
  453. });
  454. // 分页处理
  455. $total = count($filtered);
  456. $paged = array_slice(array_values($filtered), ($page - 1) * $limit, $limit);
  457. // 实时查询当前页的same_count(优化性能)
  458. $pagedPaths = array_column($paged, 'path');
  459. $sameCountMap = [];
  460. if ($pagedPaths) {
  461. $sameCountMap = Db::name('text_to_image')
  462. ->whereIn('old_image_url', $pagedPaths)
  463. // ->where('new_image_url', '<>', '')
  464. ->where('new_image_url', '<>', '')
  465. ->where('taskId', '<>', '')
  466. ->group('old_image_url')
  467. ->column('count(*) as cnt', 'old_image_url');
  468. }
  469. // 构建最终结果
  470. $result = [];
  471. foreach ($paged as $i => $data) {
  472. $path = $data['path'];
  473. $item = $data['item'];
  474. $info = $data['info'];
  475. $result[] = [
  476. 'id' => ($page - 1) * $limit + $i + 1,
  477. 'ids' => $item['img_id'],
  478. 'path' => $path,
  479. // 实时数据
  480. 'status' => $data['dbStatus'],
  481. 'status_name' => $data['dbStatusName'],
  482. 'same_count' => $sameCountMap[$path] ?? 0,
  483. 'is_processed' => $data['isProcessed'] ? 1 : 0,
  484. 'queue_status' => $data['queueStatus'],
  485. 'taskId' => $item['taskId'] ?? '',
  486. 'sys_id' => $item['sys_id'] ?? '',
  487. 'new_image_url' => $item['new_image_url'] ?? '',
  488. 'custom_image_url' => $item['custom_image_url'] ?? '',
  489. 'imgtoimg_url' => $item['imgtoimg_url'] ?? '',
  490. 'chinese_description' => $item['chinese_description'] ?? '',
  491. 'english_description' => $item['english_description'] ?? '',
  492. 'img_name' => $item['img_name'] ?? '',
  493. // 来自缓存
  494. 'width' => $info['width'],
  495. 'height' => $info['height'],
  496. 'created_time' => $info['created_time']
  497. ];
  498. }
  499. return json([
  500. 'code' => 0,
  501. 'msg' => '获取成功',
  502. 'data' => $result,
  503. 'total' => $total,
  504. 'page' => $page,
  505. 'limit' => $limit
  506. ]);
  507. }
  508. /**
  509. * 通过服务器中获取对应目录
  510. */
  511. public function getlsit()
  512. {
  513. // 获取前端传入的图片路径参数
  514. $params = $this->request->param('path', '');
  515. $sys_id = $this->request->param('sys_id', '');
  516. // 查询数据库
  517. $res = Db::name('text_to_image')
  518. ->field('id,chinese_description,english_description,new_image_url,custom_image_url,size,old_image_url,img_name,model,imgtoimg_url')
  519. ->where('old_image_url', $params)
  520. ->where('img_name', '<>', '')
  521. ->where('sys_id',$sys_id)
  522. ->order('id desc')
  523. ->select();
  524. if($res){
  525. return json(['code' => 0, 'msg' => '查询成功', 'data' => $res,'count'=>count($res)]);
  526. }else{
  527. return json(['code' => 0, 'msg' => '查询成功', 'data' => [],'count'=>0]);
  528. }
  529. }
  530. /**
  531. * 图片上传
  532. */
  533. public function ImgUpload()
  534. {
  535. // 处理 CORS OPTIONS 预检请求
  536. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  537. header('Access-Control-Allow-Origin: *');
  538. header('Access-Control-Allow-Methods: POST, OPTIONS');
  539. header('Access-Control-Allow-Headers: Content-Type, Authorization');
  540. header('Access-Control-Max-Age: 86400');
  541. exit(204);
  542. }
  543. // 实际请求必须返回 CORS 头
  544. header('Access-Control-Allow-Origin: *');
  545. // 获取上传的文件
  546. $file = request()->file('image');
  547. if ($file) {
  548. // 生成日期格式的文件夹名 image_YYYYMMDD
  549. $dateFolder = 'image_' . date('Ymd');
  550. // 指定目标目录(包含日期文件夹)
  551. $targetPath = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'operate' . DS . 'ai' . DS . 'Preview' . DS . $dateFolder;
  552. // 若目录不存在则创建
  553. if (!is_dir($targetPath)) {
  554. mkdir($targetPath, 0755, true);
  555. }
  556. // 获取原始文件名(或自定义新文件名)
  557. $originalName = $file->getInfo('name'); // 原始文件名
  558. $extension = pathinfo($originalName, PATHINFO_EXTENSION); // 文件扩展名
  559. $newFileName = uniqid() . '.' . $extension; // 生成唯一文件名(避免冲突)
  560. // 移动文件到指定目录,并验证大小/格式,同时指定自定义文件名
  561. $info = $file->validate([
  562. 'size' => 10485760, // 最大10MB
  563. 'ext' => 'jpg,png'
  564. ])->move($targetPath, $newFileName); // 关键:手动指定文件名,避免自动生成日期目录
  565. if ($info) {
  566. // 直接拼接路径,不依赖 getSaveName() 的返回值
  567. $imageUrl = '/uploads/operate/ai/Preview/' . $dateFolder . '/' . $newFileName;
  568. return json(['code' => 0, 'msg' => '成功', 'data' => ['url' => $imageUrl]]);
  569. } else {
  570. $res = $file->getError();
  571. return json(['code' => 1, 'msg' => '失败', 'data' => $res]);
  572. }
  573. }
  574. return json(['code' => 1, 'msg' => '没有文件上传', 'data' => null]);
  575. }
  576. /**
  577. * 打包图片(支持对象结构中的 path 字段)
  578. */
  579. public function packImagess()
  580. {
  581. try {
  582. $params = $this->request->post();
  583. $paths = $params['paths'] ?? [];
  584. if (empty($paths) || !is_array($paths)) {
  585. return json(['code' => 1, 'msg' => '路径参数不能为空或格式不正确']);
  586. }
  587. // 提取所有合法的路径(支持字符串或对象中带 path 字段)
  588. $validPaths = [];
  589. foreach ($paths as $item) {
  590. if (is_string($item)) {
  591. $validPaths[] = $item;
  592. } elseif (is_array($item) && isset($item['path']) && is_string($item['path'])) {
  593. $validPaths[] = $item['path'];
  594. }
  595. }
  596. if (empty($validPaths)) {
  597. return json(['code' => 1, 'msg' => '没有有效的图片路径']);
  598. }
  599. // 设置基本路径和 zip 目录
  600. $basePath = ROOT_PATH . 'public/';
  601. $zipDir = $basePath . 'uploads/operate/ai/zip/';
  602. if (!is_dir($zipDir)) {
  603. mkdir($zipDir, 0755, true);
  604. }
  605. // 生成压缩文件路径
  606. $fileName = 'images_' . date('Ymd_His') . '.zip';
  607. $zipPath = $zipDir . $fileName;
  608. $zip = new \ZipArchive();
  609. if ($zip->open($zipPath, \ZipArchive::CREATE) !== TRUE) {
  610. return json(['code' => 1, 'msg' => '无法创建压缩包']);
  611. }
  612. $addCount = 0;
  613. foreach ($validPaths as $relativePath) {
  614. $relativePath = ltrim($relativePath, '/'); // 去除前导斜杠
  615. $fullPath = $basePath . $relativePath;
  616. if (file_exists($fullPath)) {
  617. // 使用 basename 作为压缩包内的文件名(不保留路径结构)
  618. $zip->addFile($fullPath, basename($fullPath));
  619. $addCount++;
  620. }
  621. }
  622. $zip->close();
  623. if ($addCount === 0) {
  624. @unlink($zipPath);
  625. return json(['code' => 1, 'msg' => '未找到有效图片文件,未生成压缩包']);
  626. }
  627. $downloadUrl = request()->domain() . '/uploads/operate/ai/zip/' . $fileName;
  628. return json([
  629. 'code' => 0,
  630. 'msg' => '打包成功',
  631. 'download_url' => $downloadUrl
  632. ]);
  633. } catch (\Exception $e) {
  634. return json([
  635. 'code' => 1,
  636. 'msg' => '异常错误:' . $e->getMessage()
  637. ]);
  638. }
  639. }
  640. /**
  641. * 获取所有模版列表,并返回当前使用模版 ID
  642. */
  643. public function TemplateList()
  644. {
  645. $params = Request::instance()->param();
  646. $path = $params['path'] ?? '';
  647. if (empty($path)) {
  648. return json([
  649. 'code' => 400,
  650. 'msg' => '缺少 path 参数',
  651. 'data' => []
  652. ]);
  653. }
  654. // 先查询是否已有数据
  655. $list = Db::name("template")
  656. ->where('path', $path)
  657. ->order('ids', 'desc')
  658. ->select();
  659. // 如果有记录,直接返回
  660. if ($list) {
  661. return json([
  662. 'code' => 0,
  663. 'msg' => '模版列表',
  664. 'data' => [
  665. 'list' => $list,
  666. 'usedId' => Db::name("template")->where('path', $path)->where('ids', 1)->value('id')
  667. ]
  668. ]);
  669. }
  670. $res_ids = Db::name("template")->where('ids', 99)->find();
  671. // 否则插入默认模板
  672. $default = [
  673. 'path' => $path,
  674. 'english_content' => '',
  675. 'content' => $res_ids['content'],
  676. 'width' => '1024',
  677. 'height' => '1303',
  678. 'ids' => 1
  679. ];
  680. $insertId = Db::name("template")->insertGetId($default);
  681. if ($insertId) {
  682. $list = Db::name("template")
  683. ->where('path', $path)
  684. ->order('ids', 'desc')
  685. ->select();
  686. return json([
  687. 'code' => 0,
  688. 'msg' => '模版列表(已创建默认)',
  689. 'data' => [
  690. 'list' => $list,
  691. 'usedId' => Db::name("template")->where('path', $path)->where('ids', 1)->value('id')
  692. ]
  693. ]);
  694. }
  695. // 插入失败
  696. return json([
  697. 'code' => 500,
  698. 'msg' => '创建默认模版失败',
  699. 'data' => []
  700. ]);
  701. }
  702. /**
  703. * 查询使用模版
  704. */
  705. public function Template_ids(){
  706. $params = Request::instance()->param();
  707. $Template = Db::name("template")->where('path',$params['path'])->where('ids',1)->find();
  708. return json([
  709. 'code' => 0,
  710. 'msg' => '模版',
  711. 'data' => $Template
  712. ]);
  713. }
  714. /**
  715. * 查询模版
  716. */
  717. public function Template()
  718. {
  719. $id = Request::instance()->param('id');
  720. if (!$id) {
  721. return json(['code' => 1, 'msg' => '参数错误']);
  722. }
  723. $template = Db::name("template")->where('id', $id)->find();
  724. return json([
  725. 'code' => 0,
  726. 'msg' => '模版详情',
  727. 'data' => $template
  728. ]);
  729. }
  730. /**
  731. * 更新模版
  732. */
  733. public function updatetemplate()
  734. {
  735. if (!Request::instance()->isPost()) {
  736. return json(['code' => 1, 'msg' => '非法请求']);
  737. }
  738. $params = Request::instance()->post();
  739. if (empty($params['textareaContent']) || empty($params['width']) || empty($params['height'])) {
  740. return json(['code' => 1, 'msg' => '参数缺失']);
  741. }
  742. $data = [
  743. 'path' => $params['path'],
  744. 'english_content' => $params['english_content'],
  745. 'content' => $params['textareaContent'],
  746. 'width' => $params['width'],
  747. 'height' => $params['height'],
  748. ];
  749. if (!empty($params['id'])) {
  750. // 更新已有模版
  751. Db::name("template")->where('id', $params['id'])->update($data);
  752. } else {
  753. // 新增模版,默认设为备用
  754. $data['ids'] = 0;
  755. Db::name("template")->insert($data);
  756. }
  757. return json(['code' => 0, 'msg' => '保存成功']);
  758. }
  759. /**
  760. * 设置默认使用模版
  761. */
  762. public function setActiveTemplate()
  763. {
  764. $id = Request::instance()->param('id');
  765. $params = Request::instance()->param();
  766. if (!$id) {
  767. return json(['code' => 1, 'msg' => '参数错误']);
  768. }
  769. Db::name("template")->where('path',$params['path'])->where('ids', 1)->update(['ids' => 0]); // 清除当前使用
  770. Db::name("template")->where('path',$params['path'])->where('id', $id)->update(['ids' => 1]); // 设置新的使用模版
  771. return json(['code' => 0, 'msg' => '模版已设为当前使用']);
  772. }
  773. public function txttoimg_moxing()
  774. {
  775. // 获取所有模型数据
  776. $list = Db::name("moxing")->order('id asc')->select();
  777. // 初始化分类数组
  778. $classifiedData = [
  779. 'wenshengwen' => [], // 文生文模型 (txttotxt)
  780. 'tushengwen' => [], // 图生文模型 (imgtotxt)
  781. 'wenshengtu' => [] // 文生图模型 (txttoimg)
  782. ];
  783. // 当前使用的模型ID
  784. $usedIds = [
  785. 'wenshengwen' => null,
  786. 'tushengwen' => null,
  787. 'wenshengtu' => null
  788. ];
  789. // 分类处理数据
  790. foreach ($list as $model) {
  791. // 文生文模型 (txttotxt)
  792. if (!empty($model['txttotxt'])) {
  793. $classifiedData['wenshengwen'][] = [
  794. 'id' => $model['id'],
  795. 'txttotxt' => $model['txttotxt'],
  796. 'txttotxt_val' => $model['txttotxt_val']
  797. ];
  798. if ($model['txttotxt_val'] == 1) {
  799. $usedIds['wenshengwen'] = $model['id'];
  800. }
  801. }
  802. // 图生文模型 (imgtotxt)
  803. if (!empty($model['imgtotxt'])) {
  804. $classifiedData['tushengwen'][] = [
  805. 'id' => $model['id'],
  806. 'imgtotxt' => $model['imgtotxt'],
  807. 'imgtotxt_val' => $model['imgtotxt_val']
  808. ];
  809. if ($model['imgtotxt_val'] == 1) {
  810. $usedIds['tushengwen'] = $model['id'];
  811. }
  812. }
  813. // 文生图模型 (txttoimg)
  814. if (!empty($model['txttoimg'])) {
  815. $classifiedData['wenshengtu'][] = [
  816. 'id' => $model['id'],
  817. 'txttoimg' => $model['txttoimg'],
  818. 'txttoimg_val' => $model['txttoimg_val']
  819. ];
  820. if ($model['txttoimg_val'] == 1) {
  821. $usedIds['wenshengtu'] = $model['id'];
  822. }
  823. }
  824. }
  825. return json([
  826. 'code' => 0,
  827. 'msg' => '模型分类列表',
  828. 'data' => [
  829. 'models' => $classifiedData,
  830. 'used_ids' => $usedIds
  831. ]
  832. ]);
  833. }
  834. public function txttoimg_update()
  835. {
  836. // 获取并验证参数
  837. $id = input('id/d', 0); // 强制转换为整数
  838. $type = input('type/s', ''); // 强制转换为字符串
  839. if ($id <= 0) {
  840. return json(['code' => 1, 'msg' => '无效的模型ID']);
  841. }
  842. if (!in_array($type, ['tushengwen', 'wenshengwen', 'wenshengtu'])) {
  843. return json(['code' => 1, 'msg' => '无效的模型类型']);
  844. }
  845. // 定义模型类型与字段的映射关系
  846. $fieldMap = [
  847. 'tushengwen' => 'imgtotxt_val',
  848. 'wenshengwen' => 'txttotxt_val',
  849. 'wenshengtu' => 'txttoimg_val'
  850. ];
  851. $field = $fieldMap[$type];
  852. try {
  853. // 开启事务确保数据一致性
  854. Db::startTrans();
  855. // 1. 重置该类型下所有模型的激活状态
  856. Db::name("moxing")
  857. ->where($field, 1)
  858. ->update([$field => 0]);
  859. // 2. 设置指定模型为激活状态
  860. $result = Db::name("moxing")
  861. ->where('id', $id)
  862. ->update([$field => 1]);
  863. Db::commit();
  864. if ($result) {
  865. return json([
  866. 'code' => 0,
  867. 'msg' => '设置成功',
  868. 'data' => [
  869. 'id' => $id,
  870. 'type' => $type
  871. ]
  872. ]);
  873. }
  874. return json(['code' => 1, 'msg' => '设置失败,模型不存在或未变更']);
  875. } catch (\Exception $e) {
  876. Db::rollback();
  877. return json([
  878. 'code' => 2,
  879. 'msg' => '设置失败: ' . $e->getMessage()
  880. ]);
  881. }
  882. }
  883. //获取原文件夹数据
  884. public function getPreviewFolders()
  885. {
  886. $baseDir = rtrim(str_replace('\\', '/', ROOT_PATH), '/') . '/public/uploads/operate/ai/Preview';
  887. $cacheDir = ROOT_PATH . '/runtime/cache/';
  888. $cacheFile = $cacheDir . 'folder_list.json';
  889. $cacheTTL = 86400; // 缓存有效期:1天
  890. $forceRefresh = input('get.refresh', 0); // 是否强制刷新缓存
  891. if (!is_dir($baseDir)) {
  892. return json([
  893. 'code' => 404,
  894. 'msg' => '目录不存在',
  895. 'data' => []
  896. ]);
  897. }
  898. $folders = [];
  899. $useCache = file_exists($cacheFile) && (time() - filemtime($cacheFile)) < $cacheTTL;
  900. if ($useCache && !$forceRefresh) {
  901. $folders = json_decode(file_get_contents($cacheFile), true);
  902. } else {
  903. $folders = [];
  904. // 使用高效方式递归遍历所有子目录
  905. $directory = new RecursiveDirectoryIterator($baseDir, RecursiveDirectoryIterator::SKIP_DOTS);
  906. $iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);
  907. foreach ($iterator as $file) {
  908. if ($file->isDir()) {
  909. $fullPath = str_replace('\\', '/', $file->getPathname());
  910. $relativePath = str_replace(ROOT_PATH . 'public/', '', $fullPath);
  911. $folders[] = [
  912. 'name' => basename($fullPath),
  913. 'path' => $relativePath,
  914. 'full_path' => $fullPath
  915. ];
  916. }
  917. }
  918. // 写入缓存
  919. if (!is_dir($cacheDir)) {
  920. mkdir($cacheDir, 0777, true);
  921. }
  922. file_put_contents($cacheFile, json_encode($folders));
  923. }
  924. return json([
  925. 'code' => 0,
  926. 'msg' => '获取所有预览文件夹成功',
  927. 'data' => [
  928. 'folders' => $folders,
  929. 'total' => count($folders),
  930. 'from_cache' => $useCache && !$forceRefresh
  931. ]
  932. ]);
  933. }
  934. /**
  935. * 修改
  936. */
  937. public function getTab()
  938. {
  939. if (!$this->request->isPost()) {
  940. return json(['code' => 400, 'msg' => '请求方式错误'], 400);
  941. }
  942. $param = $this->request->post([
  943. 'id',
  944. 'chinese_description',
  945. 'english_description',
  946. 'img_name'
  947. ]);
  948. if (empty($param['id'])) {
  949. return json(['code' => 422, 'msg' => '缺少必要参数'], 422);
  950. }
  951. $result = Db::name('text_to_image')
  952. ->where('id', $param['id'])
  953. ->update([
  954. 'chinese_description' => $param['chinese_description'] ?? '',
  955. 'english_description' => $param['english_description'] ?? '',
  956. 'img_name' => $param['img_name'] ?? ''
  957. ]);
  958. if ($result !== false) {
  959. $this->success('修改成功');
  960. } else {
  961. $this->success('修改失败');
  962. }
  963. }
  964. /**
  965. *
  966. */
  967. public function getList()
  968. {
  969. $params = Request::instance()->param();
  970. // 分页参数(默认第1页,每页10条)
  971. $page = isset($params['page']) ? max((int)$params['page'], 1) : 1;
  972. $limit = isset($params['limit']) ? max((int)$params['limit'], 10) : 10;
  973. // 搜索关键词
  974. $search = isset($params['search']) ? trim($params['search']) : '';
  975. $folder = isset($params['folder']) ? trim($params['folder']) : '';
  976. $where = [];
  977. $wheres = [];
  978. if (isset($search)) {
  979. $where['id|img_name|chinese_description|english_description'] = ['like', '%'. $search . '%'];
  980. }
  981. if (isset($folder)) {
  982. $wheres['old_image_url'] = ['like', '%'. $folder . '%'];
  983. }
  984. $count = Db::name('text_to_image')
  985. ->where('new_image_url', '<>', '')
  986. ->where($where)
  987. ->where($wheres)
  988. ->where('img_name', '<>', '')
  989. ->select();
  990. $list = Db::name('text_to_image')
  991. ->where('new_image_url', '<>', '')
  992. ->where('img_name', '<>', '')
  993. ->where($where)
  994. ->where($wheres)
  995. ->order('update_time desc')
  996. ->page($page, $limit)
  997. ->select();
  998. $folder = Db::name('text_to_image')
  999. ->field('old_image_url')
  1000. ->where('new_image_url', '<>', '')
  1001. ->where('img_name', '<>', '')
  1002. ->order('update_time desc')
  1003. ->select();
  1004. // 处理路径并去重
  1005. $processedFolders = [];
  1006. foreach ($folder as $item) {
  1007. $url = $item['old_image_url'];
  1008. $lastSlashPos = strrpos($url, '/');
  1009. if ($lastSlashPos !== false) {
  1010. $folderPath = substr($url, 0, $lastSlashPos);
  1011. if (!in_array($folderPath, $processedFolders)) {
  1012. $processedFolders[] = $folderPath;
  1013. }
  1014. }
  1015. }
  1016. $this->success('获取成功', [
  1017. 'total' => count($count),
  1018. 'list' => $list,
  1019. 'folder' => $processedFolders
  1020. ]);
  1021. }
  1022. }