| 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 = [])
- {
- $base_uri = env('GZC.HOSTNAME');
- $request_uri = $base_uri.$url;
- try {
- $data = json_encode($params);
- $headerArray =array(
- "Content-type:application/json;charset='utf-8'",
- "Accept:application/json"
- );
- if ($url != '/v1/api/third/auth/token') {
- $Token = self::getTokenFromServer();
- $accessToken = $Token['extendData']['accessToken'];
- array_push($headerArray, "token:".$accessToken);
- }
- Log::info($data);
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $request_uri);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($curl);
- curl_close($curl);
- return json_decode($output, true);
- } catch (\Throwable $e) {
- return $e->getMessage();
- }
- }
- /**
- * 查询账户余额
- * @author php迷途小书童
- * @created 2021/1/14 16:34
- */
- public static function checkAmount($user)
- {
- // $data = [
- // 'platformNo' => env('GZC.PLATFORM_NO'),
- // 'appNo' => env('GZC.APP_NO'),
- // 'conName' => $user['user_name'],
- // 'conCertType' => 1,
- // 'conCertNo' => $user['user_card'],
- // ];
- // return self::request('POST', '/v1/api/third/in/in30', $data);
- $debug = [];
- $data = [
- 'platformNo' => env('GZC.PLATFORM_NO'),
- 'appNo' => env('GZC.APP_NO'),
- 'conName' => $user['user_name'] ?? '',
- 'conCertType'=> 1,
- 'conCertNo' => $user['user_card'] ?? '',
- ];
- $debug['request'] = $data;
- $res1 = self::getAmount($user,$data);
- return $res1;
- }
- private static function getAmount($user,$data)
- {
- try {
- // 记录请求(原有日志可保留或删除)
- Log::info('[checkAmount] request', $data);
- $res = self::request('POST', '/v1/api/third/in/in30', $data);
- $debug['raw_response'] = $res;
- // 尝试解析 amount(以分为单位常见)
- $amountCents = $res['extendData']['amount'] ?? $res['data']['amount'] ?? $res['amount'] ?? null;
- $debug['parsed_amount'] = $amountCents;
- // 如果没有 amount 字段或为0,附加简单推测
- if ($amountCents === null) {
- $debug['note'] = 'missing_amount';
- Log::warning('[checkAmount] missing amount', ['user' => $user, 'response' => $res]);
- } elseif ($amountCents == 0) {
- $msg = $res['msg'] ?? $res['message'] ?? '';
- $reasons = [];
- if (stripos($msg, '未开户') !== false || stripos($msg, '未实名') !== false) $reasons[] = 'third_party_unopened_or_unverified';
- if (stripos($msg, '无账户') !== false) $reasons[] = 'account_not_found';
- if (stripos($msg, 'token') !== false) $reasons[] = 'token_issue_possible';
- if (empty($reasons)) $reasons[] = 'balance_is_zero';
- $debug['zero_reasons'] = $reasons;
- Log::warning('[checkAmount] amount is 0', ['user' => $user, 'reasons' => $reasons]);
- }
- // 把 debug 附加到返回(不破坏原始返回结构)
- if (is_array($res)) {
- $res['debug'] = $debug;
- return $res;
- }
- // 如果不是数组,包装返回
- return ['code' => 200, 'msg' => 'ok', 'extendData' => ['amount' => $amountCents], 'debug' => $debug];
- } catch (\Throwable $e) {
- $debug['exception'] = $e->getMessage();
- Log::error('[checkAmount] exception', ['user' => $user, 'err' => $e->getMessage()]);
- return ['code' => 500, 'msg' => 'exception', 'debug' => $debug];
- }
- }
- 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('查询失败(第三方异常)');
- }
- // 提取 amount(兼容多种字段位置)
- $amountCents = $res['extendData']['amount'] ?? $res['data']['amount'] ?? $res['amount'] ?? null;
- $amount = $amountCents === null ? 0.0 : floatval($amountCents) * 0.01;
- $formatted = sprintf('%.2f', $amount);
- // 从 checkAmount 返回的 debug(若存在)
- $debug = $res['debug'] ?? null;
- // 返回 amount 与 debug 给调用方(前端 / 调试)
- return app('json')->success([
- 'amount' => $formatted,
- 'debug' => $debug
- ]);
- }
- /**
- * 获取TOKEN
- * @return mixed|string
- * @author php迷途小书童
- * @created 2021/1/14 14:38
- */
- public static function getTokenFromServer()
- {
- $timestamp = (int)(microtime(true) * 1000);
- $appid = env('GZC.APP_ID');
- $data = [
- 'appid' => $appid,
- 'sign' => self::getSignContent($timestamp, $appid),
- '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) {
- $appsecret = md5(env('GZC.APP_SECRET'));
- $stringToBeSigned = "appid={$appid}&secret={$appsecret}×tamp={$timestamp}";
- return md5($stringToBeSigned);
- }
- /**
- * @param $user
- * @return array|mixed|string
- * @author 史晨
- * @date 2025/10/22 11:05
- * 幸福保账户获取养老金
- */
- public static function getAmountXfb($user)
- {
- $debug = [];
- $data = [
- 'platformNo' => env('XINGFUBAO.PLATFORM_NO'),
- 'appNo' => env('XINGFUBAO.APP_NO'),
- 'conName' => $user['user_name'] ?? '',
- 'conCertType'=> 1,
- 'conCertNo' => $user['user_card'] ?? '',
- ];
- $debug['request'] = $data;
- try {
- // 记录请求(原有日志可保留或删除)
- Log::info('[checkAmount] request', $data);
- $res = self::request('POST', '/v1/api/third/in/in30', $data);
- $debug['raw_response'] = $res;
- // 尝试解析 amount(以分为单位常见)
- $amountCents = $res['extendData']['amount'] ?? $res['data']['amount'] ?? $res['amount'] ?? null;
- $debug['parsed_amount'] = $amountCents;
- // 如果没有 amount 字段或为0,附加简单推测
- if ($amountCents === null) {
- $debug['note'] = 'missing_amount';
- Log::warning('[checkAmount] missing amount', ['user' => $user, 'response' => $res]);
- } elseif ($amountCents == 0) {
- $msg = $res['msg'] ?? $res['message'] ?? '';
- $reasons = [];
- if (stripos($msg, '未开户') !== false || stripos($msg, '未实名') !== false) $reasons[] = 'third_party_unopened_or_unverified';
- if (stripos($msg, '无账户') !== false) $reasons[] = 'account_not_found';
- if (stripos($msg, 'token') !== false) $reasons[] = 'token_issue_possible';
- if (empty($reasons)) $reasons[] = 'balance_is_zero';
- $debug['zero_reasons'] = $reasons;
- Log::warning('[checkAmount] amount is 0', ['user' => $user, 'reasons' => $reasons]);
- }
- // 把 debug 附加到返回(不破坏原始返回结构)
- if (is_array($res)) {
- $res['debug'] = $debug;
- return $res;
- }
- // 如果不是数组,包装返回
- return ['code' => 200, 'msg' => 'ok', 'extendData' => ['amount' => $amountCents], 'debug' => $debug];
- } catch (\Throwable $e) {
- $debug['exception'] = $e->getMessage();
- Log::error('[checkAmount] exception', ['user' => $user, 'err' => $e->getMessage()]);
- return ['code' => 500, 'msg' => 'exception', 'debug' => $debug];
- }
- }
- }
|