123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package com.fdkankan.tk.service.impl;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.fdkankan.redis.util.RedisUtil;
- import com.fdkankan.tk.common.ResultCode;
- import com.fdkankan.tk.common.util.RedisKeyUtil;
- import com.fdkankan.tk.entity.TencentYun;
- import com.fdkankan.tk.exception.BusinessException;
- import com.fdkankan.tk.io.agora.media.RtcTokenBuilder;
- import com.fdkankan.tk.io.agora.media.RtcTokenBuilder2;
- 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 org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.security.Security;
- import java.util.HashMap;
- import java.util.List;
- /**
- * <p>
- * 服务实现类
- * </p>
- *
- * @author
- * @since 2022-09-27
- */
- @Service
- public class TencentYunServiceImpl extends ServiceImpl<ITencentYunMapper, TencentYun> implements ITencentYunService {
- @Autowired
- RedisUtil redisUtil;
- @Override
- public JSONObject getSign(String userId,TencentYun tencentYun) {
- String redisKey = RedisKeyUtil.TENCENT_YUN_KEY + userId;
- if (!redisUtil.hasKey(redisKey)) {
- TLSSigAPIv2 api = new TLSSigAPIv2(tencentYun.getSdkAppid(), tencentYun.getSecretkey());
- String sign = api.genUserSig(userId, tencentYun.getExTime() );
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("sdkAppId",tencentYun.getSdkAppid());
- jsonObject.put("expire",tencentYun.getExTime());
- jsonObject.put("sign",sign);
- 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;
- }
- private List<TencentYun> getByType(Integer type) {
- LambdaQueryWrapper<TencentYun> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(TencentYun::getOperatorType,type);
- return this.list(wrapper);
- }
- static HashMap<Integer, RtcTokenBuilder2.Role> roleMap = new HashMap<>();
- private void setRoleMap (){
- roleMap.put(RtcTokenBuilder2.Role.ROLE_PUBLISHER.initValue,RtcTokenBuilder2.Role.ROLE_PUBLISHER);
- roleMap.put(RtcTokenBuilder2.Role.ROLE_SUBSCRIBER.initValue,RtcTokenBuilder2.Role.ROLE_SUBSCRIBER);
- }
- @Override
- public Object getAgoraToken(String userId,Integer roleId,String channelName,TencentYun tencentYun) {
- if(roleMap.size() <=0){
- setRoleMap();
- }
- if(roleMap.get(roleId) == null){
- throw new BusinessException(ResultCode.AGO_ROLE_ERROR);
- }
- RtcTokenBuilder2 token = new RtcTokenBuilder2();
- String result = token.buildTokenWithUserAccount(tencentYun.getAppid(), tencentYun.getSecretkey(), channelName, userId, roleMap.get(roleId), tencentYun.getExTime(), tencentYun.getExTime());
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("sdkAppId",tencentYun.getAppid());
- jsonObject.put("expire",tencentYun.getExTime());
- jsonObject.put("sign",result);
- jsonObject.put("operatorType",tencentYun.getOperatorType());
- return jsonObject;
- }
- public static void main(String[] args) {
- RtcTokenBuilder2 token = new RtcTokenBuilder2();
- String result = token.buildTokenWithUid("dceec50eb3874a23b6bb6bf722455da4", "d9c16154cb8a4b6e9acd7135b8017026", "test001",
- 0, RtcTokenBuilder2.Role.ROLE_PUBLISHER ,3600,3600);
- System.out.println(result);
- }
- }
|