Explorar el Código

Merge branch 'dev-xpy' into dev

xupuyuan hace 9 meses
padre
commit
d73df5586e
Se han modificado 2 ficheros con 112 adiciones y 75 borrados
  1. 30 2
      app/controller/api/user/User.php
  2. 82 73
      app/traits/GzcApiRequest.php

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

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

+ 82 - 73
app/traits/GzcApiRequest.php

@@ -56,88 +56,97 @@ trait GzcApiRequest
56 56
      */
57 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 69
         $data = [
60 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 152
      * 获取TOKEN