| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- declare (strict_types=1);
- namespace app\traits;
- use think\facade\Log;
- /**
- * 请求公证处统一请求API
- * Trait GzcApiRequest
- * @package app\traits
- */
- trait GzcApiRequest
- {
- /**
- * 发送请求
- * @return mixed|string
- * @author php迷途小书童
- * @created 2021/1/14 14:34
- */
- public static function request(string $method, string $url, array $params = [], array $options = [], $type = 1)
- {
- $base_uri = env('GZC.HOSTNAME');
- $request_uri = $base_uri.$url;
- try {
- $data = json_encode($params);
- $headers = [
- "Content-type:application/json;charset='utf-8'",
- "Accept:application/json"
- ];
- if ($url !== '/v1/api/third/auth/token') {
- $Token = self::getTokenFromServer($type);
- if (isset($Token['extendData']['accessToken']) && $Token['extendData']['accessToken']) {
- $headers[] = "token:".$Token['extendData']['accessToken'];
- } else {
- return [
- 'code' => -1,
- 'message' => 'token获取失败',
- 'debug' => [
- 'raw' => $Token,
- 'http_code' => null
- ]
- ];
- }
- }
- if (!empty($options['headers']) && is_array($options['headers'])) {
- $headers = array_merge($headers, $options['headers']);
- }
- Log::info($data);
- $curl = curl_init();
- $methodUpper = strtoupper($method);
- $urlToRequest = $request_uri;
- if ($methodUpper === 'GET' && !empty($params)) {
- $query = http_build_query($params);
- $urlToRequest .= (strpos($urlToRequest, '?') === false ? '?' : '&').$query;
- }
- curl_setopt($curl, CURLOPT_URL, $urlToRequest);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- if (!empty($options['timeout'])) {
- curl_setopt($curl, CURLOPT_TIMEOUT, (int)$options['timeout']);
- }
- if ($methodUpper === 'POST') {
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- } elseif ($methodUpper !== 'GET') {
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $methodUpper);
- if (!empty($params)) {
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- }
- }
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
- $output = curl_exec($curl);
- $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- $curlErrNo = curl_errno($curl);
- $curlErr = $curlErrNo ? curl_error($curl) : null;
- curl_close($curl);
- if ($curlErrNo) {
- return [
- 'code' => -1,
- 'message' => $curlErr ?: '请求错误',
- 'debug' => [
- 'http_code' => $httpCode,
- 'curl_errno' => $curlErrNo,
- 'raw' => $output
- ]
- ];
- }
- if ($httpCode < 200 || $httpCode >= 300) {
- return [
- 'code' => -1,
- 'message' => 'HTTP状态异常',
- 'debug' => [
- 'http_code' => $httpCode,
- 'raw' => $output
- ]
- ];
- }
- $decoded = json_decode($output, true);
- if (is_array($decoded)) {
- $decoded['debug'] = [
- 'http_code' => $httpCode,
- 'raw' => $output
- ];
- return $decoded;
- }
- return [
- 'code' => -1,
- 'message' => '返回解析失败',
- 'debug' => [
- 'http_code' => $httpCode,
- 'raw' => $output
- ]
- ];
- } catch (\Throwable $e) {
- return [
- 'code' => -1,
- 'message' => $e->getMessage(),
- 'debug' => null
- ];
- }
- }
- /**
- * 查询账户余额
- * @author php迷途小书童
- * @created 2021/1/14 16:34
- */
- public static function checkAmount($user)
- {
- $cardType = $user['card_type'] ? $user['card_type'] : 10;
- $cxbData = [
- 'platformNo' => env('GZC.PLATFORM_NO'),
- 'appNo' => env('GZC.APP_NO'),
- 'conName' => $user['user_name'],
- 'conCertType' => $cardType,
- 'conCertNo' => $user['user_card'],
- ];
- Log::info('储蓄保养老金请求参数:' . json_encode($cxbData));
- $cxbResponseData = self::request('POST', '/v1/api/third/in/in30', $cxbData, [], 1);
- Log::info('储蓄保养老金请求返回:' . json_encode($cxbResponseData));
- if (!is_array($cxbResponseData) || ($cxbResponseData['code'] ?? -1) != 0) {
- return [
- 'code' => $cxbResponseData['code'] ?? -1,
- 'message' => $cxbResponseData['message'] ?? '储蓄保查询失败',
- 'debug' => $cxbResponseData['debug'] ?? null,
- ];
- }
- $xfbData = [
- 'platformNo' => env('XINGFUBAO.PLATFORM_NO'),
- 'appNo' => env('XINGFUBAO.APP_NO'),
- 'conName' => $user['user_name'],
- 'conCertType' => $cardType,
- 'conCertNo' => $user['user_card'],
- ];
- Log::info('幸福保养老金请求参数:' . json_encode($xfbData));
- $xfbResponseData = self::request('POST', '/v1/api/third/in/in30', $xfbData, [], 2);
- Log::info('幸福保养老金请求返回:' . json_encode($xfbResponseData));
- if (!is_array($xfbResponseData) || ($xfbResponseData['code'] ?? -1) != 0) {
- return [
- 'code' => $xfbResponseData['code'] ?? -1,
- 'message' => $xfbResponseData['message'] ?? '幸福保查询失败',
- 'debug' => $xfbResponseData['debug'] ?? null,
- ];
- }
- $amountCxb = $cxbResponseData['extendData']['amount'] ?? $cxbResponseData['data']['amount'] ?? $cxbResponseData['amount'] ?? 0;
- $amountXfb = $xfbResponseData['extendData']['amount'] ?? $xfbResponseData['data']['amount'] ?? $xfbResponseData['amount'] ?? 0;
- $amount = $amountCxb + $amountXfb;
- return [
- 'code' => 0,
- 'message' => 'success',
- 'extendData' => [
- 'amount' => $amount
- ],
- 'debug' => [
- 'cxb' => $cxbResponseData['debug'] ?? null,
- 'xfb' => $xfbResponseData['debug'] ?? null,
- ],
- ];
- }
- public function getAmountFromGzc()
- {
- $uid = $this->request->uid();
- $userInfo = Db::name('UserCertification')->where('uid', $uid)->field('user_name,user_card')->find();
- if (empty($userInfo['user_name']) || empty($userInfo['user_card'])) {
- Log::error('[getAmountFromGzc] 用户实名信息缺失', ['uid' => $uid, 'userInfo' => $userInfo]);
- return app('json')->fail('未找到用户实名信息');
- }
- try {
- $res = GzcApiRequest::checkAmount($userInfo);
- } catch (\Throwable $e) {
- Log::error('[getAmountFromGzc] 调用异常', ['uid' => $uid, 'error' => $e->getMessage()]);
- return app('json')->fail('查询失败(第三方异常)');
- }
- if (!is_array($res) || ($res['code'] ?? -1) !== 0) {
- return app('json')->fail($res['message'] ?? '查询失败');
- }
- $amountCents = $res['extendData']['amount'] ?? $res['data']['amount'] ?? $res['amount'] ?? 0;
- $amount = floatval($amountCents) * 0.01;
- $formatted = sprintf('%.2f', $amount);
- return app('json')->success([
- 'amount' => $formatted,
- 'debug' => $res['debug'] ?? null,
- ]);
- }
- /**
- * 获取TOKEN
- * @return mixed|string
- * @author php迷途小书童
- * @created 2021/1/14 14:38
- */
- public static function getTokenFromServer($type = 1)
- {
- $timestamp = (int)(microtime(true) * 1000);
- if ($type == 1) {
- $appid = env('GZC.APP_ID');
- }else{
- $appid = env('XINGFUBAO.APP_ID');
- }
- $data = [
- 'appid' => $appid,
- 'sign' => self::getSignContent($timestamp, $appid, $type),
- 'timestamp' => $timestamp
- ];
- return self::request('POST', '/v1/api/third/auth/token', $data);
- }
- /**
- * 获取签名
- * @return string
- * @author php迷途小书童
- * @created 2020/10/29 9:35
- */
- public static function getSignContent(int $timestamp, string $appid, $type = 1) {
- if ($type == 1) {
- $appsecret = md5(env('GZC.APP_SECRET'));
- }else{
- $appsecret = md5(env('XINGFUBAO.APP_SECRET'));
- }
- $stringToBeSigned = "appid={$appid}&secret={$appsecret}×tamp={$timestamp}";
- return md5($stringToBeSigned);
- }
- }
|