Explorar el Código

公证处检测问题

Xpy hace 8 meses
padre
commit
e879c9b0e4
Se han modificado 2 ficheros con 124 adiciones y 25 borrados
  1. 11 4
      app/controller/api/user/User.php
  2. 113 21
      app/traits/GzcApiRequest.php

+ 11 - 4
app/controller/api/user/User.php

@@ -3376,10 +3376,17 @@ class User extends BaseController
3376 3376
         $uid = $this->request->uid();
3377 3377
         $userInfo = Db::name('UserCertification')->where('uid', $uid)->field('user_name,user_card,card_type')->find();
3378 3378
         $res = GzcApiRequest::checkAmount($userInfo);
3379
-        if(is_string($res)) {
3380
-            return app('json')->fail($res);
3381
-        }
3382
-        return app('json')->success(['amount' => sprintf("%01.2f", $res * 0.01)]);
3379
+        if (!is_array($res) || ($res['code'] ?? -1) !== 0) {
3380
+            return app('json')->fail(
3381
+                $res['message'] ?? '查询失败',
3382
+                [
3383
+                    'code' => $res['code'] ?? -1,
3384
+                    'debug' => $res['debug'] ?? null,
3385
+                ]
3386
+            );//
3387
+        }
3388
+        $amountCents = $res['extendData']['amount'] ?? $res['data']['amount'] ?? $res['amount'] ?? 0;
3389
+        return app('json')->success(['amount' => sprintf('%01.2f', floatval($amountCents) * 0.01)]);
3383 3390
     }
3384 3391
 
3385 3392
     public function check_user_annual_fee()

+ 113 - 21
app/traits/GzcApiRequest.php

@@ -23,30 +23,102 @@ trait GzcApiRequest
23 23
         $base_uri = env('GZC.HOSTNAME');
24 24
         $request_uri = $base_uri.$url;
25 25
         try {
26
-            $data  = json_encode($params);
27
-            $headerArray =array(
26
+            $data = json_encode($params);
27
+            $headers = [
28 28
                 "Content-type:application/json;charset='utf-8'",
29 29
                 "Accept:application/json"
30
-            );
31
-            if ($url != '/v1/api/third/auth/token') {
30
+            ];
31
+            if ($url !== '/v1/api/third/auth/token') {
32 32
                 $Token = self::getTokenFromServer($type);
33
-                $accessToken = $Token['extendData']['accessToken'];
34
-                array_push($headerArray, "token:".$accessToken);
33
+                if (isset($Token['extendData']['accessToken']) && $Token['extendData']['accessToken']) {
34
+                    $headers[] = "token:".$Token['extendData']['accessToken'];
35
+                } else {
36
+                    return [
37
+                        'code' => -1,
38
+                        'message' => 'token获取失败',
39
+                        'debug' => [
40
+                            'raw' => $Token,
41
+                            'http_code' => null
42
+                        ]
43
+                    ];
44
+                }
45
+            }
46
+            if (!empty($options['headers']) && is_array($options['headers'])) {
47
+                $headers = array_merge($headers, $options['headers']);
35 48
             }
36 49
             Log::info($data);
37 50
             $curl = curl_init();
38
-            curl_setopt($curl, CURLOPT_URL, $request_uri);
39
-            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
40
-            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
41
-            curl_setopt($curl, CURLOPT_POST, 1);
42
-            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
43
-            curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
44
-            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
51
+            $methodUpper = strtoupper($method);
52
+            $urlToRequest = $request_uri;
53
+            if ($methodUpper === 'GET' && !empty($params)) {
54
+                $query = http_build_query($params);
55
+                $urlToRequest .= (strpos($urlToRequest, '?') === false ? '?' : '&').$query;
56
+            }
57
+            curl_setopt($curl, CURLOPT_URL, $urlToRequest);
58
+            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
59
+            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
60
+            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
61
+            if (!empty($options['timeout'])) {
62
+                curl_setopt($curl, CURLOPT_TIMEOUT, (int)$options['timeout']);
63
+            }
64
+            if ($methodUpper === 'POST') {
65
+                curl_setopt($curl, CURLOPT_POST, true);
66
+                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
67
+            } elseif ($methodUpper !== 'GET') {
68
+                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $methodUpper);
69
+                if (!empty($params)) {
70
+                    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
71
+                }
72
+            }
73
+            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
45 74
             $output = curl_exec($curl);
75
+            $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
76
+            $curlErrNo = curl_errno($curl);
77
+            $curlErr = $curlErrNo ? curl_error($curl) : null;
46 78
             curl_close($curl);
47
-            return json_decode($output, true);
79
+            if ($curlErrNo) {
80
+                return [
81
+                    'code' => -1,
82
+                    'message' => $curlErr ?: '请求错误',
83
+                    'debug' => [
84
+                        'http_code' => $httpCode,
85
+                        'curl_errno' => $curlErrNo,
86
+                        'raw' => $output
87
+                    ]
88
+                ];
89
+            }
90
+            if ($httpCode < 200 || $httpCode >= 300) {
91
+                return [
92
+                    'code' => -1,
93
+                    'message' => 'HTTP状态异常',
94
+                    'debug' => [
95
+                        'http_code' => $httpCode,
96
+                        'raw' => $output
97
+                    ]
98
+                ];
99
+            }
100
+            $decoded = json_decode($output, true);
101
+            if (is_array($decoded)) {
102
+                $decoded['debug'] = [
103
+                    'http_code' => $httpCode,
104
+                    'raw' => $output
105
+                ];
106
+                return $decoded;
107
+            }
108
+            return [
109
+                'code' => -1,
110
+                'message' => '返回解析失败',
111
+                'debug' => [
112
+                    'http_code' => $httpCode,
113
+                    'raw' => $output
114
+                ]
115
+            ];
48 116
         } catch (\Throwable $e) {
49
-            return $e->getMessage();
117
+            return [
118
+                'code' => -1,
119
+                'message' => $e->getMessage(),
120
+                'debug' => null
121
+            ];
50 122
         }
