优化代码

This commit is contained in:
Binary Wang 2019-01-12 19:52:44 +08:00
parent 333a840d00
commit f7f7f9dd3c
3 changed files with 28 additions and 52 deletions

View File

@ -1,10 +1,10 @@
package me.chanjar.weixin.mp.api;
import java.io.File;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.enums.AiLangType;
import java.io.File;
/**
* <pre>
* 微信AI开放接口语音识别微信翻译.
@ -15,24 +15,15 @@ import java.io.File;
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public interface WxMpAiOpenService {
String TRANSLATE_URL = "http://api.weixin.qq.com/cgi-bin/media/voice/translatecontent?lfrom=%s&lto=%s";
String VOICE_UPLOAD_URL = "http://api.weixin.qq.com/cgi-bin/media/voice/addvoicetorecofortext?format=%s&voice_id=%s&lang=%s";
String VOICE_QUERY_RESULT_URL = "http://api.weixin.qq.com/cgi-bin/media/voice/queryrecoresultfortext";
/**
* <pre>
* 提交语音.
* 接口调用请求说明
*
* http请求方式: POST
* http://api.weixin.qq.com/cgi-bin/media/voice/addvoicetorecofortext?access_token=ACCESS_TOKEN&format=&voice_id=xxxxxx&lang=zh_CN
* 参数说明
*
* 参数 是否必须 说明
* access_token 接口调用凭证
* format 文件格式 只支持mp316k单声道最大1M
* voice_id 语音唯一标识
* lang 语言zh_CN en_US默认中文
* 语音内容放body里或者上传文件的形式
* </pre>
*
* @param lang 语言zh_CN en_US默认中文
@ -46,16 +37,9 @@ public interface WxMpAiOpenService {
* 获取语音识别结果.
* 接口调用请求说明
*
* http请求方式: POST
* http://api.weixin.qq.com/cgi-bin/media/voice/queryrecoresultfortext?access_token=ACCESS_TOKEN&voice_id=xxxxxx&lang=zh_CN
* 请注意添加完文件之后10s内调用这个接口
*
* 参数说明
*
* 参数 是否必须 说明
* access_token 接口调用凭证
* voice_id 语音唯一标识
* lang 语言zh_CN en_US默认中文
* </pre>
*
* @param lang 语言zh_CN en_US默认中文
@ -80,18 +64,12 @@ public interface WxMpAiOpenService {
*
* http请求方式: POST
* http://api.weixin.qq.com/cgi-bin/media/voice/translatecontent?access_token=ACCESS_TOKEN&lfrom=xxx&lto=xxx
* 参数说明
*
* 参数 是否必须 说明
* access_token 接口调用凭证
* lfrom 源语言zh_CN en_US
* lto 目标语言zh_CN en_US
* 源内容放body里或者上传文件的形式utf8格式最大600Byte)
* </pre>
*
* @param langFrom 源语言zh_CN en_US
* @param langTo 目标语言zh_CN en_US
* @param content 要翻译的文本内容
* @param langTo 目标语言zh_CN en_US
* @param content 要翻译的文本内容
*/
String translate(AiLangType langFrom, AiLangType langTo, String content) throws WxErrorException;
}

View File

@ -1,17 +1,16 @@
package me.chanjar.weixin.mp.api.impl;
import com.google.gson.JsonObject;
import java.io.File;
import com.google.gson.JsonParser;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.enums.AiLangType;
import me.chanjar.weixin.mp.api.WxMpAiOpenService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.enums.AiLangType;
import me.chanjar.weixin.mp.util.requestexecuter.voice.VoiceUploadRequestExecutor;
import java.io.File;
/**
* <pre>
* Created by BinaryWang on 2018/6/9.
@ -20,9 +19,7 @@ import java.io.File;
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public class WxMpAiOpenServiceImpl implements WxMpAiOpenService {
private static final JsonParser JSON_PARSER = new JsonParser();
public static final String TRANSLATE_URL = "http://api.weixin.qq.com/cgi-bin/media/voice/translatecontent?lfrom=%s&lto=%s";
private WxMpService wxMpService;
public WxMpAiOpenServiceImpl(WxMpService wxMpService) {
@ -48,14 +45,14 @@ public class WxMpAiOpenServiceImpl implements WxMpAiOpenService {
@Override
public String translate(AiLangType langFrom, AiLangType langTo, String content) throws WxErrorException {
final String responseContent = this.wxMpService.post(String.format(TRANSLATE_URL, langFrom.getCode(), langTo.getCode()),
content);
final JsonObject jsonObject = new JsonParser().parse(responseContent).getAsJsonObject();
if (jsonObject.get("errcode") == null || jsonObject.get("errcode").getAsInt() == 0) {
return jsonObject.get("to_content").getAsString();
String response = this.wxMpService.post(String.format(TRANSLATE_URL, langFrom.getCode(), langTo.getCode()), content);
WxError error = WxError.fromJson(response, WxType.MP);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
return JSON_PARSER.parse(response).getAsJsonObject().get("to_content").getAsString();
}
@Override
@ -64,13 +61,13 @@ public class WxMpAiOpenServiceImpl implements WxMpAiOpenService {
lang = AiLangType.zh_CN;
}
final String responseContent = this.wxMpService.get(VOICE_QUERY_RESULT_URL,
final String response = this.wxMpService.get(VOICE_QUERY_RESULT_URL,
String.format("voice_id=%s&lang=%s", voiceId, lang.getCode()));
final JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
if (jsonObject.get("errcode") == null || jsonObject.get("errcode").getAsInt() == 0) {
return jsonObject.get("result").getAsString();
WxError error = WxError.fromJson(response, WxType.MP);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
return JSON_PARSER.parse(response).getAsJsonObject().get("result").getAsString();
}
}

View File

@ -1,14 +1,16 @@
package me.chanjar.weixin.mp.api.impl;
import java.io.File;
import org.testng.annotations.*;
import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.enums.AiLangType;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.test.ApiTestModule;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.mp.enums.AiLangType;
import java.io.File;
import static org.assertj.core.api.Assertions.assertThat;
/**
* <pre>
@ -35,13 +37,12 @@ public class WxMpAiOpenServiceImplTest {
String voiceId = System.currentTimeMillis() + "a";
AiLangType lang = AiLangType.zh_CN;
final String result = this.wxService.getAiOpenService().recogniseVoice(voiceId, lang, new File("d:\\t.mp3"));
System.out.println(result);
assertThat(result).isNotEmpty();
}
@Test
public void testTranslate() throws WxErrorException {
final String responseContent = this.wxService.getAiOpenService()
.translate(AiLangType.zh_CN, AiLangType.en_US, "微信文档很坑爹");
System.out.println(responseContent);
final String result = this.wxService.getAiOpenService().translate(AiLangType.zh_CN, AiLangType.en_US, "微信文档很坑爹");
assertThat(result).isNotEmpty();
}
}