parent
aca391e3a8
commit
2ec4e5ef28
File diff suppressed because one or more lines are too long
@ -0,0 +1,82 @@ |
||||
package com.hai.config; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.hai.common.exception.ErrorCode; |
||||
import com.hai.common.exception.ErrorHelp; |
||||
import com.hai.common.exception.SysCode; |
||||
import com.hai.common.utils.HttpsUtils; |
||||
import com.hai.common.utils.RedisUtil; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.util.*; |
||||
|
||||
/** |
||||
* 百度AI开放平台,语音合成 |
||||
* @author hurui |
||||
*/ |
||||
@Component |
||||
public class BaiduVoiceService { |
||||
|
||||
@Resource |
||||
private RedisUtil redisUtil; |
||||
|
||||
/** |
||||
* 获取token |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public String getToken() { |
||||
Object token = redisUtil.get("baidu_voice_token"); |
||||
if (token != null) { |
||||
return token.toString(); |
||||
} |
||||
Map<String, Object> map = new HashMap<>(); |
||||
map.put("grant_type", "client_credentials"); |
||||
map.put("client_id", "21NEfkbyDiXVZ3kf3vI8rwOG"); |
||||
map.put("client_secret", "EyN2X1oBDERL7dwMX2WIqoZnsdtvadCC"); |
||||
|
||||
// 获取token
|
||||
JSONObject tokenObject = HttpsUtils.doGet("https://aip.baidubce.com/oauth/2.0/token", map); |
||||
if (StringUtils.isNotBlank(tokenObject.getString("access_token"))) { |
||||
redisUtil.set("baidu_voice_token", tokenObject.getString("access_token"), tokenObject.getLong("expires_in")); |
||||
return tokenObject.getString("access_token"); |
||||
} |
||||
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "获取token失败"); |
||||
} |
||||
|
||||
/** |
||||
* 文本转换语音 |
||||
* @param text 文本 |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public String text2audio(String text) { |
||||
Map<String, Object> param = new HashMap<>(); |
||||
param.put("tok", this.getToken()); |
||||
param.put("tex", text); |
||||
param.put("cuid", System.currentTimeMillis()+""); |
||||
param.put("ctp", 1); |
||||
param.put("lan", "zh"); |
||||
param.put("spd", 5); |
||||
param.put("pit", 5); |
||||
param.put("vol", 8); |
||||
param.put("per", 5118); |
||||
param.put("aue", 3); |
||||
|
||||
String apiUrl = "http://tsn.baidu.com/text2audio"; |
||||
StringBuffer paramStr = new StringBuffer(); |
||||
int i = 0; |
||||
for (String key : param.keySet()) { |
||||
if (i == 0) { |
||||
paramStr.append("?"); |
||||
} else { |
||||
paramStr.append("&"); |
||||
paramStr.append(key).append("=").append(param.get(key)); |
||||
} |
||||
i++; |
||||
} |
||||
return apiUrl + paramStr; |
||||
} |
||||
} |
@ -0,0 +1,198 @@ |
||||
package com.hai.config; |
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils; |
||||
import org.apache.http.HttpEntity; |
||||
import org.apache.http.NameValuePair; |
||||
import org.apache.http.client.ClientProtocolException; |
||||
import org.apache.http.client.config.RequestConfig; |
||||
import org.apache.http.client.entity.UrlEncodedFormEntity; |
||||
import org.apache.http.client.methods.*; |
||||
import org.apache.http.client.utils.URLEncodedUtils; |
||||
import org.apache.http.impl.client.CloseableHttpClient; |
||||
import org.apache.http.impl.client.HttpClients; |
||||
import org.apache.http.message.BasicNameValuePair; |
||||
import org.apache.http.util.EntityUtils; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.HashMap; |
||||
|
||||
/** |
||||
* 商鹏打印机 |
||||
* @author hurui |
||||
*/ |
||||
public class SpPrinterConfig { |
||||
|
||||
public static final String baseUri = "https://open.spyun.net/v1/"; |
||||
|
||||
private String appid = "sp6284a57015d78"; |
||||
|
||||
private String appsecret = "2bdca1587ead21c0569e0ed1f82b19f6"; |
||||
|
||||
/* public SpPrinterConfig(String appid, String appsecret) { |
||||
this.appid = appid; |
||||
this.appsecret = appsecret; |
||||
}*/ |
||||
|
||||
// 添加打印机
|
||||
public String addPrinter(String sn, String pkey, String name) throws IOException { |
||||
ArrayList<NameValuePair> params = new ArrayList<>(); |
||||
params.add(new BasicNameValuePair("sn", sn)); |
||||
params.add(new BasicNameValuePair("pkey", pkey)); |
||||
params.add(new BasicNameValuePair("name", name)); |
||||
|
||||
return request("POST", "printer/add", params); |
||||
} |
||||
|
||||
// 删除打印机
|
||||
public String deletePrinter(String sn) throws IOException { |
||||
ArrayList<NameValuePair> params = new ArrayList<>(); |
||||
params.add(new BasicNameValuePair("sn", sn)); |
||||
|
||||
return request("DELETE", "printer/delete", params); |
||||
} |
||||
|
||||
// 修改打印机信息
|
||||
public String updatePrinter(String sn, String name) throws IOException { |
||||
ArrayList<NameValuePair> params = new ArrayList<>(); |
||||
params.add(new BasicNameValuePair("sn", sn)); |
||||
params.add(new BasicNameValuePair("name", name)); |
||||
|
||||
return request("PATCH", "printer/update", params); |
||||
} |
||||
|
||||
// 修改打印机参数
|
||||
public String updatePrinterSetting(String sn, int auto_cut, String voice) throws IOException { |
||||
ArrayList<NameValuePair> params = new ArrayList<>(); |
||||
params.add(new BasicNameValuePair("sn", sn)); |
||||
params.add(new BasicNameValuePair("auto_cut", String.valueOf(auto_cut))); |
||||
params.add(new BasicNameValuePair("voice", voice)); |
||||
|
||||
return request("PATCH", "printer/setting", params); |
||||
} |
||||
|
||||
// 获取打印机信息
|
||||
public String getPrinter(String sn) throws IOException { |
||||
ArrayList<NameValuePair> params = new ArrayList<>(); |
||||
params.add(new BasicNameValuePair("sn", sn)); |
||||
|
||||
return request("GET", "printer/info", params); |
||||
} |
||||
|
||||
// 打印订单
|
||||
public String print(String sn, String content, int times) throws IOException { |
||||
ArrayList<NameValuePair> params = new ArrayList<>(); |
||||
params.add(new BasicNameValuePair("sn", sn)); |
||||
params.add(new BasicNameValuePair("content", content)); |
||||
params.add(new BasicNameValuePair("times", String.valueOf(times))); |
||||
|
||||
return request("POST", "printer/print", params); |
||||
} |
||||
|
||||
// 清空待打印订单
|
||||
public String deletePrints(String sn) throws IOException { |
||||
ArrayList<NameValuePair> params = new ArrayList<>(); |
||||
params.add(new BasicNameValuePair("sn", sn)); |
||||
|
||||
return request("DELETE", "printer/cleansqs", params); |
||||
} |
||||
|
||||
// 查询打印订单状态
|
||||
public String getPrintsStatus(String id) throws IOException { |
||||
ArrayList<NameValuePair> params = new ArrayList<>(); |
||||
params.add(new BasicNameValuePair("id", id)); |
||||
|
||||
return request("GET", "printer/order/status", params); |
||||
} |
||||
|
||||
// 查询打印机历史打印订单数
|
||||
public String getPrintsOrders(String sn, String date) throws IOException { |
||||
ArrayList<NameValuePair> params = new ArrayList<>(); |
||||
params.add(new BasicNameValuePair("sn", sn)); |
||||
params.add(new BasicNameValuePair("date", date)); |
||||
|
||||
return request("GET", "printer/order/number", params); |
||||
} |
||||
|
||||
// 发送请求
|
||||
private String request(String method, String uri, ArrayList<NameValuePair> params) throws IOException { |
||||
RequestConfig requestConfig = RequestConfig.custom() |
||||
.setSocketTimeout(4000) //读取超时
|
||||
.setConnectTimeout(1000) //连接超时
|
||||
.build(); |
||||
|
||||
CloseableHttpClient httpClient = HttpClients.custom() |
||||
.setDefaultRequestConfig(requestConfig) |
||||
.build(); |
||||
|
||||
// 公共请求参数
|
||||
params.add(new BasicNameValuePair("appid", appid)); |
||||
params.add(new BasicNameValuePair("timestamp", String.valueOf(System.currentTimeMillis() / 1000))); |
||||
params.add(new BasicNameValuePair("sign", makeSign(params))); |
||||
|
||||
CloseableHttpResponse response = null; |
||||
String url = baseUri + uri; |
||||
if (method.equals("GET")) { |
||||
HttpGet request = new HttpGet(url + "?" + URLEncodedUtils.format(params, "utf-8")); |
||||
response = httpClient.execute(request); |
||||
} else if (method.equals("DELETE")) { |
||||
HttpDelete request = new HttpDelete(url + "?" + URLEncodedUtils.format(params, "utf-8")); |
||||
response = httpClient.execute(request); |
||||
} else if (method.equals("POST")) { |
||||
HttpPost request = new HttpPost(url); |
||||
request.setEntity(new UrlEncodedFormEntity(params,"utf-8")); |
||||
response = httpClient.execute(request); |
||||
} else if (method.equals("PATCH")) { |
||||
HttpPatch request = new HttpPatch(url); |
||||
request.setEntity(new UrlEncodedFormEntity(params,"utf-8")); |
||||
response = httpClient.execute(request); |
||||
} else if (method.equals("PUT")) { |
||||
HttpPut request = new HttpPut(url); |
||||
request.setEntity(new UrlEncodedFormEntity(params,"utf-8")); |
||||
response = httpClient.execute(request); |
||||
} |
||||
|
||||
if (response == null) { |
||||
throw new ClientProtocolException(); |
||||
} |
||||
|
||||
HttpEntity httpEntity = response.getEntity(); |
||||
if (httpEntity == null) { |
||||
throw new ClientProtocolException(); |
||||
} |
||||
|
||||
if (response.getStatusLine().getStatusCode() != 200) { |
||||
throw new ClientProtocolException(EntityUtils.toString(httpEntity)); |
||||
} |
||||
|
||||
return EntityUtils.toString(httpEntity); |
||||
} |
||||
|
||||
// 创建签名
|
||||
public String makeSign(ArrayList<NameValuePair> params) { |
||||
int size = params.size(); |
||||
String[] keys = new String[params.size()]; |
||||
HashMap<String, String> values = new HashMap<>(); |
||||
for (int i = 0; i < size; i++) { |
||||
NameValuePair p = params.get(i); |
||||
keys[i] = p.getName(); |
||||
values.put(p.getName(), p.getValue()); |
||||
} |
||||
Arrays.sort(keys); |
||||
|
||||
String sign = ""; |
||||
for (int i = 0; i < keys.length; i++) { |
||||
String v = values.get(keys[i]); |
||||
if (!keys[i].equals("sign") && !keys[i].equals("appsecret") && !v.equals("")) { |
||||
if (i > 0) { |
||||
sign += "&"; |
||||
} |
||||
sign += keys[i] + "=" + v; |
||||
} |
||||
} |
||||
sign += "&appsecret=" + appsecret; |
||||
|
||||
return DigestUtils.md5Hex(sign).toUpperCase(); |
||||
} |
||||
} |
@ -0,0 +1,105 @@ |
||||
package com.hai.config; |
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils; |
||||
import org.apache.http.HttpEntity; |
||||
import org.apache.http.NameValuePair; |
||||
import org.apache.http.client.ClientProtocolException; |
||||
import org.apache.http.client.config.RequestConfig; |
||||
import org.apache.http.client.entity.UrlEncodedFormEntity; |
||||
import org.apache.http.client.methods.*; |
||||
import org.apache.http.client.utils.URLEncodedUtils; |
||||
import org.apache.http.impl.client.CloseableHttpClient; |
||||
import org.apache.http.impl.client.HttpClients; |
||||
import org.apache.http.message.BasicNameValuePair; |
||||
import org.apache.http.util.EntityUtils; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.HashMap; |
||||
|
||||
/** |
||||
* 商鹏打印机模板 |
||||
* @author hurui |
||||
*/ |
||||
public class SpPrinterTemplate { |
||||
|
||||
/** |
||||
* 加油站收银员存根模板 |
||||
* @param gasName 油站名称 |
||||
* @param orderNo 订单号 |
||||
* @param payTime 支付时间 |
||||
* @param source 来源 |
||||
* @param gunNo 抢号 |
||||
* @param oilNo 油号 |
||||
* @param oilLiters 升数 |
||||
* @param orderPrice 加油金额 |
||||
* @return |
||||
*/ |
||||
public static String oilCashierStubTemp(String gasName, |
||||
String orderNo, |
||||
String payTime, |
||||
String source, |
||||
String gunNo, |
||||
String oilNo, |
||||
String oilLiters, |
||||
String orderPrice) { |
||||
String str = "<C><B>" + gasName + "</B></C> <BR> " + |
||||
"<C>(收银员存根)</C> <BR>" + |
||||
"------------------------------<BR>" + |
||||
"流水:" + orderNo + "<BR>" + |
||||
"------------------------------<BR>" + |
||||
"时间:" + payTime + "<BR>" + |
||||
"来源:" + source + "<BR>" + |
||||
"油枪:"+ gunNo + "号<BR>" + |
||||
"油品:" + oilNo + "#<BR>" + |
||||
"升数:" + oilLiters +"升<BR>" + |
||||
"实际加油升数以油站加油机为准!<BR>" + |
||||
"------------------------------<BR>" + |
||||
"<L2>加油金额</L2><BR>" + |
||||
"<L2>¥" + orderPrice + "元</L2><BR>" + |
||||
"------------------------------<BR>" + |
||||
"<C>来"嗨森逛“;开心又省钱</C>"; |
||||
return str; |
||||
} |
||||
|
||||
/** |
||||
* 加油站客户存根模板 |
||||
* @param gasName 油站名称 |
||||
* @param orderNo 订单号 |
||||
* @param payTime 支付时间 |
||||
* @param source 来源 |
||||
* @param gunNo 抢号 |
||||
* @param oilNo 油号 |
||||
* @param oilLiters 升数 |
||||
* @param orderPrice 加油金额 |
||||
* @return |
||||
*/ |
||||
public static String oilClientStubTemp(String gasName, |
||||
String orderNo, |
||||
String payTime, |
||||
String source, |
||||
String gunNo, |
||||
String oilNo, |
||||
String oilLiters, |
||||
String orderPrice) { |
||||
String str = "<C><B>" + gasName + "</B></C> <BR> " + |
||||
"<C>(客户存根)</C> <BR>" + |
||||
"------------------------------<BR>" + |
||||
"流水:" + orderNo + "<BR>" + |
||||
"------------------------------<BR>" + |
||||
"时间:" + payTime + "<BR>" + |
||||
"来源:" + source + "<BR>" + |
||||
"油枪:"+ gunNo + "号<BR>" + |
||||
"油品:" + oilNo + "#<BR>" + |
||||
"升数:" + oilLiters +"升<BR>" + |
||||
"实际加油升数以油站加油机为准!<BR>" + |
||||
"------------------------------<BR>" + |
||||
"<L2>加油金额</L2><BR>" + |
||||
"<L2>¥" + orderPrice + "元</L2><BR>" + |
||||
"------------------------------<BR>" + |
||||
"<C>来"嗨森逛“;开心又省钱</C>"; |
||||
return str; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue