Преглед изворни кода

Merge branch 'dev' of shop/php into master

shishichen пре 9 месеци
родитељ
комит
7311ce2e6d
2 измењених фајлова са 45 додато и 165 уклоњено
  1. 5 32
      app/controller/api/user/User.php
  2. 40 133
      app/traits/GzcApiRequest.php

+ 5 - 32
app/controller/api/user/User.php

@@ -3373,40 +3373,13 @@ 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)]);
3381 3376
         $uid = $this->request->uid();
3382
-        $userInfo = Db::name('UserCertification')->where('uid', $uid)->field('user_name,user_card')->find();
3383
-
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
-            // 储蓄保
3391
-            $res = GzcApiRequest::checkAmount($userInfo);
3392
-        } catch (\Throwable $e) {
3393
-            Log::error('[getAmountFromGzc] 调用异常', ['uid' => $uid, 'error' => $e->getMessage()]);
3394
-            return app('json')->fail('查询失败(第三方异常)');
3377
+        $userInfo = Db::name('UserCertification')->where('uid', $uid)->field('user_name,user_card,card_type')->find();
3378
+        $res = GzcApiRequest::checkAmount($userInfo);
3379
+        if(is_string($res)) {
3380
+            return app('json')->fail($res);
3395 3381
         }
3396
-
3397
-        // 提取 amount(兼容多种字段位置)
3398
-        $amountCents = $res['extendData']['amount'] ?? $res['data']['amount'] ?? $res['amount'] ?? null;
3399
-        $amount = $amountCents === null ? 0.0 : floatval($amountCents) * 0.01;
3400
-        $formatted = sprintf('%.2f', $amount);
3401
-
3402
-        // 从 checkAmount 返回的 debug(若存在)
3403
-        $debug = $res['debug'] ?? null;
3404
-
3405
-        // 返回 amount 与 debug 给调用方(前端 / 调试)
3406
-        return app('json')->success([
3407
-            'amount' => $formatted,
3408
-            'debug'  => $debug
3409
-        ]);
3382
+        return app('json')->success(['amount' => sprintf("%01.2f", $res * 0.01)]);
3410 3383
     }
3411 3384
 
3412 3385
     public function check_user_annual_fee()

+ 40 - 133
app/traits/GzcApiRequest.php

@@ -18,7 +18,7 @@ trait GzcApiRequest
18 18
      * @author php迷途小书童
19 19
      * @created 2021/1/14 14:34
20 20
      */
21
-    public static function request(string $method, string $url, array $params = [], array $options = [])
21
+    public static function request(string $method, string $url, array $params = [], array $options = [], $type = 1)
22 22
     {
23 23
         $base_uri = env('GZC.HOSTNAME');
24 24
         $request_uri = $base_uri.$url;
@@ -29,7 +29,7 @@ trait GzcApiRequest
29 29
                 "Accept:application/json"
30 30
             );
31 31
             if ($url != '/v1/api/third/auth/token') {
32
-                $Token = self::getTokenFromServer();
32
+                $Token = self::getTokenFromServer($type);
33 33
                 $accessToken = $Token['extendData']['accessToken'];
34 34
                 array_push($headerArray, "token:".$accessToken);
35 35
             }
