SettingService.php 4.2 KB

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