GzcApiRequest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 = [], $type = 1)
  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($type);
  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. $cardType = $user['card_type'];
  57. if ($user['card_type'] == 2) {
  58. $cardType = 10;
  59. }
  60. $cxbData = [
  61. 'platformNo' => env('GZC.PLATFORM_NO'),
  62. 'appNo' => env('GZC.APP_NO'),
  63. 'conName' => $user['user_name'],
  64. 'conCertType' => $cardType,
  65. 'conCertNo' => $user['user_card'],
  66. ];
  67. Log::info('储蓄保养老金请求参数:' . json_encode($cxbData));
  68. $cxbResponseData = self::request('POST', '/v1/api/third/in/in30', $cxbData, [], 1);
  69. Log::info('储蓄保养老金请求返回:' . json_encode($cxbResponseData));
  70. if($cxbResponseData['code'] != 0) {
  71. return $cxbResponseData['message'];
  72. }
  73. $xfbData = [
  74. 'platformNo' => env('XINGFUBAO.PLATFORM_NO'),
  75. 'appNo' => env('XINGFUBAO.APP_NO'),
  76. 'conName' => $user['user_name'],
  77. 'conCertType' => $cardType,
  78. 'conCertNo' => $user['user_card'],
  79. ];
  80. Log::info('幸福保养老金请求参数:' . json_encode($xfbData));
  81. $xfbResponseData = self::request('POST', '/v1/api/third/in/in30', $xfbData, [], 2);
  82. Log::info('幸福保养老金请求返回:' . json_encode($xfbResponseData));
  83. if($xfbResponseData['code'] != 0) {
  84. return $xfbResponseData['message'];
  85. }
  86. $amount = $cxbResponseData['extendData']['amount'] + $xfbResponseData['extendData']['amount'];
  87. return $amount;
  88. }
  89. public function getAmountFromGzc()
  90. {
  91. $uid = $this->request->uid();
  92. $userInfo = Db::name('UserCertification')->where('uid', $uid)->field('user_name,user_card')->find();
  93. if (empty($userInfo['user_name']) || empty($userInfo['user_card'])) {
  94. Log::error('[getAmountFromGzc] 用户实名信息缺失', ['uid' => $uid, 'userInfo' => $userInfo]);
  95. return app('json')->fail('未找到用户实名信息');
  96. }
  97. try {
  98. $res = GzcApiRequest::checkAmount($userInfo);
  99. } catch (\Throwable $e) {
  100. Log::error('[getAmountFromGzc] 调用异常', ['uid' => $uid, 'error' => $e->getMessage()]);
  101. return app('json')->fail('查询失败(第三方异常)');
  102. }
  103. // 提取 amount(兼容多种字段位置)
  104. $amountCents = $res['extendData']['amount'] ?? $res['data']['amount'] ?? $res['amount'] ?? null;
  105. $amount = $amountCents === null ? 0.0 : floatval($amountCents) * 0.01;
  106. $formatted = sprintf('%.2f', $amount);
  107. // 从 checkAmount 返回的 debug(若存在)
  108. $debug = $res['debug'] ?? null;
  109. // 返回 amount 与 debug 给调用方(前端 / 调试)
  110. return app('json')->success([
  111. 'amount' => $formatted,
  112. 'debug' => $debug
  113. ]);
  114. }
  115. /**
  116. * 获取TOKEN
  117. * @return mixed|string
  118. * @author php迷途小书童
  119. * @created 2021/1/14 14:38
  120. */
  121. public static function getTokenFromServer($type = 1)
  122. {
  123. $timestamp = (int)(microtime(true) * 1000);
  124. if ($type == 1) {
  125. $appid = env('GZC.APP_ID');
  126. }else{
  127. $appid = env('XINGFUBAO.APP_ID');
  128. }
  129. $data = [
  130. 'appid' => $appid,
  131. 'sign' => self::getSignContent($timestamp, $appid, $type),
  132. 'timestamp' => $timestamp
  133. ];
  134. return self::request('POST', '/v1/api/third/auth/token', $data);
  135. }
  136. /**
  137. * 获取签名
  138. * @return string
  139. * @author php迷途小书童
  140. * @created 2020/10/29 9:35
  141. */
  142. public static function getSignContent(int $timestamp, string $appid, $type = 1) {
  143. if ($type == 1) {
  144. $appsecret = md5(env('GZC.APP_SECRET'));
  145. }else{
  146. $appsecret = md5(env('XINGFUBAO.APP_SECRET'));
  147. }
  148. $stringToBeSigned = "appid={$appid}&secret={$appsecret}&timestamp={$timestamp}";
  149. return md5($stringToBeSigned);
  150. }
  151. }