WorkOrder.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\job\ImageJob;
  5. use app\service\AIGatewayService;
  6. use app\service\ImageService;
  7. use think\App;
  8. use think\Db;
  9. use think\Exception;
  10. use think\Log;
  11. use think\Queue;
  12. use think\queue\job\Redis;
  13. use think\Request;
  14. class WorkOrder extends Api{
  15. protected $noNeedLogin = ['*'];
  16. protected $noNeedRight = ['*'];
  17. public function index(){echo '访问成功';}
  18. /**
  19. * AI队列入口处理 出图接口
  20. * 此方法处理图像转换为文本的请求,将图像信息存入队列以供后续处理。
  21. */
  22. public function imageToText()
  23. {
  24. $params = $this->request->param();
  25. $service = new ImageService();
  26. $service->handleImage($params);
  27. $this->success('任务成功提交至队列');
  28. }
  29. /**
  30. * product 产品专用统一api调用接口
  31. * AI模型调用接口
  32. */
  33. public function GetTxtToTxt(){
  34. $params = $this->request->param();
  35. $prompt = $params['prompt'];//提示词
  36. $model = $params['model'];//模型
  37. $old_path = $params['path'] ?? '';//原图路径
  38. if(empty($model)) {
  39. return json(['code' => 1, 'msg' => '模型请求失败']);
  40. }
  41. $aiGateway = new AIGatewayService();
  42. $service = new ImageService();
  43. //判断模型入口:status_val
  44. if($params['status_val'] == '图生文'){
  45. $service->handleImgToText($params);
  46. $res = [
  47. 'code' => 0,
  48. 'msg' => '正在优化提示词,请稍等.....'
  49. ];
  50. return json($res);
  51. }else if($params['status_val'] == '文生文'){
  52. $fullPrompt = $prompt;
  53. $fullPrompt .= "
  54. 请根据上述内容生成一段完整的话术,要求:
  55. 1. 内容必须是连贯的一段话,不要使用列表、分隔线或其他结构化格式
  56. 2. 不要包含非文本元素的描述
  57. 3. 不要添加任何额外的引导语、解释或开场白
  58. 4. 语言流畅自然";
  59. $result = $service->handleTextToText($fullPrompt, $model);
  60. if(!empty($params['id'])){
  61. Db::name('product')->where('id', $params['id'])->update(['content' => $result['data']]);
  62. }
  63. $res = [
  64. 'code' => 0,
  65. 'msg' => '优化成功',
  66. 'content' => $result['data']
  67. ];
  68. return json($res);
  69. }elseif($params['status_val'] == '文生图'){
  70. $service->handleTextToImg($params);
  71. $res = [
  72. 'code' => 0,
  73. 'msg' => '正在生成图片中,请稍等.....'
  74. ];
  75. return json($res);
  76. }else{
  77. $this->error('请求失败');
  78. }
  79. }
  80. /**
  81. * 即梦AI--创建视频任务接口
  82. * 首帧图 + 尾帧图 = 新效果视频
  83. */
  84. public function Create_ImgToVideo()
  85. {
  86. $aiGateway = new AIGatewayService();
  87. $apiUrl = $aiGateway->config['Create_ImgToVideo']['api_url'];
  88. $apiKey = $aiGateway->config['Create_ImgToVideo']['api_key'];
  89. $params = $this->request->param();
  90. $modelParams = [
  91. 'resolution' => $params['resolution'] ?? '720p', // 分辨率:480p, 720p, 1080p
  92. 'duration' => $params['duration'] ?? 5, // 视频时长(秒)
  93. 'camerafixed' => $params['camerafixed'] ?? false, // 相机固定
  94. 'watermark' => $params['watermark'] ?? true, // 水印
  95. 'aspect_ratio' => $params['aspect_ratio'] ?? '16:9' // 视频比例:16:9, 4:3, 1:1等
  96. ];
  97. // 构建提示词
  98. $prompt = $params['prompt'] ?? '';
  99. $prompt .= ' --resolution ' . $modelParams['resolution'];
  100. $prompt .= ' --duration ' . $modelParams['duration'];
  101. $prompt .= ' --camerafixed ' . ($modelParams['camerafixed'] ? 'true' : 'false');
  102. $prompt .= ' --watermark ' . ($modelParams['watermark'] ? 'true' : 'false');
  103. $prompt .= ' --aspect_ratio ' . $modelParams['aspect_ratio'];
  104. // 构建请求数据
  105. $data = [
  106. 'model' => 'doubao-seedance-1-0-pro-250528',//模型
  107. 'content' => [
  108. [
  109. 'type' => 'text',
  110. 'text' => $prompt
  111. ],
  112. [
  113. 'type' => 'image_url',
  114. 'image_url' => [
  115. 'url' => $params['first_image_url']// 首帧图片URL(参数)
  116. ],
  117. 'role' => 'first_image'
  118. ],
  119. [
  120. 'type' => 'image_url',
  121. 'image_url' => [
  122. 'url' => $params['last_image_url'] // 尾帧图片URL(参数)
  123. ],
  124. 'role' => 'last_image'
  125. ]
  126. ]
  127. ];
  128. // 转换为 JSON 字符串
  129. $jsonData = json_encode($data);
  130. // 初始化 cURL
  131. $ch = curl_init();
  132. curl_setopt($ch, CURLOPT_URL, $apiUrl);
  133. curl_setopt($ch, CURLOPT_POST, true);
  134. curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
  135. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  136. 'Content-Type: application/json',
  137. 'Authorization: Bearer ' . $apiKey
  138. ]);
  139. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  140. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 开发环境临时关闭SSL验证
  141. curl_setopt($ch, CURLOPT_TIMEOUT, 60); // 超时时间
  142. // 执行请求
  143. $response = curl_exec($ch);
  144. // 检查 cURL 错误
  145. if (curl_errno($ch)) {
  146. $error = curl_error($ch);
  147. curl_close($ch);
  148. return json(['code' => 0, 'msg' => 'Curl 错误: ' . $error]);
  149. }
  150. curl_close($ch);
  151. // 解析响应
  152. $responseData = json_decode($response, true);
  153. // 检查 API 错误
  154. if (isset($responseData['error'])) {
  155. return json(['code' => 0, 'msg' => 'API 错误: ' . $responseData['error']['message']]);
  156. }
  157. // 获取任务 ID
  158. $taskId = $responseData['id'] ?? '';
  159. if (empty($taskId)) {
  160. return json(['code' => 0, 'msg' => '获取任务 ID 失败']);
  161. }
  162. // 返回结果
  163. return json([
  164. 'code' => 1,
  165. 'data' => [
  166. 'task_id' => $taskId,
  167. 'status' => $responseData['status'] ?? '',
  168. 'created_at' => $responseData['created_at'] ?? ''
  169. ]
  170. ]);
  171. }
  172. /**
  173. * 即梦AI--获取视频接口
  174. * 首帧图 + 尾帧图 = 新效果视频
  175. */
  176. public function Get_ImgToVideo()
  177. {
  178. $aiGateway = new AIGatewayService();
  179. $apiUrl = $aiGateway->config['Create_ImgToVideo']['api_url'];
  180. $apiKey = $aiGateway->config['Create_ImgToVideo']['api_key'];
  181. $params = $this->request->param();
  182. $taskId = $params['task_id'] ?? '';
  183. if (empty($taskId)) {
  184. return json(['code' => 0, 'msg' => '任务 ID 不能为空']);
  185. }
  186. // 查询任务状态
  187. $queryUrl = $apiUrl . '/' . $taskId;
  188. $ch2 = curl_init();
  189. curl_setopt($ch2, CURLOPT_URL, $queryUrl);
  190. curl_setopt($ch2, CURLOPT_HTTPHEADER, [
  191. 'Content-Type: application/json',
  192. 'Authorization: Bearer ' . $apiKey
  193. ]);
  194. curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
  195. curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false); // 开发环境临时关闭SSL验证
  196. curl_setopt($ch2, CURLOPT_TIMEOUT, 60); // 超时时间
  197. $queryResponse = curl_exec($ch2);
  198. // 检查 cURL 错误
  199. if (curl_errno($ch2)) {
  200. $error = curl_error($ch2);
  201. curl_close($ch2);
  202. return json(['code' => 0, 'msg' => 'Curl 错误: ' . $error]);
  203. }
  204. curl_close($ch2);
  205. // 解析查询响应
  206. $queryData = json_decode($queryResponse, true);
  207. // print_r($queryData);die;
  208. // 轮询任务状态,直到完成
  209. $maxPolls = 30; // 最大轮询次数
  210. $pollCount = 0;
  211. $taskStatus = $queryData['status'] ?? '';
  212. while (!in_array($taskStatus, ['completed', 'succeeded']) && $pollCount < $maxPolls) {
  213. sleep(5); // 每5秒轮询一次
  214. $pollCount++;
  215. // 再次查询任务状态
  216. $ch3 = curl_init();
  217. curl_setopt($ch3, CURLOPT_URL, $queryUrl);
  218. curl_setopt($ch3, CURLOPT_HTTPHEADER, [
  219. 'Content-Type: application/json',
  220. 'Authorization: Bearer ' . $apiKey
  221. ]);
  222. curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true);
  223. curl_setopt($ch3, CURLOPT_SSL_VERIFYPEER, false);
  224. curl_setopt($ch3, CURLOPT_TIMEOUT, 60);
  225. $pollResponse = curl_exec($ch3);
  226. curl_close($ch3);
  227. $pollData = json_decode($pollResponse, true);
  228. $taskStatus = $pollData['status'] ?? '';
  229. // 检查任务是否失败
  230. if ($taskStatus === 'failed') {
  231. return json(['code' => 0, 'msg' => '任务执行失败']);
  232. }
  233. }
  234. // 如果任务已经成功,直接使用 $queryData
  235. if (empty($pollData) && isset($queryData['status']) && $queryData['status'] === 'succeeded') {
  236. $pollData = $queryData;
  237. }
  238. // 检查轮询是否超时
  239. if (!in_array($taskStatus, ['completed', 'succeeded'])) {
  240. return json(['code' => 0, 'msg' => '任务执行超时']);
  241. }
  242. // 获取视频 URL
  243. $videoUrl = $pollData['content']['video_url'] ?? '';
  244. if (empty($videoUrl)) {
  245. return json(['code' => 0, 'msg' => '获取视频 URL 失败', 'data' => ['pollData' => $pollData]]);
  246. }
  247. // 确保保存目录存在
  248. $saveDir = ROOT_PATH . 'public/uploads/ceshi/';
  249. if (!is_dir($saveDir)) {
  250. mkdir($saveDir, 0755, true);
  251. }
  252. // 生成保存文件名
  253. $fileName = $taskId . '.mp4';
  254. $savePath = $saveDir . '/' . $fileName;
  255. // 下载视频
  256. // 设置超时时间为300秒(5分钟)
  257. $context = stream_context_create(['http' => ['timeout' => 300]]);
  258. $videoContent = file_get_contents($videoUrl, false, $context);
  259. if ($videoContent === false) {
  260. return json(['code' => 0, 'msg' => '下载视频失败', 'data' => ['videoUrl' => $videoUrl]]);
  261. }
  262. // 保存视频到文件
  263. $saveResult = file_put_contents($savePath, $videoContent);
  264. if ($saveResult === false) {
  265. return json(['code' => 0, 'msg' => '保存视频失败', 'data' => ['savePath' => $savePath, 'dirWritable' => is_writable($saveDir)]]);
  266. }
  267. // 生成视频的访问 URL
  268. $videoAccessUrl = '/uploads/ceshi/' . $fileName;
  269. // 返回结果
  270. return json([
  271. 'code' => 1,
  272. 'data' => [
  273. 'task_id' => $taskId,
  274. 'status' => $taskStatus,
  275. 'video_url' => $videoAccessUrl,
  276. 'save_path' => $savePath
  277. ]
  278. ]);
  279. }
  280. /**
  281. * 九个分镜头生成流程
  282. * 模型:gemini-3-pro-image-preview
  283. * 说明:
  284. * 第一步:(提示词 + 原始图片 = 九个分镜头图片) 或 (提示词 = 九个分镜头图片)
  285. * 第二步:使用九个分镜头图进行裁剪单图生成连贯视频
  286. * 第三步:在通过分镜头视频拼接成一个完整的视频(列如每个分镜头视频为8秒,九个为72秒形成完整视频)
  287. */
  288. // public function Get_txttonineimg()
  289. // {
  290. // // 发起接口请求
  291. //// $apiUrl = 'https://chatapi.onechats.ai/v1beta/models/gemini-3-pro-image-preview:generateContent';
  292. // $apiUrl = 'https://chatapi.onechats.ai/v1beta/models/gemini-3-pro-image-preview:streamGenerateContent';
  293. // $apiKey = 'sk-9aIV9nx7pJxJFMrB8REtNbhjYuNBxCcnEOwiJDHd6UwmN2eJ';
  294. //
  295. //// $params = $this->request->param();
  296. // $prompt = '生成一个苹果(九个分镜头图片)';
  297. //
  298. // $requestData = [
  299. // "contents" => [
  300. // [
  301. // "role" => "user",
  302. // "parts" => [
  303. // ["text" => $prompt]
  304. // ]
  305. // ]
  306. // ],
  307. // "generationConfig" => [
  308. // "responseModalities" => ["TEXT", "IMAGE"],
  309. // "imageConfig" => [
  310. // "aspectRatio" => "1:1"
  311. // ]
  312. // ]
  313. // ];
  314. //
  315. // $ch = curl_init();
  316. // curl_setopt($ch, CURLOPT_URL, $apiUrl);
  317. // curl_setopt($ch, CURLOPT_POST, true);
  318. // curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData, JSON_UNESCAPED_UNICODE));
  319. // curl_setopt($ch, CURLOPT_HTTPHEADER, [
  320. // 'Content-Type: application/json',
  321. // 'Authorization: Bearer ' . $apiKey
  322. // ]);
  323. // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  324. // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 开发环境临时关闭SSL验证
  325. // curl_setopt($ch, CURLOPT_TIMEOUT, 60); // 生成图片超时时间(建议60秒)
  326. //
  327. // $response = curl_exec($ch);
  328. // $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  329. //
  330. // $error = curl_error($ch);
  331. // curl_close($ch);
  332. // $res = json_decode($response, true);
  333. //
  334. // // 构建URL路径(使用正斜杠)
  335. // $url_path = '/uploads/txtnewimg/';
  336. // // 构建物理路径(使用正斜杠确保统一格式)
  337. // $save_path = ROOT_PATH . 'public' . '/' . 'uploads' . '/' . 'txtnewimg' . '/';
  338. // // 移除ROOT_PATH中可能存在的反斜杠,确保统一使用正斜杠
  339. // $save_path = str_replace('\\', '/', $save_path);
  340. // // 自动创建文件夹(如果不存在)
  341. // if (!is_dir($save_path)) {
  342. // mkdir($save_path, 0755, true);
  343. // }
  344. //
  345. // // 提取base64图片数据
  346. // $text_content = $res['candidates'][0]['content']['parts'][0]['inlineData']['data'];
  347. // $str = 'data:image/jpeg;base64,';
  348. // $text_content = $str. $text_content;
  349. // // 匹配base64图片数据
  350. // preg_match('/data:image\/(png|jpg|jpeg);base64,([^"]+)/', $text_content, $matches);
  351. // if (empty($matches)) {
  352. // return '未找到图片数据';
  353. // }
  354. // $image_type = $matches[1];
  355. // $base64_data = $matches[2];
  356. //
  357. // // 解码base64数据
  358. // $image_data = base64_decode($base64_data);
  359. // if ($image_data === false) {
  360. // return '图片解码失败';
  361. // }
  362. //
  363. // // 生成唯一文件名(包含扩展名)
  364. // $file_name = uniqid() . '.' . $image_type;
  365. // $full_file_path = $save_path . $file_name;
  366. //
  367. // // 保存图片到文件系统
  368. // if (!file_put_contents($full_file_path, $image_data)) {
  369. // return '图片保存失败';
  370. // }
  371. // // 生成数据库存储路径(使用正斜杠格式)
  372. // $db_img_path = $url_path . $file_name;
  373. // return $db_img_path;
  374. // }
  375. public function GetTxtToImg(){
  376. $params = $this->request->param();
  377. $prompt = $params['prompt'];//提示词
  378. $model = $params['model'];//模型
  379. $size = $params['size'];//尺寸
  380. // 调用AI生成图片
  381. $aiGateway = new AIGatewayService();
  382. $res = $aiGateway->callDalleApi($prompt, $model, $size);
  383. // 提取base64图片数据
  384. $imageData = '';
  385. $imageType = 'png'; // 默认图片类型
  386. if (isset($res['candidates'][0]['content']['parts'][0]['text'])) {
  387. $text_content = $res['candidates'][0]['content']['parts'][0]['text'];
  388. // 匹配base64图片数据和类型
  389. if (preg_match('/data:image\/([a-zA-Z0-9]+);base64,([^\s]+)/', $text_content, $matches)) {
  390. $imageType = strtolower($matches[1]);
  391. $base64Data = $matches[2];
  392. // 解码base64数据
  393. $imageData = base64_decode($base64Data);
  394. }
  395. }
  396. if (!$imageData) {
  397. return json(['code' => 1, 'msg' => '图片生成失败,未找到有效图片数据']);
  398. }
  399. // 创建保存目录(public/uploads/log/YYYY-MM/)
  400. $yearMonth = date('Ym');
  401. $saveDir = ROOT_PATH . 'public/uploads/ceshi/' . $yearMonth . '/';
  402. if (!is_dir($saveDir)) {
  403. mkdir($saveDir, 0755, true);
  404. }
  405. // 生成唯一文件名
  406. $fileName = uniqid() . '.' . $imageType;
  407. $filePath = $saveDir . $fileName;
  408. // 保存图片到本地文件
  409. if (file_put_contents($filePath, $imageData) === false) {
  410. return json(['code' => 1, 'msg' => '图片保存失败']);
  411. }
  412. // 生成前端可访问的URL
  413. $imageUrl = '/uploads/ceshi/' . $yearMonth . '/' . $fileName;
  414. // 返回标准JSON响应
  415. return json([
  416. 'code' => 0,
  417. 'msg' => '图片生成成功',
  418. 'data' => [
  419. 'url' => $imageUrl
  420. ]
  421. ]);
  422. }
  423. /**
  424. * 学生端文生视频接口 - 用于生成视频
  425. */
  426. public function GetTxtToVideo(){
  427. $aiGateway = new AIGatewayService();
  428. $apiUrl = $aiGateway->config['videos']['api_url'];
  429. $apiKey = $aiGateway->config['videos']['api_key'];
  430. // 获取并验证参数
  431. $params = $this->request->param();
  432. // echo "<pre>";
  433. // print_r($params);
  434. // echo "<pre>";die;
  435. $postData = [
  436. 'prompt' => $params['prompt'],
  437. 'model' => $params['model'],
  438. 'seconds' => $params['duration'],
  439. 'size' => $params['size'],
  440. ];
  441. // 初始化CURL
  442. $ch = curl_init();
  443. curl_setopt($ch, CURLOPT_URL, $apiUrl);
  444. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  445. curl_setopt($ch, CURLOPT_POST, true);
  446. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  447. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  448. 'Authorization: Bearer ' . $apiKey
  449. ]);
  450. curl_setopt($ch, CURLOPT_TIMEOUT, 300);
  451. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  452. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  453. curl_setopt($ch, CURLOPT_HEADER, true); // 获取响应头
  454. curl_setopt($ch, CURLOPT_VERBOSE, true); // 启用详细输出以进行调试
  455. // 创建临时文件来捕获详细的cURL输出
  456. $verbose = fopen('php://temp', 'w+');
  457. curl_setopt($ch, CURLOPT_STDERR, $verbose);
  458. // 执行请求
  459. $response = curl_exec($ch);
  460. //HTTP状态码
  461. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  462. // 获取详细的cURL调试信息
  463. rewind($verbose);
  464. //CURL调试信息
  465. $verboseLog = stream_get_contents($verbose);
  466. fclose($verbose);
  467. // 分离头部和主体
  468. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  469. //响应头部
  470. $header = substr($response, 0, $header_size);
  471. //响应主体
  472. $body = substr($response, $header_size);
  473. // 检查CURL错误
  474. $curlError = curl_error($ch);
  475. curl_close($ch);
  476. $responseData = json_decode($body, true);
  477. // 检查API是否返回了错误信息
  478. if (isset($responseData['error'])) {
  479. $errorMessage = isset($responseData['error']['message']) ? $responseData['error']['message'] : 'API请求失败';
  480. return json([
  481. 'code' => 1,
  482. 'msg' => '视频生成请求失败',
  483. 'data' => [
  484. 'error_type' => isset($responseData['error']['type']) ? $responseData['error']['type'] : 'unknown',
  485. 'error_code' => isset($responseData['error']['code']) ? $responseData['error']['code'] : 'unknown',
  486. 'error_message' => $errorMessage
  487. ]
  488. ]);
  489. }
  490. // 检查是否有自定义错误格式
  491. if (isset($responseData['code']) && $responseData['code'] === 'fail_to_fetch_task' && isset($responseData['message'])) {
  492. return json([
  493. 'code' => 1,
  494. 'msg' => '视频生成请求失败',
  495. 'data' => [
  496. 'error_code' => $responseData['code'],
  497. 'error_message' => $responseData['message']
  498. ]
  499. ]);
  500. }
  501. // 检查是否存在id字段
  502. if (!isset($responseData['id'])) {
  503. return json([
  504. 'code' => 1,
  505. 'msg' => '无法获取视频ID',
  506. 'data' => [
  507. 'response_data' => $responseData,
  508. 'http_code' => $httpCode
  509. ]
  510. ]);
  511. }
  512. // 1. 先检查视频状态
  513. $statusUrl = 'https://chatapi.onechats.ai/v1/videos/' . $responseData['id'];
  514. $statusData = $this->fetchVideoStatus($statusUrl, $apiKey);
  515. // 检查视频状态
  516. if ($statusData['status'] !== 'completed') {
  517. return json([
  518. 'code' => 202,
  519. 'msg' => '视频尚未生成完成',
  520. 'data' => [
  521. 'video_id' => $responseData['id'],
  522. 'status' => $statusData['status'],
  523. 'progress' => $statusData['progress'],
  524. 'created_at' => $statusData['created_at'],
  525. 'message' => '请稍后再试,视频仍在' . ($statusData['status'] === 'queued' ? '排队中' : '处理中')
  526. ]
  527. ]);
  528. }
  529. // 2. 视频生成完成,准备下载
  530. $apiUrl = 'https://chatapi.onechats.ai/v1/videos/' . $responseData['id'] . '/content';
  531. // 获取可选的variant参数
  532. $variant = $this->request->get('variant', '');
  533. if (!empty($variant)) {
  534. $apiUrl .= '?variant=' . urlencode($variant);
  535. }
  536. // 创建保存目录
  537. $saveDir = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'videos' . DS . date('Ymd');
  538. if (!is_dir($saveDir)) {
  539. mkdir($saveDir, 0755, true);
  540. }
  541. // 生成唯一文件名
  542. $filename = $responseData['id'] . '.mp4';
  543. $localPath = DS . 'uploads' . DS . 'videos' . DS . date('Ymd') . DS . $filename;
  544. $fullPath = $saveDir . DS . $filename;
  545. // 3. 下载视频
  546. $videoData = $this->downloadVideo($apiUrl, $apiKey);
  547. // 4. 保存视频文件
  548. if (file_put_contents($fullPath, $videoData) === false) {
  549. throw new Exception('视频保存失败');
  550. }
  551. // 确保路径使用正斜杠,并只保存相对路径部分
  552. $localPath = str_replace('\\', '/', $localPath);
  553. // 移除开头的斜杠,确保路径格式为uploads/videos/...
  554. $savePath = ltrim($localPath, '/');
  555. // 返回成功响应
  556. return json([
  557. 'code' => 0,
  558. 'msg' => '视频下载成功',
  559. 'data' => [
  560. 'video_id' => $responseData['id'],
  561. 'web_url' => $savePath
  562. ]
  563. ]);
  564. }
  565. /**
  566. * 学生端视频状态查询接口 - 用于通过video_id查询视频状态
  567. */
  568. public function Getvideo_id(){
  569. // 获取并验证参数
  570. $params = $this->request->param();
  571. $videoId = $params['video_id'] ?? '';
  572. // 验证video_id参数
  573. if (empty($videoId)) {
  574. return json([
  575. 'code' => 1,
  576. 'msg' => '缺少必要参数:video_id',
  577. 'data' => []
  578. ]);
  579. }
  580. $aiGateway = new AIGatewayService();
  581. // $apiUrl = $aiGateway->config['videos']['api_url'];
  582. $apiKey = $aiGateway->config['videos']['api_key'];
  583. $apiUrl = 'https://chatapi.onechats.ai/v1/videos/' . $videoId;
  584. // 先检查本地是否已经有该视频
  585. $localVideoPath = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'videos' . DS . date('Ymd') . DS . $videoId . '.mp4';
  586. if (file_exists($localVideoPath)) {
  587. // 视频已存在本地,直接返回
  588. $webPath = '/uploads/videos/' . date('Ymd') . '/' . $videoId . '.mp4';
  589. return json([
  590. 'code' => 0,
  591. 'msg' => '视频下载成功',
  592. 'data' => [
  593. 'video_id' => $videoId,
  594. 'web_url' => substr($webPath, 1) // 移除开头的斜杠
  595. ]
  596. ]);
  597. }
  598. // 检查视频状态
  599. $statusData = $this->fetchVideoStatus($apiUrl, $apiKey);
  600. // 检查视频状态
  601. if ($statusData['status'] !== 'completed') {
  602. return json([
  603. 'code' => 202,
  604. 'msg' => '视频尚未生成完成',
  605. 'data' => [
  606. 'video_id' => $videoId,
  607. 'status' => $statusData['status'],
  608. 'progress' => isset($statusData['progress']) ? $statusData['progress'] : 0,
  609. 'created_at' => $statusData['created_at'],
  610. 'message' => '请稍后再试,视频仍在' . ($statusData['status'] === 'queued' ? '排队中' : '处理中')
  611. ]
  612. ]);
  613. }
  614. // 视频生成完成,下载视频
  615. $downloadUrl = 'https://chatapi.onechats.ai/v1/videos/' . $videoId . '/content';
  616. // 获取可选的variant参数
  617. $variant = $this->request->get('variant', '');
  618. if (!empty($variant)) {
  619. $downloadUrl .= '?variant=' . urlencode($variant);
  620. }
  621. // 创建保存目录
  622. $saveDir = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'videos' . DS . date('Ymd');
  623. if (!is_dir($saveDir)) {
  624. mkdir($saveDir, 0755, true);
  625. }
  626. // 生成唯一文件名
  627. $filename = $videoId . '.mp4';
  628. $localPath = DS . 'uploads' . DS . 'videos' . DS . date('Ymd') . DS . $filename;
  629. $fullPath = $saveDir . DS . $filename;
  630. // 下载视频
  631. $videoData = $this->downloadVideo($downloadUrl, $apiKey);
  632. // 保存视频文件
  633. if (file_put_contents($fullPath, $videoData) === false) {
  634. throw new Exception('视频保存失败');
  635. }
  636. // 确保路径使用正斜杠,并只保存相对路径部分
  637. $localPath = str_replace(DIRECTORY_SEPARATOR, '/', $localPath);
  638. // 移除开头的斜杠,确保路径格式为uploads/videos/...
  639. $savePath = ltrim($localPath, '/');
  640. // 返回成功响应
  641. return json([
  642. 'code' => 0,
  643. 'msg' => '视频下载成功',
  644. 'data' => [
  645. 'video_id' => $videoId,
  646. 'web_url' => $savePath
  647. ]
  648. ]);
  649. }
  650. //获取视频列表
  651. public function Getvideolist(){
  652. if (!$this->request->isGet()) {
  653. $this->error('请求方式错误');
  654. }
  655. $params = $this->request->param();
  656. $search = input('search', '');
  657. $page = isset($params['page']) ? (int)$params['page'] : 1;
  658. $limit = isset($params['limit']) ? (int)$params['limit'] : 50;
  659. $where = [];
  660. if (!empty($search)) {
  661. $where['prompt'] = ['like', '%' . $search . '%'];
  662. }
  663. $list = Db::name('video')->where('mod_rq', null)
  664. ->where($where)
  665. ->order('id desc')
  666. ->limit(($page - 1) * $limit, $limit)
  667. ->select();
  668. $total = Db::name('video')->where('mod_rq', null)
  669. ->where($where)
  670. ->count();
  671. $res['code'] = 0;
  672. $res['msg'] = '成功';
  673. $res['count'] = $total;
  674. $res['data'] = $list;
  675. return json($res);
  676. }
  677. /**
  678. * 文生视频/图生视频接口
  679. * @return \think\response\Json
  680. * @throws \Exception
  681. */
  682. //文生视频
  683. public function video(){
  684. $aiGateway = new AIGatewayService();
  685. $apiUrl = $aiGateway->config['videos']['api_url'];
  686. $apiKey = $aiGateway->config['videos']['api_key'];
  687. $params = $this->request->param();
  688. $postData = [
  689. 'prompt' => $params['prompt'],
  690. 'model' => $params['model'],
  691. 'seconds' => $params['seconds'],
  692. 'size' => $params['size'],
  693. ];
  694. // 初始化CURL
  695. $ch = curl_init();
  696. curl_setopt($ch, CURLOPT_URL, $apiUrl);
  697. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  698. curl_setopt($ch, CURLOPT_POST, true);
  699. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  700. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  701. 'Authorization: Bearer ' . $apiKey
  702. ]);
  703. curl_setopt($ch, CURLOPT_TIMEOUT, 300);
  704. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  705. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  706. curl_setopt($ch, CURLOPT_HEADER, true); // 获取响应头
  707. curl_setopt($ch, CURLOPT_VERBOSE, true); // 启用详细输出以进行调试
  708. // 创建临时文件来捕获详细的cURL输出
  709. $verbose = fopen('php://temp', 'w+');
  710. curl_setopt($ch, CURLOPT_STDERR, $verbose);
  711. // 执行请求
  712. $response = curl_exec($ch);
  713. //HTTP状态码
  714. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  715. // 获取详细的cURL调试信息
  716. rewind($verbose);
  717. //CURL调试信息
  718. $verboseLog = stream_get_contents($verbose);
  719. fclose($verbose);
  720. // 分离头部和主体
  721. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  722. //响应头部
  723. $header = substr($response, 0, $header_size);
  724. //响应主体
  725. $body = substr($response, $header_size);
  726. // 检查CURL错误
  727. $curlError = curl_error($ch);
  728. curl_close($ch);
  729. $responseData = json_decode($body, true);
  730. // 检查API是否返回了错误信息
  731. if (isset($responseData['error'])) {
  732. $errorMessage = isset($responseData['error']['message']) ? $responseData['error']['message'] : 'API请求失败';
  733. return json([
  734. 'code' => 1,
  735. 'msg' => '视频生成请求失败',
  736. 'data' => [
  737. 'error_type' => isset($responseData['error']['type']) ? $responseData['error']['type'] : 'unknown',
  738. 'error_code' => isset($responseData['error']['code']) ? $responseData['error']['code'] : 'unknown',
  739. 'error_message' => $errorMessage
  740. ]
  741. ]);
  742. }
  743. // 检查是否有自定义错误格式
  744. if (isset($responseData['code']) && $responseData['code'] === 'fail_to_fetch_task' && isset($responseData['message'])) {
  745. return json([
  746. 'code' => 1,
  747. 'msg' => '视频生成请求失败',
  748. 'data' => [
  749. 'error_code' => $responseData['code'],
  750. 'error_message' => $responseData['message']
  751. ]
  752. ]);
  753. }
  754. // 检查是否存在id字段
  755. if (!isset($responseData['id'])) {
  756. return json([
  757. 'code' => 1,
  758. 'msg' => '无法获取视频ID',
  759. 'data' => [
  760. 'response_data' => $responseData,
  761. 'http_code' => $httpCode
  762. ]
  763. ]);
  764. }
  765. $videoData = [
  766. 'video_id' => $responseData['id'],
  767. 'prompt' => $postData['prompt'],
  768. 'model' => $postData['model'],
  769. 'seconds' => $postData['seconds'],
  770. 'size' => $postData['size'],
  771. 'sys_rq' => date("Y-m-d H:i:s")
  772. ];
  773. // 尝试插入数据
  774. try {
  775. $res = Db::name('video')->insert($videoData);
  776. return json([
  777. 'code' => 0,
  778. 'msg' => '视频正在生成中',
  779. 'data ' => [
  780. 'video_id' => $responseData['id'],
  781. 'insert_result' => $res
  782. ]
  783. ]);
  784. } catch (Exception $e) {
  785. return json([
  786. 'code' => 1,
  787. 'msg' => '数据库操作失败',
  788. 'data' => [
  789. 'error_message' => $e->getMessage()
  790. ]
  791. ]);
  792. }
  793. }
  794. /**
  795. * 获取视频内容
  796. * 下载已完成的视频内容
  797. */
  798. public function videoContent(){
  799. // 从请求参数获取video_id,如果没有则使用默认值
  800. $video_id = input('get.video_id');
  801. $apiKey = 'sk-sWW1GFlnjbrDRb1DkMEzePIxgdvLK6cZt0Qg93yDMVP2z1yN';
  802. // 1. 先检查视频状态
  803. $statusUrl = 'https://chatapi.onechats.ai/v1/videos/' . $video_id;
  804. $statusData = $this->fetchVideoStatus($statusUrl, $apiKey);
  805. // 检查视频状态
  806. if ($statusData['status'] !== 'completed') {
  807. return json([
  808. 'code' => 202,
  809. 'msg' => '视频尚未生成完成',
  810. 'data' => [
  811. 'video_id' => $video_id,
  812. 'status' => $statusData['status'],
  813. 'progress' => $statusData['progress'],
  814. 'created_at' => $statusData['created_at'],
  815. 'message' => '请稍后再试,视频仍在' . ($statusData['status'] === 'queued' ? '排队中' : '处理中')
  816. ]
  817. ]);
  818. }
  819. // 2. 视频生成完成,准备下载
  820. $apiUrl = 'https://chatapi.onechats.ai/v1/videos/' . $video_id . '/content';
  821. // 获取可选的variant参数
  822. $variant = $this->request->get('variant', '');
  823. if (!empty($variant)) {
  824. $apiUrl .= '?variant=' . urlencode($variant);
  825. }
  826. // 创建保存目录
  827. $saveDir = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'videos' . DS . date('Ymd');
  828. if (!is_dir($saveDir)) {
  829. mkdir($saveDir, 0755, true);
  830. }
  831. // 生成唯一文件名
  832. $filename = $video_id . '.mp4';
  833. $localPath = DS . 'uploads' . DS . 'videos' . DS . date('Ymd') . DS . $filename;
  834. $fullPath = $saveDir . DS . $filename;
  835. // 3. 下载视频
  836. $videoData = $this->downloadVideo($apiUrl, $apiKey);
  837. // 4. 保存视频文件
  838. if (file_put_contents($fullPath, $videoData) === false) {
  839. throw new Exception('视频保存失败');
  840. }
  841. // 确保路径使用正斜杠,并只保存相对路径部分
  842. $localPath = str_replace('\\', '/', $localPath);
  843. // 移除开头的斜杠,确保路径格式为uploads/videos/...
  844. $savePath = ltrim($localPath, '/');
  845. // 将正确格式的文件路径存入数据库
  846. Db::name('video')->where('video_id', $video_id)->update([
  847. 'web_url' => $savePath
  848. ]);
  849. // 返回成功响应
  850. return json([
  851. 'code' => 0,
  852. 'msg' => '视频下载成功',
  853. 'data' => [
  854. 'video_id' => $video_id,
  855. 'local_path' => $localPath,
  856. 'web_url' => $savePath,
  857. 'file_size' => filesize($fullPath)
  858. ]
  859. ]);
  860. }
  861. /**
  862. * 获取视频状态
  863. */
  864. private function fetchVideoStatus($url, $apiKey) {
  865. $ch = curl_init();
  866. curl_setopt($ch, CURLOPT_URL, $url);
  867. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  868. curl_setopt($ch, CURLOPT_HTTPGET, true);
  869. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  870. 'Authorization: Bearer ' . $apiKey,
  871. 'Accept: application/json'
  872. ]);
  873. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  874. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  875. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  876. $response = curl_exec($ch);
  877. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  878. $error = curl_error($ch);
  879. curl_close($ch);
  880. if ($error) {
  881. throw new Exception('获取视频状态失败: ' . $error);
  882. }
  883. if ($httpCode < 200 || $httpCode >= 300) {
  884. throw new Exception('获取视频状态失败,HTTP状态码: ' . $httpCode);
  885. }
  886. $data = json_decode($response, true);
  887. if (!is_array($data)) {
  888. throw new Exception('视频状态数据格式错误');
  889. }
  890. return $data;
  891. }
  892. /**
  893. * 下载视频文件
  894. */
  895. private function downloadVideo($url, $apiKey) {
  896. $ch = curl_init();
  897. curl_setopt($ch, CURLOPT_URL, $url);
  898. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  899. curl_setopt($ch, CURLOPT_HTTPGET, true);
  900. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  901. 'Authorization: Bearer ' . $apiKey
  902. ]);
  903. curl_setopt($ch, CURLOPT_TIMEOUT, 300);
  904. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  905. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  906. $response = curl_exec($ch);
  907. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  908. $error = curl_error($ch);
  909. curl_close($ch);
  910. if ($error) {
  911. throw new Exception('视频下载失败: ' . $error);
  912. }
  913. if ($httpCode < 200 || $httpCode >= 300) {
  914. throw new Exception('视频下载失败,HTTP状态码: ' . $httpCode);
  915. }
  916. return $response;
  917. }
  918. private function sendPostRequest($url, $data, $apiKey)
  919. {
  920. $ch = curl_init();
  921. curl_setopt($ch, CURLOPT_URL, $url);
  922. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  923. curl_setopt($ch, CURLOPT_POST, true);
  924. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  925. 'Authorization: Bearer ' . $apiKey,
  926. 'Accept: application/json',
  927. 'Content-Type: application/json'
  928. ]);
  929. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  930. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  931. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  932. curl_setopt($ch, CURLOPT_TIMEOUT, 60); // 延长超时时间
  933. $response = curl_exec($ch);
  934. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  935. $error = curl_error($ch);
  936. curl_close($ch);
  937. return [
  938. 'response' => $response,
  939. 'http_code' => $httpCode,
  940. 'error' => $error
  941. ];
  942. }
  943. /**
  944. * 查询队列列表
  945. * 统计文件对应的队列情况
  946. */
  947. public function get_queue_logs()
  948. {
  949. $params = $this->request->param('old_image_file', '');
  950. $queue_logs = Db::name('queue_logs')
  951. ->where('old_image_file', $params)
  952. ->order('id desc')
  953. ->select();
  954. $result = []; //初始化变量,避免未定义错误
  955. foreach ($queue_logs as &$log) {
  956. $taskId = $log['id'];
  957. $statusCount = Db::name('image_task_log')
  958. ->field('status, COUNT(*) as count')
  959. ->where('task_id', $taskId)
  960. ->where('mod_rq', null)
  961. ->group('status')
  962. ->select();
  963. $log['已完成数量'] = 0;
  964. $log['处理中数量'] = 0;
  965. $log['排队中的数量'] = 0;
  966. $log['失败数量'] = 0;
  967. foreach ($statusCount as $item) {
  968. switch ($item['status']) {
  969. case 0:
  970. $log['排队中的数量'] = $item['count'];
  971. break;
  972. case 1:
  973. $log['处理中数量'] = $item['count'];
  974. break;
  975. case 2:
  976. $log['已完成数量'] = $item['count'];
  977. break;
  978. case -1:
  979. $log['失败数量'] = $item['count'];
  980. break;
  981. }
  982. }
  983. // if ($log['排队中的数量'] >$log['已完成数量']) {
  984. // $result[] = $log;
  985. // }
  986. if ($log['排队中的数量']) {
  987. $result[] = $log;
  988. }
  989. // if ($log['处理中数量'] >= 0) {
  990. // $result[] = $log;
  991. // }
  992. }
  993. return json([
  994. 'code' => 0,
  995. 'msg' => '查询成功',
  996. 'data' => $result,
  997. 'count' => count($result)
  998. ]);
  999. }
  1000. /**
  1001. * 查询总队列状态(统计当前处理的数据量)
  1002. */
  1003. public function queueStats()
  1004. {
  1005. $statusList = Db::name('image_task_log')
  1006. ->field('status, COUNT(*) as total')
  1007. ->where('mod_rq', null)
  1008. ->where('create_time', '>=', date('Y-m-d 00:00:00'))
  1009. ->group('status')
  1010. ->select();
  1011. $statusCount = [];
  1012. foreach ($statusList as $item) {
  1013. $statusCount[$item['status']] = $item['total'];
  1014. }
  1015. // 总数为所有状态和
  1016. $total = array_sum($statusCount);
  1017. //获取队列当前状态
  1018. $statusText = Db::name('queue_logs')->order('id desc')->value('status');
  1019. return json([
  1020. 'code' => 0,
  1021. 'msg' => '获取成功',
  1022. 'data' => [
  1023. '总任务数' => $total,
  1024. '待处理' => $statusCount[0] ?? 0,
  1025. '处理中' => $statusCount[1] ?? 0,
  1026. '成功' => $statusCount[2] ?? 0,
  1027. '失败' => $statusCount[-1] ?? 0,
  1028. '当前状态' => $statusText
  1029. ]
  1030. ]);
  1031. }
  1032. /**
  1033. * 获取 Redis 连接实例
  1034. * @return \Redis|null Redis 实例或 null(如果连接失败)
  1035. */
  1036. private function getRedisConnection()
  1037. {
  1038. if (!class_exists('\Redis')) {
  1039. return null;
  1040. }
  1041. $redis = new \Redis();
  1042. $redis->connect('127.0.0.1', 6379);
  1043. $redis->auth('123456');
  1044. $redis->select(15);
  1045. return $redis;
  1046. }
  1047. /**
  1048. * 显示当前运行中的队列监听进程
  1049. */
  1050. public function viewQueueStatus()
  1051. {
  1052. $redis = $this->getRedisConnection();
  1053. if (!$redis) {
  1054. return json([
  1055. 'code' => 1,
  1056. 'msg' => 'Redis扩展未安装或未启用',
  1057. 'data' => null
  1058. ]);
  1059. }
  1060. $key = 'queues:imgtotxt';
  1061. // 判断 key 是否存在,避免报错
  1062. if (!$redis->exists($key)) {
  1063. return json([
  1064. 'code' => 0,
  1065. 'msg' => '查询成功,队列为空',
  1066. 'count' => 0,
  1067. 'tasks_preview' => []
  1068. ]);
  1069. }
  1070. $count = $redis->lLen($key);
  1071. $list = $redis->lRange($key, 0, 9);
  1072. // 解码 JSON 内容,确保每一项都有效
  1073. $parsed = array_filter(array_map(function ($item) {
  1074. return json_decode($item, true);
  1075. }, $list), function ($item) {
  1076. return !is_null($item);
  1077. });
  1078. return json([
  1079. 'code' => 0,
  1080. 'msg' => '查询成功',
  1081. 'count' => $count,
  1082. 'tasks_preview' => $parsed
  1083. ]);
  1084. }
  1085. /**
  1086. * 清空队列并删除队列日志记录
  1087. */
  1088. public function stopQueueProcesses()
  1089. {
  1090. Db::name('image_task_log')
  1091. ->where('log', '队列中')
  1092. ->whereOr('status', 1)
  1093. ->where('create_time', '>=', date('Y-m-d 00:00:00'))
  1094. ->update([
  1095. 'status' => "-1",
  1096. 'log' => '清空取消队列',
  1097. 'mod_rq' => date('Y-m-d H:i:s')
  1098. ]);
  1099. Db::name('image_task_log')
  1100. ->whereLike('log', '%处理中%')
  1101. ->where('create_time', '>=', date('Y-m-d 00:00:00'))
  1102. ->update([
  1103. 'status' => "-1",
  1104. 'log' => '清空取消队列',
  1105. 'mod_rq' => date('Y-m-d H:i:s')
  1106. ]);
  1107. $redis = $this->getRedisConnection();
  1108. if (!$redis) {
  1109. return json([
  1110. 'code' => 1,
  1111. 'msg' => 'Redis扩展未安装或未启用',
  1112. 'data' => null
  1113. ]);
  1114. }
  1115. $key_txttoimg = 'queues:txttoimg:reserved';
  1116. $key_txttotxt = 'queues:txttotxt:reserved';
  1117. $key_imgtotxt = 'queues:imgtotxt:reserved';
  1118. $key_imgtoimg = 'queues:imgtoimg:reserved';
  1119. // 清空 Redis 队列
  1120. $redis->del($key_txttoimg);
  1121. $redis->del($key_txttotxt);
  1122. $redis->del($key_imgtotxt);
  1123. $redis->del($key_imgtoimg);
  1124. $count = $redis->lLen($key_txttoimg) + $redis->lLen($key_txttotxt) + $redis->lLen($key_imgtotxt) + $redis->lLen($key_imgtoimg);
  1125. // if ($count === 0) {
  1126. // return json([
  1127. // 'code' => 1,
  1128. // 'msg' => '暂无队列需要停止'
  1129. // ]);
  1130. // }
  1131. return json([
  1132. 'code' => 0,
  1133. 'msg' => '成功停止队列任务'
  1134. ]);
  1135. }
  1136. /**
  1137. * 开启队列任务
  1138. * 暂时用不到、服务器已开启自动开启队列模式
  1139. */
  1140. // public function kaiStats()
  1141. // {
  1142. // // 判断是否已有监听进程在运行
  1143. // $check = shell_exec("ps aux | grep 'queue:listen' | grep -v grep");
  1144. // if ($check) {
  1145. // return json([
  1146. // 'code' => 1,
  1147. // 'msg' => '监听进程已存在,请勿重复启动'
  1148. // ]);
  1149. // }
  1150. // // 启动监听
  1151. // $command = 'nohup php think queue:listen --queue --timeout=300 --sleep=3 --memory=256 > /var/log/job_queue.log 2>&1 &';
  1152. // exec($command, $output, $status);
  1153. // if ($status === 0) {
  1154. // return json([
  1155. // 'code' => 0,
  1156. // 'msg' => '队列监听已启动'
  1157. // ]);
  1158. // } else {
  1159. // return json([
  1160. // 'code' => 1,
  1161. // 'msg' => '队列启动失败',
  1162. // 'output' => $output
  1163. // ]);
  1164. // }
  1165. // }
  1166. /**
  1167. * 新增模版
  1168. */
  1169. public function Add_Product_Template(){
  1170. try {
  1171. $params = $this->request->param();
  1172. $chinese_description = $params['chinese_description'] ?? '';
  1173. $template_image_url = $params['template_image_url'] ?? '';
  1174. $template_name = $params['template_name'] ?? '';
  1175. $size = $params['size'] ?? '';
  1176. $style = $params['style'] ?? '';
  1177. $type = $params['type'] ?? '';
  1178. $user_id = $params['user_id'] ?? '';
  1179. $seconds = $params['seconds'] ?? '';
  1180. $video_id = $params['video_id'] ?? '';
  1181. if (empty($template_name)) {
  1182. return json(['code' => 1, 'msg' => '模板名称不能为空']);
  1183. }
  1184. $data = [
  1185. 'toexamine' => "未审核",
  1186. 'chinese_description' => $chinese_description,
  1187. 'template_image_url' => $template_image_url,
  1188. 'template_name' => $template_name,
  1189. 'type' => $type,
  1190. 'style' => $style,
  1191. 'seconds' => $seconds,
  1192. 'size' => $size,
  1193. 'video_id' => $video_id,
  1194. 'user_id' => $user_id,
  1195. 'sys_rq' => date('Y-m-d'),
  1196. 'create_time' => date('Y-m-d H:i:s')
  1197. ];
  1198. // echo "<pre>";
  1199. // print_r($data);
  1200. // echo "<pre>";die;
  1201. $result = Db::name('product_template')->insert($data);
  1202. if ($result) {
  1203. return json(['code' => 0, 'msg' => '模板保存成功']);
  1204. } else {
  1205. return json(['code' => 1, 'msg' => '模板保存失败: 数据库操作未影响任何行']);
  1206. }
  1207. } catch (\Exception $e) {
  1208. Log::record('模板保存异常: ' . $e->getMessage(), 'error');
  1209. Log::record('异常堆栈: ' . $e->getTraceAsString(), 'error');
  1210. return json(['code' => 1, 'msg' => '模板保存失败: ' . $e->getMessage()]);
  1211. }
  1212. }
  1213. /**
  1214. * 查询模版
  1215. */
  1216. public function product_template()
  1217. {
  1218. $params = $this->request->param();
  1219. // 构建查询条件
  1220. $where = [];
  1221. if (!empty($params['search'])) {
  1222. $where['chinese_description'] = ['like', '%' . $params['search'] . '%'];
  1223. }
  1224. // 查询模版表-style分类名称
  1225. $products = Db::name('product_template')->order('id desc')->where($where)
  1226. ->where('type','文生图')
  1227. ->whereNull('mod_rq')
  1228. ->select();
  1229. $http_url = Db::name('http_url')->field('baseUrl,port')->find();
  1230. if ($products && $http_url) {
  1231. $base_url = !empty($http_url['baseUrl']) && !empty($http_url['port'])
  1232. ? 'http://' . $http_url['baseUrl'] . ':' . $http_url['port'] : '';
  1233. if ($base_url) {
  1234. foreach ($products as &$val) {
  1235. $val['template_image_url'] = $base_url . $val['template_image_url'];
  1236. }
  1237. }
  1238. }
  1239. return json([
  1240. 'code' => 0,
  1241. 'msg' => '请求成功',
  1242. 'data' => $products
  1243. ]);
  1244. }
  1245. /**
  1246. *获取服务器URL地址和端口 IP地址:端口
  1247. * 用于获取图片路径拼接时
  1248. **/
  1249. public function GetHttpUrl(){
  1250. $data = Db::name('http_url')->find();
  1251. $fullUrl = "http://" . $data['baseUrl'] . ":" . $data['port'];
  1252. $res = [
  1253. 'code' => 0,
  1254. 'msg' => '成功',
  1255. 'data' => [
  1256. 'id' => $data['id'],
  1257. 'full_url' => $fullUrl,
  1258. 'baseUrl' => $data['baseUrl'],
  1259. 'port' => $data['port']
  1260. ]
  1261. ];
  1262. return json($res);
  1263. }
  1264. }