Просмотр исходного кода

feat(user): 新增幸福保账户养老金查询功能

- 在 GzcApiRequest 中新增 getAmountXfb 方法用于查询幸福保账户金额
- 支持从多个字段位置提取金额数据并进行格式化
- 增加对空金额或零金额情况的日志记录与原因分析
- 在用户控制器中调用幸福保接口并与储蓄保金额合并计算
- 返回统一结构包含调试信息便于问题追踪
- 增加异常处理确保接口稳定性
shichen месяцев назад: 9
Родитель
Сommit
56fd5a62c1
2 измененных файлов с 72 добавлено и 1 удалено
  1. 9 1
      app/controller/api/user/User.php
  2. 63 0
      app/traits/GzcApiRequest.php

+ 9 - 1
app/controller/api/user/User.php

@@ -3387,7 +3387,10 @@ class User extends BaseController
3387 3387
         }
3388 3388
 
3389 3389
         try {
3390
+            // 储蓄保
3390 3391
             $res = GzcApiRequest::checkAmount($userInfo);
3392
+            // 幸福保
3393
+            $res2 = GzcApiRequest::getAmountXfb($userInfo);
3391 3394
         } catch (\Throwable $e) {
3392 3395
             Log::error('[getAmountFromGzc] 调用异常', ['uid' => $uid, 'error' => $e->getMessage()]);
3393 3396
             return app('json')->fail('查询失败(第三方异常)');
@@ -3396,7 +3399,12 @@ class User extends BaseController
3396 3399
         // 提取 amount(兼容多种字段位置)
3397 3400
         $amountCents = $res['extendData']['amount'] ?? $res['data']['amount'] ?? $res['amount'] ?? null;
3398 3401
         $amount = $amountCents === null ? 0.0 : floatval($amountCents) * 0.01;
3399
-        $formatted = sprintf('%.2f', $amount);
3402
+
3403
+        // 幸福保
3404
+        $amountCents2 = $res2['extendData']['amount'] ?? $res2['data']['amount'] ?? $res2['amount'] ?? null;
3405
+        $amount2 = $amountCents === null ? 0.0 : floatval($amountCents2) * 0.01;
3406
+
3407
+        $formatted = sprintf('%.2f', $amount + $amount2);
3400 3408
 
3401 3409
         // 从 checkAmount 返回的 debug(若存在)
3402 3410
         $debug = $res['debug'] ?? null;

+ 63 - 0
app/traits/GzcApiRequest.php

@@ -177,4 +177,67 @@ trait GzcApiRequest
177 177
         return md5($stringToBeSigned);
178 178
     }
179 179
 
180
+    /**
181
+     * @param $user
182
+     * @return array|mixed|string
183
+     * @author 史晨
184
+     * @date 2025/10/22 11:05
185
+     * 幸福保账户获取养老金
186
+     */
187
+    public static function getAmountXfb($user)
188
+    {
189
+        $debug = [];
190
+
191
+        $data = [
192
+            'platformNo' => env('XINGFUBAO.PLATFORM_NO'),
193
+            'appNo'      => env('XINGFUBAO.APP_NO'),
194
+            'conName'    => $user['user_name'] ?? '',
195
+            'conCertType'=> 1,
196
+            'conCertNo'  => $user['user_card'] ?? '',
197
+        ];
198
+
199
+        $debug['request'] = $data;
200
+
201
+        try {
202
+            // 记录请求(原有日志可保留或删除)
203
+            Log::info('[checkAmount] request', $data);
204
+
205
+            $res = self::request('POST', '/v1/api/third/in/in30', $data);
206
+
207
+            $debug['raw_response'] = $res;
208
+
209
+            // 尝试解析 amount(以分为单位常见)
210
+            $amountCents = $res['extendData']['amount'] ?? $res['data']['amount'] ?? $res['amount'] ?? null;
211
+            $debug['parsed_amount'] = $amountCents;
212
+
213
+            // 如果没有 amount 字段或为0,附加简单推测
214
+            if ($amountCents === null) {
215
+                $debug['note'] = 'missing_amount';
216
+                Log::warning('[checkAmount] missing amount', ['user' => $user, 'response' => $res]);
217
+            } elseif ($amountCents == 0) {
218
+                $msg = $res['msg'] ?? $res['message'] ?? '';
219
+                $reasons = [];
220
+                if (stripos($msg, '未开户') !== false || stripos($msg, '未实名') !== false) $reasons[] = 'third_party_unopened_or_unverified';
221
+                if (stripos($msg, '无账户') !== false) $reasons[] = 'account_not_found';
222
+                if (stripos($msg, 'token') !== false) $reasons[] = 'token_issue_possible';
223
+                if (empty($reasons)) $reasons[] = 'balance_is_zero';
224
+                $debug['zero_reasons'] = $reasons;
225
+                Log::warning('[checkAmount] amount is 0', ['user' => $user, 'reasons' => $reasons]);
226
+            }
227
+
228
+            // 把 debug 附加到返回(不破坏原始返回结构)
229
+            if (is_array($res)) {
230
+                $res['debug'] = $debug;
231
+                return $res;
232
+            }
233
+
234
+            // 如果不是数组,包装返回
235
+            return ['code' => 200, 'msg' => 'ok', 'extendData' => ['amount' => $amountCents], 'debug' => $debug];
236
+        } catch (\Throwable $e) {
237
+            $debug['exception'] = $e->getMessage();
238
+            Log::error('[checkAmount] exception', ['user' => $user, 'err' => $e->getMessage()]);
239
+            return ['code' => 500, 'msg' => 'exception', 'debug' => $debug];
240
+        }
241
+    }
242
+
180 243
 }