cqp 4 месяцев назад
Родитель
Сommit
ea1546626d

+ 13 - 0
app/Http/Controllers/Api/MayCurController.php

@@ -2,6 +2,7 @@
 namespace App\Http\Controllers\Api;
 
 use App\Service\MayCurServerService;
+use App\Service\MayCurVouchersServerService;
 use Illuminate\Http\Request;
 
 class MayCurController extends BaseController
@@ -35,4 +36,16 @@ class MayCurController extends BaseController
             return $this->json_return(201,$data);
         }
     }
+
+    public function voucher(Request $request){
+        $data = $request->all();
+
+        list($bool, $data) = (new MayCurVouchersServerService($data))->voucher($request->all());
+
+        if($bool){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 173 - 0
app/Service/MayCurVouchersServerService.php

@@ -0,0 +1,173 @@
+<?php
+
+namespace App\Service;
+
+
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Redis;
+
+class MayCurVouchersServerService extends Service
+{
+    protected $param = "";
+    protected $domain_url = "";
+    protected $is_formal_vouch = false;
+    protected $head = null;
+
+    public function __construct($data)
+    {
+        $param = config("maycur");
+        if(! isset($data['is_formal_vouch'])){
+            $this->param = $param['voucher_cs'] ?? [];
+            $this->domain_url = $this->param['ip'] ?? "";
+        }else{
+            $this->param = $param['voucher_zs'] ?? [];
+            $this->domain_url = $this->param['ip'] ?? "";
+            $this->is_formal_vouch = true;
+        }
+    }
+
+    private function getSecret($appCode, $appSecret, $timeStamp) {
+        // 拼接字符串并计算 SHA-256 哈希值
+        $stringToHash = $appSecret . ":" . $appCode . ":" . $timeStamp;
+        $hashedString = hash('sha256', $stringToHash);
+
+        return $hashedString;
+    }
+
+    public function getToken(){
+        //每刻所有配置
+        $url_array = $this->param;
+
+        //环境
+        $is_voucher_formal = $this->is_formal_vouch;
+        $name = $is_voucher_formal ? "每刻凭证正试环境" : "每刻凭证测试环境";
+
+        //redis 存储的token
+        $key = $this->domain_url . $url_array['login_expire_time'];
+        $token = Redis::get($key);
+        if(! empty($token)) {
+            $this->head = json_decode($token,true);
+            return [true, ''];
+        }
+
+        //获取token的参数
+        $appcode = $url_array['appkey'];
+        $appsercet = $url_array['appsecret'];
+        if(empty($appcode) || empty($appsercet)) return [false, $name . '鉴权认证参数不能为空'];
+
+        //组织获取参数
+        $url = $this->domain_url . $url_array['login'];
+        $time = microtime(true);
+        $timeStamp = intval($time * 1000);
+        //生成secret
+        $secret = $this->getSecret($appcode, $appsercet, $timeStamp);
+
+        $post = [
+            "secretToken" => $secret,
+            "userCode" => $appcode,
+            "timestamp" => $timeStamp
+        ];
+        $header = ['Content-Type:application/json'];
+        list($status, $result) = $this->post_helper($url,$post, $header);
+        if(! $status) return [$status, $result];
+        if(isset($result['code']) && $result['code'] != '200') return [false, $result['errMsg'] ?? $name . '鉴权失败,请联系开发者'];
+
+        $token_array = [
+            'Authorization:Bearer ' . $result['data']['token'],
+        ];
+        $this->head = $token_array;
+
+        Redis::setex($key, $url_array['login_expire_time'], json_encode($token_array));
+
+        return [true, ''];
+    }
+
+    public function voucher($data){
+        //获取token
+        list($status, $token) = $this->getToken();
+        if(! $status) return [false, $token];
+
+        $header = $this->head;
+        //每刻所有配置
+        $url_array = $this->param;
+        //组织获取参数
+        $url = $this->domain_url . $url_array['voucher_insert'];
+        $post = $data;
+        $header = array_merge(['Content-Type:application/json'], $header);
+        list($status, $result) = $this->post_helper($url,$post, $header);
+        if(! $status) return [$status, $result];
+        if(isset($result['code']) && $result['code'] == 1) return [false, $result['errData'][0]['errorMsg'] ?? ""];
+        if(isset($result['code']) && $result['code'] != 0 && ! empty($result['errMsg'])) return [false, $result['errMsg']];
+
+        return [true, ''];
+    }
+
+    public function post_helper($url, $data, $header = [], $timeout = 20){
+        Log::channel('apiMcLog')->info('每刻凭证POST', ["api" => $url , "param" => $data ,"header" => $header]);
+
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch,  CURLOPT_RETURNTRANSFER, true);
+        curl_setopt($ch, CURLOPT_ENCODING, '');
+        curl_setopt($ch, CURLOPT_POST, 1);
+        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
+
+        if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
+        $r = curl_exec($ch);
+
+        if ($r === false) {
+            // 获取错误号
+            $errorNumber = curl_errno($ch);
+            // 获取错误信息
+            $errorMessage = curl_error($ch);
+            $message = "cURL Error #{$errorNumber}: {$errorMessage}";
+
+            Log::channel('apiMcLog')->info('每刻凭证POST结果', ["message" => $message]);
+            return [false, $message];
+        }
+        curl_close($ch);
+
+        $return = json_decode($r, true);
+        Log::channel('apiMcLog')->info('每刻凭证POST结果', ["message" => $return]);
+
+        return [true, $return];
+    }
+
+    public function get_helper($url,$header=[],$timeout = 20){
+        Log::channel('apiMcLog')->info('每刻凭证GET', ["api" => $url ,"header" => $header]);
+        $ch = curl_init();
+        curl_setopt_array($ch, array(
+            CURLOPT_URL => $url,
+            CURLOPT_RETURNTRANSFER => true,
+            CURLOPT_ENCODING => '',
+            CURLOPT_MAXREDIRS => 10,
+            CURLOPT_TIMEOUT => $timeout,
+            CURLOPT_FOLLOWLOCATION => true,
+            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
+            CURLOPT_CUSTOMREQUEST => 'GET',
+            CURLOPT_SSL_VERIFYPEER => false,
+            CURLOPT_HTTPHEADER => $header,
+        ));
+        $r = curl_exec($ch);
+
+        if ($r === false) {
+            // 获取错误号
+            $errorNumber = curl_errno($ch);
+            // 获取错误信息
+            $errorMessage = curl_error($ch);
+            $message = "cURL Error #{$errorNumber}: {$errorMessage}";
+            Log::channel('apiMcLog')->info('每刻凭证GET', ["message" => $message]);
+            return [false, $message];
+        }
+
+        curl_close($ch);
+
+        $return = json_decode($r, true);
+        Log::channel('apiMcLog')->info('每刻凭证GET', ["message" => $return]);
+
+        return [true, $return];
+    }
+}

+ 11 - 0
config/maycur.php

@@ -25,4 +25,15 @@ return [
     'tradingPartner' =>  '/api/openapi/tradingPartner/list/V2',
     //部门
     'department' => '/api/openapi/org/department/search/v2',
+    //每刻凭证档案测试环境
+    'voucher_cs' => [
+        'ip' => "http://47.96.72.143:18880",
+        'appkey' => "voucher",
+        'appsecret' => "wGt5VL422YETsNlPvHrhVUwW",
+        'login' => '/open/auth/login',
+        //凭证接口过期时间
+        'login_expire_time' => 1150, // 1200
+        //凭证批量同步
+        'voucher_insert' => '/open/voucher/v3/',
+    ],
 ];

+ 2 - 1
routes/maycur.php

@@ -19,4 +19,5 @@ Route::middleware('auth:api')->get('/user', function (Request $request) {
 
 //Route::any('getToken', 'Api\MayCurController@getToken');
 Route::any('reimburse', 'Api\MayCurController@reimburse');
-Route::any('loan', 'Api\MayCurController@loan');
+Route::any('loan', 'Api\MayCurController@loan');
+Route::any('voucher', 'Api\MayCurController@voucher');