优化代码

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; package me.chanjar.weixin.mp.api;
import java.io.File;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.enums.AiLangType; import me.chanjar.weixin.mp.enums.AiLangType;
import java.io.File;
/** /**
* <pre> * <pre>
* 微信AI开放接口语音识别微信翻译. * 微信AI开放接口语音识别微信翻译.
@ -15,24 +15,15 @@ import java.io.File;
* @author <a href="https://github.com/binarywang">Binary Wang</a> * @author <a href="https://github.com/binarywang">Binary Wang</a>
*/ */
public interface WxMpAiOpenService { 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_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"; String VOICE_QUERY_RESULT_URL = "http://api.weixin.qq.com/cgi-bin/media/voice/queryrecoresultfortext";
/** /**
* <pre> * <pre>
* 提交语音. * 提交语音.
* 接口调用请求说明
*
* http请求方式: POST * http请求方式: POST
* http://api.weixin.qq.com/cgi-bin/media/voice/addvoicetorecofortext?access_token=ACCESS_TOKEN&format=&voice_id=xxxxxx&lang=zh_CN * 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> * </pre>
* *
* @param lang 语言zh_CN en_US默认中文 * @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 * http://api.weixin.qq.com/cgi-bin/media/voice/queryrecoresultfortext?access_token=ACCESS_TOKEN&voice_id=xxxxxx&lang=zh_CN
* 请注意添加完文件之后10s内调用这个接口 * 请注意添加完文件之后10s内调用这个接口
* *
* 参数说明
*
* 参数 是否必须 说明
* access_token 接口调用凭证
* voice_id 语音唯一标识
* lang 语言zh_CN en_US默认中文
* </pre> * </pre>
* *
* @param lang 语言zh_CN en_US默认中文 * @param lang 语言zh_CN en_US默认中文
@ -80,13 +64,7 @@ public interface WxMpAiOpenService {
* *
* http请求方式: POST * http请求方式: POST
* http://api.weixin.qq.com/cgi-bin/media/voice/translatecontent?access_token=ACCESS_TOKEN&lfrom=xxx&lto=xxx * 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> * </pre>
* *
* @param langFrom 源语言zh_CN en_US * @param langFrom 源语言zh_CN en_US

View File

@ -1,17 +1,16 @@
package me.chanjar.weixin.mp.api.impl; package me.chanjar.weixin.mp.api.impl;
import com.google.gson.JsonObject; import java.io.File;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import me.chanjar.weixin.common.WxType; import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException; 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.WxMpAiOpenService;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.enums.AiLangType;
import me.chanjar.weixin.mp.util.requestexecuter.voice.VoiceUploadRequestExecutor; import me.chanjar.weixin.mp.util.requestexecuter.voice.VoiceUploadRequestExecutor;
import java.io.File;
/** /**
* <pre> * <pre>
* Created by BinaryWang on 2018/6/9. * 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> * @author <a href="https://github.com/binarywang">Binary Wang</a>
*/ */
public class WxMpAiOpenServiceImpl implements WxMpAiOpenService { public class WxMpAiOpenServiceImpl implements WxMpAiOpenService {
private static final JsonParser JSON_PARSER = new JsonParser(); 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; private WxMpService wxMpService;
public WxMpAiOpenServiceImpl(WxMpService wxMpService) { public WxMpAiOpenServiceImpl(WxMpService wxMpService) {
@ -48,14 +45,14 @@ public class WxMpAiOpenServiceImpl implements WxMpAiOpenService {
@Override @Override
public String translate(AiLangType langFrom, AiLangType langTo, String content) throws WxErrorException { 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()), String response = this.wxMpService.post(String.format(TRANSLATE_URL, langFrom.getCode(), langTo.getCode()), content);
content);
final JsonObject jsonObject = new JsonParser().parse(responseContent).getAsJsonObject(); WxError error = WxError.fromJson(response, WxType.MP);
if (jsonObject.get("errcode") == null || jsonObject.get("errcode").getAsInt() == 0) { if (error.getErrorCode() != 0) {
return jsonObject.get("to_content").getAsString(); throw new WxErrorException(error);
} }
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP)); return JSON_PARSER.parse(response).getAsJsonObject().get("to_content").getAsString();
} }
@Override @Override
@ -64,13 +61,13 @@ public class WxMpAiOpenServiceImpl implements WxMpAiOpenService {
lang = AiLangType.zh_CN; 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())); String.format("voice_id=%s&lang=%s", voiceId, lang.getCode()));
final JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); WxError error = WxError.fromJson(response, WxType.MP);
if (jsonObject.get("errcode") == null || jsonObject.get("errcode").getAsInt() == 0) { if (error.getErrorCode() != 0) {
return jsonObject.get("result").getAsString(); 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; package me.chanjar.weixin.mp.api.impl;
import java.io.File;
import org.testng.annotations.*;
import com.google.inject.Inject; import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxErrorException; 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.WxMpService;
import me.chanjar.weixin.mp.api.test.ApiTestModule; import me.chanjar.weixin.mp.api.test.ApiTestModule;
import org.testng.annotations.Guice; import me.chanjar.weixin.mp.enums.AiLangType;
import org.testng.annotations.Test;
import java.io.File; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* <pre> * <pre>
@ -35,13 +37,12 @@ public class WxMpAiOpenServiceImplTest {
String voiceId = System.currentTimeMillis() + "a"; String voiceId = System.currentTimeMillis() + "a";
AiLangType lang = AiLangType.zh_CN; AiLangType lang = AiLangType.zh_CN;
final String result = this.wxService.getAiOpenService().recogniseVoice(voiceId, lang, new File("d:\\t.mp3")); final String result = this.wxService.getAiOpenService().recogniseVoice(voiceId, lang, new File("d:\\t.mp3"));
System.out.println(result); assertThat(result).isNotEmpty();
} }
@Test @Test
public void testTranslate() throws WxErrorException { public void testTranslate() throws WxErrorException {
final String responseContent = this.wxService.getAiOpenService() final String result = this.wxService.getAiOpenService().translate(AiLangType.zh_CN, AiLangType.en_US, "微信文档很坑爹");
.translate(AiLangType.zh_CN, AiLangType.en_US, "微信文档很坑爹"); assertThat(result).isNotEmpty();
System.out.println(responseContent);
} }
} }