Sfoglia il codice sorgente

feat(api): 支持多平台养老金金额查询- 修改 request 方法支持传递平台类型参数
- 优化 getTokenFromServer 和 getSignContent 方法支持多平台 token 获取
- checkAmount 方法增加 card_type 参数支持- 实现储蓄保和幸福保两个平台的养老金金额查询逻辑
- 增加日志记录请求参数和返回结果
- 优化接口返回结果处理,支持错误信息直接返回

shichen 9 mesi fa
parent
commit
11046a7f84
2 ha cambiato i file con 46 aggiunte e 13 eliminazioni
  1. 5 3
      app/controller/api/user/User.php
  2. 41 10
      app/traits/GzcApiRequest.php

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

@@ -3374,10 +3374,12 @@ class User extends BaseController
3374
     public function getAmountFromGzc()
3374
     public function getAmountFromGzc()
3375
     {
3375
     {
3376
         $uid = $this->request->uid();
3376
         $uid = $this->request->uid();
3377
-        $userInfo = Db::name('UserCertification')->where('uid', $uid)->field('user_name,user_card')->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
-
3380
-        return app('json')->success(['amount' => sprintf("%01.2f", $res['extendData']['amount'] * 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)]);
3381
     }
3383
     }
3382
 
3384
 
3383
     public function check_user_annual_fee()
3385
     public function check_user_annual_fee()

+ 41 - 10
app/traits/GzcApiRequest.php

@@ -18,7 +18,7 @@ trait GzcApiRequest
18
      * @author php迷途小书童
18
      * @author php迷途小书童
19
      * @created 2021/1/14 14:34
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
         $base_uri = env('GZC.HOSTNAME');
23
         $base_uri = env('GZC.HOSTNAME');
24
         $request_uri = $base_uri.$url;
24
         $request_uri = $base_uri.$url;
@@ -29,7 +29,7 @@ trait GzcApiRequest
29
                 "Accept:application/json"
29
                 "Accept:application/json"
30
             );
30
             );
31
             if ($url != '/v1/api/third/auth/token') {
31
             if ($url != '/v1/api/third/auth/token') {
32
-                $Token = self::getTokenFromServer();
32
+                $Token = self::getTokenFromServer($type);
33
                 $accessToken = $Token['extendData']['accessToken'];
33
                 $accessToken = $Token['extendData']['accessToken'];
34
                 array_push($headerArray, "token:".$accessToken);
34
                 array_push($headerArray, "token:".$accessToken);
35
             }
35
             }
@@ -56,14 +56,37 @@ trait GzcApiRequest
56
      */
56
      */
57
     public static function checkAmount($user)
57
     public static function checkAmount($user)
58
     {
58
     {
59
-        $data = [
59
+        $cardType = $user['card_type'] ? $user['card_type'] : 10;
60
+        $cxbData = [
60
             'platformNo' => env('GZC.PLATFORM_NO'),
61
             'platformNo' => env('GZC.PLATFORM_NO'),
61
             'appNo' => env('GZC.APP_NO'),
62
             'appNo' => env('GZC.APP_NO'),
62
             'conName' => $user['user_name'],
63
             'conName' => $user['user_name'],
63
-            'conCertType' => 1,
64
+            'conCertType' => $cardType,
65
+            'conCertNo' => $user['user_card'],
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
+        }
73
+
74
+        $xfbData = [
75
+            'platformNo' => env('XINGFUBAO.PLATFORM_NO'),
76
+            'appNo' => env('XINGFUBAO.APP_NO'),
77
+            'conName' => $user['user_name'],
78
+            'conCertType' => $cardType,
64
             'conCertNo' => $user['user_card'],
79
             'conCertNo' => $user['user_card'],
65
         ];
80
         ];
66
-        return self::request('POST', '/v1/api/third/in/in30', $data);
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'];
86
+        }
87
+
88
+        $amount = $cxbResponseData['extendData']['amount'] + $xfbResponseData['extendData']['amount'];
89
+        return $amount;
67
     }
90
     }
68
     public function getAmountFromGzc()
91
     public function getAmountFromGzc()
69
     {
92
     {
@@ -102,13 +125,17 @@ trait GzcApiRequest
102
      * @author php迷途小书童
125
      * @author php迷途小书童
103
      * @created 2021/1/14 14:38
126
      * @created 2021/1/14 14:38
104
      */
127
      */
105
-    public static function getTokenFromServer()
128
+    public static function getTokenFromServer($type = 1)
106
     {
129
     {
107
         $timestamp = (int)(microtime(true) * 1000);
130
         $timestamp = (int)(microtime(true) * 1000);
108
-        $appid = env('GZC.APP_ID');
131
+        if ($type == 1) {
132
+            $appid = env('GZC.APP_ID');
133
+        }else{
134
+            $appid = env('XINGFUBAO.APP_ID');
135
+        }
109
         $data = [
136
         $data = [
110
             'appid' => $appid,
137
             'appid' => $appid,
111
-            'sign' => self::getSignContent($timestamp, $appid),
138
+            'sign' => self::getSignContent($timestamp, $appid, $type),
112
             'timestamp' => $timestamp
139
             'timestamp' => $timestamp
113
         ];
140
         ];
114
         return self::request('POST', '/v1/api/third/auth/token', $data);
141
         return self::request('POST', '/v1/api/third/auth/token', $data);
@@ -119,8 +146,12 @@ trait GzcApiRequest
119
      * @author php迷途小书童
146
      * @author php迷途小书童
120
      * @created 2020/10/29 9:35
147
      * @created 2020/10/29 9:35
121
      */
148
      */
122
-    public static function getSignContent(int $timestamp, string $appid) {
123
-        $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
+        }
124
         $stringToBeSigned = "appid={$appid}&secret={$appsecret}&timestamp={$timestamp}";
155
         $stringToBeSigned = "appid={$appid}&secret={$appsecret}&timestamp={$timestamp}";
125
         return md5($stringToBeSigned);
156
         return md5($stringToBeSigned);
126
     }
157
     }