Quellcode durchsuchen

完成提示词模板管理接口

lh vor 1 Woche
Ursprung
Commit
bc1901b902

+ 130 - 0
app/Http/Controllers/PromptTemplate/PromptTemplateController.php

@@ -0,0 +1,130 @@
+<?php
+
+namespace App\Http\Controllers\PromptTemplate;
+
+use App\Libs\ApiResponse;
+use App\Libs\Utils;
+use App\Services\PromptTemplate\PromptTemplateService;
+use App\Transformer\PromptTemplate\PromptTemplateTransformer;
+use Illuminate\Http\Request;
+use Illuminate\Routing\Controller as BaseController;
+use Illuminate\Support\Facades\Validator;
+
+class PromptTemplateController extends BaseController
+{
+    use ApiResponse;
+    protected $promptTemplateService;
+
+    public function __construct(PromptTemplateService $promptTemplateService)
+    {
+        $this->promptTemplateService = $promptTemplateService;
+    }
+
+    /**
+     * 提示词模板列表
+     *
+     * @param Request $request
+     * @return mixed
+     */
+    // public function templateList(Request $request)
+    // {
+    //     $data = $request->all();
+
+    //     $result = $this->promptTemplateService->getTemplateList($data);
+    //     return $this->success($result, [new PromptTemplateTransformer(), 'buildTemplateList']);
+    // }
+
+    public function promptTemplates(Request $request)
+    {
+        $data = $request->all();
+
+        $result = $this->promptTemplateService->getTemplates($data);
+        return $this->success($result);
+    }
+
+    /**
+     * 创建提示词模板
+     *
+     * @param Request $request
+     * @return mixed
+     */
+    public function createTemplate(Request $request)
+    {
+        $data = $request->all();
+        
+        $validator = Validator::make($data, [
+            'template_name' => 'required|string|max:255',
+            'template_prompt' => 'required|string',
+            'remark' => 'nullable|string|max:500',
+        ], [
+            'template_name.required' => '模板名称不能为空',
+            'template_name.max' => '模板名称不能超过255个字符',
+            'template_prompt.required' => '模板提示词不能为空',
+            'remark.max' => '备注不能超过500个字符',
+        ]);
+
+        if ($validator->fails()) {
+            Utils::throwError('1002:'.$validator->errors()->first());
+        }
+
+        $result = $this->promptTemplateService->createTemplate($data);
+        return $this->success($result);
+    }
+
+    /**
+     * 编辑提示词模板
+     *
+     * @param Request $request
+     * @return mixed
+     */
+    public function editTemplate(Request $request)
+    {
+        $data = $request->all();
+        
+        $validator = Validator::make($data, [
+            'template_id' => 'required|integer|min:1',
+            'template_name' => 'nullable|string|max:255',
+            'template_prompt' => 'nullable|string',
+            'remark' => 'nullable|string|max:500',
+        ], [
+            'template_id.required' => '模板ID不能为空',
+            'template_id.integer' => '模板ID必须为整数',
+            'template_id.min' => '模板ID必须大于0',
+            'template_name.max' => '模板名称不能超过255个字符',
+            'remark.max' => '备注不能超过500个字符',
+        ]);
+
+        if ($validator->fails()) {
+            Utils::throwError('1002:'.$validator->errors()->first());
+        }
+
+        $result = $this->promptTemplateService->editTemplate($data);
+        return $this->success($result);
+    }
+
+    /**
+     * 删除提示词模板
+     *
+     * @param Request $request
+     * @return mixed
+     */
+    public function deleteTemplate(Request $request)
+    {
+        $data = $request->all();
+        
+        $validator = Validator::make($data, [
+            'template_id' => 'required|integer|min:1',
+        ], [
+            'template_id.required' => '模板ID不能为空',
+            'template_id.integer' => '模板ID必须为整数',
+            'template_id.min' => '模板ID必须大于0',
+        ]);
+
+        if ($validator->fails()) {
+            Utils::throwError('1002:'.$validator->errors()->first());
+        }
+
+        $result = $this->promptTemplateService->deleteTemplate($data['template_id']);
+        return $this->success(['success' => $result ? 1 : 0]);
+    }
+}

