package cn.iocoder.yudao.module.system.api.duix; import org.springframework.stereotype.Service; import com.auth0.jwt.JWT; import com.auth0.jwt.algorithms.Algorithm; import java.util.Calendar; import java.util.Date; import java.util.Map; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; @Service public class DuixApiImpl implements DuixApi{ /** * 生成touken * appId string 会话标识,会话创建成功后获取 xxxxxxx * appKey string 会话密钥,会话创建成功后获取 xxxxxxx * sigExp Integer 签名有效时间,单位秒 18000 */ public static String createSig(String appId, String appKey, int sigExp) { Calendar nowTime = Calendar.getInstance(); nowTime.add(Calendar.SECOND, sigExp); Date expiresDate = nowTime.getTime(); return JWT.create() //Release date .withIssuedAt(new Date()) //effective time .withExpiresAt(expiresDate) //load .withClaim("appId", appId) //encryption .sign(Algorithm.HMAC256(appKey)); } /** * 获取APP实时并发号 * 查询指定 APP 下的并发号 * appId 应用程序 字符串 查询 从平台创建的 APPID * * @param appId */ @Override public Map getAppConcurrent(String appId) { String urlString = "https://your-domain.com/duix-openapi-v2/sdk/v2/getconcurrentNumber?appId=" + appId; try { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 设置请求方法为 GET conn.setRequestMethod("GET"); // 设置请求头(如果需要身份验证,比如token,可以添加如下内容) // conn.setRequestProperty("Authorization", "Bearer your_token"); int responseCode = conn.getResponseCode(); System.out.println("Response Code: " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader( conn.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 打印返回结果 System.out.println("Response: " + response.toString()); } catch (Exception e) { e.printStackTrace(); } return null; } }