ApiService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Services\Vendor\GeTui;
  3. use App\Services\Service;
  4. use Illuminate\Support\Carbon;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\Redis;
  7. use Ixudra\Curl\Facades\Curl;
  8. class ApiService extends Service
  9. {
  10. /**
  11. * @var string 域名
  12. */
  13. private $host = "https://restapi.getui.com";
  14. private $auth_token;
  15. /**
  16. * @var string appid
  17. */
  18. private $appid;
  19. /**
  20. * @var string
  21. */
  22. private $appkey;
  23. /**
  24. * @var string
  25. */
  26. private $mastersecret;
  27. /**
  28. * @var array
  29. */
  30. private $result;
  31. public function __construct()
  32. {
  33. $this->appid = config('getui.appid');
  34. $this->appkey = config('getui.appkey');
  35. $this->mastersecret = config('getui.mastersecret');
  36. }
  37. /**
  38. * 设置appid
  39. * @param string $appid
  40. */
  41. public function setAppid(string $appid)
  42. {
  43. $this->appid = $appid;
  44. }
  45. /**
  46. * 设置appkey
  47. * @param string $appkey
  48. */
  49. public function setAppkey(string $appkey)
  50. {
  51. $this->appkey = $appkey;
  52. }
  53. /**
  54. * @param string $cid
  55. * @param string $alias
  56. * @return bool
  57. */
  58. public function bindAlias(string $cid, string $alias)
  59. {
  60. $this->authSign();
  61. $api = "{$this->host}/v1/{$this->appid}/bind_alias";
  62. $data = array(
  63. 'alias_list' => [
  64. [
  65. 'cid' => $cid,
  66. 'alias' => $alias,
  67. ],
  68. ],
  69. );
  70. $result = Curl::to($api)->withHeader("authtoken:{$this->auth_token}")->asJson(true)->withData($data)->post();
  71. $this->setResult($result['result']);
  72. if ('ok' == $this->result) {
  73. return true;
  74. }
  75. }
  76. /**
  77. * 鉴权
  78. */
  79. public function authSign()
  80. {
  81. if (!$this->auth_token = Cache::get("gt:authsign:{$this->appid}", false)) {
  82. $api = "{$this->host}/v1/{$this->appid}/auth_sign";
  83. $data = array(
  84. 'timestamp' => micro_time(),
  85. 'appkey' => $this->appkey,
  86. );
  87. $data['sign'] = $this->sign($data);
  88. $result = Curl::to($api)->asJson(true)->withData($data)->post();
  89. $this->setResult($result['result']);
  90. if ('ok' == $this->result) {
  91. Cache::put(
  92. "gt:authsign:{$this->appid}",
  93. $result['auth_token'],
  94. Carbon::createFromTimestampMs($result['expire_time'])
  95. );
  96. $this->auth_token = $result['auth_token'];
  97. }
  98. }
  99. Redis::set("gt:authsign", $this->auth_token);
  100. }
  101. /**
  102. * 计算sign值
  103. * @param array $data
  104. * @return string
  105. */
  106. private function sign(array $data): string
  107. {
  108. return hash('sha256', $data['appkey'] . $data['timestamp'] . $this->mastersecret);
  109. }
  110. public function setResult($result)
  111. {
  112. $this->result = $result;
  113. }
  114. public function pushSingle()
  115. {
  116. $this->authSign();
  117. $api = "{$this->host}/v1/{$this->appid}/push_single";
  118. $data = array(
  119. 'message' => [
  120. 'appkey' => $this->appkey,
  121. 'is_offline' => true,
  122. 'offline_expire_time' => 10000000,
  123. 'msgtype' => 'notification',
  124. ],
  125. 'notification' => [
  126. 'style' => [
  127. 'type' => 1,
  128. 'text' => '请填写通知内容',
  129. 'title' => '请填写通知标题',
  130. ],
  131. 'transmission_type' => true,
  132. 'transmission_content' => '透传内容',
  133. ],
  134. 'cid' => '256d2ee813a80a824326641d352f7ab6',
  135. 'requestid' => uniqid(),
  136. );
  137. dump($data, $this->auth_token);
  138. $result = Curl::to($api)->withHeader("authtoken:{$this->auth_token}")->asJson(true)->withData($data)->post();
  139. $this->setResult($result['result']);
  140. dump($result);
  141. }
  142. }