11package me .chanjar .weixin .mp .api .impl ;
22
3- import com .google .gson .JsonObject ;
3+ import java .io .File ;
4+
45import com .google .gson .JsonParser ;
56import me .chanjar .weixin .common .WxType ;
67import me .chanjar .weixin .common .error .WxError ;
78import me .chanjar .weixin .common .error .WxErrorException ;
8- import me .chanjar .weixin .mp .enums .AiLangType ;
99import me .chanjar .weixin .mp .api .WxMpAiOpenService ;
1010import me .chanjar .weixin .mp .api .WxMpService ;
11+ import me .chanjar .weixin .mp .enums .AiLangType ;
1112import me .chanjar .weixin .mp .util .requestexecuter .voice .VoiceUploadRequestExecutor ;
1213
13- import java .io .File ;
14-
1514/**
1615 * <pre>
1716 * Created by BinaryWang on 2018/6/9.
2019 * @author <a href="https://github.com/binarywang">Binary Wang</a>
2120 */
2221public class WxMpAiOpenServiceImpl implements WxMpAiOpenService {
23-
2422 private static final JsonParser JSON_PARSER = new JsonParser ();
25- public static final String TRANSLATE_URL = "http://api.weixin.qq.com/cgi-bin/media/voice/translatecontent?lfrom=%s<o=%s" ;
2623 private WxMpService wxMpService ;
2724
2825 public WxMpAiOpenServiceImpl (WxMpService wxMpService ) {
@@ -48,14 +45,14 @@ public String recogniseVoice(String voiceId, AiLangType lang, File voiceFile) th
4845
4946 @ Override
5047 public String translate (AiLangType langFrom , AiLangType langTo , String content ) throws WxErrorException {
51- final String responseContent = this .wxMpService .post (String .format (TRANSLATE_URL , langFrom .getCode (), langTo .getCode ()),
52- content );
53- final JsonObject jsonObject = new JsonParser (). parse ( responseContent ). getAsJsonObject ( );
54- if (jsonObject . get ( "errcode" ) == null || jsonObject . get ( "errcode" ). getAsInt () = = 0 ) {
55- return jsonObject . get ( "to_content" ). getAsString ( );
48+ String response = this .wxMpService .post (String .format (TRANSLATE_URL , langFrom .getCode (), langTo .getCode ()), content );
49+
50+ WxError error = WxError . fromJson ( response , WxType . MP );
51+ if (error . getErrorCode () ! = 0 ) {
52+ throw new WxErrorException ( error );
5653 }
5754
58- throw new WxErrorException ( WxError . fromJson ( responseContent , WxType . MP ) );
55+ return JSON_PARSER . parse ( response ). getAsJsonObject (). get ( "to_content" ). getAsString ( );
5956 }
6057
6158 @ Override
@@ -64,13 +61,13 @@ public String queryRecognitionResult(String voiceId, AiLangType lang) throws WxE
6461 lang = AiLangType .zh_CN ;
6562 }
6663
67- final String responseContent = this .wxMpService .get (VOICE_QUERY_RESULT_URL ,
64+ final String response = this .wxMpService .get (VOICE_QUERY_RESULT_URL ,
6865 String .format ("voice_id=%s&lang=%s" , voiceId , lang .getCode ()));
69- final JsonObject jsonObject = JSON_PARSER . parse ( responseContent ). getAsJsonObject ( );
70- if (jsonObject . get ( "errcode" ) == null || jsonObject . get ( "errcode" ). getAsInt () = = 0 ) {
71- return jsonObject . get ( "result" ). getAsString ( );
66+ WxError error = WxError . fromJson ( response , WxType . MP );
67+ if (error . getErrorCode () ! = 0 ) {
68+ throw new WxErrorException ( error );
7269 }
7370
74- throw new WxErrorException ( WxError . fromJson ( responseContent , WxType . MP ) );
71+ return JSON_PARSER . parse ( response ). getAsJsonObject (). get ( "result" ). getAsString ( );
7572 }
7673}
0 commit comments