|
|
@@ -17,7 +17,6 @@ class WorkOrder extends Api{
|
|
|
protected $noNeedLogin = ['*'];
|
|
|
protected $noNeedRight = ['*'];
|
|
|
public function index(){echo '访问成功';}
|
|
|
-
|
|
|
/**
|
|
|
* AI队列入口处理 出图接口
|
|
|
* 此方法处理图像转换为文本的请求,将图像信息存入队列以供后续处理。
|
|
|
@@ -30,13 +29,11 @@ class WorkOrder extends Api{
|
|
|
$this->success('任务成功提交至队列');
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* product 产品专用统一api调用接口
|
|
|
* AI模型调用接口
|
|
|
*/
|
|
|
public function GetTxtToTxt(){
|
|
|
-
|
|
|
$params = $this->request->param();
|
|
|
|
|
|
$prompt = $params['prompt'];//提示词
|
|
|
@@ -64,21 +61,20 @@ class WorkOrder extends Api{
|
|
|
2. 不要包含非文本元素的描述
|
|
|
3. 不要添加任何额外的引导语、解释或开场白
|
|
|
4. 语言流畅自然";
|
|
|
- $result = $service->handleTextToText($fullPrompt, $model);
|
|
|
-
|
|
|
- if(!empty($params['id'])){
|
|
|
- Db::name('product')->where('id', $params['id'])->update(['content' => $result['data']]);
|
|
|
- }
|
|
|
+ $result = $service->handleTextToText($fullPrompt, $model);
|
|
|
|
|
|
- $res = [
|
|
|
- 'code' => 0,
|
|
|
- 'msg' => '优化成功',
|
|
|
- 'content' => $result['data']
|
|
|
- ];
|
|
|
- return json($res);
|
|
|
+ if(!empty($params['id'])){
|
|
|
+ Db::name('product')->where('id', $params['id'])->update(['content' => $result['data']]);
|
|
|
+ }
|
|
|
+ $res = [
|
|
|
+ 'code' => 0,
|
|
|
+ 'msg' => '优化成功',
|
|
|
+ 'content' => $result['data']
|
|
|
+ ];
|
|
|
+ return json($res);
|
|
|
|
|
|
}elseif($params['status_val'] == '文生图'){
|
|
|
- $service->handleTextToImg($params);
|
|
|
+ $service->handleTextToImg($params);
|
|
|
$res = [
|
|
|
'code' => 0,
|
|
|
'msg' => '正在生成图片中,请稍等.....'
|
|
|
@@ -89,6 +85,335 @@ class WorkOrder extends Api{
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 即梦AI--创建视频任务接口
|
|
|
+ * 首帧图 + 尾帧图 = 新效果视频
|
|
|
+ */
|
|
|
+ public function Create_ImgToVideo()
|
|
|
+ {
|
|
|
+ $aiGateway = new AIGatewayService();
|
|
|
+ $apiUrl = $aiGateway->config['Create_ImgToVideo']['api_url'];
|
|
|
+ $apiKey = $aiGateway->config['Create_ImgToVideo']['api_key'];
|
|
|
+
|
|
|
+ $params = $this->request->param();
|
|
|
+ $modelParams = [
|
|
|
+ 'resolution' => $params['resolution'] ?? '720p', // 分辨率:480p, 720p, 1080p
|
|
|
+ 'duration' => $params['duration'] ?? 5, // 视频时长(秒)
|
|
|
+ 'camerafixed' => $params['camerafixed'] ?? false, // 相机固定
|
|
|
+ 'watermark' => $params['watermark'] ?? true, // 水印
|
|
|
+ 'aspect_ratio' => $params['aspect_ratio'] ?? '16:9' // 视频比例:16:9, 4:3, 1:1等
|
|
|
+ ];
|
|
|
+ // 构建提示词
|
|
|
+ $prompt = $params['prompt'] ?? '';
|
|
|
+ $prompt .= ' --resolution ' . $modelParams['resolution'];
|
|
|
+ $prompt .= ' --duration ' . $modelParams['duration'];
|
|
|
+ $prompt .= ' --camerafixed ' . ($modelParams['camerafixed'] ? 'true' : 'false');
|
|
|
+ $prompt .= ' --watermark ' . ($modelParams['watermark'] ? 'true' : 'false');
|
|
|
+ $prompt .= ' --aspect_ratio ' . $modelParams['aspect_ratio'];
|
|
|
+
|
|
|
+ // 构建请求数据
|
|
|
+ $data = [
|
|
|
+ 'model' => 'doubao-seedance-1-0-pro-250528',//模型
|
|
|
+ 'content' => [
|
|
|
+ [
|
|
|
+ 'type' => 'text',
|
|
|
+ 'text' => $prompt
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'type' => 'image_url',
|
|
|
+ 'image_url' => [
|
|
|
+ 'url' => $params['first_image_url']// 首帧图片URL(参数)
|
|
|
+ ],
|
|
|
+ 'role' => 'first_image'
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'type' => 'image_url',
|
|
|
+ 'image_url' => [
|
|
|
+ 'url' => $params['last_image_url'] // 尾帧图片URL(参数)
|
|
|
+ ],
|
|
|
+ 'role' => 'last_image'
|
|
|
+ ]
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+ // 转换为 JSON 字符串
|
|
|
+ $jsonData = json_encode($data);
|
|
|
+
|
|
|
+ // 初始化 cURL
|
|
|
+ $ch = curl_init();
|
|
|
+ curl_setopt($ch, CURLOPT_URL, $apiUrl);
|
|
|
+ curl_setopt($ch, CURLOPT_POST, true);
|
|
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
|
|
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
|
+ 'Content-Type: application/json',
|
|
|
+ 'Authorization: Bearer ' . $apiKey
|
|
|
+ ]);
|
|
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 开发环境临时关闭SSL验证
|
|
|
+ curl_setopt($ch, CURLOPT_TIMEOUT, 60); // 超时时间
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ $response = curl_exec($ch);
|
|
|
+
|
|
|
+ // 检查 cURL 错误
|
|
|
+ if (curl_errno($ch)) {
|
|
|
+ $error = curl_error($ch);
|
|
|
+ curl_close($ch);
|
|
|
+ return json(['code' => 0, 'msg' => 'Curl 错误: ' . $error]);
|
|
|
+ }
|
|
|
+ curl_close($ch);
|
|
|
+
|
|
|
+ // 解析响应
|
|
|
+ $responseData = json_decode($response, true);
|
|
|
+
|
|
|
+ // 检查 API 错误
|
|
|
+ if (isset($responseData['error'])) {
|
|
|
+ return json(['code' => 0, 'msg' => 'API 错误: ' . $responseData['error']['message']]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取任务 ID
|
|
|
+ $taskId = $responseData['id'] ?? '';
|
|
|
+ if (empty($taskId)) {
|
|
|
+ return json(['code' => 0, 'msg' => '获取任务 ID 失败']);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 返回结果
|
|
|
+ return json([
|
|
|
+ 'code' => 1,
|
|
|
+ 'data' => [
|
|
|
+ 'task_id' => $taskId,
|
|
|
+ 'status' => $responseData['status'] ?? '',
|
|
|
+ 'created_at' => $responseData['created_at'] ?? ''
|
|
|
+ ]
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 即梦AI--获取视频接口
|
|
|
+ * 首帧图 + 尾帧图 = 新效果视频
|
|
|
+ */
|
|
|
+ public function Get_ImgToVideo()
|
|
|
+ {
|
|
|
+ $aiGateway = new AIGatewayService();
|
|
|
+ $apiUrl = $aiGateway->config['Create_ImgToVideo']['api_url'];
|
|
|
+ $apiKey = $aiGateway->config['Create_ImgToVideo']['api_key'];
|
|
|
+
|
|
|
+ $params = $this->request->param();
|
|
|
+ $taskId = $params['task_id'] ?? '';
|
|
|
+ if (empty($taskId)) {
|
|
|
+ return json(['code' => 0, 'msg' => '任务 ID 不能为空']);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询任务状态
|
|
|
+ $queryUrl = $apiUrl . '/' . $taskId;
|
|
|
+ $ch2 = curl_init();
|
|
|
+ curl_setopt($ch2, CURLOPT_URL, $queryUrl);
|
|
|
+ curl_setopt($ch2, CURLOPT_HTTPHEADER, [
|
|
|
+ 'Content-Type: application/json',
|
|
|
+ 'Authorization: Bearer ' . $apiKey
|
|
|
+ ]);
|
|
|
+ curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
|
|
|
+ curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false); // 开发环境临时关闭SSL验证
|
|
|
+ curl_setopt($ch2, CURLOPT_TIMEOUT, 60); // 超时时间
|
|
|
+
|
|
|
+ $queryResponse = curl_exec($ch2);
|
|
|
+
|
|
|
+ // 检查 cURL 错误
|
|
|
+ if (curl_errno($ch2)) {
|
|
|
+ $error = curl_error($ch2);
|
|
|
+ curl_close($ch2);
|
|
|
+ return json(['code' => 0, 'msg' => 'Curl 错误: ' . $error]);
|
|
|
+ }
|
|
|
+ curl_close($ch2);
|
|
|
+
|
|
|
+ // 解析查询响应
|
|
|
+ $queryData = json_decode($queryResponse, true);
|
|
|
+ // print_r($queryData);die;
|
|
|
+
|
|
|
+ // 轮询任务状态,直到完成
|
|
|
+ $maxPolls = 30; // 最大轮询次数
|
|
|
+ $pollCount = 0;
|
|
|
+ $taskStatus = $queryData['status'] ?? '';
|
|
|
+
|
|
|
+ while (!in_array($taskStatus, ['completed', 'succeeded']) && $pollCount < $maxPolls) {
|
|
|
+ sleep(5); // 每5秒轮询一次
|
|
|
+ $pollCount++;
|
|
|
+
|
|
|
+ // 再次查询任务状态
|
|
|
+ $ch3 = curl_init();
|
|
|
+ curl_setopt($ch3, CURLOPT_URL, $queryUrl);
|
|
|
+ curl_setopt($ch3, CURLOPT_HTTPHEADER, [
|
|
|
+ 'Content-Type: application/json',
|
|
|
+ 'Authorization: Bearer ' . $apiKey
|
|
|
+ ]);
|
|
|
+ curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true);
|
|
|
+ curl_setopt($ch3, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
+ curl_setopt($ch3, CURLOPT_TIMEOUT, 60);
|
|
|
+
|
|
|
+ $pollResponse = curl_exec($ch3);
|
|
|
+ curl_close($ch3);
|
|
|
+
|
|
|
+ $pollData = json_decode($pollResponse, true);
|
|
|
+ $taskStatus = $pollData['status'] ?? '';
|
|
|
+
|
|
|
+ // 检查任务是否失败
|
|
|
+ if ($taskStatus === 'failed') {
|
|
|
+ return json(['code' => 0, 'msg' => '任务执行失败']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果任务已经成功,直接使用 $queryData
|
|
|
+ if (empty($pollData) && isset($queryData['status']) && $queryData['status'] === 'succeeded') {
|
|
|
+ $pollData = $queryData;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查轮询是否超时
|
|
|
+ if (!in_array($taskStatus, ['completed', 'succeeded'])) {
|
|
|
+ return json(['code' => 0, 'msg' => '任务执行超时']);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取视频 URL
|
|
|
+ $videoUrl = $pollData['content']['video_url'] ?? '';
|
|
|
+ if (empty($videoUrl)) {
|
|
|
+ return json(['code' => 0, 'msg' => '获取视频 URL 失败', 'data' => ['pollData' => $pollData]]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确保保存目录存在
|
|
|
+ $saveDir = ROOT_PATH . 'public/uploads/ceshi/';
|
|
|
+ if (!is_dir($saveDir)) {
|
|
|
+ mkdir($saveDir, 0755, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成保存文件名
|
|
|
+ $fileName = $taskId . '.mp4';
|
|
|
+ $savePath = $saveDir . '/' . $fileName;
|
|
|
+
|
|
|
+ // 下载视频
|
|
|
+ // 设置超时时间为300秒(5分钟)
|
|
|
+ $context = stream_context_create(['http' => ['timeout' => 300]]);
|
|
|
+ $videoContent = file_get_contents($videoUrl, false, $context);
|
|
|
+ if ($videoContent === false) {
|
|
|
+ return json(['code' => 0, 'msg' => '下载视频失败', 'data' => ['videoUrl' => $videoUrl]]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存视频到文件
|
|
|
+ $saveResult = file_put_contents($savePath, $videoContent);
|
|
|
+ if ($saveResult === false) {
|
|
|
+ return json(['code' => 0, 'msg' => '保存视频失败', 'data' => ['savePath' => $savePath, 'dirWritable' => is_writable($saveDir)]]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成视频的访问 URL
|
|
|
+ $videoAccessUrl = '/uploads/ceshi/' . $fileName;
|
|
|
+
|
|
|
+ // 返回结果
|
|
|
+ return json([
|
|
|
+ 'code' => 1,
|
|
|
+ 'data' => [
|
|
|
+ 'task_id' => $taskId,
|
|
|
+ 'status' => $taskStatus,
|
|
|
+ 'video_url' => $videoAccessUrl,
|
|
|
+ 'save_path' => $savePath
|
|
|
+ ]
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 九个分镜头生成流程
|
|
|
+ * 模型:gemini-3-pro-image-preview
|
|
|
+ * 说明:
|
|
|
+ * 第一步:(提示词 + 原始图片 = 九个分镜头图片) 或 (提示词 = 九个分镜头图片)
|
|
|
+ * 第二步:使用九个分镜头图进行裁剪单图生成连贯视频
|
|
|
+ * 第三步:在通过分镜头视频拼接成一个完整的视频(列如每个分镜头视频为8秒,九个为72秒形成完整视频)
|
|
|
+ */
|
|
|
+// public function Get_txttonineimg()
|
|
|
+// {
|
|
|
+// // 发起接口请求
|
|
|
+//// $apiUrl = 'https://chatapi.onechats.ai/v1beta/models/gemini-3-pro-image-preview:generateContent';
|
|
|
+// $apiUrl = 'https://chatapi.onechats.ai/v1beta/models/gemini-3-pro-image-preview:streamGenerateContent';
|
|
|
+// $apiKey = 'sk-9aIV9nx7pJxJFMrB8REtNbhjYuNBxCcnEOwiJDHd6UwmN2eJ';
|
|
|
+//
|
|
|
+//// $params = $this->request->param();
|
|
|
+// $prompt = '生成一个苹果(九个分镜头图片)';
|
|
|
+//
|
|
|
+// $requestData = [
|
|
|
+// "contents" => [
|
|
|
+// [
|
|
|
+// "role" => "user",
|
|
|
+// "parts" => [
|
|
|
+// ["text" => $prompt]
|
|
|
+// ]
|
|
|
+// ]
|
|
|
+// ],
|
|
|
+// "generationConfig" => [
|
|
|
+// "responseModalities" => ["TEXT", "IMAGE"],
|
|
|
+// "imageConfig" => [
|
|
|
+// "aspectRatio" => "1:1"
|
|
|
+// ]
|
|
|
+// ]
|
|
|
+// ];
|
|
|
+//
|
|
|
+// $ch = curl_init();
|
|
|
+// curl_setopt($ch, CURLOPT_URL, $apiUrl);
|
|
|
+// curl_setopt($ch, CURLOPT_POST, true);
|
|
|
+// curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData, JSON_UNESCAPED_UNICODE));
|
|
|
+// curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
|
+// 'Content-Type: application/json',
|
|
|
+// 'Authorization: Bearer ' . $apiKey
|
|
|
+// ]);
|
|
|
+// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
+// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 开发环境临时关闭SSL验证
|
|
|
+// curl_setopt($ch, CURLOPT_TIMEOUT, 60); // 生成图片超时时间(建议60秒)
|
|
|
+//
|
|
|
+// $response = curl_exec($ch);
|
|
|
+// $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
+//
|
|
|
+// $error = curl_error($ch);
|
|
|
+// curl_close($ch);
|
|
|
+// $res = json_decode($response, true);
|
|
|
+//
|
|
|
+// // 构建URL路径(使用正斜杠)
|
|
|
+// $url_path = '/uploads/txtnewimg/';
|
|
|
+// // 构建物理路径(使用正斜杠确保统一格式)
|
|
|
+// $save_path = ROOT_PATH . 'public' . '/' . 'uploads' . '/' . 'txtnewimg' . '/';
|
|
|
+// // 移除ROOT_PATH中可能存在的反斜杠,确保统一使用正斜杠
|
|
|
+// $save_path = str_replace('\\', '/', $save_path);
|
|
|
+// // 自动创建文件夹(如果不存在)
|
|
|
+// if (!is_dir($save_path)) {
|
|
|
+// mkdir($save_path, 0755, true);
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 提取base64图片数据
|
|
|
+// $text_content = $res['candidates'][0]['content']['parts'][0]['inlineData']['data'];
|
|
|
+// $str = 'data:image/jpeg;base64,';
|
|
|
+// $text_content = $str. $text_content;
|
|
|
+// // 匹配base64图片数据
|
|
|
+// preg_match('/data:image\/(png|jpg|jpeg);base64,([^"]+)/', $text_content, $matches);
|
|
|
+// if (empty($matches)) {
|
|
|
+// return '未找到图片数据';
|
|
|
+// }
|
|
|
+// $image_type = $matches[1];
|
|
|
+// $base64_data = $matches[2];
|
|
|
+//
|
|
|
+// // 解码base64数据
|
|
|
+// $image_data = base64_decode($base64_data);
|
|
|
+// if ($image_data === false) {
|
|
|
+// return '图片解码失败';
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 生成唯一文件名(包含扩展名)
|
|
|
+// $file_name = uniqid() . '.' . $image_type;
|
|
|
+// $full_file_path = $save_path . $file_name;
|
|
|
+//
|
|
|
+// // 保存图片到文件系统
|
|
|
+// if (!file_put_contents($full_file_path, $image_data)) {
|
|
|
+// return '图片保存失败';
|
|
|
+// }
|
|
|
+// // 生成数据库存储路径(使用正斜杠格式)
|
|
|
+// $db_img_path = $url_path . $file_name;
|
|
|
+// return $db_img_path;
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
public function GetTxtToImg(){
|
|
|
$params = $this->request->param();
|
|
|
|
|
|
@@ -120,7 +445,7 @@ class WorkOrder extends Api{
|
|
|
|
|
|
// 创建保存目录(public/uploads/log/YYYY-MM/)
|
|
|
$yearMonth = date('Ym');
|
|
|
- $saveDir = ROOT_PATH . 'public/uploads/log/' . $yearMonth . '/';
|
|
|
+ $saveDir = ROOT_PATH . 'public/uploads/ceshi/' . $yearMonth . '/';
|
|
|
if (!is_dir($saveDir)) {
|
|
|
mkdir($saveDir, 0755, true);
|
|
|
}
|
|
|
@@ -135,7 +460,7 @@ class WorkOrder extends Api{
|
|
|
}
|
|
|
|
|
|
// 生成前端可访问的URL
|
|
|
- $imageUrl = '/uploads/log/' . $yearMonth . '/' . $fileName;
|
|
|
+ $imageUrl = '/uploads/ceshi/' . $yearMonth . '/' . $fileName;
|
|
|
|
|
|
// 返回标准JSON响应
|
|
|
return json([
|
|
|
@@ -143,7 +468,6 @@ class WorkOrder extends Api{
|
|
|
'msg' => '图片生成成功',
|
|
|
'data' => [
|
|
|
'url' => $imageUrl
|
|
|
-// 'url' => '/uploads/log/202601/695b141099d1e.jpeg'
|
|
|
]
|
|
|
]);
|
|
|
}
|
|
|
@@ -152,15 +476,15 @@ class WorkOrder extends Api{
|
|
|
* 学生端文生视频接口 - 用于生成视频
|
|
|
*/
|
|
|
public function GetTxtToVideo(){
|
|
|
+ $aiGateway = new AIGatewayService();
|
|
|
+ $apiUrl = $aiGateway->config['videos']['api_url'];
|
|
|
+ $apiKey = $aiGateway->config['videos']['api_key'];
|
|
|
+
|
|
|
// 获取并验证参数
|
|
|
$params = $this->request->param();
|
|
|
-// echo "<pre>";
|
|
|
-// print_r($params);
|
|
|
-// echo "<pre>";die;
|
|
|
-
|
|
|
- // 正常的视频生成请求处理
|
|
|
- $apiUrl = 'https://chatapi.onechats.ai/v1/videos';
|
|
|
- $apiKey = 'sk-sWW1GFlnjbrDRb1DkMEzePIxgdvLK6cZt0Qg93yDMVP2z1yN';
|
|
|
+ // echo "<pre>";
|
|
|
+ // print_r($params);
|
|
|
+ // echo "<pre>";die;
|
|
|
|
|
|
$postData = [
|
|
|
'prompt' => $params['prompt'],
|
|
|
@@ -325,8 +649,11 @@ class WorkOrder extends Api{
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
+ $aiGateway = new AIGatewayService();
|
|
|
+// $apiUrl = $aiGateway->config['videos']['api_url'];
|
|
|
+ $apiKey = $aiGateway->config['videos']['api_key'];
|
|
|
+
|
|
|
$apiUrl = 'https://chatapi.onechats.ai/v1/videos/' . $videoId;
|
|
|
- $apiKey = 'sk-sWW1GFlnjbrDRb1DkMEzePIxgdvLK6cZt0Qg93yDMVP2z1yN';
|
|
|
|
|
|
// 先检查本地是否已经有该视频
|
|
|
$localVideoPath = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'videos' . DS . date('Ymd') . DS . $videoId . '.mp4';
|
|
|
@@ -405,22 +732,7 @@ class WorkOrder extends Api{
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
- //获取服务器URL IP地址:端口
|
|
|
- public function GetHttpUrl(){
|
|
|
- $data = Db::name('http_url')->find();
|
|
|
- $fullUrl = "http://" . $data['baseUrl'] . ":" . $data['port'];
|
|
|
- $res = [
|
|
|
- 'code' => 0,
|
|
|
- 'msg' => '成功',
|
|
|
- 'data' => [
|
|
|
- 'full_url' => $fullUrl,
|
|
|
- 'id' => $data['id'],
|
|
|
- 'baseUrl' => $data['baseUrl'],
|
|
|
- 'port' => $data['port']
|
|
|
- ]
|
|
|
- ];
|
|
|
- return json($res);
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
|
|
|
//获取视频列表
|
|
|
@@ -458,10 +770,11 @@ class WorkOrder extends Api{
|
|
|
*/
|
|
|
//文生视频
|
|
|
public function video(){
|
|
|
- $params = $this->request->param();
|
|
|
+ $aiGateway = new AIGatewayService();
|
|
|
+ $apiUrl = $aiGateway->config['videos']['api_url'];
|
|
|
+ $apiKey = $aiGateway->config['videos']['api_key'];
|
|
|
|
|
|
- $apiUrl = 'https://chatapi.onechats.ai/v1/videos';
|
|
|
- $apiKey = 'sk-sWW1GFlnjbrDRb1DkMEzePIxgdvLK6cZt0Qg93yDMVP2z1yN';
|
|
|
+ $params = $this->request->param();
|
|
|
$postData = [
|
|
|
'prompt' => $params['prompt'],
|
|
|
'model' => $params['model'],
|
|
|
@@ -752,181 +1065,6 @@ class WorkOrder extends Api{
|
|
|
];
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 文本生成图片并保存第一张结果
|
|
|
- * @param array $params 请求参数
|
|
|
- * @return array 返回结果
|
|
|
- */
|
|
|
- public function txttowimg()
|
|
|
- {
|
|
|
- // API配置
|
|
|
- $config = [
|
|
|
- 'api_url' => 'https://chatapi.onechats.ai/mj/submit/imagine',
|
|
|
- 'fetch_url' => 'https://chatapi.onechats.ai/mj/task/',
|
|
|
- 'api_key' => 'sk-iURfrAgzAjhZ4PpPLwzmWIAhM7zKfrkwDvyxk4RVBQ4ouJNK',
|
|
|
- 'default_prompt' => '一个猫',
|
|
|
- 'wait_time' => 3 // 等待生成完成的秒数
|
|
|
- ];
|
|
|
- try {
|
|
|
- // 1. 准备请求数据
|
|
|
- $prompt = $config['default_prompt'];
|
|
|
- $postData = [
|
|
|
- 'botType' => 'MID_JOURNEY',
|
|
|
- 'prompt' => $prompt,
|
|
|
- 'base64Array' => [],
|
|
|
- 'accountFilter' => [
|
|
|
- 'channelId' => "",
|
|
|
- 'instanceId' => "",
|
|
|
- 'modes' => [],
|
|
|
- 'remark' => "",
|
|
|
- 'remix' => true,
|
|
|
- 'remixAutoConsidered' => true
|
|
|
- ],
|
|
|
- 'notifyHook' => "",
|
|
|
- 'state' => ""
|
|
|
- ];
|
|
|
-
|
|
|
- // 2. 提交生成请求
|
|
|
- $generateResponse = $this->sendApiRequest($config['api_url'], $postData, $config['api_key']);
|
|
|
- $generateData = json_decode($generateResponse, true);
|
|
|
- if (empty($generateData['result'])) {
|
|
|
- throw new Exception('生成失败: '.($generateData['message'] ?? '未知错误'));
|
|
|
- }
|
|
|
- $taskId = $generateData['result'];
|
|
|
- // 3. 等待图片生成完成
|
|
|
- sleep($config['wait_time']);
|
|
|
- // 4. 获取生成结果
|
|
|
- $fetchUrl = $config['fetch_url'].$taskId.'/fetch';
|
|
|
- $fetchResponse = $this->sendApiRequest($fetchUrl, [], $config['api_key'], 'GET');
|
|
|
- $fetchData = json_decode($fetchResponse, true);
|
|
|
- if (empty($fetchData['imageUrl'])) {
|
|
|
- throw new Exception('获取图片失败: '.($fetchData['message'] ?? '未知错误'));
|
|
|
- }
|
|
|
- // 5. 处理返回的图片数组(取第一张)
|
|
|
- $imageUrls = is_array($fetchData['imageUrl']) ? $fetchData['imageUrl'] : [$fetchData['imageUrl']];
|
|
|
- $firstImageUrl = $imageUrls[0];
|
|
|
- // 6. 保存图片到本地
|
|
|
- $savePath = $this->saveImage($firstImageUrl);
|
|
|
- // 7. 返回结果
|
|
|
- return [
|
|
|
- 'code' => 200,
|
|
|
- 'msg' => '图片生成并保存成功',
|
|
|
- 'data' => [
|
|
|
- 'local_path' => $savePath,
|
|
|
- 'web_url' => request()->domain().$savePath,
|
|
|
- 'task_id' => $taskId
|
|
|
- ]
|
|
|
- ];
|
|
|
- } catch (Exception $e) {
|
|
|
- // 错误处理
|
|
|
- return [
|
|
|
- 'code' => 500,
|
|
|
- 'msg' => '处理失败: '.$e->getMessage(),
|
|
|
- 'data' => null
|
|
|
- ];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 发送API请求
|
|
|
- * @param string $url 请求地址
|
|
|
- * @param array $data 请求数据
|
|
|
- * @param string $apiKey API密钥
|
|
|
- * @param string $method 请求方法
|
|
|
- * @return string 响应内容
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- private function sendApiRequest($url, $data, $apiKey, $method = 'POST')
|
|
|
- {
|
|
|
- $ch = curl_init();
|
|
|
-
|
|
|
- curl_setopt_array($ch, [
|
|
|
- CURLOPT_URL => $url,
|
|
|
- CURLOPT_RETURNTRANSFER => true,
|
|
|
- CURLOPT_CUSTOMREQUEST => $method,
|
|
|
- CURLOPT_HTTPHEADER => [
|
|
|
- 'Authorization: Bearer '.$apiKey,
|
|
|
- 'Accept: application/json',
|
|
|
- 'Content-Type: application/json'
|
|
|
- ],
|
|
|
- CURLOPT_POSTFIELDS => $method === 'POST' ? json_encode($data) : null,
|
|
|
- CURLOPT_SSL_VERIFYPEER => false,
|
|
|
- CURLOPT_SSL_VERIFYHOST => false,
|
|
|
- CURLOPT_TIMEOUT => 60,
|
|
|
- CURLOPT_FAILONERROR => true
|
|
|
- ]);
|
|
|
-
|
|
|
- $response = curl_exec($ch);
|
|
|
- $error = curl_error($ch);
|
|
|
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
- curl_close($ch);
|
|
|
-
|
|
|
- if ($error) {
|
|
|
- throw new Exception('API请求失败: '.$error);
|
|
|
- }
|
|
|
-
|
|
|
- if ($httpCode < 200 || $httpCode >= 300) {
|
|
|
- throw new Exception('API返回错误状态码: '.$httpCode);
|
|
|
- }
|
|
|
-
|
|
|
- return $response;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 保存图片到本地
|
|
|
- * @param string $imageUrl 图片URL
|
|
|
- * @return string 本地保存路径
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- private function saveImage($imageUrl)
|
|
|
- {
|
|
|
- // 1. 创建存储目录
|
|
|
- $saveDir = ROOT_PATH.'public'.DS.'uploads'.DS.'midjourney'.DS.date('Ymd');
|
|
|
- if (!is_dir($saveDir)) {
|
|
|
- mkdir($saveDir, 0755, true);
|
|
|
- }
|
|
|
-
|
|
|
- // 2. 生成唯一文件名
|
|
|
- $filename = uniqid().'.png';
|
|
|
- $localPath = DS.'uploads'.DS.'midjourney'.DS.date('Ymd').DS.$filename;
|
|
|
- $fullPath = $saveDir.DS.$filename;
|
|
|
-
|
|
|
- // 3. 下载图片
|
|
|
- $ch = curl_init($imageUrl);
|
|
|
- curl_setopt_array($ch, [
|
|
|
- CURLOPT_RETURNTRANSFER => true,
|
|
|
- CURLOPT_FOLLOWLOCATION => true,
|
|
|
- CURLOPT_SSL_VERIFYPEER => false,
|
|
|
- CURLOPT_CONNECTTIMEOUT => 15
|
|
|
- ]);
|
|
|
-
|
|
|
- $imageData = curl_exec($ch);
|
|
|
- $error = curl_error($ch);
|
|
|
- curl_close($ch);
|
|
|
-
|
|
|
- if (!$imageData) {
|
|
|
- throw new Exception('图片下载失败: '.$error);
|
|
|
- }
|
|
|
-
|
|
|
- // 4. 验证图片类型
|
|
|
- $imageInfo = getimagesizefromstring($imageData);
|
|
|
- if (!in_array($imageInfo['mime'] ?? '', ['image/png', 'image/jpeg'])) {
|
|
|
- throw new Exception('下载内容不是有效图片');
|
|
|
- }
|
|
|
-
|
|
|
- // 5. 保存文件
|
|
|
- if (!file_put_contents($fullPath, $imageData)) {
|
|
|
- throw new Exception('图片保存失败');
|
|
|
- }
|
|
|
-
|
|
|
- return $localPath;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* 查询队列列表
|
|
|
* 统计文件对应的队列情况
|
|
|
@@ -1030,14 +1168,34 @@ class WorkOrder extends Api{
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 显示当前运行中的队列监听进程
|
|
|
+ * 获取 Redis 连接实例
|
|
|
+ * @return \Redis|null Redis 实例或 null(如果连接失败)
|
|
|
*/
|
|
|
- public function viewQueueStatus()
|
|
|
+ private function getRedisConnection()
|
|
|
{
|
|
|
+ if (!class_exists('\Redis')) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
$redis = new \Redis();
|
|
|
$redis->connect('127.0.0.1', 6379);
|
|
|
$redis->auth('123456');
|
|
|
$redis->select(15);
|
|
|
+ return $redis;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 显示当前运行中的队列监听进程
|
|
|
+ */
|
|
|
+ public function viewQueueStatus()
|
|
|
+ {
|
|
|
+ $redis = $this->getRedisConnection();
|
|
|
+ if (!$redis) {
|
|
|
+ return json([
|
|
|
+ 'code' => 1,
|
|
|
+ 'msg' => 'Redis扩展未安装或未启用',
|
|
|
+ 'data' => null
|
|
|
+ ]);
|
|
|
+ }
|
|
|
|
|
|
$key = 'queues:imgtotxt';
|
|
|
|
|
|
@@ -1074,7 +1232,6 @@ class WorkOrder extends Api{
|
|
|
*/
|
|
|
public function stopQueueProcesses()
|
|
|
{
|
|
|
-
|
|
|
Db::name('image_task_log')
|
|
|
->where('log', '队列中')
|
|
|
->whereOr('status', 1)
|
|
|
@@ -1094,10 +1251,14 @@ class WorkOrder extends Api{
|
|
|
'mod_rq' => date('Y-m-d H:i:s')
|
|
|
]);
|
|
|
|
|
|
- $redis = new \Redis();
|
|
|
- $redis->connect('127.0.0.1', 6379);
|
|
|
- $redis->auth('123456');
|
|
|
- $redis->select(15);
|
|
|
+ $redis = $this->getRedisConnection();
|
|
|
+ if (!$redis) {
|
|
|
+ return json([
|
|
|
+ 'code' => 1,
|
|
|
+ 'msg' => 'Redis扩展未安装或未启用',
|
|
|
+ 'data' => null
|
|
|
+ ]);
|
|
|
+ }
|
|
|
|
|
|
$key_txttoimg = 'queues:txttoimg:reserved';
|
|
|
$key_txttotxt = 'queues:txttotxt:reserved';
|
|
|
@@ -1155,44 +1316,12 @@ class WorkOrder extends Api{
|
|
|
// ]);
|
|
|
// }
|
|
|
// }
|
|
|
- /**
|
|
|
- * 通过店铺ID-查询对应店铺表数据
|
|
|
- *
|
|
|
- */
|
|
|
- public function PatternApi()
|
|
|
- {
|
|
|
- $params = $this->request->param('pattern_id', '');
|
|
|
- $tableName = 'pattern-' . $params;
|
|
|
-
|
|
|
- // 连接 MongoDB
|
|
|
- $mongo = Db::connect('mongodb');
|
|
|
-
|
|
|
- // 查询指定 skc 的数据
|
|
|
- $data = $mongo->table($tableName)
|
|
|
- ->field('
|
|
|
- name,
|
|
|
- skc,
|
|
|
- file
|
|
|
- ')
|
|
|
- ->where("skc", '0853004152036')
|
|
|
- ->select();
|
|
|
-
|
|
|
- $data = json_decode(json_encode($data), true); // 数组
|
|
|
-
|
|
|
- return json([
|
|
|
- 'code' => 0,
|
|
|
- 'msg' => '获取成功',
|
|
|
- 'data' => $data
|
|
|
- ]);
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
/**
|
|
|
- * 新增产品模版
|
|
|
+ * 新增模版
|
|
|
*/
|
|
|
public function Add_Product_Template(){
|
|
|
try {
|
|
|
- // 获取请求参数
|
|
|
$params = $this->request->param();
|
|
|
|
|
|
$chinese_description = $params['chinese_description'] ?? '';
|
|
|
@@ -1223,46 +1352,38 @@ class WorkOrder extends Api{
|
|
|
'sys_rq' => date('Y-m-d'),
|
|
|
'create_time' => date('Y-m-d H:i:s')
|
|
|
];
|
|
|
-
|
|
|
// echo "<pre>";
|
|
|
// print_r($data);
|
|
|
// echo "<pre>";die;
|
|
|
- // 记录插入的数据用于调试
|
|
|
-// Log::record('准备插入模板数据: ' . json_encode($data), 'info');
|
|
|
-
|
|
|
$result = Db::name('product_template')->insert($data);
|
|
|
-
|
|
|
if ($result) {
|
|
|
-// Log::record('模板保存成功,插入数据: ' . json_encode($data), 'info');
|
|
|
return json(['code' => 0, 'msg' => '模板保存成功']);
|
|
|
} else {
|
|
|
-// Log::record('模板保存失败,插入数据: ' . json_encode($data) . ',受影响行数: ' . $result, 'error');
|
|
|
return json(['code' => 1, 'msg' => '模板保存失败: 数据库操作未影响任何行']);
|
|
|
}
|
|
|
} catch (\Exception $e) {
|
|
|
- // 捕获并记录异常信息
|
|
|
Log::record('模板保存异常: ' . $e->getMessage(), 'error');
|
|
|
Log::record('异常堆栈: ' . $e->getTraceAsString(), 'error');
|
|
|
return json(['code' => 1, 'msg' => '模板保存失败: ' . $e->getMessage()]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
- * 新查询产品模版
|
|
|
+ * 查询模版
|
|
|
*/
|
|
|
public function product_template()
|
|
|
{
|
|
|
$params = $this->request->param();
|
|
|
-
|
|
|
// 构建查询条件
|
|
|
$where = [];
|
|
|
if (!empty($params['search'])) {
|
|
|
$where['chinese_description'] = ['like', '%' . $params['search'] . '%'];
|
|
|
}
|
|
|
-
|
|
|
- // 查询模版表
|
|
|
- $products = Db::name('product_template')->order('id desc')->where($where)->select();
|
|
|
+ // 查询模版表-style分类名称
|
|
|
+ $products = Db::name('product_template')->order('id desc')->where($where)
|
|
|
+ ->where('type','文生图')
|
|
|
+ ->whereNull('mod_rq')
|
|
|
+ ->select();
|
|
|
|
|
|
$http_url = Db::name('http_url')->field('baseUrl,port')->find();
|
|
|
if ($products && $http_url) {
|
|
|
@@ -1275,7 +1396,6 @@ class WorkOrder extends Api{
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
return json([
|
|
|
'code' => 0,
|
|
|
'msg' => '请求成功',
|
|
|
@@ -1283,6 +1403,23 @@ class WorkOrder extends Api{
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+ /**
|
|
|
+ *获取服务器URL地址和端口 IP地址:端口
|
|
|
+ * 用于获取图片路径拼接时
|
|
|
+ **/
|
|
|
+ public function GetHttpUrl(){
|
|
|
+ $data = Db::name('http_url')->find();
|
|
|
+ $fullUrl = "http://" . $data['baseUrl'] . ":" . $data['port'];
|
|
|
+ $res = [
|
|
|
+ 'code' => 0,
|
|
|
+ 'msg' => '成功',
|
|
|
+ 'data' => [
|
|
|
+ 'id' => $data['id'],
|
|
|
+ 'full_url' => $fullUrl,
|
|
|
+ 'baseUrl' => $data['baseUrl'],
|
|
|
+ 'port' => $data['port']
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+ return json($res);
|
|
|
+ }
|
|
|
}
|