package cn.huge.module.xfyun.controller.wechat;
|
|
import cn.huge.base.common.utils.ReturnFailUtils;
|
import cn.huge.base.common.utils.ReturnSucUtils;
|
import cn.huge.module.constant.CacheConsts;
|
import cn.huge.module.xfyun.config.XfyunConfig;
|
import cn.huge.module.xfyun.iflytek.WebIATWS;
|
import lombok.extern.slf4j.Slf4j;
|
import okhttp3.OkHttpClient;
|
import okhttp3.Request;
|
import okhttp3.WebSocket;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
import java.io.ByteArrayInputStream;
|
import java.io.InputStream;
|
import java.util.Iterator;
|
import java.util.concurrent.CountDownLatch;
|
|
|
/**
|
* @title: 讯飞语音转文字api
|
* @description: 讯飞语音转文字api
|
* @company: hugeinfo
|
* @author: chenx
|
* @time: 2022-05-17 14:55:19
|
* @version: 1.0.0
|
*/
|
@Slf4j
|
@RestController
|
@RequestMapping("/api/wechat/xfyun")
|
public class XfyunWechatController {
|
|
/**
|
* 讯飞语音转文字
|
* @url {ctx}/api/v1/xfyun/speech
|
* @return Object
|
*/
|
@PostMapping(value = "/speech")
|
public Object speech(MultipartHttpServletRequest rq){
|
try{
|
Iterator<String> itr = rq.getFileNames();
|
while (itr.hasNext()) {
|
MultipartFile file = rq.getFile(itr.next());
|
byte [] byteArr = file.getBytes();
|
InputStream inputStream = new ByteArrayInputStream(byteArr);
|
// 构建鉴权url
|
String authUrl = WebIATWS.getAuthUrl(XfyunConfig.hostUrl, XfyunConfig.apiKey, XfyunConfig.apiSecret);
|
OkHttpClient client = new OkHttpClient.Builder().build();
|
String url = authUrl.toString().replace("http://", "ws://").replace("https://", "wss://");
|
Request request = new Request.Builder().url(url).build();
|
CountDownLatch countDownLatch = new CountDownLatch(1);
|
WebSocket webSocket = client.newWebSocket(request, new WebIATWS(inputStream, countDownLatch));
|
countDownLatch.await();
|
}
|
return ReturnSucUtils.getRepInfo(CacheConsts.XF_RESULT);
|
}catch (Exception e){
|
log.error("Controller接口[XfyunController.speech]请求异常:"+e, e);
|
return ReturnFailUtils.getRepInfo(e.getMessage());
|
}
|
}
|
|
}
|