Quellcode durchsuchen

生成剧本资产接口新增扣积分逻辑

lh vor 1 Monat
Ursprung
Commit
ce8f004da4
1 geänderte Dateien mit 18 neuen und 0 gelöschten Zeilen
  1. 18 0
      app/Services/DeepSeek/DeepSeekService.php

+ 18 - 0
app/Services/DeepSeek/DeepSeekService.php

@@ -350,9 +350,11 @@ class DeepSeekService
         $templateId = getProp($data, 'template_id', 0); // 新增: 模板ID
         $script_id = getProp($data, 'script_id', 0); // 新增: 剧本ID
         $sequence = getProp($data, 'sequence', 0); // 新增: 剧集序号
+        $uid = Site::getUid();
         
         // 新增: 如果提供了script_id,从数据库获取剧本内容作为原文
         $originalContent = '';
+        $hasValidScript = false; // 是否已加载到有效剧本(存在且内容非空)
         if ($script_id > 0) {
             $script = DB::table('mp_scripts')
                 ->where('id', $script_id)
@@ -368,6 +370,7 @@ class DeepSeekService
             if (empty($originalContent)) {
                 Utils::throwError('20003:剧本内容为空');
             }
+            $hasValidScript = true;
             
             // 将剧本内容添加到提示词前面
             if (!empty($originalContent)) {
@@ -564,6 +567,11 @@ class DeepSeekService
             }
         }
         
+        // 积分余额预检(仅存在有效剧本时按 chat 类型计费,不足直接报错)
+        if ($hasValidScript) {
+            $this->pointsService->checkUserPointsEnough(PointsService::CHAT_CHARGE_POINTS, $uid);
+        }
+
         // 根据模型类型选择调用方法(复用现有的chatOnly方法)
         $aiResult = null;
         if (in_array($model, ['deepseek-reasoner', 'deepseek-chat', 'deepseek-v4-flash', 'deepseek-v4-pro'])) {
@@ -616,6 +624,16 @@ class DeepSeekService
         } else {
             Utils::throwError('20003:不支持的模型: ' . $model);
         }
+
+        // 调用成功且存在有效剧本时,返回结果前扣除积分并记录明细(chat类型10积分)
+        if ($hasValidScript && $aiResult && !empty($aiResult['content'])) {
+            $this->chargeChatSuccess($uid, $this->pointsService->getTokensFromUsage($aiResult['usage'] ?? []), [
+                'model' => $model,
+                'script_id' => $script_id,
+                'sequence' => $sequence,
+                'source' => 'newGenerateText',
+            ]);
+        }
         
         // 新增: 如果提供了script_id,保存对话记录
         if ($script_id > 0 && $aiResult) {