TonGiftsService.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Game\Services;
  3. use App\Util;
  4. use GuzzleHttp\Client;
  5. class TonGiftsService
  6. {
  7. protected $client;
  8. protected $baseUrl;
  9. public $secretKey;
  10. public $id;
  11. public function __construct()
  12. {
  13. $this->client = new Client();
  14. $this->baseUrl = "https://HOST";
  15. $this->secretKey = "";
  16. $this->id = "";
  17. }
  18. public function register($name,$email)
  19. {
  20. return $this->postRequest('/api/merchants/register',['name' => $name,'email' => $email]);
  21. }
  22. public function updateTask($userIds,$taskId,$status){
  23. $data = [
  24. 'userIds' => $userIds,
  25. 'taskId' => $taskId,
  26. 'status' => $status
  27. ];
  28. return $this->postRequest('/api/open/updateTask',$data);
  29. }
  30. // 发送POST请求
  31. protected function postRequest($endpoint, $data,$needSignature = true)
  32. {
  33. if($needSignature){
  34. if(!isset($data['t'])){
  35. $data['t'] = time();
  36. }
  37. $data['sign'] = $this->_generateSignature($data);
  38. $data['k'] = $this->id;
  39. }
  40. $post = json_encode($data);
  41. $headers = ['Content-Type' => 'application/json'];
  42. $rs = Util::curlPost2($this->baseUrl . $endpoint,$post,true,$headers);
  43. Util::WriteLog('TonGIfts', $post);
  44. Util::WriteLog('TonGIfts', $rs);
  45. return json_decode($rs, true);
  46. }
  47. private function _generateSignature($params)
  48. {
  49. ksort($params);
  50. $queryString = '';
  51. foreach ($params as $key => $value) {
  52. if(is_array($value)){
  53. $value = json_encode($value);
  54. }
  55. $queryString .= $key . '=' . $value . '&';
  56. }
  57. $queryString = rtrim($queryString, '&');
  58. $secretKey = $this->secretKey;
  59. return hash_hmac('sha256', $queryString, $secretKey);
  60. }
  61. }