SettingService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Author: luzheng.liu
  4. * Time: 2020/12/6 17:25
  5. */
  6. namespace app\common\service;
  7. use app\index\exception\ApiException;
  8. use GuzzleHttp\Client;
  9. use GuzzleHttp\Exception\RequestException;
  10. use think\Exception;
  11. class SettingService {
  12. const TOKEN = 'Bearer a1aw1av25bk1usqjv7r84mw9knpv3sma';
  13. const BASE_URL = 'http://api.webcasting.bizconfstreaming.com/';
  14. public function verifyChannelName(int $channelId,string $channelName) {
  15. $client = new Client();
  16. $res =$client->request('GET', self::BASE_URL.'v1/activities/'.$channelId, [
  17. 'headers' => [
  18. 'Authorization' => self::TOKEN
  19. ]
  20. ]);
  21. $info = json_decode((string)$res->getBody(),true);
  22. if ($channelName !== $info['name']) {
  23. throw new ApiException("频道名称不一致,频道{$channelId}名称为 ".$info['name']);
  24. }
  25. }
  26. public function setAuthUrl(int $channelId, string $redirectUrl) {
  27. // $client = new Client();
  28. // $res =$client->request('Post', self::BASE_URL.'v1/activities/'.$channelId.'/setAuth', [
  29. // 'headers' => [
  30. // 'Authorization' => self::TOKEN
  31. // ],
  32. // 'json' => [
  33. // 'method' => 'CUSTOM_VERIFY',
  34. // 'redirect_url' => $redirectUrl
  35. // ]
  36. // ]);
  37. $curl = curl_init();
  38. $data = [
  39. 'method' => 'CUSTOM_VERIFY',
  40. 'redirect_url' => $redirectUrl
  41. ];
  42. $data = json_encode($data);
  43. curl_setopt_array($curl, array(
  44. CURLOPT_URL => self::BASE_URL.'v1/activities/'.$channelId.'/setAuth',
  45. CURLOPT_RETURNTRANSFER => true,
  46. CURLOPT_ENCODING => '',
  47. CURLOPT_MAXREDIRS => 10,
  48. CURLOPT_TIMEOUT => 0,
  49. CURLOPT_FOLLOWLOCATION => true,
  50. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  51. CURLOPT_CUSTOMREQUEST => 'POST',
  52. CURLOPT_POSTFIELDS => $data,
  53. CURLOPT_HTTPHEADER => array(
  54. 'Authorization: '.self::TOKEN,
  55. 'Content-Type: application/json'
  56. ),
  57. ));
  58. $response = curl_exec($curl);
  59. curl_close($curl);
  60. $info = json_decode((string)$response,true);
  61. if ($info['status'] !== 'y') {
  62. throw new ApiException("设置自定义授权url失败--".(string)$response);
  63. }
  64. }
  65. public function getAuthCode(int $channelId) {
  66. $client = new Client();
  67. $res =$client->request('Post', self::BASE_URL.'v1/activities/'.$channelId.'/getAuthKey', [
  68. 'headers' => [
  69. 'Authorization' => self::TOKEN
  70. ]
  71. ]);
  72. $info = json_decode((string)$res->getBody(),true);
  73. if (empty($info)){
  74. throw new ApiException($info['message']);
  75. }
  76. return $info['authkey'];
  77. }
  78. }