package cn.com.goldenwater.dcproj.service.impl; import cn.com.goldenwater.dcproj.model.AgoraStudioDesc; import cn.com.goldenwater.dcproj.param.AgoraStudioDescParam; import cn.com.goldenwater.dcproj.service.AgoraChannelService; import cn.com.goldenwater.dcproj.service.AgoraStudioDescService; import com.alibaba.fastjson.JSON; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import net.sf.json.JSONObject; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.IOException; import java.util.*; /** * @author lhc * @date 2021-7-4 */ @Service @Transactional public class AgoraChannelServiceImpl implements AgoraChannelService { private Logger logger = LoggerFactory.getLogger(getClass()); private static final String APP_ID = "6f592a3023ef4960a2d5342a6fc456e7"; /** * 客户 ID */ private static final String CUSTOMER_KEY = "77864af0d5e449ae9b8271a930aba2f4"; /** * 客户密钥 */ private static final String CUSTOMER_SECRET = "216c9520cc974121806034bad52f533c"; @Autowired private AgoraStudioDescService agoraStudioDescService; @Override public PageInfo pageList(AgoraStudioDescParam agoraStudioDescParam) { PageHelper.startPage(agoraStudioDescParam); List list = this.agoraStudioDescService.findList(agoraStudioDescParam); updateState(list); return new PageInfo<>(list); } private void updateState(List list) { Set onlineChannelList = getOnlineChannel(); if (null == onlineChannelList || onlineChannelList.size() == 0) { return; } list.forEach(agoraStudioDesc -> { if (onlineChannelList.contains(agoraStudioDesc.getChannelName())) { agoraStudioDesc.setState("1"); } }); } private Set getOnlineChannel() { Set rt = new HashSet<>(); // 拼接客户 ID 和客户密钥并使用 base64 编码 String plainCredentials = CUSTOMER_KEY + ":" + CUSTOMER_SECRET; String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes())); // 创建 authorization header String authorizationHeader = "Basic " + base64Credentials; try (CloseableHttpClient httpCilent = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet("https://api.agora.io/dev/v1/channel/" + APP_ID); httpGet.setHeader("Accept", "application/json"); httpGet.setHeader("Authorization", authorizationHeader); HttpResponse httpResponse = httpCilent.execute(httpGet); String result = EntityUtils.toString(httpResponse.getEntity()); // "key": "{\"success\":true,\"data\":{\"channels\":[],\"total_size\":0}}" String channels = Optional.ofNullable(result) .map(m -> jsonToMap(m).get("data")) .map(m -> MapUtils.getString(jsonToMap(m), "channels")).orElse(null); if (StringUtils.isBlank(channels)) { return null; } List channelList = JSON.parseArray(channels, String.class); return new HashSet<>(channelList); } catch (IOException e) { e.printStackTrace(); } return null; } /** * json string 转换为 map 对象 * * @param jsonObj * @return */ public Map jsonToMap(Object jsonObj) { JSONObject jsonObject = JSONObject.fromObject(jsonObj); return (Map) jsonObject; } }