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