package com.zhgd.xmgl.call; import cn.hutool.core.codec.Base64; import cn.hutool.core.io.FileUtil; 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.modules.broadcast.entity.SmartBroadcastConfig; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import java.util.List; import java.util.Objects; @Slf4j @Component @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public class ShiZiWangBroadcastCall implements BroadcastManufacturer { private SmartBroadcastConfig config; @Lazy @Autowired private RedisRepository redisRepository; @Override public SmartBroadcastConfig getConfig() { return config; } @Override public void setConfig(SmartBroadcastConfig config) { this.config = config; } @Override public void playVoiceFile(List devSns, String fileName, String filePath) { JSONObject body = new JSONObject(); body.put("SerialNums", devSns); body.put("PlayText", fileName); body.put("PlayFileContent", Base64.encode(FileUtil.readBytes(filePath))); JSONObject header = new JSONObject(); header.put("Token", getToken()); HttpUtils.sendPostBodyRtJo("狮子王播放语音文件", "https://norsos.lionking110.com/sos/v1/mntn/adap/business/external/group/play", body, 20000, header); } /** * 获取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); } }