SettingService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.mudu.tv/';
  14. public function verifyChannelName(int $channelId, string $channelName) {
  15. // $client = new Client();
  16. // $res = $client->request('GET', 'http://api.mudu.tv/v1/activities/' . $channelId, [
  17. // 'headers' => [
  18. // 'Authorization' => self::TOKEN
  19. // ]
  20. // ]);
  21. $curl = curl_init();
  22. curl_setopt_array($curl, [
  23. CURLOPT_URL => 'http://api.mudu.tv/v1/activities/'.$channelId,
  24. CURLOPT_RETURNTRANSFER => true,
  25. CURLOPT_ENCODING => '',
  26. CURLOPT_MAXREDIRS => 10,
  27. CURLOPT_TIMEOUT => 0,
  28. CURLOPT_FOLLOWLOCATION => true,
  29. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  30. CURLOPT_CUSTOMREQUEST => 'GET',
  31. CURLOPT_HTTPHEADER => [
  32. 'Authorization: '.self::TOKEN,
  33. 'Content-Type: application/json'
  34. ],
  35. ]);
  36. $response = curl_exec($curl);
  37. curl_close($curl);
  38. $info = json_decode($response, true);
  39. if ($channelName !== $info['name']) {
  40. throw new ApiException("频道名称不一致,频道{$channelId}名称为 " . $info['name']);
  41. }
  42. }
  43. public function setAuthUrl(int $channelId, string $redirectUrl) {
  44. // $client = new Client();
  45. // $res =$client->request('Post', self::BASE_URL.'v1/activities/'.$channelId.'/setAuth', [
  46. // 'headers' => [
  47. // 'Authorization' => self::TOKEN
  48. // ],
  49. // 'json' => [
  50. // 'method' => 'CUSTOM_VERIFY',
  51. // 'redirect_url' => $redirectUrl
  52. // ]
  53. // ]);
  54. $curl = curl_init();
  55. $data = [
  56. 'method' => 'CUSTOM_VERIFY',
  57. 'redirect_url' => $redirectUrl
  58. ];
  59. $data = json_encode($data);
  60. curl_setopt_array($curl, [
  61. CURLOPT_URL => 'http://api.mudu.tv/v1/activities/' . $channelId . '/setAuth',
  62. CURLOPT_RETURNTRANSFER => true,
  63. CURLOPT_ENCODING => '',
  64. CURLOPT_MAXREDIRS => 10,
  65. CURLOPT_TIMEOUT => 0,
  66. CURLOPT_FOLLOWLOCATION => true,
  67. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  68. CURLOPT_CUSTOMREQUEST => 'POST',
  69. CURLOPT_POSTFIELDS => $data,
  70. CURLOPT_HTTPHEADER => [
  71. "Authorization: ".self::TOKEN,
  72. 'Content-Type: application/json',
  73. ],
  74. ]);
  75. $response = curl_exec($curl);
  76. curl_close($curl);
  77. $info = json_decode($response, true);
  78. if ($info['status'] !== 'y') {
  79. throw new ApiException("设置自定义授权url失败--" . (string)$response);
  80. }
  81. }
  82. public function getAuthCode(int $channelId) {
  83. // $client = new Client();
  84. // $res = $client->request('Post', self::BASE_URL . 'v1/activities/' . $channelId . '/getAuthKey', [
  85. // 'headers' => [
  86. // 'Authorization' => self::TOKEN
  87. // ]
  88. // ]);
  89. $curl = curl_init();
  90. curl_setopt_array($curl, [
  91. CURLOPT_URL => 'http://api.mudu.tv/v1/activities/' . $channelId . '/getAuthKey',
  92. CURLOPT_RETURNTRANSFER => true,
  93. CURLOPT_ENCODING => '',
  94. CURLOPT_MAXREDIRS => 10,
  95. CURLOPT_TIMEOUT => 0,
  96. CURLOPT_FOLLOWLOCATION => true,
  97. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  98. CURLOPT_CUSTOMREQUEST => 'POST',
  99. CURLOPT_HTTPHEADER => [
  100. 'Authorization: '.self::TOKEN
  101. ],
  102. ]);
  103. $response = curl_exec($curl);
  104. curl_close($curl);
  105. $info = json_decode((string)$response, true);
  106. if (empty($info)) {
  107. throw new ApiException($info['message']);
  108. }
  109. return $info['authkey'];
  110. }
  111. }