Просмотр исходного кода

提示词模版新增公共库和个人库区分

lh 12 часов назад
Родитель
Сommit
18da61c0b5

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

@@ -34,10 +34,27 @@ class PromptTemplateController extends BaseController
     //     return $this->success($result, [new PromptTemplateTransformer(), 'buildTemplateList']);
     // }
 
+    /**
+     * 提示词模板列表
+     *
+     * @param Request $request
+     * @return mixed
+     */
     public function promptTemplates(Request $request)
     {
         $data = $request->all();
 
+        $validator = Validator::make($data, [
+            'public_type' => 'nullable|integer|in:0,1,2',
+        ], [
+            'public_type.integer' => 'public_type必须为整数',
+            'public_type.in' => 'public_type必须为0、1或2',
+        ]);
+
+        if ($validator->fails()) {
+            Utils::throwError('1002:'.$validator->errors()->first());
+        }
+
         $result = $this->promptTemplateService->getTemplates($data);
         return $this->success($result);
     }
@@ -56,11 +73,14 @@ class PromptTemplateController extends BaseController
             'template_name' => 'required|string|max:255',
             'template_prompt' => 'required|string',
             'remark' => 'nullable|string|max:500',
+            'is_public' => 'nullable|integer|in:0,1',
         ], [
             'template_name.required' => '模板名称不能为空',
             'template_name.max' => '模板名称不能超过255个字符',
             'template_prompt.required' => '模板提示词不能为空',
             'remark.max' => '备注不能超过500个字符',
+            'is_public.integer' => 'is_public必须为整数',
+            'is_public.in' => 'is_public必须为0或1',
         ]);
 
         if ($validator->fails()) {

+ 75 - 5
app/Services/PromptTemplate/PromptTemplateService.php

@@ -19,11 +19,33 @@ class PromptTemplateService
     public function getTemplates($data = [])
     {
         $cpid = Site::getCpid();
+        $uid = Site::getUid();
         $templateId = getProp($data, 'template_id');
         $templateName = getProp($data, 'template_name');
         $remark = getProp($data, 'remark');
+        $publicType = getProp($data, 'public_type', 2); // 0=个人库, 1=公共库, 2=所有库(默认)
 
-        $query = DB::table('mp_prompt_templates')->where('cpid', $cpid)->where('is_deleted', 0)->select('id as template_id', 'template_name', 'template_prompt', 'remark');
+        $query = DB::table('mp_prompt_templates')
+            ->where('cpid', $cpid)
+            ->where('is_deleted', 0)
+            ->select(
+                'id as template_id', 
+                'template_name', 
+                'template_prompt', 
+                'remark',
+                'user_id',
+                DB::raw('IF(user_id = 0, 1, 0) as is_public')
+            );
+
+        // 根据 public_type 筛选
+        if ($publicType == 0) {
+            // 个人库:user_id 等于当前用户
+            $query->where('user_id', $uid);
+        } elseif ($publicType == 1) {
+            // 公共库:user_id 为 0
+            $query->where('user_id', 0);
+        }
+        // public_type == 2 时不添加 user_id 条件,返回所有库
 
         // 模板ID精确查询
         if (!empty($templateId)) {
@@ -40,8 +62,8 @@ class PromptTemplateService
             $query->where('remark', 'like', "%{$remark}%");
         }
 
-        // 查询所有数据
-        $list = $query->orderBy('id', 'desc')->get();
+        // 查询所有数据,排序:个人库在前(user_id > 0),公共库在后(user_id = 0)
+        $list = $query->orderByRaw('IF(user_id = 0, 1, 0) ASC')->orderBy('id', 'desc')->get();
 
         return $list;
     }
@@ -56,13 +78,27 @@ class PromptTemplateService
     public function createTemplate($data)
     {
         $cpid = Site::getCpid();
+        $uid = Site::getUid();
+        $role = Site::getRole();
         $templateName = trim($data['template_name']);
         $templatePrompt = trim($data['template_prompt']);
         $remark = $data['remark'] ?? '';
+        $isPublic = getProp($data, 'is_public', 0); // 是否创建为公共库,默认为 0(个人库)
+
+        // 如果要创建公共库数据,需要验证是否为 admin 角色
+        if ($isPublic == 1) {
+            if ($role !== 'admin') {
+                Utils::throwError('20003:权限不足,只有管理员可以操作公共库');
+            }
+        }
+
+        // 确定 user_id:公共库为 0,个人库为当前用户 ID
+        $userId = $isPublic == 1 ? 0 : $uid;
 
-        // 检查模板名称是否已存在(同cpid下)
+        // 检查模板名称是否已存在(同cpid、同user_id下)
         $exists = DB::table('mp_prompt_templates')
             ->where('cpid', $cpid)
+            ->where('user_id', $userId)
             ->where('template_name', $templateName)
             ->where('is_deleted', 0)
             ->exists();
@@ -73,6 +109,7 @@ class PromptTemplateService
 
         $insertData = [
             'cpid' => $cpid,
+            'user_id' => $userId,
             'template_name' => $templateName,
             'template_prompt' => $templatePrompt,
             'remark' => $remark,
@@ -87,6 +124,7 @@ class PromptTemplateService
             'template_name' => $templateName,
             'template_prompt' => $templatePrompt,
             'remark' => $remark,
+            'is_public' => $isPublic,
         ];
     }
 
@@ -100,6 +138,8 @@ class PromptTemplateService
     public function editTemplate($data)
     {
         $cpid = Site::getCpid();
+        $uid = Site::getUid();
+        $role = Site::getRole();
         $templateId = $data['template_id'];
 
         // 检查模板是否存在
@@ -117,14 +157,28 @@ class PromptTemplateService
             Utils::throwError('20003:没有权限操作此模板');
         }
 
+        // 判断是否为公共库数据
+        $isPublic = $template->user_id == 0;
+
+        // 如果是公共库数据,需要验证是否为 admin 角色
+        if ($isPublic && $role !== 'admin') {
+            Utils::throwError('20003:权限不足,只有管理员可以操作公共库');
+        }
+
+        // 如果是个人库数据,需要验证是否为所有者
+        if (!$isPublic && $template->user_id != $uid) {
+            Utils::throwError('20003:没有权限操作此模板');
+        }
+
         $updateData = ['updated_at' => date('Y-m-d H:i:s')];
 
-        // 如果要更新模板名称,检查是否重复(同cpid下)
+        // 如果要更新模板名称,检查是否重复(同cpid、同user_id下)
         if (isset($data['template_name'])) {
             $templateName = trim($data['template_name']);
             
             $exists = DB::table('mp_prompt_templates')
                 ->where('cpid', $cpid)
+                ->where('user_id', $template->user_id)
                 ->where('template_name', $templateName)
                 ->where('id', '<>', $templateId)
                 ->where('is_deleted', 0)
@@ -159,6 +213,7 @@ class PromptTemplateService
             'template_name' => $updatedTemplate->template_name,
             'template_prompt' => $updatedTemplate->template_prompt,
             'remark' => $updatedTemplate->remark ?? '',
+            'is_public' => $updatedTemplate->user_id == 0 ? 1 : 0,
             'updated_at' => $updatedTemplate->updated_at,
         ];
     }
@@ -173,6 +228,8 @@ class PromptTemplateService
     public function deleteTemplate($templateId)
     {
         $cpid = Site::getCpid();
+        $uid = Site::getUid();
+        $role = Site::getRole();
 
         // 检查模板是否存在
         $template = DB::table('mp_prompt_templates')
@@ -189,6 +246,19 @@ class PromptTemplateService
             Utils::throwError('20003:没有权限操作此模板');
         }
 
+        // 判断是否为公共库数据
+        $isPublic = $template->user_id == 0;
+
+        // 如果是公共库数据,需要验证是否为 admin 角色
+        if ($isPublic && $role !== 'admin') {
+            Utils::throwError('20003:权限不足,只有管理员可以操作公共库');
+        }
+
+        // 如果是个人库数据,需要验证是否为所有者
+        if (!$isPublic && $template->user_id != $uid) {
+            Utils::throwError('20003:没有权限操作此模板');
+        }
+
         return DB::table('mp_prompt_templates')
             ->where('id', $templateId)
             ->update([