优化狮子王接口
This commit is contained in:
parent
48f41ef72a
commit
fc1c57457e
@ -1,12 +1,14 @@
|
|||||||
package com.zhgd.jeecg.common.util.pass;
|
package com.zhgd.jeecg.common.util.pass;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.core.net.NetUtil;
|
import cn.hutool.core.net.NetUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.http.HttpRequest;
|
import cn.hutool.http.HttpRequest;
|
||||||
import cn.hutool.http.HttpResponse;
|
import cn.hutool.http.HttpResponse;
|
||||||
import cn.hutool.http.HttpUtil;
|
import cn.hutool.http.HttpUtil;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.gexin.fastjson.JSON;
|
import com.gexin.fastjson.JSON;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
@ -35,6 +37,7 @@ public class HttpUtils {
|
|||||||
* @param url
|
* @param url
|
||||||
* @param body
|
* @param body
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public static String postJson(String url, Object body) {
|
public static String postJson(String url, Object body) {
|
||||||
String b = JSON.toJSONString(body);
|
String b = JSON.toJSONString(body);
|
||||||
log.info("postJson(rq):url:{},body:{}", url, b);
|
log.info("postJson(rq):url:{},body:{}", url, b);
|
||||||
@ -355,4 +358,32 @@ public class HttpUtils {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送post的body方式返回JSONObject
|
||||||
|
*
|
||||||
|
* @param print
|
||||||
|
* @param url
|
||||||
|
* @param body
|
||||||
|
* @param timeout
|
||||||
|
* @param header
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static JSONObject sendPostBodyRtJo(String print, String url, JSONObject body, int timeout, JSONObject header) {
|
||||||
|
String bodyStr = body.toJSONString();
|
||||||
|
String headerStr = "";
|
||||||
|
HttpRequest request = HttpRequest.post(url)
|
||||||
|
.body(bodyStr)
|
||||||
|
.timeout(timeout);
|
||||||
|
if (header != null) {
|
||||||
|
headerStr = header.toJSONString();
|
||||||
|
for (Map.Entry<String, Object> headerEntry : header.entrySet()) {
|
||||||
|
request.header(headerEntry.getKey(), Convert.toStr(headerEntry.getValue()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.info(print + "url:{},body:{},headers:{}", url, bodyStr, headerStr);
|
||||||
|
String result = request
|
||||||
|
.execute().body();
|
||||||
|
log.info(print + "结果:{}", result);
|
||||||
|
return JSONObject.parseObject(result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,22 +2,30 @@ package com.zhgd.xmgl.call;
|
|||||||
|
|
||||||
import cn.hutool.core.codec.Base64;
|
import cn.hutool.core.codec.Base64;
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.http.HttpRequest;
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.zhgd.jeecg.common.execption.OpenAlertException;
|
||||||
|
import com.zhgd.jeecg.common.util.pass.HttpUtils;
|
||||||
|
import com.zhgd.redis.lock.RedisRepository;
|
||||||
import com.zhgd.xmgl.call.api.BroadcastManufacturer;
|
import com.zhgd.xmgl.call.api.BroadcastManufacturer;
|
||||||
import com.zhgd.xmgl.modules.broadcast.entity.SmartBroadcastConfig;
|
import com.zhgd.xmgl.modules.broadcast.entity.SmartBroadcastConfig;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||||
|
import org.springframework.context.annotation.Lazy;
|
||||||
import org.springframework.context.annotation.Scope;
|
import org.springframework.context.annotation.Scope;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||||
public class ShiZiWangBroadcastCall implements BroadcastManufacturer {
|
public class ShiZiWangBroadcastCall implements BroadcastManufacturer {
|
||||||
private SmartBroadcastConfig config;
|
private SmartBroadcastConfig config;
|
||||||
|
@Lazy
|
||||||
|
@Autowired
|
||||||
|
private RedisRepository redisRepository;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SmartBroadcastConfig getConfig() {
|
public SmartBroadcastConfig getConfig() {
|
||||||
@ -31,17 +39,32 @@ public class ShiZiWangBroadcastCall implements BroadcastManufacturer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void playVoiceFile(List<String> devSns, String fileName, String filePath) {
|
public void playVoiceFile(List<String> devSns, String fileName, String filePath) {
|
||||||
JSONObject jsonObject = new JSONObject();
|
JSONObject body = new JSONObject();
|
||||||
jsonObject.put("SerialNums", devSns);
|
body.put("SerialNums", devSns);
|
||||||
jsonObject.put("PlayText", fileName);
|
body.put("PlayText", fileName);
|
||||||
jsonObject.put("PlayFileContent", Base64.encode(FileUtil.readBytes(filePath)));
|
body.put("PlayFileContent", Base64.encode(FileUtil.readBytes(filePath)));
|
||||||
String body = jsonObject.toJSONString();
|
JSONObject header = new JSONObject();
|
||||||
String url = "https://norsos.lionking110.com/sos/v1/mntn/adap/business/external/group/play";
|
header.put("Token", getToken());
|
||||||
log.info("狮子王播放语音文件url:{},body:{}", url, body);
|
HttpUtils.sendPostBodyRtJo("狮子王播放语音文件", "https://norsos.lionking110.com/sos/v1/mntn/adap/business/external/group/play", body, 20000, header);
|
||||||
String result = HttpRequest.post(url)
|
|
||||||
.body(body)
|
|
||||||
.timeout(20000)
|
|
||||||
.execute().body();
|
|
||||||
log.info("狮子王播放语音文件结果:{}", result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取token
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private String getToken() {
|
||||||
|
return redisRepository.getOrSet("SHIZIWANG:TOKEN:" + config.getAppId(), () -> {
|
||||||
|
JSONObject body = new JSONObject();
|
||||||
|
body.put("AppId", config.getAppId());
|
||||||
|
body.put("AppCode", config.getAppCode());
|
||||||
|
JSONObject jsonObject = HttpUtils.sendPostBodyRtJo("狮子王通过appId信息获取token", "https://norsos.lionking110.com/sos/v1/mntn/account/appId/token", body, 20000, null);
|
||||||
|
if (!Objects.equals(jsonObject.getInteger("Status"), 0)) {
|
||||||
|
throw new OpenAlertException("狮子王通过appId信息获取token异常,错误信息:" + jsonObject.getString("StatusMsg"));
|
||||||
|
}
|
||||||
|
return jsonObject.getString("Token");
|
||||||
|
}, 7200L);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -49,11 +49,6 @@ public class SmartBroadcastConfig implements Serializable {
|
|||||||
*/
|
*/
|
||||||
@ApiModelProperty(value = "app_code")
|
@ApiModelProperty(value = "app_code")
|
||||||
private java.lang.String appCode;
|
private java.lang.String appCode;
|
||||||
/**
|
|
||||||
* 云片apikey
|
|
||||||
*/
|
|
||||||
@ApiModelProperty(value = "云片apikey")
|
|
||||||
private java.lang.String apiKey;
|
|
||||||
/**
|
/**
|
||||||
* 是否启用,1是,0否
|
* 是否启用,1是,0否
|
||||||
*/
|
*/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user