@@ -56,76 +56,38 @@ 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
-
69
-        $data = [
59
+        $cardType = $user['card_type'] ? $user['card_type'] : 10;
60
+        $cxbData = [
70 61
             'platformNo' => env('GZC.PLATFORM_NO'),
71
-            'appNo'      => env('GZC.APP_NO'),
72
-            'conName'    => $user['user_name'] ?? '',
73
-            'conCertType'=> 1,
74
-            'conCertNo'  => $user['user_card'] ?? '',
62
+            'appNo' => env('GZC.APP_NO'),
63
+            'conName' => $user['user_name'],
64
+            'conCertType' => $cardType,
65
+            'conCertNo' => $user['user_card'],
75 66
         ];
67
+        Log::info('储蓄保养老金请求参数:' . json_encode($cxbData));
68
+        $cxbResponseData = self::request('POST', '/v1/api/third/in/in30', $cxbData, [], 1);
69
+        Log::info('储蓄保养老金请求返回:' . json_encode($cxbResponseData));
70
+        if($cxbResponseData['code'] != 0) {
71
+            return $cxbResponseData['message'];
72
+        }
76 73
 
77
-        $debug['request'] = $data;
78
-
79
-        $res1 = self::getAmount($user,$data);
80
-
81
-        $data2 = [
74
+        $xfbData = [
82 75
             'platformNo' => env('XINGFUBAO.PLATFORM_NO'),
83
-            'appNo'      => env('XINGFUBAO.APP_NO'),
84
-            'conName'    => $user['user_name'] ?? '',
85
-            'conCertType'=> 1,
86
-            'conCertNo'  => $user['user_card'] ?? '',
76
+            'appNo' => env('XINGFUBAO.APP_NO'),
77
+            'conName' => $user['user_name'],
78
+            'conCertType' => $cardType,
79
+            'conCertNo' => $user['user_card'],
87 80
         ];
88
-        $res2 = self::getAmount($user,$data2);
89
-        return $res1 + $res2;
90
-    }
91
-
92
-    private static function getAmount($user,$data)
93
-    {
94
-        try {
95
-            $res = self::request('POST', '/v1/api/third/in/in30', $data);
96
-
97
-
98
-            // 尝试解析 amount(以分为单位常见)
99
-            $amountCents = $res['extendData']['amount'] ?? $res['data']['amount'] ?? $res['amount'] ?? null;
100
-
101
-            // 如果没有 amount 字段或为0,附加简单推测
102
-            if ($amountCents === null) {
103
-                $debug['note'] = 'missing_amount';
104
-                Log::warning('[checkAmount] missing amount', ['user' => $user, 'response' => $res]);
105
-            } elseif ($amountCents == 0) {
106
-                $msg = $res['msg'] ?? $res['message'] ?? '';
107
-                $reasons = [];
108
-                if (stripos($msg, '未开户') !== false || stripos($msg, '未实名') !== false) $reasons[] = 'third_party_unopened_or_unverified';
109
-                if (stripos($msg, '无账户') !== false) $reasons[] = 'account_not_found';
110
-                if (stripos($msg, 'token') !== false) $reasons[] = 'token_issue_possible';
111
-                if (empty($reasons)) $reasons[] = 'balance_is_zero';
112
-                $debug['zero_reasons'] = $reasons;
113
-            }
114
-
115
-            // 把 debug 附加到返回(不破坏原始返回结构)
116
-            if (is_array($res)) {
117
-                $res['debug'] = $debug;
118
-                return $res;
119
-            }
120
-
121
-            return $amountCents;
122
-        } catch (\Throwable $e) {
123
-            $debug['exception'] = $e->getMessage();
124
-            Log::error('[checkAmount] exception', ['user' => $user, 'err' => $e->getMessage()]);
125
-            return ['code' => 500, 'msg' => 'exception', 'debug' => $debug];
81
+        Log::info('幸福保养老金请求参数:' . json_encode($xfbData));
82
+        $xfbResponseData = self::request('POST', '/v1/api/third/in/in30', $xfbData, [], 2);
83
+        Log::info('幸福保养老金请求返回:' . json_encode($xfbResponseData));
84
+        if($xfbResponseData['code'] != 0) {
85
+            return $xfbResponseData['message'];
126 86
         }
127
-    }
128 87
 
88
+        $amount = $cxbResponseData['extendData']['amount'] + $xfbResponseData['extendData']['amount'];
89
+        return $amount;
90
+    }
129 91
     public function getAmountFromGzc()
130 92
     {
131 93
         $uid = $this->request->uid();
@@ -163,13 +125,17 @@ trait GzcApiRequest
163 125
      * @author php迷途小书童
164 126
      * @created 2021/1/14 14:38
165 127
      */
166
-    public static function getTokenFromServer()
128
+    public static function getTokenFromServer($type = 1)
167 129
     {
168 130
         $timestamp = (int)(microtime(true) * 1000);
169
-        $appid = env('GZC.APP_ID');
131
+        if ($type == 1) {
132
+            $appid = env('GZC.APP_ID');
133
+        }else{
134
+            $appid = env('XINGFUBAO.APP_ID');
135
+        }
170 136
         $data = [
171 137
             'appid' => $appid,
172
-            'sign' => self::getSignContent($timestamp, $appid),
138
+            'sign' => self::getSignContent($timestamp, $appid, $type),
173 139
             'timestamp' => $timestamp
174 140
         ];
175 141
         return self::request('POST', '/v1/api/third/auth/token', $data);
@@ -180,73 +146,14 @@ trait GzcApiRequest
180 146
      * @author php迷途小书童
181 147
      * @created 2020/10/29 9:35
182 148
      */
183
-    public static function getSignContent(int $timestamp, string $appid) {
184
-        $appsecret = md5(env('GZC.APP_SECRET'));
149
+    public static function getSignContent(int $timestamp, string $appid, $type = 1) {
150
+        if ($type == 1) {
151
+            $appsecret = md5(env('GZC.APP_SECRET'));
152
+        }else{
153
+            $appsecret = md5(env('XINGFUBAO.APP_SECRET'));
154
+        }
185 155
         $stringToBeSigned = "appid={$appid}&secret={$appsecret}&timestamp={$timestamp}";
186 156
         return md5($stringToBeSigned);
187 157
     }
188 158
 
189
-    /**
190
-     * @param $user
191
-     * @return array|mixed|string
192
-     * @author 史晨
193
-     * @date 2025/10/22 11:05
194
-     * 幸福保账户获取养老金
195
-     */
196
-    public static function getAmountXfb($user)
197
-    {
198
-        $debug = [];
199
-
200
-        $data = [
201
-            'platformNo' => env('XINGFUBAO.PLATFORM_NO'),
202
-            'appNo'      => env('XINGFUBAO.APP_NO'),
203
-            'conName'    => $user['user_name'] ?? '',
204
-            'conCertType'=> 1,
205
-            'conCertNo'  => $user['user_card'] ?? '',
206
-        ];
207
-
208
-        $debug['request'] = $data;
209
-
210
-        try {
211
-            // 记录请求(原有日志可保留或删除)
212
-            Log::info('[checkAmount] request', $data);
213
-
214
-            $res = self::request('POST', '/v1/api/third/in/in30', $data);
215
-
216
-            $debug['raw_response'] = $res;
217
-
218
-            // 尝试解析 amount(以分为单位常见)
219
-            $amountCents = $res['extendData']['amount'] ?? $res['data']['amount'] ?? $res['amount'] ?? null;
220
-            $debug['parsed_amount'] = $amountCents;
221
-
222
-            // 如果没有 amount 字段或为0,附加简单推测
223
-            if ($amountCents === null) {
224
-                $debug['note'] = 'missing_amount';
225
-                Log::warning('[checkAmount] missing amount', ['user' => $user, 'response' => $res]);
226
-            } elseif ($amountCents == 0) {
227
-                $msg = $res['msg'] ?? $res['message'] ?? '';
228
-                $reasons = [];
229
-                if (stripos($msg, '未开户') !== false || stripos($msg, '未实名') !== false) $reasons[] = 'third_party_unopened_or_unverified';
230
-                if (stripos($msg, '无账户') !== false) $reasons[] = 'account_not_found';
231
-                if (stripos($msg, 'token') !== false) $reasons[] = 'token_issue_possible';
232
-                if (empty($reasons)) $reasons[] = 'balance_is_zero';
233
-                $debug['zero_reasons'] = $reasons;
234
-                Log::warning('[checkAmount] amount is 0', ['user' => $user, 'reasons' => $reasons]);
235
-            }
236
-
237
-            // 把 debug 附加到返回(不破坏原始返回结构)
238
-            if (is_array($res)) {
239
-                $res['debug'] = $debug;
240
-                return $res;
241
-            }
242
-
243
-            // 如果不是数组,包装返回
244
-            return ['code' => 200, 'msg' => 'ok', 'extendData' => ['amount' => $amountCents], 'debug' => $debug];
245
-        } catch (\Throwable $e) {
246
-            $debug['exception'] = $e->getMessage();
247
-            Log::error('[checkAmount] exception', ['user' => $user, 'err' => $e->getMessage()]);
248
-            return ['code' => 500, 'msg' => 'exception', 'debug' => $debug];
249
-        }
250
-    }
251
-
252 159
 }