xupuyuan месяцев назад: 9
Родитель
Сommit
1e0e9f9d1a
2 измененных файлов с 112 добавлено и 75 удалено
  1. 30 2
      app/controller/api/user/User.php
  2. 82 73
      app/traits/GzcApiRequest.php

+ 30 - 2
app/controller/api/user/User.php

@@ -3370,11 +3370,39 @@ class User extends BaseController
3370
 
3370
 
3371
     public function getAmountFromGzc()
3371
     public function getAmountFromGzc()
3372
     {
3372
     {
3373
+//        $uid = $this->request->uid();
3374
+//        $userInfo = Db::name('UserCertification')->where('uid', $uid)->field('user_name,user_card')->find();
3375
+//        $res = GzcApiRequest::checkAmount($userInfo);
3376
+//
3377
+//        return app('json')->success(['amount' => sprintf("%01.2f", $res['extendData']['amount'] * 0.01)]);
3373
         $uid = $this->request->uid();
3378
         $uid = $this->request->uid();
3374
         $userInfo = Db::name('UserCertification')->where('uid', $uid)->field('user_name,user_card')->find();
3379
         $userInfo = Db::name('UserCertification')->where('uid', $uid)->field('user_name,user_card')->find();
3375
-        $res = GzcApiRequest::checkAmount($userInfo);
3376
 
3380
 
3377
-        return app('json')->success(['amount' => sprintf("%01.2f", $res['extendData']['amount'] * 0.01)]);
3381
+        if (empty($userInfo['user_name']) || empty($userInfo['user_card'])) {
3382
+            Log::error('[getAmountFromGzc] 用户实名信息缺失', ['uid' => $uid, 'userInfo' => $userInfo]);
3383
+            return app('json')->fail('未找到用户实名信息');
3384
+        }
3385
+
3386
+        try {
3387
+            $res = GzcApiRequest::checkAmount($userInfo);
3388
+        } catch (\Throwable $e) {
3389
+            Log::error('[getAmountFromGzc] 调用异常', ['uid' => $uid, 'error' => $e->getMessage()]);
3390
+            return app('json')->fail('查询失败(第三方异常)');
3391
+        }
3392
+
3393
+        // 提取 amount(兼容多种字段位置)
3394
+        $amountCents = $res['extendData']['amount'] ?? $res['data']['amount'] ?? $res['amount'] ?? null;
3395
+        $amount = $amountCents === null ? 0.0 : floatval($amountCents) * 0.01;
3396
+        $formatted = sprintf('%.2f', $amount);
3397
+
3398
+        // 从 checkAmount 返回的 debug(若存在)
3399
+        $debug = $res['debug'] ?? null;
3400
+
3401
+        // 返回 amount 与 debug 给调用方(前端 / 调试)
3402
+        return app('json')->success([
3403
+            'amount' => $formatted,
3404
+            'debug'  => $debug
3405
+        ]);
3378
     }
3406
     }
3379
 
3407
 
3380
     public function check_user_annual_fee()
3408
     public function check_user_annual_fee()

+ 82 - 73
app/traits/GzcApiRequest.php

@@ -56,88 +56,97 @@ trait GzcApiRequest
56
      */
56
      */
57
     public static function checkAmount($user)
57
     public static function checkAmount($user)
