- <?php
-
- namespace App\Services;
-
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
- use App\Enums\ErrorCode;
- use App\Helpers\Utils\Alibaba\NLSFileTrans;
- use App\Helpers\Utils\Alibaba\OSS;
- use App\Models\Work;
- use Spatie\Permission\Exceptions\UnauthorizedException;
-
- class ApiService
- {
- /**
- * 提交录音文件识别请求,获取任务ID
- * @param string $fileLink
- * @return array
- */
- public function asrCreateTask(string $fileLink) {
- $accessKeyId = config('constant.alibaba_access_key_id');
- $accessKeySecret = config('constant.alibaba_access_key_secret');
- $appKey = config('constant.alibaba_app_key');
-
- /**
- * 第一步:设置一个全局客户端。
- * 使用阿里云RAM账号的AccessKey ID和AccessKey Secret进行鉴权。
- */
- AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
- ->regionId("cn-shanghai")
- ->asGlobalClient();
-
- $fileTrans = new NLSFileTrans();
- /**
- * @var $appKeyId string
- * @ref: https://nls-portal.console.aliyun.com/applist 我的所有项目(不是access key)
- * 确认开通了asr+tts服务
- * https://nls-portal.console.aliyun.com/
- */
- try {
- $taskId = $fileTrans->submitFileTransRequest($appKey, $fileLink);
- if ($taskId != NULL) {
- $msg = "录音文件识别请求成功";
- return ['message' => $msg, 'code' => ErrorCode::OK, 'result' => ['task_id'=>$taskId], 'time'=>time()];
- }
- } catch (ClientException $e) {
- // 获取错误消息
- return ['message' => "failed", 'code'=> $e->getErrorCode(), 'error'=>$e->getErrorMessage()];
- } catch (ServerException $e) {
- return ['message' => "failed", 'code'=> $e->getErrorCode(), 'error'=>$e->getErrorMessage()];
- }
- return ['message'=>"failed", 'code'=> ErrorCode::API_ERROR, 'error'=>"语音识别任务创建失败"];
- }
-
- /**
- * 异步查询ASR任务结果
- * @param string $taskId
- * @return array
- * @throws ClientException
- */
- public function asrQuery(string $taskId) {
- $accessKeyId = config('constant.alibaba_access_key_id');
- $accessKeySecret = config('constant.alibaba_access_key_secret');
- /**
- * 第一步:设置一个全局客户端。
- * 使用阿里云RAM账号的AccessKey ID和AccessKey Secret进行鉴权。
- */
- AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
- ->regionId("cn-shanghai")
- ->asGlobalClient();
-
- $fileTrans = new NLSFileTrans();
- /**
- * 第三步:根据任务ID轮询识别结果。
- * @var $result \AlibabaCloud\Client\Result\Result
- */
- $result = $fileTrans->getFileTransResult($taskId);
- if (is_null($result)) {
- return ['message'=>"failed", 'code'=> ErrorCode::API_ERROR, 'error'=>"录音文件识别结果查询失败!",'time'=>time()];
- }
- /** @var $s string JSON string */
- $s = $result->toJson();
- header("Content-Type: application/json");
- echo $s;
- exit(0);
- }
-
- public function fetchWork(int $workId, int $memberId, string $downloadType = 'wav') {
- /** @var $work Work */
- $work = Work::find($workId);
- if (empty($work)) {
- throw new \InvalidArgumentException("作品ID不存在", ErrorCode::PARAM_ERROR);
- }
- if (0 != $work->getMemberId()-$memberId) {
- throw new UnauthorizedException(ErrorCode::PERMISSION_DENIED, "只能下载自己的作品");
- }
- return $work;
- }
-
- public function upload(string $path, string $ext, string $bucket) {
- $oss = new OSS();
- $filename = md5_file($path);
-
- $uri = sprintf("%s/%s.%s", date("Y-m-d", time()), $filename, $ext);
- $oss->bucket($bucket)->upload($uri, $path, $bucket);
- return "https://".$bucket.".".OSS::END_POINT."/".$uri;
- }
- }
复制代码
|