+ 199 - 0
app/Services/PromptTemplate/PromptTemplateService.php

@@ -0,0 +1,199 @@
+<?php
+
+namespace App\Services\PromptTemplate;
+
+use App\Consts\ErrorConst;
+use App\Exceptions\ApiException;
+use App\Facade\Site;
+use App\Libs\Utils;
+use Illuminate\Support\Facades\DB;
+
+class PromptTemplateService
+{
+    /**
+     * 获取提示词模板列表
+     *
+     * @param array $data
+     * @return array
+     */
+    public function getTemplates($data = [])
+    {
+        $cpid = Site::getCpid();
+        $templateId = getProp($data, 'template_id');
+        $templateName = getProp($data, 'template_name');
+        $remark = getProp($data, 'remark');
+
+        $query = DB::table('mp_prompt_templates')->where('cpid', $cpid)->where('is_deleted', 0)->select('id as template_id', 'template_name', 'template_prompt', 'remark');
+
+        // 模板ID精确查询
+        if (!empty($templateId)) {
+            $query->where('id', $templateId);
+        }
+
+        // 模板名称模糊查询
+        if (!empty($templateName)) {
+            $query->where('template_name', 'like', "%{$templateName}%");
+        }
+
+        // 备注标签模糊查询
+        if (!empty($remark)) {
+            $query->where('remark', 'like', "%{$remark}%");
+        }
+
+        // 查询所有数据
+        $list = $query->orderBy('id', 'desc')->get();
+
+        return $list;
+    }
+
+    /**
+     * 创建提示词模板
+     *
+     * @param array $data
+     * @return array
+     * @throws ApiException
+     */
+    public function createTemplate($data)
+    {
+        $cpid = Site::getCpid();
+        $templateName = trim($data['template_name']);
+        $templatePrompt = trim($data['template_prompt']);
+        $remark = $data['remark'] ?? '';
+
+        // 检查模板名称是否已存在(同cpid下)
+        $exists = DB::table('mp_prompt_templates')
+            ->where('cpid', $cpid)
+            ->where('template_name', $templateName)
+            ->where('is_deleted', 0)
+            ->exists();
+
+        if ($exists) {
+            Utils::throwError('20003:模版名已存在,请调整');
+        }
+
+        $insertData = [
+            'cpid' => $cpid,
+            'template_name' => $templateName,
+            'template_prompt' => $templatePrompt,
+            'remark' => $remark,
+            'created_at' => date('Y-m-d H:i:s'),
+            'updated_at' => date('Y-m-d H:i:s'),
+        ];
+
+        $templateId = DB::table('mp_prompt_templates')->insertGetId($insertData);
+
+        return [
+            'template_id' => $templateId,
+            'template_name' => $templateName,
+            'template_prompt' => $templatePrompt,
+            'remark' => $remark,
+        ];
+    }
+
+    /**
+     * 编辑提示词模板
+     *
+     * @param array $data
+     * @return array
+     * @throws ApiException
+     */
+    public function editTemplate($data)
+    {
+        $cpid = Site::getCpid();
+        $templateId = $data['template_id'];
+
+        // 检查模板是否存在
+        $template = DB::table('mp_prompt_templates')
+            ->where('id', $templateId)
+            ->where('is_deleted', 0)
+            ->first();
+
+        if (!$template) {
+            Utils::throwError('20003:模板不存在');
+        }
+
+        // 验证cpid权限
+        if ($template->cpid != $cpid) {
+            Utils::throwError('20003:没有权限操作此模板');
+        }
+
+        $updateData = ['updated_at' => date('Y-m-d H:i:s')];
+
+        // 如果要更新模板名称,检查是否重复(同cpid下)
+        if (isset($data['template_name'])) {
+            $templateName = trim($data['template_name']);
+            
+            $exists = DB::table('mp_prompt_templates')
+                ->where('cpid', $cpid)
+                ->where('template_name', $templateName)
+                ->where('id', '<>', $templateId)
+                ->where('is_deleted', 0)
+                ->exists();
+
+            if ($exists) {
+                Utils::throwError('20003:模版名已存在,请调整');
+            }
+
+            $updateData['template_name'] = $templateName;
+        }
+
+        if (isset($data['template_prompt'])) {
+            $updateData['template_prompt'] = trim($data['template_prompt']);
+        }
+
+        if (isset($data['remark'])) {
+            $updateData['remark'] = $data['remark'];
+        }
+
+        DB::table('mp_prompt_templates')
+            ->where('id', $templateId)
+            ->update($updateData);
+
+        // 返回更新后的数据
+        $updatedTemplate = DB::table('mp_prompt_templates')
+            ->where('id', $templateId)
+            ->first();
+
+        return [
+            'template_id' => $updatedTemplate->id,
+            'template_name' => $updatedTemplate->template_name,
+            'template_prompt' => $updatedTemplate->template_prompt,
+            'remark' => $updatedTemplate->remark ?? '',
+            'updated_at' => $updatedTemplate->updated_at,
+        ];
+    }
+
+    /**
+     * 删除提示词模板
+     *
+     * @param int $templateId
+     * @return array
+     * @throws ApiException
+     */
+    public function deleteTemplate($templateId)
+    {
+        $cpid = Site::getCpid();
+
+        // 检查模板是否存在
+        $template = DB::table('mp_prompt_templates')
+            ->where('id', $templateId)
+            ->where('is_deleted', 0)
+            ->first();
+
+        if (!$template) {
+            Utils::throwError('20003:模板不存在');
+        }
+
+        // 验证cpid权限
+        if ($template->cpid != $cpid) {
+            Utils::throwError('20003:没有权限操作此模板');
+        }
+
+        return DB::table('mp_prompt_templates')
+            ->where('id', $templateId)
+            ->update([
+                'is_deleted' => 1,
+                'updated_at' => date('Y-m-d H:i:s')
+            ]);
+    }
+}

