This commit is contained in:
mkm 2023-10-13 18:17:51 +08:00
parent 92fd78bafd
commit 9c636041f2

View File

@ -41,10 +41,10 @@ class Pusher
{
$data = json_decode($data, true);
// $tts_type=0;
// if(isset($data['tts'])){
// $tts_type = $data['tts'];
// }
$tts_type = 0;
if (isset($data['tts'])) {
$tts_type = $data['tts'];
}
if (isset($data['data'])) {
$data = $data['data'];
}
@ -60,6 +60,7 @@ class Pusher
$data = $this->getBody($this->app_id, $data);
$client->send($data);
$text = '';
// 从 WebSocket 服务器接收数据
while (true) {
$response = $client->receive();
@ -68,21 +69,27 @@ class Pusher
// echo "从服务器接收到的数据: " . $response;
if (0 == $code) {
$status = $resp["header"]["status"];
$arr = explode('。', $resp['payload']['choices']['text'][0]['content']);
if ($status != 2) {
// if($tts_type==1){
// $url=$this->tts($resp['payload']['choices']['text'][0]['content']);
// $resp['payload']['choices']['mp3']=$url;
// $response=json_encode($resp,true);
// }
if ($tts_type == 1) {
if (count($arr) == 2) {
$resp['payload']['choices']['tts_text'] = $text.$arr[0];
$text =$arr[1]??'';
$response = json_encode($resp, true);
} else {
$resp['payload']['choices']['tts_text'] = '';
$text .= $resp['payload']['choices']['text'][0]['content'];
$response = json_encode($resp, true);
}
}
$connection->send($response);
} else {
// if($tts_type==1){
// $url=$this->tts($resp['payload']['choices']['text'][0]['content']);
// $resp['payload']['choices']['mp3']=$url;
// $response=json_encode($resp,true);
if (strlen($text) > 1) {
$resp['payload']['choices']['tts_text'] = $text;
$text = '';
$response = json_encode($resp, true);
}
// }
$connection->send($response);
// $total_tokens = $resp['payload']['usage']['text']['total_tokens'];
@ -177,11 +184,11 @@ class Pusher
return $authAddr;
}
function tts($text){
function tts($text)
{
$name = time() . rand(1, 100000) . '.mp3';
$tts = new extend\IFlytek\Xfyun\Speech\TtsClient($this->app_id, $this->api_key, $this->api_secret, $this->ttsConfig);
file_put_contents(public_path('tts') . '/' . $name, $tts->request($text)->getBody()->getContents());
return 'https://chat.lihaink.cn/tts/' . $name;
}
}