Xpy месяцев назад: 8
Родитель
Сommit
2903dcc44e
2 измененных файлов с 25 добавлено и 124 удалено
  1. 4 11
      app/controller/api/user/User.php
  2. 21 113
      app/traits/GzcApiRequest.php

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

@@ -3376,17 +3376,10 @@ class User extends BaseController
3376
         $uid = $this->request->uid();
3376
         $uid = $this->request->uid();
3377
         $userInfo = Db::name('UserCertification')->where('uid', $uid)->field('user_name,user_card,card_type')->find();
3377
         $userInfo = Db::name('UserCertification')->where('uid', $uid)->field('user_name,user_card,card_type')->find();
3378
         $res = GzcApiRequest::checkAmount($userInfo);
3378
         $res = GzcApiRequest::checkAmount($userInfo);
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)]);
3379
+        if(is_string($res)) {
3380
+            return app('json')->fail($res);
3381
+        }
3382
+        return app('json')->success(['amount' => sprintf("%01.2f", $res * 0.01)]);
3390
     }
3383
     }
3391
 
3384
 
3392
     public function check_user_annual_fee()
3385
     public function check_user_annual_fee()

+ 21 - 113
app/traits/GzcApiRequest.php

@@ -23,102 +23,30 @@ trait GzcApiRequest
23
         $base_uri = env('GZC.HOSTNAME');
23
         $base_uri = env('GZC.HOSTNAME');
24
         $request_uri = $base_uri.$url;
24
         $request_uri = $base_uri.$url;
25
         try {
25
         try {
26
-            $data = json_encode($params);
27
-            $headers = [
26
+            $data  = json_encode($params);
27
+            $headerArray =array(
28
                 "Content-type:application/json;charset='utf-8'",
28
                 "Content-type:application/json;charset='utf-8'",
29
                 "Accept:application/json"
29
                 "Accept:application/json"
30
-            ];
31
-            if ($url !== '/v1/api/third/auth/token') {
30
+            );
31
+            if ($url != '/v1/api/third/auth/token') {
32
                 $Token = self::getTokenFromServer($type);
32
                 $Token = self::getTokenFromServer($type);
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']);
33
+                $accessToken = $Token['extendData']['accessToken'];
34
+                array_push($headerArray, "token:".$accessToken);
48
             }
35
             }
49
             Log::info($data);
36
             Log::info($data);
50
             $curl = curl_init();
37
             $curl = curl_init();
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);
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);
74
             $output = curl_exec($curl);
45
             $output = curl_exec($curl);
75
-            $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
76
-            $curlErrNo = curl_errno($curl);
77
-            $curlErr = $curlErrNo ? curl_error($curl) : null;
78
             curl_close($curl);
46
             curl_close($curl);
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
-            ];
47
+            return json_decode($output, true);
116
         } catch (\Throwable $e) {
48
         } catch (\Throwable $e) {
117
-            return [
118
-                'code' => -1,
119
-                'message' => $e->getMessage(),
120
-                'debug' => null
121
-            ];
49
+            return $e->getMessage();
122
         }
50
         }
123
     }
51
     }
124
     /**
52
     /**
@@ -139,12 +67,8 @@ trait GzcApiRequest
139
         Log::info('储蓄保养老金请求参数:' . json_encode($cxbData));
67
         Log::info('储蓄保养老金请求参数:' . json_encode($cxbData));
140
         $cxbResponseData = self::request('POST', '/v1/api/third/in/in30', $cxbData, [], 1);
68
         $cxbResponseData = self::request('POST', '/v1/api/third/in/in30', $cxbData, [], 1);
141
         Log::info('储蓄保养老金请求返回:' . json_encode($cxbResponseData));
69
         Log::info('储蓄保养老金请求返回:' . json_encode($cxbResponseData));
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
-            ];
70
+        if($cxbResponseData['code'] != 0) {
71
+            return $cxbResponseData['message'];
148
         }
72
         }
149
 
73
 
150
         $xfbData = [
74
         $xfbData = [
@@ -157,28 +81,12 @@ trait GzcApiRequest
157
         Log::info('幸福保养老金请求参数:' . json_encode($xfbData));
81
         Log::info('幸福保养老金请求参数:' . json_encode($xfbData));
158
         $xfbResponseData = self::request('POST', '/v1/api/third/in/in30', $xfbData, [], 2);
82
         $xfbResponseData = self::request('POST', '/v1/api/third/in/in30', $xfbData, [], 2);
159
         Log::info('幸福保养老金请求返回:' . json_encode($xfbResponseData));
83
         Log::info('幸福保养老金请求返回:' . json_encode($xfbResponseData));
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
-            ];
84
+        if($xfbResponseData['code'] != 0) {
85
+            return $xfbResponseData['message'];
166
         }
86
         }
167
 
87
 
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
-        ];
88
+        $amount = $cxbResponseData['extendData']['amount'] + $xfbResponseData['extendData']['amount'];
89
+        return $amount;
182
     }
90
     }
183
     public function getAmountFromGzc()
91
     public function getAmountFromGzc()
184
     {
92
     {