Skip to content

Commit f7f7f9d

Browse files
committed
优化代码
1 parent 333a840 commit f7f7f9d

File tree

3 files changed

+28
-52
lines changed

3 files changed

+28
-52
lines changed
Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package me.chanjar.weixin.mp.api;
22

3+
import java.io.File;
4+
35
import me.chanjar.weixin.common.error.WxErrorException;
46
import me.chanjar.weixin.mp.enums.AiLangType;
57

6-
import java.io.File;
7-
88
/**
99
* <pre>
1010
* 微信AI开放接口(语音识别,微信翻译).
@@ -15,24 +15,15 @@
1515
* @author <a href="https://github.com/binarywang">Binary Wang</a>
1616
*/
1717
public interface WxMpAiOpenService {
18+
String TRANSLATE_URL = "http://api.weixin.qq.com/cgi-bin/media/voice/translatecontent?lfrom=%s&lto=%s";
1819
String VOICE_UPLOAD_URL = "http://api.weixin.qq.com/cgi-bin/media/voice/addvoicetorecofortext?format=%s&voice_id=%s&lang=%s";
1920
String VOICE_QUERY_RESULT_URL = "http://api.weixin.qq.com/cgi-bin/media/voice/queryrecoresultfortext";
2021

2122
/**
2223
* <pre>
2324
* 提交语音.
24-
* 接口调用请求说明
25-
*
2625
* http请求方式: POST
2726
* http://api.weixin.qq.com/cgi-bin/media/voice/addvoicetorecofortext?access_token=ACCESS_TOKEN&format=&voice_id=xxxxxx&lang=zh_CN
28-
* 参数说明
29-
*
30-
* 参数 是否必须 说明
31-
* access_token 是 接口调用凭证
32-
* format 是 文件格式 (只支持mp3,16k,单声道,最大1M)
33-
* voice_id 是 语音唯一标识
34-
* lang 否 语言,zh_CN 或 en_US,默认中文
35-
* 语音内容放body里或者上传文件的形式
3627
* </pre>
3728
*
3829
* @param lang 语言,zh_CN 或 en_US,默认中文
@@ -46,16 +37,9 @@ public interface WxMpAiOpenService {
4637
* 获取语音识别结果.
4738
* 接口调用请求说明
4839
*
49-
* http请求方式: POST
5040
* http://api.weixin.qq.com/cgi-bin/media/voice/queryrecoresultfortext?access_token=ACCESS_TOKEN&voice_id=xxxxxx&lang=zh_CN
5141
* 请注意,添加完文件之后10s内调用这个接口
5242
*
53-
* 参数说明
54-
*
55-
* 参数 是否必须 说明
56-
* access_token 是 接口调用凭证
57-
* voice_id 是 语音唯一标识
58-
* lang 否 语言,zh_CN 或 en_US,默认中文
5943
* </pre>
6044
*
6145
* @param lang 语言,zh_CN 或 en_US,默认中文
@@ -80,18 +64,12 @@ public interface WxMpAiOpenService {
8064
*
8165
* http请求方式: POST
8266
* http://api.weixin.qq.com/cgi-bin/media/voice/translatecontent?access_token=ACCESS_TOKEN&lfrom=xxx&lto=xxx
83-
* 参数说明
8467
*
85-
* 参数 是否必须 说明
86-
* access_token 是 接口调用凭证
87-
* lfrom 是 源语言,zh_CN 或 en_US
88-
* lto 是 目标语言,zh_CN 或 en_US
89-
* 源内容放body里或者上传文件的形式(utf8格式,最大600Byte)
9068
* </pre>
9169
*
9270
* @param langFrom 源语言,zh_CN 或 en_US
93-
* @param langTo 目标语言,zh_CN 或 en_US
94-
* @param content 要翻译的文本内容
71+
* @param langTo 目标语言,zh_CN 或 en_US
72+
* @param content 要翻译的文本内容
9573
*/
9674
String translate(AiLangType langFrom, AiLangType langTo, String content) throws WxErrorException;
9775
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpAiOpenServiceImpl.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3-
import com.google.gson.JsonObject;
3+
import java.io.File;
4+
45
import com.google.gson.JsonParser;
56
import me.chanjar.weixin.common.WxType;
67
import me.chanjar.weixin.common.error.WxError;
78
import me.chanjar.weixin.common.error.WxErrorException;
8-
import me.chanjar.weixin.mp.enums.AiLangType;
99
import me.chanjar.weixin.mp.api.WxMpAiOpenService;
1010
import me.chanjar.weixin.mp.api.WxMpService;
11+
import me.chanjar.weixin.mp.enums.AiLangType;
1112
import 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.
@@ -20,9 +19,7 @@
2019
* @author <a href="https://github.com/binarywang">Binary Wang</a>
2120
*/
2221
public 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&lto=%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
}

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpAiOpenServiceImplTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3+
import java.io.File;
4+
5+
import org.testng.annotations.*;
6+
37
import com.google.inject.Inject;
48
import me.chanjar.weixin.common.error.WxErrorException;
5-
import me.chanjar.weixin.mp.enums.AiLangType;
69
import me.chanjar.weixin.mp.api.WxMpService;
710
import me.chanjar.weixin.mp.api.test.ApiTestModule;
8-
import org.testng.annotations.Guice;
9-
import org.testng.annotations.Test;
11+
import me.chanjar.weixin.mp.enums.AiLangType;
1012

11-
import java.io.File;
13+
import static org.assertj.core.api.Assertions.assertThat;
1214

1315
/**
1416
* <pre>
@@ -35,13 +37,12 @@ public void testRecogniseVoice() throws WxErrorException {
3537
String voiceId = System.currentTimeMillis() + "a";
3638
AiLangType lang = AiLangType.zh_CN;
3739
final String result = this.wxService.getAiOpenService().recogniseVoice(voiceId, lang, new File("d:\\t.mp3"));
38-
System.out.println(result);
40+
assertThat(result).isNotEmpty();
3941
}
4042

4143
@Test
4244
public void testTranslate() throws WxErrorException {
43-
final String responseContent = this.wxService.getAiOpenService()
44-
.translate(AiLangType.zh_CN, AiLangType.en_US, "微信文档很坑爹");
45-
System.out.println(responseContent);
45+
final String result = this.wxService.getAiOpenService().translate(AiLangType.zh_CN, AiLangType.en_US, "微信文档很坑爹");
46+
assertThat(result).isNotEmpty();
4647
}
4748
}

0 commit comments

Comments
 (0)