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){ if(empty($data['body'])) return [false, '凭证同步入参请放入body内']; //获取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['body'] ?? []; $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]; } }