+ 32 - 0
app/Transformer/PromptTemplate/PromptTemplateTransformer.php

@@ -0,0 +1,32 @@
+<?php
+
+namespace App\Transformer\PromptTemplate;
+
+class PromptTemplateTransformer
+{
+    public function buildTemplateList($data): array
+    {
+        return [
+            'list' => $this->eachTemplateList($data),
+        ];
+    }
+
+    private function eachTemplateList($list): array
+    {
+        $result = [];
+        if (empty($list)) return $result;
+
+        foreach ($list as $item) {
+            $result[] = [
+                'template_id'       => getProp($item, 'id'),
+                'template_name'     => getProp($item, 'template_name'),
+                'template_prompt'   => getProp($item, 'template_prompt'),
+                'remark'            => getProp($item, 'remark') ?? '',
+                'created_at'        => getProp($item, 'created_at'),
+                'updated_at'        => getProp($item, 'updated_at'),
+            ];
+        }
+
+        return $result;
+    }
+}

+ 6 - 0
routes/api.php

@@ -7,6 +7,7 @@ use App\Http\Controllers\Timbre\TimbreController;
 use App\Http\Controllers\AIGeneration\ImageGenerationController;
 use App\Http\Controllers\AIGeneration\VideoGenerationController;
 use App\Http\Controllers\Anime\AnimeController;
+use App\Http\Controllers\PromptTemplate\PromptTemplateController;
 use Illuminate\Support\Facades\Route;
 
 /*
@@ -225,6 +226,11 @@ Route::group(['middleware' => ['bindToken', 'bindExportToken', 'checkLogin']], f
             // 生成三视图(基于参考图生成主体的标准正面、侧面、后面三视图,主体完整。在最左侧添加主体正视图的特写,主体清晰完整。背景修改成纯白色,整体保持与参考图一致的美术风格和色彩搭配。画面无说明文字)
             Route::post('generateThreeView', [AnimeController::class, 'generateThreeView']);
 
+            // 提示词模板管理
+            Route::get('promptTemplates', [PromptTemplateController::class, 'promptTemplates']);          // 提示词模板列表
+            Route::post('createPromptTemplate', [PromptTemplateController::class, 'createTemplate']);     // 创建提示词模板
+            Route::post('editPromptTemplate', [PromptTemplateController::class, 'editTemplate']);         // 编辑提示词模板
+            Route::get('deletePromptTemplate', [PromptTemplateController::class, 'deleteTemplate']);      // 删除提示词模板
         });
     });