广州市综治平台后端
xusd
2025-06-07 36306491396230522fa20585c2621a7fc899849a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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());
        }
    }
 
}