58
     {
58
     {
59
+//        $data = [
60
+//            'platformNo' => env('GZC.PLATFORM_NO'),
61
+//            'appNo' => env('GZC.APP_NO'),
62
+//            'conName' => $user['user_name'],
63
+//            'conCertType' => 1,
64
+//            'conCertNo' => $user['user_card'],
65
+//        ];
66
+//        return self::request('POST', '/v1/api/third/in/in30', $data);
67
+        $debug = [];
68
+
59
         $data = [
69
         $data = [
60
             'platformNo' => env('GZC.PLATFORM_NO'),
70
             'platformNo' => env('GZC.PLATFORM_NO'),
61
-            'appNo' => env('GZC.APP_NO'),
62
-            'conName' => $user['user_name'],
63
-            'conCertType' => 1,
64
-            'conCertNo' => $user['user_card'],
71
+            'appNo'      => env('GZC.APP_NO'),
72
+            'conName'    => $user['user_name'] ?? '',
73
+            'conCertType'=> 1,
74
+            'conCertNo'  => $user['user_card'] ?? '',
65
         ];
75
         ];
66
-        foreach (['conName', 'conCertNo'] as $key) {
67
-            if (empty($data[$key])) {
68
-                Log::error('[checkAmount] 参数缺失', [
69
-                    'missing_field' => $key,
70
-                    'user' => $user,
71
-                    'data' => $data
72
-                ]);
76
+
77
+        $debug['request'] = $data;
78
+
79
+        try {
80
+            // 记录请求(原有日志可保留或删除)
81
+            Log::info('[checkAmount] request', $data);
82
+
83
+            $res = self::request('POST', '/v1/api/third/in/in30', $data);
84
+
85
+            $debug['raw_response'] = $res;
86
+
87
+            // 尝试解析 amount(以分为单位常见)
88
+            $amountCents = $res['extendData']['amount'] ?? $res['data']['amount'] ?? $res['amount'] ?? null;
89
+            $debug['parsed_amount'] = $amountCents;
90
+
91
+            // 如果没有 amount 字段或为0,附加简单推测
92
+            if ($amountCents === null) {
93
+                $debug['note'] = 'missing_amount';
94
+                Log::warning('[checkAmount] missing amount', ['user' => $user, 'response' => $res]);
95
+            } elseif ($amountCents == 0) {
96
+                $msg = $res['msg'] ?? $res['message'] ?? '';
97
+                $reasons = [];
98
+                if (stripos($msg, '未开户') !== false || stripos($msg, '未实名') !== false) $reasons[] = 'third_party_unopened_or_unverified';
99
+                if (stripos($msg, '无账户') !== false) $reasons[] = 'account_not_found';
100
+                if (stripos($msg, 'token') !== false) $reasons[] = 'token_issue_possible';
101
+                if (empty($reasons)) $reasons[] = 'balance_is_zero';
102
+                $debug['zero_reasons'] = $reasons;
103
+                Log::warning('[checkAmount] amount is 0', ['user' => $user, 'reasons' => $reasons]);
73
             }
104
             }
105
+
106
+            // 把 debug 附加到返回(不破坏原始返回结构)
107
+            if (is_array($res)) {
108
+                $res['debug'] = $debug;
109
+                return $res;
110
+            }
111
+
112
+            // 如果不是数组,包装返回
113
+            return ['code' => 200, 'msg' => 'ok', 'extendData' => ['amount' => $amountCents], 'debug' => $debug];
114
+        } catch (\Throwable $e) {
115
+            $debug['exception'] = $e->getMessage();
116
+            Log::error('[checkAmount] exception', ['user' => $user, 'err' => $e->getMessage()]);
117
+            return ['code' => 500, 'msg' => 'exception', 'debug' => $debug];
74
         }
118
         }
75
-        $tokenData = self::getTokenFromServer();
76
-        $token = $tokenData['extendData']['accessToken'] ?? null;
77
-        if (empty($token)) {
78
-            Log::error('[checkAmount] Token 获取失败', [
79
-                'tokenData' => $tokenData,
80
-                'user' => $user
81
-            ]);
82
-        } else {
83
-            Log::info('[checkAmount] 成功获取 Token', ['token' => $token]);
84
-        }
85
-        Log::info('[checkAmount] 请求开始', [
86
-            'url' => '/v1/api/third/in/in30',
87
-            'data' => $data,
88
-            'user' => $user
89
-        ]);
90
-        $response = self::request('POST', '/v1/api/third/in/in30', $data);
91
-        Log::info('[checkAmount] 接口返回', [
92
-            'response' => $response
93
-        ]);
94
-        $code = $response['code'] ?? null;
95
-        $msg  = $response['msg'] ?? '';
96
-        $amount = $response['data']['amount'] ?? null;
97
-        if ($code !== 200) {
98
-            Log::error('[checkAmount] 接口返回异常状态码', [
99
-                'code' => $code,
100
-                'msg' => $msg,
101
-                'user' => $user,
102
-                'response' => $response
103
-            ]);
119
+    }
120
+    public function getAmountFromGzc()
121
+    {
122
+        $uid = $this->request->uid();
123
+        $userInfo = Db::name('UserCertification')->where('uid', $uid)->field('user_name,user_card')->find();
124
+
125
+        if (empty($userInfo['user_name']) || empty($userInfo['user_card'])) {
126
+            Log::error('[getAmountFromGzc] 用户实名信息缺失', ['uid' => $uid, 'userInfo' => $userInfo]);
127
+            return app('json')->fail('未找到用户实名信息');
104
         }
128
         }
105
-        if (!isset($response['data']['amount'])) {
106
-            Log::error('[checkAmount] 响应结构异常,缺少金额字段', [
107
-                'user' => $user,
108
-                'response' => $response
109
-            ]);
129
+
130
+        try {
131
+            $res = GzcApiRequest::checkAmount($userInfo);
132
+        } catch (\Throwable $e) {
133
+            Log::error('[getAmountFromGzc] 调用异常', ['uid' => $uid, 'error' => $e->getMessage()]);
134
+            return app('json')->fail('查询失败(第三方异常)');
110
         }
135
         }
111
-        // 情况三:金额为 0,分析可能原因
112
-        if ($amount === 0) {
113
-            $possibleReasons = [];
114
-            if (empty($token)) {
115
-                $possibleReasons[] = 'Token 获取失败或未附带';
116
-            }
117
 
136
 
118
-            if (empty($data['conCertNo']) || empty($data['conName'])) {
119
-                $possibleReasons[] = '用户姓名或身份证号为空';
120
-            }
137
+        // 提取 amount(兼容多种字段位置)
138
+        $amountCents = $res['extendData']['amount'] ?? $res['data']['amount'] ?? $res['amount'] ?? null;
139
+        $amount = $amountCents === null ? 0.0 : floatval($amountCents) * 0.01;
140
+        $formatted = sprintf('%.2f', $amount);
121
 
141
 
122
-            if (stripos($msg, '未开户') !== false || stripos($msg, '未实名') !== false) {
123
-                $possibleReasons[] = '用户未开户或未实名';
124
-            }
125
-            if (stripos($msg, '无账户') !== false) {
126
-                $possibleReasons[] = '第三方系统中不存在此账户';
127
-            }
128
-            if (stripos($msg, 'success') !== false && $amount === 0) {
129
-                $possibleReasons[] = '接口返回成功但余额确实为0(正常情况)';
130
-            }
131
-            Log::warning('[checkAmount] 返回金额为0,可能原因如下', [
132
-                'user' => $user,
133
-                'params' => $data,
134
-                'response' => $response,
135
-                'token' => $token,
136
-                'possible_reasons' => $possibleReasons
137
-            ]);
138
-        }
139
-        return $response;
140
-//        return self::request('POST', '/v1/api/third/in/in30', $data);
142
+        // 从 checkAmount 返回的 debug(若存在)
143
+        $debug = $res['debug'] ?? null;
144
+
145
+        // 返回 amount 与 debug 给调用方(前端 / 调试)
146
+        return app('json')->success([
147
+            'amount' => $formatted,
148
+            'debug'  => $debug
149
+        ]);
141
     }
150
     }
142
     /**
151
     /**
143
      * 获取TOKEN
152
      * 获取TOKEN