diff --git a/app/Tts.php b/app/Tts.php index cf17c50..892a933 100644 --- a/app/Tts.php +++ b/app/Tts.php @@ -38,23 +38,27 @@ class Tts public function onMessage(TcpConnection $connection, $data) { - $data = json_decode($data, true); - // $tts_type=0; - // if(isset($data['tts'])){ - // $tts_type = $data['tts']; - // } - if (isset($data['data'])) { - $data = $data['data']; + $data = json_decode($data); + if (isset($data->data)) { + $data = $data->data; } + if ($data == '') { return $connection->send(['header' => ['code' => 10003, 'message' => '消息不能为空']]); } + if ($data != '') { - $url = $this->tts($data); - $resp['mp3'] = $url; - $response = json_encode($resp, true); - $connection->send($response); + $arr=explode('。',$data); + foreach($arr as $k=>$v){ + if(strlen($v)>1){ + $url = $this->tts($v); + $resp['mp3'] = $url; + $response = json_encode($resp, true); + $connection->send($response); + } + } + } } @@ -65,6 +69,7 @@ class Tts 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());