param = config("maycur"); $this->domain_url = $this->param['dd_url']; } public function getToken(){ //每刻所有配置 $url_array = $this->param; //redis 存储的token $key = $this->domain_url . $url_array['login_redis_topic']; $token = Redis::get($key); if(! empty($token)) { $this->head = json_decode($token,true); return [true, '']; } //获取token的参数 $appcode = env('maycur_appcode'); $appsercet = env('maycur_appsercet'); if(empty($appcode) || empty($appsercet)) return [false, '每刻鉴权认证参数不能为空']; //组织获取参数 $url = $this->domain_url . $url_array['login']; $time = microtime(true); $timeStamp = intval($time * 1000); //生成secret $secret = $this->getSecret($appcode, $appsercet, $timeStamp); $post = [ "secret" => $secret, "appCode" => $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'] != 'ACK') return [false, $result['message'] ?? '鉴权失败,请联系开发者']; $token_array = [ 'tokenId:' . $result['data']['tokenId'], 'entCode:' . $result['data']['entCode'], ]; $this->head = $token_array; Redis::setex($key, $url_array['login_expire_time'], json_encode($token_array)); return [true, '']; } private function getSecret($appCode, $appSecret, $timeStamp) { // 拼接字符串并计算 SHA-256 哈希值 $stringToHash = $appSecret . ":" . $appCode . ":" . $timeStamp; $hashedString = hash('sha256', $stringToHash); return $hashedString; } public function reimburse($data){ //校验 list($status, $msg) = $this->reimburseRule($data); if(! $status) return [false, $msg]; //获取token list($status, $token) = $this->getToken(); if(! $status) return [false, $token]; $header = $this->head; //每刻所有配置 $url_array = $this->param; //组织获取参数 $url = $this->domain_url . $url_array['reimburse']; $header = array_merge(['Content-Type:application/json'], $header); list($status, $result) = $this->post_helper($url,$msg, $header); if(! $status) return [$status, $result]; if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']]; //组织返回数据 if(empty($result['data'])) return [true, ['list' => [], 'hasNextPage' => false]]; //获取报销单详情填充 list($status, $return) = $this->reimburseDetailGet($result['data']); if(! $status) return [false, $return]; return [true, $return]; } public function reimburseRule($data){ if(empty($data['settledAtStart']) || empty($data['settledAtEnd'])) return [false, '单据的支付时间不能为空']; $settledAtStart = strtotime($data['settledAtStart'] . '00:00:00'); $settledAtEnd = strtotime($data['settledAtEnd'] . '23:59:59'); if(empty($settledAtStart)) return [false, '单据的支付时间格式错误']; if(empty($settledAtEnd)) return [false, '单据的支付时间格式错误']; $pageNo = 1; $pageSize = 10; if(! empty($data['pageNo'])) $pageNo = $data['pageNo']; if(! empty($data['pageSize'])) { if($data['pageSize'] >= 10) { $pageSize = 10; }else{ $pageSize = $data['pageSize']; } } $return = [ 'settledAtStart' => $settledAtStart * 1000, 'settledAtEnd' => $settledAtEnd * 1000, 'pageNo' => $pageNo, 'pageSize' => $pageSize, 'formStatus' => 'COMPLETED', "legalEntityBizCodes" => [ "ELC2106251C5CMNI8" ], ]; return [true, $return]; } public function reimburseDetailGet($list){ if(empty($list['list'])) return [true, $list]; $return = []; foreach ($list['list'] as $key => $value){ list($status, $msg) = $this->reimburseDetail($value); if(! $status) return [false, $msg]; $return[] = $msg; } return [true, ['list' => $return, 'hasNextPage' => $list['hasNextPage']]]; } public function reimburseDetail($data){ if(empty($data['formCode'])) return [false, ["报销单详情请求缺失formCode"]]; $post = [ "formCode"=> $data['formCode'], "enablePayeeInfo"=> true, "enablePayerInfo"=> true, "enableAssociatedForms"=> true, "enableTravelRouteList"=> true, "enableCreditInfo"=> true, "enableExpenseList"=> true, "enableInvoiceList"=> true, "enableExpenseAllocationList"=> true, "enableTravelPartnerInfo"=> true, "enableAttachments"=> true, "enableCapitalPlanPaymentLog"=> true ]; $header = $this->head; //每刻所有配置 $url_array = $this->param; //组织获取参数 $url = $this->domain_url . $url_array['reimburseDetail']; $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'] != 'ACK') return [false, $result['message']]; return [true, $result['data'] ?? []]; } public function loan($data){ //校验 list($status, $msg) = $this->loanRule($data); if(! $status) return [false, $msg]; //获取token list($status, $token) = $this->getToken(); if(! $status) return [false, $token]; $header = $this->head; //每刻所有配置 $url_array = $this->param; //组织获取参数 $url = $this->domain_url . $url_array['loan']; $header = array_merge(['Content-Type:application/json'], $header); list($status, $result) = $this->post_helper($url,$msg, $header); if(! $status) return [$status, $result]; if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']]; //组织返回数据 if(empty($result['data'])) return [true, ['list' => [], 'hasNextPage' => false]]; //获取报销单详情填充 list($status, $return) = $this->loanDetailGet($result['data']); if(! $status) return [false, $return]; return [true, $return]; } public function loanRule($data){ if(empty($data['settledAtStart']) || empty($data['settledAtEnd'])) return [false, '单据的创建时间不能为空']; $settledAtStart = strtotime($data['settledAtStart'] . '00:00:00'); $settledAtEnd = strtotime($data['settledAtEnd'] . '23:59:59'); if(empty($settledAtStart)) return [false, '单据的创建开始时间格式错误']; if(empty($settledAtEnd)) return [false, '单据的创建结束时间格式错误']; $pageNo = 1; $pageSize = 10; if(! empty($data['pageNo'])) $pageNo = $data['pageNo']; if(! empty($data['pageSize'])) { if($data['pageSize'] >= 10) { $pageSize = 10; }else{ $pageSize = $data['pageSize']; } } $return = [ 'settledAtStart' => $settledAtStart * 1000, 'settledAtEnd' => $settledAtEnd * 1000, 'pageNo' => $pageNo, 'pageSize' => $pageSize, 'formStatus' => 'COMPLETED', "legalEntityBizCodes" => [ "ELC2106251C5CMNI8" ], ]; return [true, $return]; } public function loanDetailGet($list){ if(empty($list['list'])) return [true, $list]; $return = []; foreach ($list['list'] as $value){ list($status, $msg) = $this->loanDetail($value); if(! $status) return [false, $msg]; $return[] = $msg; } return [true, ['list' => $return, 'hasNextPage' => $list['hasNextPage']]]; } public function loanDetail($data){ if(empty($data['formCode'])) return [false, ["借款单详情请求缺失formCode"]]; $header = $this->head; //每刻所有配置 $url_array = $this->param; //组织获取参数 $url = $this->domain_url . $url_array['loanDetail'] . $data['formCode']; $header = array_merge(['Content-Type:application/json'], $header); list($status, $result) = $this->get_helper($url,$header); if(! $status) return [$status, $result]; if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']]; return [true, $result['data'] ?? []]; } 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]; } }