51 123
     }
52 124
     /**
@@ -67,8 +139,12 @@ trait GzcApiRequest
67 139
         Log::info('储蓄保养老金请求参数:' . json_encode($cxbData));
68 140
         $cxbResponseData = self::request('POST', '/v1/api/third/in/in30', $cxbData, [], 1);
69 141
         Log::info('储蓄保养老金请求返回:' . json_encode($cxbResponseData));
70
-        if($cxbResponseData['code'] != 0) {
71
-            return $cxbResponseData['message'];
142
+        if (!is_array($cxbResponseData) || ($cxbResponseData['code'] ?? -1) != 0) {
143
+            return [
144
+                'code' => $cxbResponseData['code'] ?? -1,
145
+                'message' => $cxbResponseData['message'] ?? '储蓄保查询失败',
146
+                'debug' => $cxbResponseData['debug'] ?? null,
147
+            ];
72 148
         }
73 149
 
74 150
         $xfbData = [
@@ -81,12 +157,28 @@ trait GzcApiRequest
81 157
         Log::info('幸福保养老金请求参数:' . json_encode($xfbData));
82 158
         $xfbResponseData = self::request('POST', '/v1/api/third/in/in30', $xfbData, [], 2);
83 159
         Log::info('幸福保养老金请求返回:' . json_encode($xfbResponseData));
84
-        if($xfbResponseData['code'] != 0) {
85
-            return $xfbResponseData['message'];
160
+        if (!is_array($xfbResponseData) || ($xfbResponseData['code'] ?? -1) != 0) {
161
+            return [
162
+                'code' => $xfbResponseData['code'] ?? -1,
163
+                'message' => $xfbResponseData['message'] ?? '幸福保查询失败',
164
+                'debug' => $xfbResponseData['debug'] ?? null,
165
+            ];
86 166
         }
87 167
 
88
-        $amount = $cxbResponseData['extendData']['amount'] + $xfbResponseData['extendData']['amount'];
89
-        return $amount;
168
+        $amountCxb = $cxbResponseData['extendData']['amount'] ?? $cxbResponseData['data']['amount'] ?? $cxbResponseData['amount'] ?? 0;
169
+        $amountXfb = $xfbResponseData['extendData']['amount'] ?? $xfbResponseData['data']['amount'] ?? $xfbResponseData['amount'] ?? 0;
170
+        $amount = $amountCxb + $amountXfb;
171
+        return [
172
+            'code' => 0,
173
+            'message' => 'success',
174
+            'extendData' => [
175
+                'amount' => $amount
176
+            ],
177
+            'debug' => [
178
+                'cxb' => $cxbResponseData['debug'] ?? null,
179
+                'xfb' => $xfbResponseData['debug'] ?? null,
180
+            ],
181
+        ];
90 182
     }
91 183
     public function getAmountFromGzc()
92 184
     {