TencentYunServiceImpl.java 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package com.fdkankan.tk.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.fdkankan.redis.util.RedisUtil;
  4. import com.fdkankan.tk.common.ResultCode;
  5. import com.fdkankan.tk.common.util.RedisKeyUtil;
  6. import com.fdkankan.tk.entity.TencentYun;
  7. import com.fdkankan.tk.exception.BusinessException;
  8. import com.fdkankan.tk.mapper.ITencentYunMapper;
  9. import com.fdkankan.tk.service.ITencentYunService;
  10. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  11. import com.tencentyun.TLSSigAPIv2;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Service;
  14. import java.util.List;
  15. /**
  16. * <p>
  17. * 服务实现类
  18. * </p>
  19. *
  20. * @author
  21. * @since 2022-09-27
  22. */
  23. @Service
  24. public class TencentYunServiceImpl extends ServiceImpl<ITencentYunMapper, TencentYun> implements ITencentYunService {
  25. @Autowired
  26. RedisUtil redisUtil;
  27. @Override
  28. public JSONObject getSign(String userId) {
  29. String redisKey = RedisKeyUtil.TENCENT_YUN_KEY + userId;
  30. if (!redisUtil.hasKey(redisKey)) {
  31. List<TencentYun> list = this.list();
  32. if(list == null || list.size() <=0){
  33. throw new BusinessException(ResultCode.TENCENT_YUN_EMPTY);
  34. }
  35. if(list.size() >1){
  36. throw new BusinessException(ResultCode.TENCENT_YUN_ERROR);
  37. }
  38. TencentYun tencentYun = list.get(0);
  39. TLSSigAPIv2 api = new TLSSigAPIv2(tencentYun.getSdkAppid(), tencentYun.getSecretkey());
  40. String sign = api.genUserSig(userId, tencentYun.getExTime() );
  41. JSONObject jsonObject = new JSONObject();
  42. jsonObject.put("sdkAppId",tencentYun.getSdkAppid());
  43. jsonObject.put("expire",tencentYun.getExTime());
  44. jsonObject.put("sign",sign);
  45. redisUtil.set(redisKey ,jsonObject.toJSONString(),tencentYun.getExTime() - 60);
  46. return jsonObject;
  47. }
  48. JSONObject jsonObject = JSONObject.parseObject(redisUtil.get(redisKey));
  49. jsonObject.put("expire",redisUtil.getExpire(redisKey));
  50. return jsonObject;
  51. }
  52. }