GzcApiRequest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. $headers = [
  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. if (isset($Token['extendData']['accessToken']) && $Token['extendData']['accessToken']) {
  31. $headers[] = "token:".$Token['extendData']['accessToken'];
  32. } else {
  33. return [
  34. 'code' => -1,
  35. 'message' => 'token获取失败',
  36. 'debug' => [
  37. 'raw' => $Token,
  38. 'http_code' => null
  39. ]
  40. ];
  41. }
  42. }
  43. if (!empty($options['headers']) && is_array($options['headers'])) {
  44. $headers = array_merge($headers, $options['headers']);
  45. }
  46. Log::info($data);
  47. $curl = curl_init();
  48. $methodUpper = strtoupper($method);
  49. $urlToRequest = $request_uri;
  50. if ($methodUpper === 'GET' && !empty($params)) {
  51. $query = http_build_query($params);
  52. $urlToRequest .= (strpos($urlToRequest, '?') === false ? '?' : '&').$query;
  53. }
  54. curl_setopt($curl, CURLOPT_URL, $urlToRequest);
  55. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  56. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  57. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  58. if (!empty($options['timeout'])) {
  59. curl_setopt($curl, CURLOPT_TIMEOUT, (int)$options['timeout']);
  60. }
  61. if ($methodUpper === 'POST') {
  62. curl_setopt($curl, CURLOPT_POST, true);
  63. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  64. } elseif ($methodUpper !== 'GET') {
  65. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $methodUpper);
  66. if (!empty($params)) {
  67. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  68. }
  69. }
  70. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  71. $output = curl_exec($curl);
  72. $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  73. $curlErrNo = curl_errno($curl);
  74. $curlErr = $curlErrNo ? curl_error($curl) : null;
  75. curl_close($curl);
  76. if ($curlErrNo) {
  77. return [
  78. 'code' => -1,
  79. 'message' => $curlErr ?: '请求错误',
  80. 'debug' => [
  81. 'http_code' => $httpCode,
  82. 'curl_errno' => $curlErrNo,
  83. 'raw' => $output
  84. ]
  85. ];
  86. }
  87. if ($httpCode < 200 || $httpCode >= 300) {
  88. return [
  89. 'code' => -1,
  90. 'message' => 'HTTP状态异常',
  91. 'debug' => [
  92. 'http_code' => $httpCode,
  93. 'raw' => $output
  94. ]
  95. ];
  96. }
  97. $decoded = json_decode($output, true);
  98. if (is_array($decoded)) {
  99. $decoded['debug'] = [
  100. 'http_code' => $httpCode,
  101. 'raw' => $output
  102. ];
  103. return $decoded;
  104. }
  105. return [
  106. 'code' => -1,
  107. 'message' => '返回解析失败',
  108. 'debug' => [
  109. 'http_code' => $httpCode,
  110. 'raw' => $output
  111. ]
  112. ];
  113. } catch (\Throwable $e) {
  114. return [
  115. 'code' => -1,
  116. 'message' => $e->getMessage(),
  117. 'debug' => null
  118. ];
  119. }
  120. }
  121. /**
  122. * 查询账户余额
  123. * @author php迷途小书童
  124. * @created 2021/1/14 16:34
  125. */
  126. public static function checkAmount($user)
  127. {
  128. $cardType = $user['card_type'] ? $user['card_type'] : 10;
  129. $cxbData = [
  130. 'platformNo' => env('GZC.PLATFORM_NO'),
  131. 'appNo' => env('GZC.APP_NO'),
  132. 'conName' => $user['user_name'],
  133. 'conCertType' => $cardType,
  134. 'conCertNo' => $user['user_card'],
  135. ];
  136. Log::info('储蓄保养老金请求参数:' . json_encode($cxbData));
  137. $cxbResponseData = self::request('POST', '/v1/api/third/in/in30', $cxbData, [], 1);
  138. Log::info('储蓄保养老金请求返回:' . json_encode($cxbResponseData));
  139. if (!is_array($cxbResponseData) || ($cxbResponseData['code'] ?? -1) != 0) {
  140. return [
  141. 'code' => $cxbResponseData['code'] ?? -1,
  142. 'message' => $cxbResponseData['message'] ?? '储蓄保查询失败',
  143. 'debug' => $cxbResponseData['debug'] ?? null,
  144. ];
  145. }
  146. $xfbData = [
  147. 'platformNo' => env('XINGFUBAO.PLATFORM_NO'),
  148. 'appNo' => env('XINGFUBAO.APP_NO'),
  149. 'conName' => $user['user_name'],
  150. 'conCertType' => $cardType,
  151. 'conCertNo' => $user['user_card'],
  152. ];
  153. Log::info('幸福保养老金请求参数:' . json_encode($xfbData));
  154. $xfbResponseData = self::request('POST', '/v1/api/third/in/in30', $xfbData, [], 2);
  155. Log::info('幸福保养老金请求返回:' . json_encode($xfbResponseData));
  156. if (!is_array($xfbResponseData) || ($xfbResponseData['code'] ?? -1) != 0) {
  157. return [
  158. 'code' => $xfbResponseData['code'] ?? -1,
  159. 'message' => $xfbResponseData['message'] ?? '幸福保查询失败',
  160. 'debug' => $xfbResponseData['debug'] ?? null,
  161. ];
  162. }
  163. $amountCxb = $cxbResponseData['extendData']['amount'] ?? $cxbResponseData['data']['amount'] ?? $cxbResponseData['amount'] ?? 0;
  164. $amountXfb = $xfbResponseData['extendData']['amount'] ?? $xfbResponseData['data']['amount'] ?? $xfbResponseData['amount'] ?? 0;
  165. $amount = $amountCxb + $amountXfb;
  166. return [
  167. 'code' => 0,
  168. 'message' => 'success',
  169. 'extendData' => [
  170. 'amount' => $amount
  171. ],
  172. 'debug' => [
  173. 'cxb' => $cxbResponseData['debug'] ?? null,
  174. 'xfb' => $xfbResponseData['debug'] ?? null,
  175. ],
  176. ];
  177. }
  178. public function getAmountFromGzc()
  179. {
  180. $uid = $this->request->uid();
  181. $userInfo = Db::name('UserCertification')->where('uid', $uid)->field('user_name,user_card')->find();
  182. if (empty($userInfo['user_name']) || empty($userInfo['user_card'])) {
  183. Log::error('[getAmountFromGzc] 用户实名信息缺失', ['uid' => $uid, 'userInfo' => $userInfo]);
  184. return app('json')->fail('未找到用户实名信息');
  185. }
  186. try {
  187. $res = GzcApiRequest::checkAmount($userInfo);
  188. } catch (\Throwable $e) {
  189. Log::error('[getAmountFromGzc] 调用异常', ['uid' => $uid, 'error' => $e->getMessage()]);
  190. return app('json')->fail('查询失败(第三方异常)');
  191. }
  192. if (!is_array($res) || ($res['code'] ?? -1) !== 0) {
  193. return app('json')->fail($res['message'] ?? '查询失败');
  194. }
  195. $amountCents = $res['extendData']['amount'] ?? $res['data']['amount'] ?? $res['amount'] ?? 0;
  196. $amount = floatval($amountCents) * 0.01;
  197. $formatted = sprintf('%.2f', $amount);
  198. return app('json')->success([
  199. 'amount' => $formatted,
  200. 'debug' => $res['debug'] ?? null,
  201. ]);
  202. }
  203. /**
  204. * 获取TOKEN
  205. * @return mixed|string
  206. * @author php迷途小书童
  207. * @created 2021/1/14 14:38
  208. */
  209. public static function getTokenFromServer($type = 1)
  210. {
  211. $timestamp = (int)(microtime(true) * 1000);
  212. if ($type == 1) {
  213. $appid = env('GZC.APP_ID');
  214. }else{
  215. $appid = env('XINGFUBAO.APP_ID');
  216. }
  217. $data = [
  218. 'appid' => $appid,
  219. 'sign' => self::getSignContent($timestamp, $appid, $type),
  220. 'timestamp' => $timestamp
  221. ];
  222. return self::request('POST', '/v1/api/third/auth/token', $data);
  223. }
  224. /**
  225. * 获取签名
  226. * @return string
  227. * @author php迷途小书童
  228. * @created 2020/10/29 9:35
  229. */
  230. public static function getSignContent(int $timestamp, string $appid, $type = 1) {
  231. if ($type == 1) {
  232. $appsecret = md5(env('GZC.APP_SECRET'));
  233. }else{
  234. $appsecret = md5(env('XINGFUBAO.APP_SECRET'));
  235. }
  236. $stringToBeSigned = "appid={$appid}&secret={$appsecret}&timestamp={$timestamp}";
  237. return md5($stringToBeSigned);
  238. }
  239. }