SignTencent.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Created by KLXQ
  4. * user: youyi
  5. * Date:2024/6/5 11:47.
  6. * Tencent SDK.
  7. * 目前主要用于获取用户签名.
  8. */
  9. namespace App\Http\Helpers\Tencent;
  10. use Tencent\TLSSigAPIv2;
  11. class SignTencent
  12. {
  13. private int $sdkAppId;
  14. private string $sdkSecretKey;
  15. public function __construct()
  16. {
  17. $this->sdkAppId = config('tencent.trtc.app_id');
  18. $this->sdkSecretKey = config('tencent.trtc.secret_key');
  19. }
  20. // 签名
  21. public function genUserSign($identifier, $expire = 86400 * 180): string
  22. {
  23. try {
  24. $api = new TLSSigAPIv2($this->sdkAppId, $this->sdkSecretKey);
  25. return $api->genUserSig($identifier, $expire);
  26. } catch (\Exception $e) {
  27. echo $e;
  28. }
  29. }
  30. /**
  31. * userbuf签名.
  32. */
  33. public function genSigWithUserBuf($identifier, $expire, $userbuf): string
  34. {
  35. try {
  36. $api = new TLSSigAPIv2($this->sdkAppId, $this->sdkSecretKey);
  37. return $api->genPrivateMapKeyWithStringRoomID($identifier, $expire,'', $userbuf);
  38. } catch (\Exception $e) {
  39. echo $e;
  40. }
  41. }
  42. public function verifySigWithUserBuf($sig, $identifier, $init_time, $expire_time, $userbuf, $error_msg)
  43. {
  44. try {
  45. $api = new TLSSigAPIv2($this->sdkAppId, $this->sdkSecretKey);
  46. return $api->verifySigWithUserBuf($sig, $identifier, $init_time, $expire_time, $userbuf, $error_msg);
  47. } catch (\Exception $e) {
  48. echo $e;
  49. }
  50. }
  51. /**
  52. * 批量用户生成签名.
  53. *
  54. * @return array
  55. */
  56. public function genUserSigns($userIds, $expire)
  57. {
  58. $retData = [];
  59. foreach ($userIds as $id) {
  60. $retData[$id] = $this->genUserSign($id, $expire);
  61. }
  62. return $retData;
  63. }
  64. }