TencentYunServiceImpl.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.fdkankan.tk.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.fdkankan.redis.util.RedisUtil;
  5. import com.fdkankan.tk.common.ResultCode;
  6. import com.fdkankan.tk.common.util.RedisKeyUtil;
  7. import com.fdkankan.tk.entity.TencentYun;
  8. import com.fdkankan.tk.exception.BusinessException;
  9. import com.fdkankan.tk.io.agora.media.RtcTokenBuilder;
  10. import com.fdkankan.tk.io.agora.media.RtcTokenBuilder2;
  11. import com.fdkankan.tk.mapper.ITencentYunMapper;
  12. import com.fdkankan.tk.service.ITencentYunService;
  13. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  14. import com.tencentyun.TLSSigAPIv2;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import java.security.Security;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. /**
  21. * <p>
  22. * 服务实现类
  23. * </p>
  24. *
  25. * @author
  26. * @since 2022-09-27
  27. */
  28. @Service
  29. public class TencentYunServiceImpl extends ServiceImpl<ITencentYunMapper, TencentYun> implements ITencentYunService {
  30. @Autowired
  31. RedisUtil redisUtil;
  32. @Override
  33. public JSONObject getSign(String userId,TencentYun tencentYun) {
  34. String redisKey = RedisKeyUtil.TENCENT_YUN_KEY + userId;
  35. if (!redisUtil.hasKey(redisKey)) {
  36. TLSSigAPIv2 api = new TLSSigAPIv2(tencentYun.getSdkAppid(), tencentYun.getSecretkey());
  37. String sign = api.genUserSig(userId, tencentYun.getExTime() );
  38. JSONObject jsonObject = new JSONObject();
  39. jsonObject.put("sdkAppId",tencentYun.getSdkAppid());
  40. jsonObject.put("expire",tencentYun.getExTime());
  41. jsonObject.put("sign",sign);
  42. jsonObject.put("operatorType",tencentYun.getOperatorType());
  43. redisUtil.set(redisKey ,jsonObject.toJSONString(),tencentYun.getExTime() - 60);
  44. return jsonObject;
  45. }
  46. JSONObject jsonObject = JSONObject.parseObject(redisUtil.get(redisKey));
  47. jsonObject.put("expire",redisUtil.getExpire(redisKey));
  48. return jsonObject;
  49. }
  50. private List<TencentYun> getByType(Integer type) {
  51. LambdaQueryWrapper<TencentYun> wrapper = new LambdaQueryWrapper<>();
  52. wrapper.eq(TencentYun::getOperatorType,type);
  53. return this.list(wrapper);
  54. }
  55. static HashMap<Integer, RtcTokenBuilder2.Role> roleMap = new HashMap<>();
  56. private void setRoleMap (){
  57. roleMap.put(RtcTokenBuilder2.Role.ROLE_PUBLISHER.initValue,RtcTokenBuilder2.Role.ROLE_PUBLISHER);
  58. roleMap.put(RtcTokenBuilder2.Role.ROLE_SUBSCRIBER.initValue,RtcTokenBuilder2.Role.ROLE_SUBSCRIBER);
  59. }
  60. @Override
  61. public Object getAgoraToken(String userId,Integer roleId,String channelName,TencentYun tencentYun) {
  62. if(roleMap.size() <=0){
  63. setRoleMap();
  64. }
  65. if(roleMap.get(roleId) == null){
  66. throw new BusinessException(ResultCode.AGO_ROLE_ERROR);
  67. }
  68. RtcTokenBuilder2 token = new RtcTokenBuilder2();
  69. String result = token.buildTokenWithUserAccount(tencentYun.getAppid(), tencentYun.getSecretkey(), channelName, userId, roleMap.get(roleId), tencentYun.getExTime(), tencentYun.getExTime());
  70. JSONObject jsonObject = new JSONObject();
  71. jsonObject.put("sdkAppId",tencentYun.getAppid());
  72. jsonObject.put("expire",tencentYun.getExTime());
  73. jsonObject.put("sign",result);
  74. jsonObject.put("operatorType",tencentYun.getOperatorType());
  75. return jsonObject;
  76. }
  77. public static void main(String[] args) {
  78. RtcTokenBuilder2 token = new RtcTokenBuilder2();
  79. String result = token.buildTokenWithUid("dceec50eb3874a23b6bb6bf722455da4", "d9c16154cb8a4b6e9acd7135b8017026", "test001",
  80. 0, RtcTokenBuilder2.Role.ROLE_PUBLISHER ,3600,3600);
  81. System.out.println(result);
  82. }
  83. }