修改nls、微信授权地址,通过修改状态来控制测试和正式
This commit is contained in:
@@ -2,6 +2,7 @@ package com.ruoyi.cms.controller.app;
|
|||||||
|
|
||||||
import com.alibaba.nls.client.AccessToken;
|
import com.alibaba.nls.client.AccessToken;
|
||||||
import com.ruoyi.cms.handler.SpeechRecognizerAI;
|
import com.ruoyi.cms.handler.SpeechRecognizerAI;
|
||||||
|
import com.ruoyi.cms.util.AliyunNlsUtils;
|
||||||
import com.ruoyi.common.core.controller.BaseController;
|
import com.ruoyi.common.core.controller.BaseController;
|
||||||
import com.ruoyi.common.core.domain.AjaxResult;
|
import com.ruoyi.common.core.domain.AjaxResult;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
@@ -24,14 +25,14 @@ public class AppSpeechController extends BaseController
|
|||||||
private String appKey = "4lFYn2yPsQymwGu8";
|
private String appKey = "4lFYn2yPsQymwGu8";
|
||||||
private String id = "LTAI5t9hhSqdDHqwH3RjgyYj";
|
private String id = "LTAI5t9hhSqdDHqwH3RjgyYj";
|
||||||
private String secret = "ni5aW3vxrWouMwcGqJPfh9Uu56PBuv";
|
private String secret = "ni5aW3vxrWouMwcGqJPfh9Uu56PBuv";
|
||||||
private String url = System.getenv().getOrDefault("NLS_GATEWAY_URL", "wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1");
|
private String url = System.getenv().getOrDefault("NLS_GATEWAY_URL", AliyunNlsUtils.getNlsUrl()+"/ws/v1/");
|
||||||
@ApiOperation("统计")
|
@ApiOperation("统计")
|
||||||
@GetMapping("/getToken")
|
@GetMapping("/getToken")
|
||||||
public AjaxResult getToken()
|
public AjaxResult getToken()
|
||||||
{
|
{
|
||||||
SpeechRecognizerAI recognizerDemo = new SpeechRecognizerAI(appKey, id, secret, url);
|
SpeechRecognizerAI recognizerDemo = new SpeechRecognizerAI(appKey, id, secret, url);
|
||||||
AccessToken accessToken = recognizerDemo.getAccessToken();
|
AccessToken accessToken = recognizerDemo.getAccessToken();
|
||||||
String token = "wss://nls-gateway.cn-shanghai.aliyuncs.com/ws/v1?appkey="+appKey+"&token="+accessToken.getToken();
|
String token = AliyunNlsUtils.getNlsUrl()+"/ws/v1/?appkey="+appKey+"&token="+accessToken.getToken();
|
||||||
return AjaxResult.success(token);
|
return AjaxResult.success(token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.ruoyi.cms.handler;
|
package com.ruoyi.cms.handler;
|
||||||
|
|
||||||
import com.alibaba.nls.client.protocol.asr.SpeechRecognizer;
|
import com.ruoyi.cms.util.AliyunNlsUtils;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.websocket.*;
|
import javax.websocket.*;
|
||||||
@@ -20,7 +20,7 @@ public class SpeechRecognitionWebSocketHandler {
|
|||||||
String appKey = "4lFYn2yPsQymwGu8";
|
String appKey = "4lFYn2yPsQymwGu8";
|
||||||
String id = "LTAI5t9hhSqdDHqwH3RjgyYj";
|
String id = "LTAI5t9hhSqdDHqwH3RjgyYj";
|
||||||
String secret = "ni5aW3vxrWouMwcGqJPfh9Uu56PBuv";
|
String secret = "ni5aW3vxrWouMwcGqJPfh9Uu56PBuv";
|
||||||
String url = System.getenv().getOrDefault("NLS_GATEWAY_URL", "wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1");
|
String url = System.getenv().getOrDefault("NLS_GATEWAY_URL", AliyunNlsUtils.getNlsUrl()+"/ws/v1/");
|
||||||
recognizerDemo = new SpeechRecognizerAI(appKey, id, secret, url);
|
recognizerDemo = new SpeechRecognizerAI(appKey, id, secret, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import com.alibaba.nls.client.protocol.SampleRateEnum;
|
|||||||
import com.alibaba.nls.client.protocol.asr.SpeechRecognizer;
|
import com.alibaba.nls.client.protocol.asr.SpeechRecognizer;
|
||||||
import com.alibaba.nls.client.protocol.asr.SpeechRecognizerListener;
|
import com.alibaba.nls.client.protocol.asr.SpeechRecognizerListener;
|
||||||
import com.alibaba.nls.client.protocol.asr.SpeechRecognizerResponse;
|
import com.alibaba.nls.client.protocol.asr.SpeechRecognizerResponse;
|
||||||
|
import com.ruoyi.cms.util.AliyunNlsUtils;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -26,7 +27,12 @@ public class SpeechRecognizerAI {
|
|||||||
// 获取 AccessToken
|
// 获取 AccessToken
|
||||||
accessToken = new AccessToken(id, secret);
|
accessToken = new AccessToken(id, secret);
|
||||||
try {
|
try {
|
||||||
|
if(AliyunNlsUtils.USE_TEST_ENV){
|
||||||
accessToken.apply(); // 申请 Token
|
accessToken.apply(); // 申请 Token
|
||||||
|
}else{
|
||||||
|
AliyunNlsTokenUtil.generateToken(id, secret, this.accessToken);
|
||||||
|
}
|
||||||
|
//accessToken.apply(); // 申请 Token
|
||||||
logger.info("Token: {}, Expire Time: {}", accessToken.getToken(), accessToken.getExpireTime());
|
logger.info("Token: {}, Expire Time: {}", accessToken.getToken(), accessToken.getExpireTime());
|
||||||
|
|
||||||
// 初始化 NlsClient
|
// 初始化 NlsClient
|
||||||
@@ -35,7 +41,7 @@ public class SpeechRecognizerAI {
|
|||||||
} else {
|
} else {
|
||||||
this.client = new NlsClient(url, accessToken.getToken()); // 使用自定义服务地址
|
this.client = new NlsClient(url, accessToken.getToken()); // 使用自定义服务地址
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
logger.error("Failed to initialize NlsClient: {}", e.getMessage());
|
logger.error("Failed to initialize NlsClient: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import com.alibaba.nls.client.protocol.SampleRateEnum;
|
|||||||
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizer;
|
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizer;
|
||||||
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizerListener;
|
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizerListener;
|
||||||
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizerResponse;
|
import com.alibaba.nls.client.protocol.tts.SpeechSynthesizerResponse;
|
||||||
|
import com.ruoyi.cms.util.AliyunNlsUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -25,13 +26,18 @@ public class SpeechSynthesisWebSocketHandler {
|
|||||||
private String appKey = "mtA2pwmvCeefHT3Y";
|
private String appKey = "mtA2pwmvCeefHT3Y";
|
||||||
private String accessKeyId = "LTAI5tRBahK93vPNF1JDVEPA";
|
private String accessKeyId = "LTAI5tRBahK93vPNF1JDVEPA";
|
||||||
private String accessKeySecret = "x95OWb4cV6ccQVtbEJ2Gxm2Uwl2thJ";
|
private String accessKeySecret = "x95OWb4cV6ccQVtbEJ2Gxm2Uwl2thJ";
|
||||||
private String url = "wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1";
|
private String url = AliyunNlsUtils.getNlsUrl()+"/ws/v1/";
|
||||||
|
|
||||||
public SpeechSynthesisWebSocketHandler() {
|
public SpeechSynthesisWebSocketHandler() {
|
||||||
// Initialize NLS client with token
|
// Initialize NLS client with token
|
||||||
AccessToken accessToken = new AccessToken(accessKeyId, accessKeySecret);
|
AccessToken accessToken = new AccessToken(accessKeyId, accessKeySecret);
|
||||||
try {
|
try {
|
||||||
|
if(AliyunNlsUtils.USE_TEST_ENV){
|
||||||
accessToken.apply();
|
accessToken.apply();
|
||||||
|
}else{
|
||||||
|
AliyunNlsTokenUtil.generateToken(accessKeyId, accessKeySecret, accessToken);
|
||||||
|
}
|
||||||
|
//accessToken.apply();
|
||||||
String token = accessToken.getToken();
|
String token = accessToken.getToken();
|
||||||
if(url.isEmpty()) {
|
if(url.isEmpty()) {
|
||||||
this.client = new NlsClient(token);
|
this.client = new NlsClient(token);
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package com.ruoyi.cms.util;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 阿里云配置
|
||||||
|
*/
|
||||||
|
public class AliyunNlsUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 标志
|
||||||
|
*/
|
||||||
|
public static final boolean USE_TEST_ENV=true;
|
||||||
|
/**
|
||||||
|
* 测试nls
|
||||||
|
*/
|
||||||
|
public static final String NLS_TEST_URL="wss://nls-gateway-cn-shanghai.aliyuncs.com";
|
||||||
|
/**
|
||||||
|
* 测试微信获取oppenid链接
|
||||||
|
*/
|
||||||
|
public static final String WX_TEST_URL="https://api.weixin.qq.com/sns/jscode2session";
|
||||||
|
/**
|
||||||
|
* 正式nls
|
||||||
|
*/
|
||||||
|
public static final String NLS_FORMAL_URL="http://192.168.2.102:10044";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据环境判断正式还是测试
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getNlsUrl(){
|
||||||
|
String url = USE_TEST_ENV ? NLS_TEST_URL : NLS_FORMAL_URL;
|
||||||
|
System.out.println("nls当前环境:" + (USE_TEST_ENV ? "测试" : "正式") + ",WebSocket地址:" + url);
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信授权登录
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getWXoppenidUrl(){
|
||||||
|
String url = USE_TEST_ENV ? WX_TEST_URL : NLS_FORMAL_URL+"/weixin";
|
||||||
|
System.out.println("微信授权登录当前环境:" + (USE_TEST_ENV ? "测试" : "正式") + ",获取oppenid地址:" + url);
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -249,7 +249,7 @@ public class WechatUtil {
|
|||||||
try {
|
try {
|
||||||
System.out.println("appid==============="+appid);
|
System.out.println("appid==============="+appid);
|
||||||
System.out.println("secret================"+secret);
|
System.out.println("secret================"+secret);
|
||||||
String response = getAccessData("https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code");
|
String response = getAccessData(AliyunNlsUtils.getWXoppenidUrl()+"?appid="+appid+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code");
|
||||||
JSONObject result = JSONObject.parseObject(response);
|
JSONObject result = JSONObject.parseObject(response);
|
||||||
// 微信返回错误码处理
|
// 微信返回错误码处理
|
||||||
if (result.containsKey("errcode") && result.getInteger("errcode") != 0) {
|
if (result.containsKey("errcode") && result.getInteger("errcode") != 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user