GzcApiRequest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\traits;
  4. use think\facade\Log;
  5. /**
  6. * 请求公证处统一请求API
  7. * Trait GzcApiRequest
  8. * @package app\traits
  9. */
  10. trait GzcApiRequest
  11. {
  12. /**
  13. * 发送请求
  14. * @return mixed|string
  15. * @author php迷途小书童
  16. * @created 2021/1/14 14:34
  17. */
  18. public static function request(string $method, string $url, array $params = [], array $options = [])
  19. {
  20. $base_uri = env('GZC.HOSTNAME');
  21. $request_uri = $base_uri.$url;
  22. try {
  23. $data = json_encode($params);
  24. $headerArray =array(
  25. "Content-type:application/json;charset='utf-8'",
  26. "Accept:application/json"
  27. );
  28. if ($url != '/v1/api/third/auth/token') {
  29. $Token = self::getTokenFromServer();
  30. $accessToken = $Token['extendData']['accessToken'];
  31. array_push($headerArray, "token:".$accessToken);
  32. }
  33. Log::info($data);
  34. $curl = curl_init();
  35. curl_setopt($curl, CURLOPT_URL, $request_uri);
  36. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  37. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
  38. curl_setopt($curl, CURLOPT_POST, 1);
  39. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  40. curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
  41. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  42. $output = curl_exec($curl);
  43. curl_close($curl);
  44. return json_decode($output, true);
  45. } catch (\Throwable $e) {
  46. return $e->getMessage();
  47. }
  48. }
  49. /**
  50. * 查询账户余额
  51. * @author php迷途小书童
  52. * @created 2021/1/14 16:34
  53. */
  54. public static function checkAmount($user)
  55. {
  56. $data = [
  57. 'platformNo' => env('GZC.PLATFORM_NO'),
  58. 'appNo' => env('GZC.APP_NO'),
  59. 'conName' => $user['user_name'],
  60. 'conCertType' => 1,
  61. 'conCertNo' => $user['user_card'],
  62. ];
  63. return self::request('POST', '/v1/api/third/in/in30', $data);
  64. }
  65. /**
  66. * 获取TOKEN
  67. * @return mixed|string
  68. * @author php迷途小书童
  69. * @created 2021/1/14 14:38
  70. */
  71. public static function getTokenFromServer()
  72. {
  73. $timestamp = (int)(microtime(true) * 1000);
  74. $appid = env('GZC.APP_ID');
  75. $data = [
  76. 'appid' => $appid,
  77. 'sign' => self::getSignContent($timestamp, $appid),
  78. 'timestamp' => $timestamp
  79. ];
  80. return self::request('POST', '/v1/api/third/auth/token', $data);
  81. }
  82. /**
  83. * 获取签名
  84. * @return string
  85. * @author php迷途小书童
  86. * @created 2020/10/29 9:35
  87. */
  88. public static function getSignContent(int $timestamp, string $appid) {
  89. $appsecret = md5(env('GZC.APP_SECRET'));
  90. $stringToBeSigned = "appid={$appid}&secret={$appsecret}&timestamp={$timestamp}";
  91. return md5($stringToBeSigned);
  92. }
  93. }