DeepSeekController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Http\Controllers\DeepSeek;
  3. use App\Facade\Site;
  4. use App\Consts\ErrorConst;
  5. use App\Exceptions\ApiException;
  6. use App\Libs\ApiResponse;
  7. use App\Libs\Utils;
  8. use App\Services\DeepSeek\DeepSeekService;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Routing\Controller as BaseController;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Redis;
  13. use Illuminate\Support\Facades\Validator;
  14. class DeepSeekController extends BaseController
  15. {
  16. use ApiResponse;
  17. protected $deepseekService;
  18. public function __construct(
  19. DeepSeekService $deepseekService
  20. ) {
  21. $this->deepseekService = $deepseekService;
  22. }
  23. /**
  24. * 可选供应商
  25. *
  26. * @param Request $request
  27. * @return mixed
  28. */
  29. public function chatWithReasoner(Request $request) {
  30. // 忽略所有超时限制
  31. set_time_limit(0);
  32. ini_set('max_execution_time', '0');
  33. $data = $request->all();
  34. $result = $this->deepseekService->chatWithReasoner($data);
  35. return $this->success($result);
  36. }
  37. /**
  38. * 音色列表
  39. *
  40. * @param Request $request
  41. * @return mixed
  42. */
  43. public function timbreList(Request $request) {
  44. $data = $request->all();
  45. $result = $this->deepseekService->timbreList($data);
  46. return $this->success($result);
  47. }
  48. // 保存段落音频
  49. public function saveParagraphAudio(Request $request) {
  50. $data = $request->all();
  51. $result = $this->deepseekService->saveParagraphAudio($data);
  52. return $this->success(['success'=>$result ? 1 : 0]);
  53. }
  54. /**
  55. * 新增合成任务
  56. *
  57. * @param Request $request
  58. * @return mixed
  59. */
  60. public function addGenerateTask(Request $request) {
  61. $data = $request->all();
  62. $result = $this->deepseekService->addGenerateTask($data);
  63. return $this->success(['success'=>$result ? 1 : 0]);
  64. }
  65. // 获取火山临时token
  66. public function setStsToken(Request $request) {
  67. $data = $request->all();
  68. $result = $this->deepseekService->setStsToken($data);
  69. return $this->success($result);
  70. }
  71. public function sseLink(Request $request) {
  72. $data = $request->all();
  73. $bid = getProp($data, 'bid');
  74. $version_id = getProp($data, 'version_id');
  75. $cid = getProp($data, 'cid');
  76. // 禁用输出缓冲
  77. while (ob_get_level()) ob_end_clean();
  78. // 设置SSE所需的HTTP头
  79. header('Content-Type: text/event-stream');
  80. header('Cache-Control: no-cache');
  81. header('Connection: keep-alive');
  82. header('X-Accel-Buffering: no'); // 防止Nginx缓冲
  83. // 允许跨域请求(根据需求设置)
  84. header('Access-Control-Allow-Origin: *');
  85. header('Access-Control-Allow-Credentials: true');
  86. // 设置脚本执行时间无限
  87. set_time_limit(0);
  88. // 手动刷新输出缓冲区
  89. ob_implicit_flush(true);
  90. if(ob_get_level()>0) ob_flush();
  91. flush();
  92. // 客户端ID(用于多用户区分)
  93. $clientId = $_SERVER['REMOTE_ADDR'] . ':' . $_SERVER['REMOTE_PORT'];
  94. $lastEventId = isset($_SERVER['HTTP_LAST_EVENT_ID']) ? intval($_SERVER['HTTP_LAST_EVENT_ID']) : 0;
  95. // dd($clientId, $lastEventId);
  96. // 发送初始欢迎消息
  97. $this->sendEvent([
  98. // 'message' => "欢迎使用SSE服务! 你的客户端ID: {$clientId}",
  99. // 'type' => 'info',
  100. // 'timestamp' => date('H:i:s')
  101. 'code' => 0,
  102. 'msg' => "已连接! 你的客户端ID: {$clientId}",
  103. 'data' => [],
  104. ]);
  105. if (!$bid && !$version_id && !$cid) {
  106. $this->sendEvent([
  107. // 'message' => '参数异常',
  108. // 'type' => 'error',
  109. // 'timestamp' => date('H:i:s')
  110. 'code' => -1,
  111. 'msg' => "参数异常",
  112. 'data' => [],
  113. ]);
  114. }
  115. // 计数器
  116. $counter = 0;
  117. $tmp_url_count = 0;
  118. // 主循环 - 定期发送数据
  119. while (true) {
  120. // 检查客户端是否断开连接
  121. if (connection_aborted()) {
  122. exit();
  123. }
  124. $counter++;
  125. if ($counter % 10 == 0) {
  126. // 每10s判断是否有新url生成,如果有则发送消息
  127. $paragraph_urls = DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('version_id', $version_id)
  128. ->where('cid', $cid)->where(function($query) {
  129. return $query->where('paragraph_audio_url', '!=', '')->orWhere('error_msg', '!=', '');
  130. })->select('sequence', 'paragraph_audio_url', 'error_msg')
  131. ->get()->map(function ($value) {
  132. return (array)$value;
  133. })->toArray();
  134. if (count($paragraph_urls) != $tmp_url_count) {
  135. $this->sendEvent([
  136. 'code' => 0,
  137. 'msg' => "",
  138. 'data' => $paragraph_urls,
  139. // 'message' => '数据更新',
  140. // 'type' => 'update',
  141. // 'timestamp' => date('H:i:s')
  142. ]);
  143. $tmp_url_count = count($paragraph_urls);
  144. }
  145. }
  146. // 等待1秒后发送下一条消息
  147. sleep(1);
  148. }
  149. }
  150. private function sendEvent($data, $event = null, $id = null) {
  151. // // 事件ID
  152. // if ($id !== null) {
  153. // echo "id: {$id}\n";
  154. // }
  155. // 事件类型
  156. if ($event !== null) {
  157. echo "event: {$event}\n";
  158. }
  159. // 数据部分(JSON格式)
  160. echo "data: " . json_encode($data) . "\n\n";
  161. // 立即发送数据
  162. if(ob_get_level()>0) ob_flush();
  163. flush();
  164. }
  165. }