|
@@ -10,9 +10,12 @@ import com.fdkankan.tk.mapper.ITencentYunMapper;
|
|
|
import com.fdkankan.tk.service.ITencentYunService;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.tencentyun.TLSSigAPIv2;
|
|
|
+import io.agora.media.RtcTokenBuilder;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.security.Security;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
@@ -58,4 +61,51 @@ public class TencentYunServiceImpl extends ServiceImpl<ITencentYunMapper, Tencen
|
|
|
jsonObject.put("expire",redisUtil.getExpire(redisKey));
|
|
|
return jsonObject;
|
|
|
}
|
|
|
+
|
|
|
+ static int tokenExpirationInSeconds = 3600;
|
|
|
+ static int privilegeExpirationInSeconds = 3600;
|
|
|
+
|
|
|
+ static HashMap<Integer,RtcTokenBuilder.Role> roleMap = new HashMap<>();
|
|
|
+ private void setRoleMap (){
|
|
|
+ roleMap.put(RtcTokenBuilder.Role.Role_Admin.initValue,RtcTokenBuilder.Role.Role_Admin);
|
|
|
+ roleMap.put(RtcTokenBuilder.Role.Role_Publisher.initValue,RtcTokenBuilder.Role.Role_Publisher);
|
|
|
+ roleMap.put(RtcTokenBuilder.Role.Role_Subscriber.initValue,RtcTokenBuilder.Role.Role_Subscriber);
|
|
|
+ roleMap.put(RtcTokenBuilder.Role.Role_Attendee.initValue,RtcTokenBuilder.Role.Role_Attendee);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getAgoraToken(Integer userId,Integer roleId,String channelName) {
|
|
|
+ if(roleMap.size() <=0){
|
|
|
+ setRoleMap();
|
|
|
+ }
|
|
|
+ if(roleMap.get(roleId) == null){
|
|
|
+ throw new BusinessException(ResultCode.AGO_ROLE_ERROR);
|
|
|
+ }
|
|
|
+ String redisKey = String.format(RedisKeyUtil.AGORA_KEY ,channelName,roleId,userId);
|
|
|
+ if (!redisUtil.hasKey(redisKey)) {
|
|
|
+ List<TencentYun> list = this.list();
|
|
|
+ if(list == null || list.size() <=0){
|
|
|
+ throw new BusinessException(ResultCode.TENCENT_YUN_EMPTY);
|
|
|
+ }
|
|
|
+ if(list.size() >1){
|
|
|
+ throw new BusinessException(ResultCode.TENCENT_YUN_ERROR);
|
|
|
+ }
|
|
|
+ TencentYun tencentYun = list.get(0);
|
|
|
+ RtcTokenBuilder token = new RtcTokenBuilder();
|
|
|
+ String result = token.buildTokenWithUid(tencentYun.getAppid(), tencentYun.getSecretkey(), channelName, userId,roleMap.get(roleId) ,privilegeExpirationInSeconds);
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("sdkAppId",tencentYun.getSdkAppid());
|
|
|
+ jsonObject.put("expire",tencentYun.getExTime());
|
|
|
+ jsonObject.put("sign",result);
|
|
|
+ jsonObject.put("operatorType",tencentYun.getOperatorType());
|
|
|
+ redisUtil.set(redisKey ,jsonObject.toJSONString(),tencentYun.getExTime() - 60);
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(redisUtil.get(redisKey));
|
|
|
+ jsonObject.put("expire",redisUtil.getExpire(redisKey));
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|