| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Routing\Controller as BaseController;
- use App\Libs\Utils;
- use App\Services\AiImageGenerationService;
- use App\Transformer\ImageGenerationTransformer;
- use Illuminate\Http\Request;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Support\Facades\Validator;
- use App\Libs\ApiResponse;
- class ImageGenerationController extends BaseController
- {
- use ApiResponse;
- private $aiImageGenerationService;
- public function __construct(AiImageGenerationService $aiImageGenerationService)
- {
- $this->aiImageGenerationService = $aiImageGenerationService;
- }
- /**
- * 创建图片生成任务
- *
- * @param Request $request
- * @return JsonResponse
- */
- public function createTask(Request $request): JsonResponse
- {
- $data = $request->all();
- $validator = Validator::make($data, [
- 'prompt' => 'required|string|max:2000',
- 'width' => 'required|numeric|between:1024,4096',
- 'height' => 'required|numeric|between:1024,4096',
- 'image_num' => 'required|numeric|between:1,4',
- 'scale' => 'required|numeric|between:0,100',
- ], [
- 'prompt.required' => '提示词不能为空',
- 'prompt.max' => '提示词不能超过2000个字符',
- 'width.required' => '宽不能为空',
- 'width.numeric' => '宽必须是数字',
- 'width.between' => '宽必须在1024到4096之间',
- 'height.required' => '高不能为空',
- 'height.numeric' => '高必须是数字',
- 'height.between' => '高必须在1024到4096之间',
- 'image_num.required' => '生成图片数量不能为空',
- 'image_num.numeric' => '生成图片数量必须是数字',
- 'image_num.between' => '生成图片数量必须在1到4之间',
- 'scale.required' => '缩放比例不能为空',
- 'scale.numeric' => '缩放比例必须是数字',
- 'scale.between' => '缩放比例必须在0到100之间',
- ]);
- if ($validator->fails()) {
- Utils::throwError('1002:'.$validator->errors()->first());
- }
- try {
- $task = $this->aiImageGenerationService->createImageGenerationTask($data);
- return $this->success(['task_id' => $task->task_id, 'status' => $task->status]);
- } catch (\Exception $e) {
- Utils::throwError('1001:'.$e->getMessage());
- }
- }
- /**
- * 查询任务状态
- *
- * @param string $taskId
- * @return JsonResponse
- */
- public function taskStatus(string $taskId): JsonResponse
- {
- $task = \App\Models\MpGeneratePicTask::where('task_id', $taskId)->first();
- if (!$task) {
- return response()->json([
- 'success' => false,
- 'message' => '任务不存在'
- ], 404);
- }
- return response()->json([
- 'success' => true,
- 'data' => [
- 'task_id' => $task->task_id,
- 'status' => $task->status,
- 'result_url' => $task->result_url,
- 'error_message' => $task->error_message,
- 'created_at' => $task->created_at,
- 'started_at' => $task->started_at,
- 'completed_at' => $task->completed_at
- ]
- ]);
- }
- /**
- * 获取任务列表
- *
- * @param Request $request
- * @return mixed
- */
- public function taskList(Request $request)
- {
- $data = $request->all();
- $result = $this->aiImageGenerationService->getTaskList($data);
- return $this->success($result, [new ImageGenerationTransformer(), 'newBuildTaskList']);
- }
- }
|